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

**Due Date:** Thursday 09/23/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 you have questions or concerns, please feel free to ask the instructor.
 
 * **Please save your work as pdf file(s), 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, include = FALSE}
# NOTE: The file _common.R and the majority of the code snippets below are from the OpenIntro: IMS Github repository https://github.com/OpenIntroStat/ims
source("_common.R")
```

## I. Simple Linear Regression (SLR)

I.A. Consider the scatterplot of $x$ and $y$ variables with a best fit linear model shown in Fig. \@ref(fig:x-y-scatterplot) and the table of sample statistics in Table \@ref(tab:summary-stats-of-xy). For the following problems, you can use R as a calculator but write the formula you used to get to your answer.

```{r echo=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(gridExtra)
# make synthetic data
set.seed(5)
n <- 500
slope_unreal <- 1.75
intercept_unreal <- 0
x <- runif(n, min=0, max=1)*100
y <- abs(intercept_unreal + slope_unreal*x + rnorm(n, mean=0, sd=20))
df <- data.frame(x,y)
lm_mod <- lm(y ~ x, data = df)
cor_xy <- cor(x,y)
residuals <- data.frame(e=resid(lm_mod))
residuals <- residuals %>% mutate(x = x, y = y)
sst <- sum((y - mean(y))^2)
sse <- sum(residuals$e^2)
```

  1. The correlation coefficient of the two numerical variables $x$ and $y$ is $r =$ `r cor_xy`. Compute the slope estimate of the linear fit using the least squares method.
  
  2. Apply the point-slope equation using the means $(\bar{x},\bar{y})$ and the slope to write the equation of the line.
  
  3. Suppose that each observation is a book where x is the `complexity` in \% and y is `read_time` in hours. Write the linear equation in this context and interpret the slope.
  
  4. The SST of the model is `r sst` and the SSE of the model is `r sse`. Compute the coefficient of determination ($R^2$). Interpret this value in the context of the model.
  
  5. Consider the histogram of residuals in Fig. \@ref(fig:resid-scatter-hist) (left) and the scatterplot of residuals in \@ref(fig:resid-scatter-hist) (right). Is the linear regression model appropriate for the data it represents? Explain why.

```{r summary-stats-of-xy, echo=FALSE, fig.align='center', fig.pos="H"}
library(kableExtra)
stats <- rbind(x = data.frame(n = n, mean = mean(x), sd = sd(x)), y = data.frame(n = n, mean = mean(y), sd = sd(y)))
kable(stats, format='latex', booktabs=TRUE, caption = "The number of observations, mean, and standard deviation of the x and y variables.") %>% kable_styling(position = "center",latex_options = c("striped", "hold_position"))
```

```{r x-y-scatterplot, fig.cap = "Scatterplot of variables x and y. The red line is the best fit linear model of the data.", fig.pos="H", out.width='50%'}
ggplot(df, aes(x = x, y = y)) +
  geom_point(alpha = 0.50, fill = IMSCOL["black", "full"], shape = 21, size = 3) +
  geom_smooth(method = "glm", color = IMSCOL["red", "full"], se = FALSE) +
  labs(x = "x",y = "y") +
  scale_x_continuous() +
  scale_y_continuous()
```

```{r resid-scatter-hist, fig.cap = "Histogram and scatterplot of the residuals.", echo=FALSE, fig.align='center', fig.pos="H", out.width='50%'}
histogram <- ggplot(data=residuals, aes(x=e)) + geom_histogram(bins=10, color="black") + 
  xlab("Residuals") +  ylab("Frequency") + ggtitle("Histogram of Residuals")

scatter <- ggplot(data=residuals, aes(x = x, y = e)) + geom_point(size = 1.5) + # scatter plot
  xlab("x") +  ylab("Residual") + # x and y label +
  geom_hline(yintercept=0, linetype='dashed', col = 'red') +
  ggtitle("x vs Residuals")

grid.arrange(histogram, scatter, nrow=1, ncol=2)
```

\newpage

I.B. Consider the scatterplot of $x$ and $y$ variables with a best fit linear model shown in Fig. \@ref(fig:x-y-scatterplot-2) and the table of sample statistics in Table \@ref(tab:summary-stats-of-xy-2). For the following problems, you can use R as a calculator but write the formula you used to get to your answer.

```{r echo=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(gridExtra)
# make synthetic data
set.seed(5)
n <- 100
slope_unreal <- -2
intercept_unreal <- 10000
x <- seq(0,1,length.out=n)*100 #runif(n, min=0, max=1)*100
y <- intercept_unreal + slope_unreal*x^2 + rnorm(n, mean=0, sd=1000)
df <- data.frame(x,y)
lm_mod <- lm(y ~ x, data = df)
cor_xy <- cor(x,y)
residuals <- data.frame(e=resid(lm_mod))
residuals <- residuals %>% mutate(x = x, y = y)
sst <- sum((y - mean(y))^2)
sse <- sum(residuals$e^2)
```

  1. State the assumptions of the least squares regression.

  2. The correlation coefficient of the two numerical variables $x$ and $y$ is $r =$ `r cor_xy`. Compute the slope estimate of the linear fit using the least squares method.
  
  3. Apply the point-slope equation using the means and the slope to write the equation of the line.
  
  4. The SST of the model is `r sst` and the SSE of the model is `r sse`. Compute the coefficient of determination ($R^2$). Interpret this value in the context of the model.
  
  5. Consider the histogram of residuals in Fig. \@ref(fig:resid-scatter-hist) (left) and the scatterplot of residuals in \@ref(fig:resid-scatter-hist) (right). Even though the $R^2$ value is decent, explain why we might want to use other models for this type of data. What assumption is not met? What type of model can you suggest that might best fit this data?

```{r summary-stats-of-xy-2, echo=FALSE, fig.align='center', fig.pos="H"}
library(kableExtra)
stats <- rbind(x = data.frame(n = n, mean = mean(x), sd = sd(x)), y = data.frame(n = n, mean = mean(y), sd = sd(y)))
kable(stats, format='latex', booktabs=TRUE, caption = "The number of observations, mean, and standard deviation of the x and y variables.") %>% kable_styling(position = "center",latex_options = c("striped", "hold_position"))
```

```{r x-y-scatterplot-2, fig.cap = "Scatterplot of variables x and y. The red line is the best fit linear model of the data.", fig.pos="H", out.width='50%'}
ggplot(df, aes(x = x, y = y)) +
  geom_point(alpha = 0.50, fill = IMSCOL["black", "full"], shape = 21, size = 3) +
  geom_smooth(method = "glm", color = IMSCOL["red", "full"], se = FALSE) +
  labs(x = "x",y = "y") +
  scale_x_continuous() +
  scale_y_continuous()
```

```{r resid-scatter-hist-2, fig.cap = "Histogram and scatterplot of the residuals.", echo=FALSE, fig.align='center', fig.pos="H", out.width='50%'}
histogram <- ggplot(data=residuals, aes(x=e)) + geom_histogram(bins=10, color="black") + 
  xlab("Residuals") +  ylab("Frequency") + ggtitle("Histogram of Residuals")

scatter <- ggplot(data=residuals, aes(x = x, y = e)) + geom_point(size = 1.5) + # scatter plot
  xlab("x") +  ylab("Residual") + # x and y label +
  geom_hline(yintercept=0, linetype='dashed', col = 'red') +
  ggtitle("x vs Residuals")

grid.arrange(histogram, scatter, nrow=1, ncol=2)
```

\newpage

## II. Categorical Variables and MLR

II.A. Consider a SLR with one categorical predictor with two levels. We want to build a model that predicts a numerical response variable called "complexity" with a given predictor either group "A" or group "B". 

  1. Since the predictor is a categorical variable with text as levels, we can simplify the model by indicating that it is either group B or not group B (which means group A). Write the linear model equation in this context with the variable $x_B$ with intercept $b_0$ and slope $b_1$. Explain the meaning of the variable $x_B$ and its inputs.

```{r echo=FALSE, message=FALSE}
set.seed(5)
n_A <- 1000
n_B <- 1000
levels <- c("A","B")
group <- rep(levels,times=c(n_A,n_B))
out1_random <- rnorm(n_A,50,8)
out2_random <- rnorm(n_B,70,8)
df <- data.frame(group=group,complexity=c(out1_random,out2_random))
lm_bin <- lm(complexity~group,data=df)
cf <- coef(lm_bin)
rs <- summary(lm_bin)$r.squared
```

  2. Suppose that we fit this model into the data - which is shown as boxplots in Fig. \@ref(fig:boxplot-groups), the least squares regression method yields the intercept $b_0 =$ `r cf[1]` and the slope $b_1 =$ `r cf[2]`. If the value of $x_{B} = 0$ - which means that the input is group A, what is the estimated mean complexity? Does it fall close to the actual means?
  
  3. If the value of $x_{B} = 1$ - which means that the input is group B, what is the estimated mean complexity?

  4. Interpret the slope and intercept in this particular context.

```{r boxplot-groups, fig.cap = "Histogram and scatterplot of the residuals.", echo=FALSE, fig.align='center', fig.pos="H", out.width='50%'}
library(ggplot2)
boxplot <- ggplot(data=df, aes(y=complexity))
boxplot + geom_boxplot(color="black", aes(fill=group)) + 
  ylab("complexity") + ggtitle("Boxplot of Complexity by Group") + 
  theme(axis.ticks.x = element_blank(), # it removes the x or y axis ticks and texts
        axis.text.x = element_blank())
```

\newpage

II.B. The questions below are concepts regarding the Multiple Linear Regression (MLR).

  1. Write the general form of the multiple linear regression model. What are the coefficients and what are the outcome and predictors - or the response and the explanatory variables.
  
  2. What is a common issue of using Multiple Linear Regression (MLR) and how do we prevent such issue?
  
  3. We learned that $R^2$ is the a measure of how well the linear regression fits the data. Explain the difference between $R^2$ and the adjusted $R_{adj}^2$? Include in your explanation in terms of the number of predictors in a linear regression model.

\newpage

## III. Textbook Exercises

**Note:** To view the selected exercises below, please refer to the textbook, *[OpenIntro: Introduction to Modern Statistics (2021)](https://www.openintro.org/book/ims/){target="_blank"} by Mine Çetinkaya-Rundel and Johanna Hardin, First Edition.*

### [Section 7.5](https://openintro-ims.netlify.app/model-slr.html#chp7-exercises){target="_blank"}: Choose any 4 exercise problems below to answer.

**6.** Partners’ ages and heights.

**12.** Crawling babies, correlation.

**20.** Starbucks, calories and carbs.

**22.** Body measurements, regression.

**24.** Cat weights.

**30.** Cherry trees.

<br>

### [Section 8.6](https://openintro-ims.netlify.app/model-mlr.html#chp8-exercises){target="_blank"}: Choose any 4 exercise problems below to answer.

**2.** Dealing with categorical predictors.

**4.** Multiple regression fact checking.

**6.** Baby weights and mature moms.

**8.** Movie returns by genre.

**10.** Palmer penguins, predicting body mass.

**12.** Palmer penguins, backwards elimination.

<br>
