---
title: "Lab 12: Inference for Simple Linear Regression"
author: "Insert Name"
output:
  pdf_document
 
---

# Due: 12am night before lab

# Instructions
Work through the problems below, and submit this document as a knitted .pdf file with answers to the Problem Set assignment on Gradescope. 


**For each problem, put your solution between the bars of red stars.**


```{r, include=FALSE}
# Do not edit this code block/chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE, fig.width = 5, fig.height = 3)
```

## Load packages

For this lab we will be using the `tidyverse` for data wrangling and visualization, `moderndive` for linear modeling, `infer` for statistical inference, and a data set will come from the `oilabs` package.

```{r, include=FALSE, message = FALSE, warning = FALSE}
library(tidyverse)
library(knitr)
library(moderndive)
library(infer)
library(oilabs)
```


 

## Twins and IQ

In the mid 20th century, a study was conducted that tracked down
identical twins that were separated at birth: one child was raised in
the home of their biological parents and the other in a foster home. In
an attempt to answer the question of whether intelligence is the result
of nature or nurture, both children were given IQ tests.

The researcher that collected this data was named Cyril
Burt, and his work was the subject of some controversy, including the claim that he falsified some of his research data.  One of the suspect datasets is the identical twins data that we will look at.  This has come to be called "The Burt Affair".  Take a moment and read about it before beginning this lab.

<https://en.wikipedia.org/wiki/Cyril_Burt#%22The_Burt_Affair%22>

This data is in a `.csv` file, which is a very general format for data.
You can load our usual packages as well as the data (into the `twins`
data frame) with the following commands:

```{r}
 
twins <- read.csv("http://andrewpbray.github.io/data/twins.csv")
options(scipen = 99)
set.seed(0909)
```

To reduce scientific notation appearing in the output, we can also
specify the option `scipen` to be a large positive value. More info is
available via `?options`.

Since this data was read in from a `.csv` file, there is no help file
associated with it, but the IQ of the foster
child is recorded in `Foster` and the IQ of the biologically-raised
child in `Biological`. The socieoeconomic status of the biological
family was also recorded in `Social`.

### Exercise 1

Create a plot of the relationship between the IQs of the two
    children and describe the relationship in words (there is no clear
    independent variable here, so just put `Biological` on the x-axis).
    
**********************************************************************

**********************************************************************

### Exercise 2

We will use `infer`to generate a plot with 12 facets, each one a
    scatterplot illustrating what the data would look like if the IQ of
    the foster child is independent of the IQ of the child raised by the
    biological parents. 
    
Run the following code to generate the plots. Be sure you understand what each line of code is doing.

```{r eval = T}
twins %>%
  specify(Foster ~ Biological) %>%
  hypothesize(null = "independence" ) %>%
  generate(reps = 12, type = "permute" ) %>%
  ggplot(aes(x = Biological , y = Foster)) +
  geom_point() +
  facet_wrap(~ replicate)
```

Does your plot from question 1 look like these other plots or is it 
  systematically different? How so?


**********************************************************************

**********************************************************************

### Exercise 3

Fit a linear model to this data and have a look at the regression table
for the model. Which one of the following is *false* and why?
    
  A. For each 10 point increase in the biological twin’s IQ, we
        expect the foster twin’s IQ to increase on average by roughly 9
        points.
        
  B. The linear model (the estimated mean function) is approximately
        $\hat{y} =9.2+0.9$x.
        
  C. Foster children with higher than average IQs (average of the
        foster children) will *always* have a biological twins with higher than
        average IQs as well (average of the biological children).


**********************************************************************

**********************************************************************

### Exercise 4

Is it reasonable to draw conclusions from this model? Assess the
    conditions for a valid model based on the context of this problem
    using appropriate plots. Recall that it is only appropraite to 
    make predictions based on a regression model if:
    
  1. The data should show a linear trend.
  
  2. Observations in the sample are independent.
  
  3. The distribution of residuals is approximately Normal.
  
  4. The residuals have equal variability.
  
  

**********************************************************************

**********************************************************************

### Exercise 5

This study was used to weigh in on the question of whether IQ is a
    result of nature (your genes) or nurture (your environment). If IQ
    was purely a result of nurture, what slope would you expect to see
    in your linear model?
    
Run the following code to generate a null distribution for slopes, assuming
    Foster IQ and Biological IQ are independent. Then use `infer` to visualize
    the distribution and compute the p_value. Compare the P-value you get here
    to the one given for slope in the regression table in Exercise 2.

```{r cache = T}
twins_null<-twins %>% 
  specify(Foster ~ Biological) %>% 
  hypothesize(null = "independence") %>% 
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "slope")
```

**********************************************************************

**********************************************************************

### Exercise 6

At the other end of the argument, the distribution of IQ might be
    entirely determined by genes. What slope would you expect to see
    if this were true?

Run the following code to generate a bootstrap distribution for slopes. 
    Then use `infer` and the percentile method to create a 95% confidence interval
    on your slope. Compare the confidence interval here to the one given in the
    regression table in Exercise 2. Finally, assess whether this claim is reasonable
    given your data set.
    
```{r}
twins_boot<-twins %>% 
  specify(Foster ~ Biological) %>% 
  generate(reps = 1000, type = "bootstrap") %>% 
  calculate(stat = "slope")
```


**********************************************************************

**********************************************************************

### Exercise 7

Now consider how the association between IQs would change if social
    class were taken into account. Create a plot to illustrate the
    relationship between all three variables (recall the use of the
    `color` argument in `aes()`). Just by looking at the plot and
    without fitting any models, does it appear that the relationship
    between the IQs is the same for high social class children as it is
    for low social class children?

**********************************************************************

**********************************************************************

### Exercise 8

Considering the results that you found and what you know about the controversy surrounding the data, would you report these results?  Why, or why not?

**********************************************************************

**********************************************************************




### Data Set: Friday the 13th

The `friday` data set from the `openintro` package contains traffic and shopping patterns, and accident frequencies for Fridays the 6th and Fridays the 13th between October 1989 and November 1992, in Southeast England. It was originally collected by Scanlon, T.J., Luben, R.N., Scanlon, F.L., Singleton, N. with assistance from the British Department of Transport. The data can be loaded with the following code:

```{r message = F, warning  = F}
library(openintro)
data("friday")
```

The data frame contains 61 observation on 6 variables. Run the following code to view the codebook explaining each variable in the data. *Note that `eval = F` is used so the help file does not produce an error when knitting the document.*

```{r eval = F}
?friday
```

## Exercise 9

This problem makes use of Dataset `friday`. Be sure to run the corresponding code chunks before beginning this problem.

The researchers who originally collected and organized the data were interested in determining how superstitions surrounding Friday the 13th (a day considered by some to be unlucky) effects behavior. Specifically, they are interested in whether the amount of traffic flow differs between a Friday the 13th and the Friday the 6th immediately preceding it. The differences in traffic flow amounts (`sixth - thirteenth`) at a given location are recorded in the `diff` column of the data frame.

a. In the `friday` dataset, how many observations in the data set correspond to traffic flow? What does a single observational unit represent? What is one reason you think that there may be so few observations?

*********************************************************************************

*********************************************************************************

b. The `friday` dataset contains traffic data from two locations (approximately 10 miles apart on the same motorway). How reasonable is it to assume the traffic flow on a given day at the two locations are independent? Briefly explain. Does it seem reasonable to treat these observations as an SRS from a single population? Briefly explain.

*********************************************************************************

*********************************************************************************

c.  Does this investigation represent an experiment or an observational study? Briefly explain. What elements of randomization are present in the investigation?

*********************************************************************************

*********************************************************************************

d. Consider the variable `diff` measuring difference in traffic flow. Write null and alternative hypotheses involving parameters for this variable that could be used to answer the research question.

*********************************************************************************

*********************************************************************************


e. Use `infer` to create and visualize a null distribution with 2000 replicates. Describe the shape, center and spread of the distribution.

*********************************************************************************

*********************************************************************************

f. Use `infer` to calculate the P-value of this sample and make a conclusion about your hypotheses at the $\alpha = 0.05$ significance level.

*********************************************************************************

*********************************************************************************

### Exercise 10

This exercise continues the investigation into the `friday` dataset introduced in the previous exercise.

a. Use theory-based methods to calculate the relevant test statisic for the hypotheses identified in part 3d.

*********************************************************************************

*********************************************************************************


b. Use thery-based methods to find the p-value for this sample.

*********************************************************************************

*********************************************************************************

c. Compare the p-values obtained in parts 3f and 4b. Which procedure do you feel like is more appropraite for this situation?


*********************************************************************************

*********************************************************************************

d. Based on your answer to part 3f and 4b, **AND** your answers to parts 3a - 3c, do you feel this sample gives strong evidence that, in general, Friday the 13th is associated with a decrease in traffic flow amounts? Briefly elaborate.

*********************************************************************************

*********************************************************************************
