---
title: "Midterm 1 Review"
date: "Math 141, Week 6"
output:
  pdf_document
urlcolor: blue
---

```{r setup, include=FALSE}
# Do not modify this chunk.
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(knitr)
library(moderndive)
```


For extra practice, several review problems are provided on this Review Sheet. However, they are not comprehensive, so do not limit your studying to just these problems. While the problems are intended to match the difficulty of those on the exam, the length of this review set does not represent the actual length of the exam. Although detailed solutions will not be posted, you are welcome to talk to me about any of them before the test. 




### Problem 1

A recent study examines the relationship between sleep habits, alcohol use, academic performance, measures of depression and strees, and other variables in US college students. The data were obtained from a sample of 253 studnets who did skills tests to measure congnitive function, completed a suvey that asked many questions about attitudes and habits, and kept a sleep diary to record time and quality of sleep over a two-week period. Some data is available in the `SleepStudy` data frame loaded with the code below.

```{r}
# Load the data
library(Lock5Data)
data(SleepStudy)
```

Run this code chunk to view the documentation for this data set. **Do not** change `eval = F` to `eval = T` for this code chunk.
```{r eval = F}
?SleepStudy
```


 **We want to determine if you can model the sleep quality of student using other variables**

a. Produce a plot comparing sleep quality (response) and DAS score (explanatory). Include the least squares regression line. Discuss any trend you observe.
 

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

 



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



b. Recreate the plot but this time the `LarkOwl` variable, as well. What new trends are evident?

 

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





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



c. Assess whether a linear model is appropriate for this data. Support your answer with appropriate visualizations.

 

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

 



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


d.  Regardless of your previous answer, build the linear regression model for sleep quality using DASScore  Interpret the slope and intercept terms in the context of this model.
 

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

 



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


e.  For a student with DASScore of 50, predict the sleep quality.


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

 



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


f. Now, build a linear regression model predicting sleep quality as a function of LarkOwl level. Which level is used as a baseline? What is the mean sleep quality for this level?


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

 



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

g. Based on the coefficients of your linear model in the previous part, which Lark Owl level has the highest sleep quality? Which has the lowest?


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

 



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


h. What is the predicted sleep quality for Night Owl? What is the residual for a Night Owl?


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

 



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



 
### Problem 2


Biologists studying crows will capture a crow, tag it, and release it. These crows seem to remember the scientists who caught them and will scold them later. A study to examine this effect had several scientists wear caveman masks while they trapped and tagged seven crows. Another group did not tag any crows and wore a different mask. The two masks did not elicit different reactions from the crows before the tagging. Volunteers then strolled around town wearing one or the other of the two masks. The crows scolded a person wearing a caveman mask in 158 out of 444 encounters with crows, whereas crows scolded a person in a neutral mask in 109 out of 922 encounters.


a. Is this an experiment or an observational study? Explain how you know.

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

 



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

b. What is the population the researchers wish to study? What is the sample?

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

 



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

c. List the explanatory and response variables in this investigation, and for each, classify whether it is quantitative or categorical.

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

 



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

d. If you identified this investigation as an observational study, describe what sampling techniques were used to construct the sample. If you identified this as an experiment, discuss whether a control was used, and whether the experiment was blind, double-blind, or neither.
 
********************************************************

 



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

#### Problem 3

Use the following graphic to answer this question.


```{r,echo = FALSE, eval = F}
include_graphics("/home/courses/math141s20/Img/m1geoms.png")
#external url: https://reed-statistics.github.io/math141s21/img/m1geoms.png
```

a. What are the variables displayed in this graphic?  For each variable, specify if it is categorical or numerical.

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

  

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

b. What **geom** are the variables mapped to?

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

  

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

c. What are the **aesthetic**s of the **geom**? For each **aesthetic**, give the variable that sets the values of that **aesthetic**.

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

  

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


### Problem 4

The `mtcars` data set contains information about 32 different car models from the 1970s. For each model, the data set includes records: the name of the model `name`, the fuel efficiency `mpg`, the number of engine cylinders `cyl`, the weight `wt`, the horsepower `hp`, and the transmission type `am` (with 0 corresponding to automatic and 1 corresponding to manual).

Load the data using the following code chunk:

```{r}
data(mtcars)

mtcars <- mtcars %>% mutate(am = as.factor(am)) %>% rownames_to_column(var = "name")
```

a. Use `dplyr` verbs to create a data frame consisting of: three columns recording the name of each model, the number of cylinders, and the transmission type, for exactly those cars which have a fuel efficiency between 10 and 25 miles per gallon, ordered alphabetically by model name.

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

  

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

b. Create a scatterplot comparing `mpg` and `wt`. Based on the graphic, do the variables appear to have a weak, moderate, or strong correlation? If the correlation is weak or moderate, is it positive or negative? 


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

  

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


c. Give a line-by-line explanation for what the following code does:

```{r echo = T, eval = F}
mtcars %>% 
  filter(carb != 8) %>% 
  mutate(hp_per_cyl = hp/cyl) %>% 
  group_by(am) %>% 
  summarize(avg = mean(hp_per_cyl, na.rm = TRUE))
```



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

  

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

d. Create a new categorical variable `fuel` in the `mtcars` dataframe that takes the value `efficient` if `mpg` is greater than or equal to 20, and `inefficient` if `mpg` is less than 20.

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

  

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

e. Identify all reasons why the following code won't run (other than the `eval = FALSE` in the chunk options). Make corrections to create side-by-side boxplots with `mpg` on the horizontal axis (labeled Miles per Gallon) and `am` on the vertical axis (labeled Transmission Type).

```{r eval = FALSE}
mtcars %>% 
  ggplot(x = mpg, y = am) %>% 
  geom_box() %>% 
  labs(x = Miles per Gallon, y = Transmission Type)
```


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

  

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