---
title: 'Stat 140 Lab: Inference for a Difference in Means'
author: "Evan Ray"
date: "November 27, 2017"
output:
  html_document:
    fig_height: 3
    fig_width: 5
  pdf_document:
    fig_height: 2.8
    fig_width: 6.5
  word_document:
    fig_height: 3
    fig_width: 5
---


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

```{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())  
options(digits=4)

# 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
)
# This loads the mosaic data sets.  (Could be deleted if you are not using them.)
require(mosaicData)                
```


# Comparing Fuel Efficiency for automatic transmission vs. manual transmission cars

Below are summary statistics on fuel efficiency (in miles/gallon) from random samples of cars with manual and automatic transmissions manufactured in 2012. Do these data provide strong evidence of a difference between the average fuel efficiency of cars with manual and automatic transmissions in terms of their average city mileage?

```{r}
# load data ---------------------------------------------------------
fuel_eff <- read_csv("https://mhc-stat140-2017.github.io/data/misc/fuel_eff.csv")

# select a small sample ---------------------------------------------
man_rows <- which(fuel_eff$transmission == "M")
aut_rows <- which(fuel_eff$transmission == "A")

set.seed(3583)
man_rows_samp <- sample(man_rows, 26)
aut_rows_samp <- sample(aut_rows, 26)

fuel_eff_samp <- fuel_eff[c(man_rows_samp,aut_rows_samp), ]
fuel_eff_samp$transmission <- factor(fuel_eff_samp$transmission)

levels(fuel_eff_samp$transmission) <- c("automatic", "manual")

ggplot() +
  geom_density(mapping = aes(x = comb_mpg, color = transmission), data = fuel_eff_samp)

fuel_eff_man <- filter(fuel_eff_samp, transmission == "manual")
fuel_eff_aut <- filter(fuel_eff_samp, transmission == "automatic")

mean(fuel_eff_man$comb_mpg)
sd(fuel_eff_man$comb_mp)g
mean(fuel_eff_aut$comb_mpg)
sd(fuel_eff_aut$comb_mpg)
```

## State the null and alternative hypotheses



## Check Assumptions for Inference

#### Paired or Unpaired?

Are these data paired or unpaired?

SOLUTION:



#### 1. Independence

 * Among observations within each group and observations in different groups for a two-sample test
 * Among the different pairs for a paired test

SOLUTION:



#### 2. Nearly Normal

 * Check separately for both groups for a two-sample test
 * Check for the differences for a paired test

SOLUTION:



#### 3. Sample size big enough

 * Check separately for both groups for a two-sample test
 * Check for the number of pairs for a paired test

SOLUTION:



## Do the mechanics of the test

You can use `t.test()` function.

```{r}
# call to t.test() here
```

## Draw a conclusion in context of the problem

SOLUTION:



## State a 95% confidence interval and interpret it in context.

SOLUTION:




# Accidents on Friday the 13th

The *British Medical Journal* published an article titled "Is Friday the 13th Bad for Your Health?"  The article examined the number of people admitted to emergency rooms for vehicular accidents on 12 Friday evenings (6 each on the 6th and 13th).  Here are the data:

```{r, eval = FALSE}
friday13 <- read_csv("https://mhc-stat140-2017.github.io/data/sdm4/Friday_the_13th_Part_2.csv") %>%
  mutate(difference = X13th - X6th)
head(friday13)
```

Is there a difference between rates of accidents on Friday the 13th and Friday the 6th?

## State Hypotheses

SOLUTION:



## Check Assumptions for Inference

#### Paired or Unpaired?

Are these data paired or unpaired?

SOLUTION:



#### 1. Independence

 * Among observations within each group and observations in different groups for a two-sample test
 * Among the different pairs for a paired test

SOLUTION:



#### 2. Nearly Normal

 * Check separately for both groups for a two-sample test
 * Check for the differences for a paired test

SOLUTION:



#### 3. Sample size big enough

 * Check separately for both groups for a two-sample test
 * Check for the number of pairs for a paired test

SOLUTION:



## Calculate a p-value

You can use `t.test()` function.

SOLUTION:

```{r}
# Your code goes here

```



## Draw a Conclusion

SOLUTION:



## State a 99% confidence interval and interpret it in context.

SOLUTION:


