---
title: "Regression Assumptions"
author: "Your Name"
output: html_document
---

```{r}
# Load required packages
library(ggplot2)
library(dplyr)

# Read in the data
homes <- read.delim("http://sites.williams.edu/rdeveaux/files/2014/09/Saratoga.txt")

# Look at the first 6 rows
head(homes)
```


```{r}
# Randomly sample 500 homes
homes_sample <- sample_n(homes, size = 500)
```

**A first try at a model**

```{r}
ggplot(homes_sample, aes(x = ???, y = ???)) +
    geom_???() +
    geom_smooth()
```

```{r}
# Fit the model
mod1 <- lm(Price ~ Age, data = homes_sample)

# Display the summary table
summary(mod1)
```

```{r}
# Put the residuals and predicted values into a dataset
mod1_output <- data.frame(
    residual = residuals(mod1),
    predicted = fitted.values(mod1)
)

# Plot
ggplot(mod1_output, aes(x = ???, y = ???)) +
    geom_point() +
    geom_smooth(color = "blue", lwd = 3) + # Add a smooth trend
    geom_hline(yintercept = 0, color = "red") # Add the y = 0 line
```

```{r}
ggplot(mod1_output, aes(sample = residual)) +
    geom_qq() +
    geom_qq_line()
```

**A second model**

```{r}
# log() = natural logarithm
# The %>% is a "pipe": it takes the dataset to the left and provides it as input to the code that follows
homes_sample <- homes_sample %>%
    mutate(
        log_price = log(Price),
        log_age = log(Age + 1) # Some Age's are 0, so add 1 to prevent log(0), which is undefined
    )
```

```{r}
mod2 <- lm(???, data = homes_sample)
mod2_output <- data.frame(
    residual = residuals(???),
    predicted = fitted.values(???)
)
```

**Implications for statistical inference**

```{r}
confint(mod1)
confint(mod2)
```

If you're curious about the code used to run the simulation in the "Implications for statistical inference" exercise, it's shown below:

```{r echo=FALSE}
# The homes dataset is the full "population"
# Add the natural log-transformed price and age variables
homes <- homes %>%
    mutate(
        log_price = log(Price),
        log_age = log(Age + 1)
    )
# Fit the linear regression models on the full population data
mod1_population <- lm(Price ~ Age, data = homes)
mod2_population <- lm(log_price ~ log_age, data = homes)

# True population values for the coefficients on Age and log_age
true_coeff_mod1 <- coefficients(mod1_population)[2]
true_coeff_mod2 <- coefficients(mod2_population)[2]

# set.seed() ensures that the same random numbers are generated each time the code is run
# It is set for reproducibility reasons
set.seed(333)
# Do the simulation 1000 times
sim_results <- replicate(1000, {
    # Sample 500 homes from the population
    homes_new_sample <- sample_n(homes, size = 500)
    # Fit model 1 and get CI for the Age coefficient
    mod1_new_sample <- lm(Price ~ Age, data = homes_new_sample)
    ci_mod1 <- confint(mod1_new_sample)[2,]
    # Fit model 2 and get CI for the log_age coefficient
    mod2_new_sample <- lm(log_price ~ log_age, data = homes_new_sample)
    ci_mod2 <- confint(mod2_new_sample)[2,]
    # Check if each CI contains the true population value
    c(contains_truth_mod1 = true_coeff_mod1 >= ci_mod1[1] & true_coeff_mod1 <= ci_mod1[2], contains_truth_mod2 = true_coeff_mod2 >= ci_mod2[1] & true_coeff_mod2 <= ci_mod2[2])
})
```


