---
title: "AE 06: Linear regression"
author: "Your Name Here"
date: "Date Here"
format: html
---

Due Thursday, April 16.

## Learning goals

-   Use linear regression to quantify linear association between two continuous variables.

-   Interpret linear regression output

## Data: mtcars

We'll reuse the mtcars dataset from the previous lecture/AE.

Load the data by running the code:

```{r}
#| warning: false
#| message: false
data(mtcars)
library(tidyverse)
library(ggcorrplot)
```

Let's take another quick look at the contents:

```{r}
?mtcars
glimpse(mtcars)

```

Let's focus on a small number of variables:

-   mpg: miles per gallon (fuel efficiency)

-   cyl: number of cylinders (bigger/stronger engines often have more cylinders).

-   hp: horsepower, a measure of engine power, or the rate at which work is done.

-   qsec: Time to complete a quarter mile (i.e. speed of car in a drag race).

Let's limit the data to just these variables, and then look again at the correlation plot (we saw the full correlation plot for all variables in lecture).

```{r}
data <- mtcars |>
  select(mpg, cyl, hp, qsec)

corr <- round(cor(data), 1)
ggcorrplot(corr)
```

The darker red colors are higher correlations (close to 1), the darker purple correlations are lower correlations (close to -1), and the paler colors are smaller correlations (closer to 0).

We'll take a closer look at some pairs of variables using linear regression to understand their relationships better.

How are the number of cylinders and miles per gallon associated?

```{r}
fit_cyl_mpg <- lm(mpg ~ cyl, data=mtcars)
plot(mtcars$cyl, mtcars$mpg)

# you can plug in the line from the linear regression to the plot like this:
abline(fit_cyl_mpg) 

# Summarize the linear regression model results:
summary(fit_cyl_mpg)
```

The plot shows an approximately linear association between the number of cylinders in the engine, and the miles per gallon.

From the linear regression model results, we conclude that at $\alpha=.05$, we have evidence that the number of cylinders is associated with miles per gallon of the car. For every additional cylinder, we expect a decrease of 2.88 mpg.


## Exercise 1 - horsepower and quarter mile time

a. Using code from above, make a scatter plot with horsepower on the x-axis, and quarter-mile time on the y-axis; fit the linear regression model with quarter mile time as the outcome variable and horsepower as the independent variable; and add the regression line to the plot.
```{r}
# Linear regression and scatter plot

# [type code here]
```

b. How would you interpret and explain the linear regression and scatter plot you obtained?

\[type response here\]

## Exercise 2 — horsepower and miles per gallon

a. Using code from above, make a scatter plot with horsepower on the x-axis, and mpg on the y-axis; fit the linear regression model with mpg as the outcome variable and horsepower as the independent variable; and add the regression line to the plot.
```{r}
# Linear regression and scatter plot

# [type code here]
```

b. How would you interpret and explain the linear regression and scatter plot you obtained?


## Submission

Render to PDF and submit on Canvas \> Assignments.
