---
title: "💻 LSE 202A 2023: Week 03 - Lab"
author: "MY NAME (MY CANDIDATE NUMBER)"
date: 09 October 2023
format: html
self-contained: true
---

# ⚙️ Setup

**Import required libraries:**

```{r setup, message=FALSE, warning=FALSE}
# Remember to install.packages() any missing packages
library(tidyverse)
library(tidymodels)
```

**Read the data set:**

It is the same data set you used in the previous lab (as well as in the first formative assignment):

```{r}
# Modify the filepath if needed
filepath <- "data/UK-HPI-full-file-2023-06.csv"
uk_hpi <- read_csv(filepath)
```

**Pre-process the dataset:**

```{r}
# Replicate roughly the same steps you did in the previous lab:

# 1. Select the columns you need
# 2. Rename the columns
# 3. Filter the data set to include only the regions you need (let's focus on England for now)
# 4. Preprocess the date column

selected_cols    <- c("Date", "RegionName", "12m%Change")

df_england <-
    uk_hpi %>%
    select(all_of(selected_cols)) %>%
    rename(date = Date, region = RegionName, yearly_change = `12m%Change`) %>%
    filter(region == "England") %>%
    drop_na(yearly_change) %>% 
    mutate(date = lubridate::dmy(date)) %>% 
    arrange(desc(date))

df_england  %>% head(8) %>% knitr::kable()
```

**Add lagged variables:**

```{r}
# Add lagged variables for the dependent variable
df_england <- 
    df_england %>%
    mutate(yearly_change_lag1 = lag(yearly_change, 1)) %>%
    drop_na()
```


# 📋 Lab Tasks

## Part I - the `lm` command (25 min)

👨🏻‍🏫 **TEACHING MOMENT:** Your class teacher will demonstrate how to run the `lm` function.

**Task 1:** Run a linear regression model with `YearlyChange` as the dependent variable and `Change1MoAgo` as the independent variable. Save the model as `model1`.

```{r}
# Add the code here
```

Print the model summary:

```{r}
# Add the code here
```

🗣️ **DISCUSSION:**

- What is the value of the intercept?

> Write your answer here.

- What is the value of the slope?

> Write your answer here.

- How should you interpret this model?

> Write your answer in these lines. 
> 
> 
> 

**👨🏻‍🏫 TEACHING MOMENT:** Your class teacher will demonstrate how to plot the model.

**Task 2:** Plot the residuals against the fitted values.

```{r}
# Add the code here
```

Alternatively, see all four basic lm diagnostic plots:

```{r}
# Add the code here
```

🗣️ **DISCUSSION:**

Is this a good fit? Why? Why not?

> Write your answer here.

## Part II - the `tidymodels` way (20 min)

👨🏻‍🏫 **TEACHING MOMENT:** Your class teacher will demonstrate how to get the same thing done using the `tidymodels` framework.

**Task 3:** Run a linear regression model with `YearlyChange` as the dependent variable and `Change1MoAgo` as the independent variable. Save the model as `model2`.

```{r}
# Add the code here
```

Print the model coefficients in a tidy way:

```{r}
# Add the code here
```

See the full model summary:

```{r}
# Add the code here
```

Overall statistics:

```{r}
# Add the code here
```

**Task 4:** Augument the model with the residuals and the fitted values and use the resulting data frame to plot the residuals against the fitted values.

```{r}
# Add the code here
```

Then, use `ggplot2` to plot the residuals against the fitted values:

```{r}
# Add the code here
```


## Part III - evaluating the model (45 min)
