```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

**Due Date:** Wednesday 12/08/2021, 11:59 PM

**Instructions:** <br>
 
 * Please provide complete answers/solutions for each question/problem. 
 
 * **If it involves mathematical computations, please provide your reasoning and/or detailed solutions.**
 
 * There are two ways you can write your answers, a: by handwriting (either physically or digitally), or b: by typing on a template document with file type options, Word or RMarkdown, which can be downloaded from the [course website](https://reed-statistics.github.io/math141-fall2021/homeworks.html){target="_blank"}.
 
 * If you had handwritten your answers/solutions on a physical paper, make sure to label it properly and please scan your document using a scanner app for convenience. Suggestions: (1) ["Tiny Scanner" for Android](https://play.google.com/store/apps/details?id=com.appxy.tinyscanner&hl=en_US&gl=US){target="_blank"} or (2) ["Scanner App" for iOS](https://apps.apple.com/us/app/scanner-app-scan-pdf-document/id595563753){target="_blank"}.
 
 * If a problem asks you to show your R code, R outputs, or R plots, please provide them as additional pages into your current homework pdf while labeling them properly. This means that, **if you have handwritten your homework solutions and saved it as pdf, you would need to merge the separate pdf which contains your R code, R outputs, or R plots. Note that all of the problems that require R does not require you to show your R code - unless the problem specifically says so.**
 
 * If you have questions or concerns, please feel free to ask the instructor.
 
 * **Please save your work as one pdf file, don't put your name in any part of the document, and submit it to the Gradescope page for this course. Your document upload will correspond to your name automatically in Gradescope.**

\newpage

```{r echo=FALSE, message=FALSE}
# load libraries
library(openintro)
library(tidyverse)
library(dplyr)
library(kableExtra)
library(broom)
library(GGally)
library(patchwork)
```

## I. Inference for Comparing Many Means

The exercise problems shown below was taken and slightly modified from your textbook [OpenIntro: Introduction to Modern Statistics Section 22.5 ](https://openintro-ims.netlify.app/inference-many-means.html#chp22-exercises){target="_blank"}

### 1. **Student Performance Across Discussion Sections.**

Consider the following problem statement.

> A professor who teaches a large introductory statistics class (197 students) with eight discussion sections would like to test if student performance differs by discussion section, where each discussion section has a different teaching assistant. The summary table below shows the average final exam score for each discussion section as well as the standard deviation of scores and the number of students in each section.

```{r echo=FALSE}
tribble(
  ~` `,   ~`Sec 1`, ~`Sec 2`, ~`Sec 3`, ~`Sec 4`, ~`Sec 5`, ~`Sec 6`, ~`Sec 7`, ~`Sec 8`,
  "Mean",    92.94,    91.11,    91.80,    92.45,    89.30,    88.30,    90.12,    93.35,
  "SD",       4.21,     5.58,     3.43,     5.92,     9.32,     7.27,     6.93,     4.57,
  "n",          33,       19,       10,       29,       33,       10,       32,       31
) %>%
  kbl(linesep = "", booktabs = TRUE) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position", 
                full_width = FALSE) %>%
  column_spec(1:9, width = "4em")
```

The ANOVA output below can be used to test for differences between the average scores from the different discussion sections.
    
```{r echo=FALSE}
tribble(
  ~` `,         ~`df`,  ~`sumsq`, ~`meansq`, ~`statistic`, ~`p.value`,
  "section",        7,    525.01,     75.00,         1.87,     0.0767,
  "Residuals",    189,   7584.11,     40.13,           NA,         NA,
  "Total",        196,   8109.12,        NA,           NA,         NA  
) %>%
  kbl(linesep = "", booktabs = TRUE, align = "lrrrrrr", 
      format.args = list(big.mark = ",")) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position", 
                full_width = FALSE) %>%
  column_spec(1:6, width = "5em")
```

  a. Conduct a hypothesis test to determine if these data provide convincing evidence that the average score varies across some (or all) groups. 
  
  b. Check conditions and describe any assumptions you must make to proceed with the test.

\newpage

### 2. **Work Hours and Education.**

Consider the following problem statement.

> The General Social Survey collects data on demographics, education, and work, among many other characteristics of US residents. [NORC 2010](https://gss.norc.org/){target="_blank"} Using ANOVA (ANalysis Of VAriance), we can consider educational attainment levels for all 1,172 respondents at once. Below are the distributions of hours worked by educational attainment and relevant summary statistics that will be helpful in carrying out this analysis.

```{r out.width = "70%", echo=FALSE, fig.align='center'}
gss2010_hrs_degree  <- gss2010 %>%
  drop_na(hrs1, degree) %>%
  mutate(
    degree = str_to_title(degree),
    degree = fct_relevel(degree, "Lt High School", "High School",
                         "Junior College", "Bachelor", "Graduate")
  )

gss2010_hrs_degree %>%
  group_by(degree) %>%
  summarise(
    mean = mean(hrs1),
    sd   = sd(hrs1),
    n    = n()
  ) %>%
  kbl(linesep = "", booktabs = TRUE,
      col.names = c("Educational attainment", "Mean", "SD", "n")) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position",
                full_width = FALSE) %>%
  column_spec(1, width = "12em") %>%
  column_spec(2:4, width = "5em")

ggplot(gss2010_hrs_degree, aes(x = degree, y = hrs1)) +
  geom_boxplot() +
  labs(x = NULL, y = "Hours worked per week")
```

  a. Write hypotheses for evaluating whether the average number of hours worked varies across the five groups.

  b. Check conditions and describe any assumptions you must make to proceed with the test.

  c. Below is the output associated with this test. What is the conclusion of the test?

```{r echo=FALSE, out.width="70%"}
lm(hrs1 ~ degree, data = gss2010_hrs_degree) %>%
  anova() %>%
  tidy() %>%
  mutate(p.value = ifelse(p.value < 0.0001, "<0.0001", round(p.value, 4))) %>%
  kbl(linesep = "", booktabs = TRUE,
      digits = 2, format.args = list(big.mark = ",")) %>% 
  kable_styling(bootstrap_options = c("striped", "condensed"), 
            latex_options = "HOLD_position") %>%
  column_spec(1, width = "8em", monospace = TRUE) %>%
  column_spec(2:6, width = "5em")
```
    
\newpage
  
## II. Inference for Multiple Linear Regression

The exercise problems shown below was taken and slightly modified from your textbook [OpenIntro: Introduction to Modern Statistics Section 25.5](https://openintro-ims.netlify.app/inf-model-mlr.html#chp25-exercises){target="_blank"}.

### 1. **Cherry Trees.**

Consider the following problem statement.

> Timber yield is approximately equal to the volume of a tree, however, this value is difficult to measure without first cutting the tree down. Instead, other variables, such as height and diameter, may be used to predict a tree's volume and yield. Researchers wanting to understand the relationship between these variables for black cherry trees collected data from 31 such trees in the Allegheny National Forest, Pennsylvania. Height is measured in feet, diameter in inches (at 54 inches above ground), and volume in cubic feet. [Hand 1994](https://openintro-ims.netlify.app/references.html#ref-Hand:1994){target="_blank"}

The [`cherry`](http://openintrostat.github.io/openintro/reference/cherry.html) data used in this exercise can be found in the [**openintro**](http://openintrostat.github.io/openintro) R package.

The plots below show the distribution of each of these variables (on the diagonal) as well as provide information on the pairwise correlations between them.

```{r out.width = "70%", fig.asp = 1, echo=FALSE, fig.align='center', message=FALSE}
cherry %>%
  ggpairs(diag = list(continuous = "barDiag"),
          switch = "both") +
  theme(legend.position = "none",
    panel.grid.major = element_blank(),
    axis.ticks = element_blank(),
    axis.title.x = element_text(angle = 180, vjust = 1, color = "black"),
    panel.border = element_rect(fill = NA))
```
  
\newpage
  
Provided below are three regression model outputs: `volume` vs. `diam`, `volume` vs. `height`, and `volume` vs. `height + diam`.
    
```{r echo=FALSE, message=FALSE}
lm(volume ~ diam, data = cherry) %>% 
  tidy() %>%
  mutate(p.value = ifelse(p.value < 0.0001, "<0.0001", round(p.value, 4))) %>%
  kbl(linesep = "", booktabs = TRUE, align = "lrrrr", digits = 3) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position",
                full_width = FALSE) %>%
  column_spec(1, width = "10em", monospace = TRUE) %>%
  column_spec(2:5, width = "5em")

lm(volume ~ height, data = cherry) %>% 
  tidy() %>%
  mutate(p.value = ifelse(p.value < 0.0001, "<0.0001", round(p.value, 4))) %>%
  kbl(linesep = "", booktabs = TRUE, align = "lrrrr", digits = 3) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position",
                full_width = FALSE) %>%
  column_spec(1, width = "10em", monospace = TRUE) %>%
  column_spec(2:5, width = "5em")

lm(volume ~ height + diam, data = cherry) %>% 
  tidy() %>%
  mutate(p.value = ifelse(p.value < 0.0001, "<0.0001", round(p.value, 4))) %>%
  kbl(linesep = "", booktabs = TRUE, align = "lrrrr", digits = 3) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position",
                full_width = FALSE) %>%
  column_spec(1, width = "10em", monospace = TRUE) %>%
  column_spec(2:5, width = "5em")
```

  a. There are three variables described in the figure, and each is paired with each other to create three different scatterplots. Rate the pairwise relationships from most correlated to least correlated.
    
  b. When using only one variable to model a tree's `volume`, is `diam`eter a significant predictor variable?  Is `height` a significant predictor variable?  Explain.
    
  c. When using both `diam`eter and `height` to predict a tree's `volume`, are both predictor variables still significant? Explain.
    
\newpage
    
### **2. Baby's Weight.**

Consider the following problem statement.

> US Department of Health and Human Services, Centers for Disease Control and Prevention collect information on births recorded in the country. The data used here are a random sample of 1,000 births from 2014. Here, we study the relationship between smoking and weight of the baby. The variable `smoke` is coded 1 if the mother is a smoker, and 0 if not. [ICPSR 2014](https://www.icpsr.umich.edu/web/ICPSR/studies/36461/versions/V1){target="_blank"}

The summary table below shows the results of a linear regression model for predicting the average birth weight of babies, measured in pounds, based on the smoking status of the mother. 

The [`births14`](http://openintrostat.github.io/openintro/reference/births14.html) data used in this exercise can be found in the [**openintro**](http://openintrostat.github.io/openintro) R package.

```{r echo=FALSE, message=FALSE}
m_babies_full <- lm(weight ~ weeks + mage + sex + visits + habit, data = births14)

m_babies_full %>%
  tidy() %>%
  mutate(p.value = ifelse(p.value < 0.0001, "<0.0001", round(p.value, 4))) %>%
  kbl(linesep = "", booktabs = TRUE, align = "lrrrr", digits = 2) %>%
  kable_styling(bootstrap_options = c("striped", "condensed"), 
                latex_options = "HOLD_position",
                full_width = FALSE) %>%
  column_spec(1, width = "10em", monospace = TRUE) %>%
  column_spec(2:5, width = "5em")
```

Also shown below are a series of diagnostics plots.

```{r fig.asp = 1, echo=FALSE, out.width="60%", fig.align='center'}
m_babies_full_aug <- augment(m_babies_full)

p_res_hist <- ggplot(m_babies_full_aug, aes(x = .resid)) +
  geom_histogram(bins=30) +
  labs(x = "Residual", y = "Count", title = "Histogram of residuals")

p_res_order <- ggplot(m_babies_full_aug, aes(y = .resid, x = as.numeric(.rownames))) +
  geom_point() +
  labs(x = "Data order", y = "Residual", title = "Residuals vs. \ndata order")

p_res_pred_1 <- ggplot(m_babies_full_aug, aes(x = .fitted, y = .resid)) +
  geom_point(alpha = 0.8, size = 1) +
  labs(x = "Predicted weight", y = "Residual", title = "Residuals vs. \npredicted values")

p_res_pred_2 <- ggplot(m_babies_full_aug %>% filter(.fitted > 6 & .fitted < 8.5), aes(x = .fitted, y = .resid)) +
  geom_point(alpha = 0.8, size = 1) +
  labs(x = "Predicted weight", y = "Residual", 
       title = "Residuals vs. \npredicted values",
       subtitle = "Predicted weights between \n6 and 8.5 pounds")

(p_res_hist + p_res_order) /
  (p_res_pred_1 + p_res_pred_2)
```
    
  a. Determine if the conditions for doing inference based on mathematical models with these data are met using the diagnostic plots above. If not, describe how to proceed with the analysis.
    
  b. Using the regression output, evaluate whether the true slope of `habit` (i.e. whether the mother is a smoker) is different than 0, given the other variables in the model. State the null and alternative hypotheses, report the p-value (using a mathematical model), and state your conclusion.
    