---
title: "Practice Set 4"
author: "STUDENT NAME"
date: now
date-format: "YYYY-MM-DDTHH:mm:ssZ"
format:
  html:
    toc: true
    toc-depth: 2
    embed-resources: true
    code-tools: true
    df-print: paged
---

```{r setup}
#| include: false

# Do NOT modify this chunk
knitr::opts_chunk$set(
  collapse = TRUE, 
  warning = FALSE,
  message = FALSE,
  fig.height = 2.75, 
  fig.width = 4.25,
  fig.env = 'figure',
  fig.pos = 'h',
  fig.align = 'center')

# Use a color blind friendly color palette throughout doc
library(tidyverse)
cb_palette <- c("black", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
scale_colour_discrete <- function(...) scale_colour_manual(values = cb_palette, ...)
scale_fill_discrete   <- function(...) scale_fill_manual(values = cb_palette, ...)
theme_set(theme_bw())
```


# Purpose

The goal of this set of practice problems is to practice the following skills:

- Visualize interactions between categorical and quantitative predictors
- Write a model formula with an interaction term
- Interpret coefficients in an interaction model in context
- Critically determine whether interaction terms should be included in multiple linear regression models



# Directions

1. Create a code chunk in which you load the `ggplot2`, `dplyr`, and `readr` packages. Include the following commands in the code chunk to read in *two* data sets: 

`enroll <- read_csv("https://mac-stat.github.io/data/school_enrollment.csv")`
`bikes <- read_csv("https://mac-stat.github.io/data/bikeshare.csv")`

2. Continue with the exercises below. You will need to create new code chunks to construct visualizations and models and write interpretations beneath. Put text responses in blockquotes as shown below:

> Response here. (The > at the start of the line starts a blockquote and makes the text larger and easier to read.)

3. Render your work for submission:
    - Click the "Render" button in the menu bar for this pane (blue arrow pointing right). This will create an HTML file containing all of the directions, code, and responses from this activity. A preview of the HTML will appear in the browser.
    - Scroll through and inspect the document to check that your work translated to the HTML format correctly.
    - Close the browser tab.
    - Go to the "Background Jobs" pane in RStudio and click the Stop button to end the rendering process.
    - Locate the rendered HTML file in the folder where this file is saved. Open the HTML to ensure that your work looks as it should (code appears, output displays, interpretations appear). Upload this HTML file to Moodle.



# Exercises

## Context

### School Enrollment

For Exercises 1-4, we'll revisit the data from the World Bank on secondary school enrollment that we worked with in Practice Problems #2. As a reminder, the context from that assignment is pasted below.

We have access to the following information:

- `Country`: country name
- `Year`: year enrollment was measured (ranges from 2004 - 2019)
- `GER`: gross enrollment rate in secondary school (%)
- `NER`: net enrollment rate in secondary school (%)

Net enrollment rate (NER) is the ratio of children who are *of secondary school age* who are enrolled in secondary school, out of the total number of children of secondary school age. In contrast, gross enrollment rate (GER) is the ratio of children who are enrolled in secondary school *regardless of age*, out of the total number of children of secondary school age. NER ranges from 0 to 100%, since it is a true proportion, while GER can exceed 100% (some children who are *not* of secondary school age may in fact be enrolled).

Historically, NER is more difficult to measure than GER (particularly in low- and middle-income countries), since it requires knowledge of the age of the children enrolled in school. 

A relevant research question is: If we know GER, can we accurately predict NER? In order to answer this question, we first want to better understand the relationship between GER and NER.

### Bikes

For Exercises 5-7, we'll revisit the data on bike ridership. We've looked at many of the variables included in this dataset so far in this course, but suppose we are now interested in how season impacts the relationship between actual temperature and number of total riders. This is the question we'll explore in this assignment.

## Exercise 1: Define a new variable and visualize

### Part a

Just as in Practice Problems #2, construct an appropriate visualization of the relationship of `NER` (the response variable) with `GER`.

```{r}

```

### Part b

At roughly what value of GER does the relationship between GER and NER appear to change?

### Part c

In the `enroll` dataset, define a TRUE/FALSE variable called `GER_high`, that is TRUE when `GER` is greater than or equal to 100, and FALSE when `GER` is less than 100.
Show the first 6 rows of the dataset to demonstrate your results.

```{r}
# Define GER_high 
# enroll <- enroll %>% 
#   ___(___ = (GER >= 100))

# Check out the first 6 rows

```


## Exercise 2: Compare models with and without interaction

In Practice Problems #2, we noted that a linear regression model did not accurately reflect the relationship between GER and NER. We'll now compare that same simple linear regression model to a multiple linear regression model *with an interaction term* between `GER` and `GER_high`.
Fit both of these models.
NOTE:

- This exercise just requires R code, no discussion.
- Since the next exercises depend upon your models here, you will receive 0 points on this exercise if either model is incorrect.
- If your second model doesn't have an interaction term it is incorrect!


```{r}
# Fit a simple linear regression model of NER by GER 


# Show the model summary table

```

```{r}
# Fit a multiple linear regression model of NER by GER and GER_high
# INCLUDING an interaction between GER and GER_high


# Show the model summary table

```


## Exercise 3: Assess 

### Part a

Construct residual plots for *both* of the models you fit in Exercise 2.

### Part b

Does the model with the interaction term appear to better predict `NER` than the simple linear regression model? Explain why or why not.

### Part c

Which model is stronger?
Support your answer with specific numerical evidence.



## Exercise 4: Visualize Interaction Term - School Enrollment

It may seem strange to include an interaction term between `GER` and another variable that was created from `GER` itself, but this is actually a special type of interaction term that creates what is called a *piecewise linear regression model*. It allows both the slope and intercept of the regression line to change at specific values of a predictor. 

### Part a 

Let's visualize our 2 models:

- create a scatterplot of NER vs GER (as you did in Exercise 1)
- add a line that represents the simple linear regression model in blue
- add this line of code to draw the piecewise linear regression model, where you'll need to update the model name: `geom_line(aes(y = YOUR_MODEL_2$fitted.values), color = "red", size = 1)`

### Part b

Use your multiple linear regression model to provide the formula for the relationship of NER with GER when GER is high (at least 100).
That is, provide the formula for the second piece of the *piecewise linear regression model*.
Show work / the steps that went into obtaining the coefficients.

E[NER | GER] = ???




## Exercise 5: Visualize Interaction Term - Bikes

Let's now turn to the bike data and explore the following research question: Does the relationship of total daily ridership (`riders_total`) with *actual* temperature vary by season?


### Part a

Construct a visualization of the relationship between total ridership, actual temperature, and season.
Include a representation of the multiple linear regression model that helps us address the above research question.


### Part b

Discuss this visualization relative to the above research question.
Also think about / comment on why your observations makes sense in context!


## Exercise 6: Model statement and fitting

### Part a

Eventually, we want to build a *model* of total ridership by actual temperature and season that allows us to address our research question: Does the relationship of total daily ridership (`riders_total`) with *actual* temperature vary by season?
Write a model statement for the model we need to build.
(Use $\beta$ notation, do not yet estimate the $\beta$s.)

E[total_rides | temp_actual, season] = $\beta_0$ + [FILL IN THE REST]

### Part b

Which coefficients in the above model will help us understand how the relationship between ridership and temperature varies by season?


### Part c

Fit this multiple linear regression model using the `bikes` data, and show a model summary table.




## Exercise 7: Interpretation and Conclusions

Remember: When interpreting coefficients, *make sure to use appropriate causation vs. association language, include units, and talk about averages rather than individual cases.*

### Part a

Interpret the `temp_actual` coefficient, in context.


### Part b

Interpret the coefficient (or combined coefficients) that would directly address our research question: Does the relationship of total daily ridership with actual temperature vary by season?
There are multiple coefficients that you could use -- pick the most meaningful one.


### Part c

Write a 3-4 sentence conclusion about the results of your analysis, fit for a news article.
Does season appear to *meaningfully* impact the relationship between actual temperature and number of total riders?
What impact would this have on cities looking to start bikeshare programs? 
Should more bikes be provided or expected in certain seasons based on temperature conditions?
Explain why or why not.





# Disclosures & citations

In this final section, please share whether you worked with others on this PS, whether you attended office hours to discuss this PS, and whether and how you used AI.
This is here to both help *you* reflect on your approach to learning / assignment completion, and to help the *instructor / preceptors* understand what resources are being utilized.

## Working with others

You're encouraged to work with others on PSs, though all submitted work must be in your own words / code and you must be able to explain everything therein.
Did you discuss this PS / work on this PS with any other STAT 155 students?
If so, include their name(s) here.
NOTE: No worries if you put somebody's name and they don't put yours, or vice versa.

**Your response:**

## Attending office hours

Did you attend any office hours to get help on / discuss this PS?
If so, include the name of the preceptor or instructor whose office hours you attended and roughly how much time you spent in office hours.

**Your response:**

## AI

You're encouraged to AVOID the use of AI and to NEVER use it as your first approach to an exercise.
Learning comes from you doing the puzzling, not from you producing a correct answer.
Did you use AI for any part of this PS?
If so, describe: where you used it (on which exercises), how long you worked on the exercises before turning to AI, and what prompts you used / typed into AI.

**Your response:**









