---
title: "SDM4 in R: Stats Starts Here (Chapter 1)"
author: "Nicholas Horton (nhorton@amherst.edu)"
date: "July 17, 2017"
output: 
  pdf_document:
    fig_height: 4
    fig_width: 6
  html_document:
    fig_height: 3
    fig_width: 5
  word_document:
    fig_height: 4
    fig_width: 6
---


```{r, include=FALSE}
# Don't delete this chunk if you are using the mosaic package
# This loads the mosaic and dplyr packages
require(mosaic)
```

```{r, include=FALSE}
# Some customization.  You can alter or delete as desired (if you know what you are doing).

# This changes the default colors in lattice plots.
trellis.par.set(theme=theme.mosaic())  

# knitr settings to control how R chunks work.
require(knitr)
opts_chunk$set(
  tidy=FALSE,     # display code as typed
  size="small"    # slightly smaller font for code
)
```

## Introduction and background 

This document is intended to help describe how to undertake analyses introduced 
as examples in the Fourth Edition of \emph{Stats: Data and Models} (2014) by De Veaux, Velleman, and Bock.
More information about the book can be found at http://wps.aw.com/aw_deveaux_stats_series.  This
file as well as the associated R Markdown reproducible analysis source file used to create it can be found at http://nhorton.people.amherst.edu/sdm4.

This work leverages initiatives undertaken by Project MOSAIC (http://www.mosaic-web.org), an NSF-funded effort to improve the teaching of statistics, calculus, science and computing in the undergraduate curriculum. In particular, we utilize the `mosaic` package, which was written to simplify the use of R for introductory statistics courses. A short summary of the R needed to teach introductory statistics can be found in the mosaic package vignettes (http://cran.r-project.org/web/packages/mosaic).  A paper describing the mosaic approach was published in t
he *R Journal*: https://journal.r-project.org/archive/2017/RJ-2017-024.

## Chapter 1: Stats Starts Here

### Section 1.1: What is Statistics?
### Section 1.2: Data
### Section 1.3: Variables

See table on page 7.

```{r}
library(mosaic); library(readr)
options(digits=3)
Tour <- read.delim("http://nhorton.people.amherst.edu/sdm4/data/Tour_de_France_2014.txt", 
    sep="\t", stringsAsFactors=FALSE)
names(Tour)
dim(Tour)
head(Tour, 3)
tail(Tour, 8)
```

#### Let's find who was the winner in 1998

```{r}
filter(Tour, Year==1998)
```

#### How many stages did Alberto Contador win in the years when he won the Tour?

```{r}
filter(Tour, Winner=="Contador Alberto")
```

Note that the following commands generate the same output.

```{r}
Tour %>%
  filter(Winner=="Contador Alberto")
```

The pipe operator (`%>%`) can be used to connect one dataframe or command to 
another.

#### What was the slowest average speed of any tour?  Fastest?
```{r}
filter(Tour, Average.Speed==min(Average.Speed))
filter(Tour, Average.Speed==max(Average.Speed))
```

#### What can we say about the Average Speeds?

```{r}
favstats(~ Average.Speed, data=Tour)
```
