---
title: "Homework 3"
subtitle: "Psychology 2812B FW23"
output: html_document
---

# Lab Component

## 1. ISLR2 Auto dataset

Load the `tidyverse`.

Install and load the `ISLR2` package and inspect the dataset called `Auto`:

```{r}
library(tidyverse)
library(ISLR2)
head(Auto)
```

## 2. Plot car's weight versus fuel efficiency

Plot each car's `weight` (pounds) against its fuel efficiency (`mpg`) (miles per gallon of fuel) and overlay a linear fit using `geom_smooth(method="lm", se=FALSE)`:

```{r}
# your code goes here
```

## 3. Correlation coefficient

Compute the correlation (Pearson's R) between `weight` and `mpg`:

```{r}
# your code goes here
```

## 4. Re-plot with $R$ shown on Figure

Re-plot the data and show the correlation coefficient on the plot.

Hint: the `stat_correlation()` function in the [`ggpmisc`](https://cran.r-project.org/web/packages/ggpmisc/vignettes/model-based-annotations.html) package can do this easily (you may have to `install.packages("ggpmisc")`).

Hint2: you can also add a line using the `stat_poly_line(se=FALSE)` function (or you can do like before and use the `geom_smooth(method="lm", se=FALSE)` function, your choice):

```{r}
# your code goes here
```

## 5. Goodness of fit: $R^{2}$

We are interested in predicting the fuel efficiency of cars (`mpg`, miles per gallon) from their `weight` (pounds). In the previous assignment we fit a linear regression model to predict `mpg` from `weight`.

Report the $R^{2}$ value for the goodness of fit of the regression model. Note you can extract this from the `summary()` of your `lm()` model, it's called `r.squared`.

```{r}
# your code goes here
```

> your sentence goes here

Write a sentence about what this value of $R^{2}$ means (hint: variance accounted for).

> your sentence goes here

## 6. Goodness of fit: $s_{est}$

Report the standard error of the estimate $s_{est}$ of the regression model. Note you can extract this from the `summary()` of your `lm()` model, it's called `sigma`.

```{r}
# your code goes here
```

> your sentence goes here

Write a sentence about what this $s_{est}$ value means in the context of prediction.

> your sentence goes here

## 7. Confidence intervals

Compute the 95% confidence intervals for the regression coefficients (intercept and slope) in your regression model. You can do this by hand using the formula,

**or**

you can use the `confint()` function, just feed it two things, first your regression model (`object=mymod`, assuming you named your regression model `mymod`), and second, the confidence level you want (`level=95%`).

```{r}
# your code goes here
```

> your sentence goes here

Write a sentence about what these confidence intervals mean.

> your sentence goes here


# Home Component

## 8. Interpretation & Inference

Write a sentence or two describing the relationship between `weight` and `mpg`. Say something about the **strength** and the **direction** of the correlation.

> your sentence goes here

Conduct a significance test of the correlation between weight and mpg using `cor.test()`. 

```{r}
# your code goes here
```

Check the normality assumption using shapiro.test().

```{r}
# your code goes here
```

Report the results of your tests.

> your sentences go here

If the normality assumption is violated, re-do the correlation significance test using Spearman's rank correlation and report the results.

```{r}
# your code goes here
```

Based on your work so far write a sentence or two about whether you think a linear relationship exists (or not) and why (or why not).

> your sentence goes here


## 9. Linear Regression model

Compute a linear regression model in which you predict `mpg` from `weight`. Use the `lm()` function in R to define your model. Then use the `summary()` function to display information about your regression model.

```{r}
# your code goes here
```

# Home Component

## 10. Model coefficients

What is the Y-intercept of your regression line?

```{r}
# your code goes here
```

What is the slope of your regression line?

```{r}
# your code goes here
```

Write a sentence that explains the meaning of the slope.

> your sentence goes here

## 11. Significance tests

Report the results of the significance test on the model as a whole. Report the F-statistic, the degrees of freedom, and the p-value. Write sentences for each, e.g. "The F-statistic is ___". 

Hint: this information is given when you execute the `summary()` command and feed it the name of your `lm()` object, e.g. `summary(mymod)` assuming you named your `lm()` model object `mymod`.

```{r}
# your code goes here
```

> your sentence goes here

Write a sentence to say what the p-value means. Specifically, say what this is the probability **of**. Be precise in your language.  If you mention the null hypothesis, be precise about what the null hypothesis is.

> your sentence goes here

Also report the p-values for each of the model coefficients (intercept, slope). Also write a sentence to say what each of these p-values mean. Each of them is the probability of **what** precisely? Say what the null hypotheses are for each p-value test.

> your sentence goes here

## 12. Model prediction

Using your regression model, predict the fuel efficiency in `mpg` of a car whose `weight` is 2000 pounds. Note you can do this by hand using the model coefficients from above,

**or**

you can use the `predict.lm()` function in R to do it. Type `?predict.lm` on the console to view the help documentation. You just feed it two arguments: first, your `lm()` model object, and second, a tibble containing a value for the predictor variable `weight` (e.g. `tibble(weight=2000)`) and it uses the model to predict the corresponding value of the predicted variable `mpg`.

Either of the two above methods is acceptable.

```{r}
# your code goes here
```

> your answer goes here

Write a sentence that explains what this number means.

> your sentence goes here

## 13. Does mpg improve over the years?

Using your knowledge of the functionality within dplyr and ggplot2 packages, repeat the plot from Question 4 above, but plot two separate `geom_smooth()` lines, one for each of two subsets of the cars: (1) for "older" cars with model years between (and including) 1970, 1971, and 1972, and (2) for "newer" cars with model years 1980, 1981, 1982. Hint: remember you can add a `color=___` argument to the `aes()` mapping argument to separate out geoms by color.

Hint: Before you generate your ggplot, you can use the `filter()` function from dplyr to create a new tibble that includes only older and newer cars according to the definition above. Then you could add a new variable to the tibble called `age` (using `mutate()`) that has a value of `"older"` if the `year` is `<=72` and `"newer"` if the `year` is `>=80`. Then you could use the option `color=age` in your ggplot `aes()` mapping.

Or of course you could do it in many other ways of your choosing.

```{r}
# your code goes here
```

## 14. Regression models

Fit two regression models predicting `mpg` from `weight`, one for "older" cars and one for "newer" cars as defined above.

Report the y-intercept for "older" and "newer" cars.

```{r}
# your code goes here
```

> your sentence goes here

Report the slope for "older" and "newer" cars.

> your sentence goes here

```{r}
# your code goes here
```

Write a sentence or two about what a difference in slope and intercept between older and newer cars might mean in the context of the relationship between weight and fuel efficiency.

> your sentence goes here

Answer the following question: based only on the regression lines in the plot above and the differences in slope and intercept, is there anything that we can actually **conclude** about whether fuel efficiency actually improved over the years? Explain your answer. Hint: remember our discussions about randomness and what the null hypothesis is, and about the difference between a sample and a population.

> your sentence goes here

