# Stub of the Linear Regression Lab on the Boston and Carseats Data page: the exercises, without the solutions. Solutions: https://morrison-lab.github.io/lds/chapters/linear-regression-lab.html

# Exercise 1
# #### Lab: fit a line to the Boston data
#
# Download the Boston data from [`data/boston.csv`](https://morrison-lab.github.io/lds/chapters/data/boston.csv)
# and save it as `data/boston.csv` next to your code.
# It has $506$ Boston neighborhoods from the 1970 census.
# The copy here is the one in the Python package `ISLP` and the R package `ISLR2`.
# The response `medv` is the median home value, in thousands of dollars,
# and `lstat` is the percentage of the population with lower socioeconomic status.
#
# Fit `medv` on `lstat`.
# Report each [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) with its [standard error](https://morrison-lab.github.io/sds/estimation.html#def-SE).
#
# ::: {.callout-tip}
#
# #### Hint
#
# Read the data with the [data frame functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas),
# then [fit a model with a formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels)
# and read its [coefficients](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) and [standard errors](https://morrison-lab.github.io/sds/estimation.html#def-SE) from the results.
#
# :::



# Exercise 2
# #### Lab: fit a line to the Boston data (part 2)
#
# Use the setup of Exercise 1.
#
# [Predict](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) `medv` for `lstat` values $5$, $10$ and $15$.
#
# ::: {.callout-tip}
#
# #### Hint
#
# A [fitted model](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) [predicts at new values](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of the [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature)
# when you pass them in a data frame with the same column name.
#
# :::



# Exercise 3
# #### Lab: fit a line to the Boston data (part 3)
#
# Use the setup of Exercise 1.
#
# Give the $95\%$ [confidence interval](https://morrison-lab.github.io/sds/inference.html#def-confidence-interval) for the [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) [response](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable) at each value.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [prediction object](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) holds the [confidence interval](https://morrison-lab.github.io/sds/inference.html#def-confidence-interval) of the [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) [response](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable) at each new value.
#
# :::



# Exercise 4
# #### Lab: fit a line to the Boston data (part 4)
#
# Use the setup of Exercise 1.
#
# Give the $95\%$ [prediction interval](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-prediction-interval) at each value.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [prediction object](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) also holds the [prediction interval](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-prediction-interval) at each new value.
#
# :::



# Exercise 5
# #### Lab: fit a line to the Boston data (part 5)
#
# Use the setup of Exercise 1.
#
# Why is the [prediction interval](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-prediction-interval) wider?
#
# ::: {.callout-tip}
#
# #### Hint
#
# Compare what the [confidence interval](https://morrison-lab.github.io/sds/inference.html#def-confidence-interval) covers with what the [prediction interval](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-prediction-interval) covers.
#
# :::



# Exercise 6
# #### Lab: fit a line to the Boston data (part 6)
#
# Use the setup of Exercise 1.
#
# Draw a scatter plot of `medv` against `lstat` with the [fitted](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) line.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [plotting functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) draw the scatter plot,
# and a [fitted line over the data](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-reporting) comes from [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) on a grid of [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) values.
#
# :::



# Exercise 7
# #### Lab: fit a line to the Boston data (part 7)
#
# Use the setup of Exercise 1.
#
# Draw the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [residuals and fitted values](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-diagnostics) of a [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) are the two axes of the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot).
#
# :::



# Exercise 8
# #### Lab: fit a line to the Boston data (part 8)
#
# Use the setup of Exercise 1.
#
# What do the plots say about a straight line for these data?
#
# ::: {.callout-tip}
#
# #### Hint
#
# A straight line suits the data
# when the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot) shows no pattern.
#
# :::



# Exercise 9
# #### Lab: many features on the Boston data
#
# Fit `medv` on `lstat` and `age`,
# the percentage of owner-occupied homes built before 1940.
# How does the `lstat` [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) compare with that of Exercise 1?
#
# ::: {.callout-tip}
#
# #### Hint
#
# A [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) names each [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) after a `+`,
# and the [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) of `lstat` is in the [results](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels).
#
# :::



# Exercise 10
# #### Lab: many features on the Boston data (part 2)
#
# Use the setup of Exercise 9.
#
# Fit `medv` on all $12$ other columns.
#
# ::: {.callout-tip}
#
# #### Hint
#
# A [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) can list the other columns by name, or [build the formula as text from a list of the column names](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels).
#
# :::



# Exercise 11
# #### Lab: many features on the Boston data (part 3)
#
# Use the setup of Exercise 9.
#
# Which [coefficients](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) have $p$-values above $0.05$?
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [summary](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) holds a [$p$-value](https://morrison-lab.github.io/sds/inference.html#def-p-value) for each [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter),
# and a [mask](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) keeps those above $0.05$.
#
# :::



# Exercise 12
# #### Lab: many features on the Boston data (part 4)
#
# Use the setup of Exercise 9.
#
# Compute the [variance inflation factor](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-vif)
# for each [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) of the full [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [variance inflation factor](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-vif) of each [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) [comes from the design matrix](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model).
#
# :::



# Exercise 13
# #### Lab: many features on the Boston data (part 5)
#
# Use the setup of Exercise 9.
#
# Which [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) has the largest, and what is it?
#
# ::: {.callout-tip}
#
# #### Hint
#
# Look for the largest of the [variance inflation factors](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-vif),
# and name the [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) that has it.
#
# :::



# Exercise 14
# #### Lab: an interaction on the Boston data
#
# Fit `medv` on `lstat`, `age` and their product $\text{lstat} \times \text{age}$,
# the [interaction](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-interaction) of the two [features](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature).
# Report the coefficients with their $p$-values.
#
# ::: {.callout-tip}
#
# #### Hint
#
# In a [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels), `*` adds both [features](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) and their product.
#
# :::



# Exercise 15
# #### Lab: an interaction on the Boston data (part 2)
#
# Use the setup of Exercise 14.
#
# Is there evidence that the [slope](https://morrison-lab.github.io/mds/algebra-exponentials.html#def-slope) on `lstat` depends on `age`?
#
# ::: {.callout-tip}
#
# #### Hint
#
# Read the [$p$-value](https://morrison-lab.github.io/sds/inference.html#def-p-value) of the [interaction term](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-interaction) in the [summary](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of the [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model).
#
# :::



# Exercise 16
# #### Lab: a curve in `lstat`
#
# Add $\text{lstat}^2$ to the [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) of Exercise 9,
# so the [features](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) are `lstat`, $\text{lstat}^2$ and `age`.
# Report the [coefficients](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) with their $p$-values.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Wrap the square in `I(...)` inside the [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels)
# so that `**` is read as a power.
# In R, write `^` instead of `**`.
#
# :::



# Exercise 17
# #### Lab: a curve in `lstat` (part 2)
#
# Use the setup of Exercise 16.
#
# Compare the two [models](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) with the [partial $F$-test](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-partial-f-test).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [`anova_lm` function](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels-glm) compares two nested [models](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) with an $F$ test.
# In R, `anova()` does the same.
#
# :::



# Exercise 18
# #### Lab: a curve in `lstat` (part 3)
#
# Use the setup of Exercise 16.
#
# What does it say about the squared term?
#
# ::: {.callout-tip}
#
# #### Hint
#
# Read the [$p$-value](https://morrison-lab.github.io/sds/inference.html#def-p-value) of the $F$ test:
# a small value says the larger [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model) [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model)s better than the smaller one.
#
# :::



# Exercise 19
# #### Lab: a curve in `lstat` (part 4)
#
# Use the setup of Exercise 16.
#
# Draw the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot) of the new [model](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-model).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [residuals and fitted values](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-diagnostics) of the new [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) are the two axes of its [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot).
#
# :::



# Exercise 20
# #### Lab: a curve in `lstat` (part 5)
#
# Use the setup of Exercise 16.
#
# Compare it with the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot) of Exercise 1.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Compare whether the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot) still shows the curve that the [residual plot](https://morrison-lab.github.io/lds/chapters/linear-regression-inference.html#def-residual-plot) of Exercise 1 showed.
#
# :::



# Exercise 21
# #### Lab: a category with three levels
#
# Download the Carseats data from [`data/carseats.csv`](https://morrison-lab.github.io/lds/chapters/data/carseats.csv)
# and save it as `data/carseats.csv` next to your code.
# It is simulated data on sales of child car seats at $400$ stores,
# from the book.
# `Sales` is unit sales in thousands.
# `ShelveLoc` is the quality of the shelf space for the car seats at each store:
# `Bad`, `Medium` or `Good`.
#
# Fit `Sales` on all the other columns,
# plus the interactions $\text{Income} \times \text{Advertising}$
# and $\text{Price} \times \text{Age}$.
#
# ::: {.callout-tip}
#
# #### Hint
#
# In a [formula](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels), `Income:Advertising` adds only the product,
# and a column of text is treated as a category automatically.
#
# :::



# Exercise 22
# #### Lab: a category with three levels (part 2)
#
# Use the setup of Exercise 21.
#
# Which indicator columns does the software build for `ShelveLoc`?
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [category terms](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels) of a formula name one indicator column for each level except the baseline;
# read the names in the [summary](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels).
#
# :::



# Exercise 23
# #### Lab: a category with three levels (part 3)
#
# Use the setup of Exercise 21.
#
# Which level has no column of its own?
#
# ::: {.callout-tip}
#
# #### Hint
#
# The baseline is the level whose indicator is missing from the [summary](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-statsmodels).
#
# :::



# Exercise 24
# #### Lab: a category with three levels (part 4)
#
# Use the setup of Exercise 21.
#
# Interpret the [coefficients](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) of the two `ShelveLoc` indicators.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Each [coefficient](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-parameter) of an indicator is a difference from the baseline, with the other [features](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) held fixed.
#
# :::


