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

## Overview

The questions and exercises below all include examples using the `mtcars` dataset from the `car` package. This dataset contains information about various cars, e.g., horsepower, engine type, etc. Alternatively, use another dataset of your choosing with a continuous Y variable and a categorical X variable (with two levels).

In this exercise, you will run models in which a car's average miles per gallon (mpg) is predicted by its weight (wt) and/or the type of transmission (am), i.e., automatic vs. manual.

## 0. Load the car package (install if necessary) and `mtcars` dataset. Create a new dataframe with the `mtcars` data  (Also load the tidyverse package.)

Then, view the first 10 rows of the `mtcars` dataset.

```{r}

library(tidyverse)
library(car)

df <- mtcars
head(df, n = 10)

```

## 1. First we need to prep our variables. Start by centering the weight variable (wt).

Bonus: Double check that your code worked by looking at the mean of this new centered variable.

```{r}

df$wt.c <- scale(df$wt, center = TRUE, scale = FALSE)

mean(df$wt.c)

# make sure this variable is a vector
df$wt.c <- as.vector(df$wt.c)

```

## 2. Now, let's take a look at the transmission type (`am`) variable. We went over two ways of coding categorical variables: dummy coding & effect coding. Which one does the dataframe use by default?

Hint: You can get more information about the variables in the `mtcars` dataset by running ?mtcars.

Now, make a new column where you recode `am` in a *different* way. 

```{r}

# dataframe uses dummy coding, where automatic = 0 and manual = 1

df$am.e <- dplyr::recode(df$am, "0" = -0.5, "1" = 0.5)

head(df)

```

# 3. Now, fit a linear model predicting average miles per gallon as a function of weight, transmission type (dummy coded), and their interaction. Print the model output and describe the intercept, main effects, and interaction terms. 

```{r}

# model with the dummy coded variable
m1 <-lm(mpg ~ wt.c*am, data = df)
summary(m1)

```

Intercept: For cars of average weight, for cars with automatic transmisison, the mpg is 19.2358. 

Wt.c: As weight increases by one unit, for cars with automatic transmisssion, mpg decreases by 3.78. This is a significant difference. 

am: When moving from automatic to manual transimission, for cars of average weight, mpg decreases 2.17. The effect of transmission, however, is not significant. 

wt.c:am: There is a significant interaction. Weight decreases mpg more for automatic cars than manual ones. 

# 4. Fit the same linear model, but this time use the effect coded version of transmission type. Print the model output and describe the intercept, main effects, and interaction term. 

```{r}

# model with the effect coded variable
m2 <- lm(mpg ~ wt.c*am.e, data = df)
summary(m2)

```

Intercept: For cars of average weight, for both automatic and manual transmisison, the mpg is 18.52. 

Wt.c: As weight increases by one unit, averaging across cars with automatic and manual transmission transmisssion, mpg decreases by 6.43. This is a significant difference. 

am: When moving from automatic to manual transimission, for cars of average weight, mpg decreases 2.17. The effect of transmission, however, is not significant. 

wt.c:am: There is a significant interaction. Weight decreases mpg more for automatic cars than manual ones.

#5. Compare the two models. Which terms are the same and which are different? Why? How does this change the interpretation of the model? Does the significance change?

The main effects of am and the am:wt.c interaction are the same, while the intercept and the main effect of wt.c are different. 

The intercept and effect of wt.c are different because they hold the am variable at 0, and the value of 0 changes depending on whether the am variable has been dummy or effect coded. 

The intercept in the effect coded version is interpreted as the value of mpg when the car is average weight, across both automatic and transmission (because the average of -.5 and .5 is 0). In the dummy coded version, the intercept is the value of mpg for a car of average weight and automatic (because atumatic is 0 and manual is 1). Similarly, the wt.c effect in the effect coded version is the unit increase in wt.c on mpg averaging across both transmission types, while in the dummy coded its the unit increase for automatic cars. 

There is no difference in significance, only the interpretation of the estimates. 

#6. Plot the model output. You can plot either the dummy coded or the effect coded model--it's up to you!

```{r}

# turn variables into vectors (again, to be safe)
df$wt.c <- as.vector(df$wt.c)
df$am.e <-as.vector(df$am.e)

# create high and low data frames
dfhigh <- data.frame(
  wt.c = seq(min(df$wt.c, na.rm = T), max(df$wt.c, na.rm = T), .1),
  am.e = .5) 

dflow <- data.frame(
  wt.c = seq(min(df$wt.c, na.rm = T), max(df$wt.c, na.rm = T), .1),
  am.e = -.5)

# create predicted regression lines
high <- cbind(dfhigh, predict(m2, dfhigh, interval = "confidence"))
low <- cbind(dflow, predict(m2, dflow, interval = "confidence"))

plot <- ggplot(high, aes(wt.c, fit)) +
  geom_point(data = df,
             aes(wt.c, mpg, color = as.factor(am.e)),
             alpha = .4, size = 1, show.legend = T) +
  geom_ribbon(data = high, aes(ymin = lwr, ymax = upr), 
              alpha = .3, fill = "orange") +
  geom_line(data = high, aes(wt.c, fit), 
            size = 2, color = "orange", linetype = "dashed") +
  geom_ribbon(data = low, aes(ymin = lwr, ymax = upr), 
              alpha = .3, fill = "green") +
  geom_line(data = low, aes(wt.c, fit), 
            size = 2, color = "green") +
  labs(x = "Weight", y = "MPG",  title = " ") +
  scale_color_manual(name = "AM", labels = c("Automatic", "Manual"), values = c("green", "orange"))

plot

```

# Bonus

Plot the model output for the other model. Does anything change?

Run a model with just a main effect (either of the `am` variables or `wt.c`). Interpret the output and plot the model. 

