library(ggplot2)
library(dplyr)
library(tidyverse)
library(knitr)
library(ggthemes)
library(ISLR2)
theme_set(theme_bw())
\[\hat{y}=\hat{\beta_0}+\hat{\beta_1}x_1+...+\hat{\beta_p}x_p\]
Closed form solutions are more complicated and the text omits them, but they are easily computed. We will not compute these by hand.
\[H_0: \beta_1=\beta_2=...=\beta_p\]
\[H_a: \text{at least one }\beta\text{ is non-zero}\]
This hypothesis test uses the F-Statistic:
\[F=\frac{(TSS-RSS)/p}{RSS/(n-p-1)}\] If the linear model assumptions hold: the expected value of the denominator is \(\sigma^2\). If \(H_0\) is true: the expected value of the numerator is \(\sigma^2\). If \(H_a\) is true, we expect that the numerator is \(>\sigma^2\) so we expect \(F>1\).
We can calculate these statistics for particular subsets of coefficients. equation 3.24 and text: these are exactly equivalent to the F-test omiting a single variable allowing for the partial effect of adding a variable to the model (F and p).
each individual p value has 5% chance to be below 0.05 by random chance (so 20 non-correlated values would have one by random chance. xkcd jellybeans). p value of F is only under .05 5% of the time no matter how many predictors/observations we have.
image = "https://imgs.xkcd.com/comics/significant.png"
knitr::include_graphics(image)
Variable selection: using only the relevant variables (X). We will briefly cover some ideas today, and go into more detail next week.
Potential models (no interaction) grows quickly (\(2^p\) for p variables)
Forward selection can always be used. Backward cannot be used when \(p>n\). Forward selection is a greedy approach, which Mixed selection can help.
\(R^2=Corr(Y,\hat{Y})^2\). Will always increase as you add new variables. RSS will always decrease. RSE can increase given the p in the denominator \(RSE=\sqrt{\frac{1}{n-p-1}RSS}\)
Graphing can help you to see interaction effects (e.g. edges, see text)
We predict a least squares plane instead of a least squares line
There is model bias (not everything is linear), but we and the text will ignore that
Even if we knew f(X) we couldn’t perfectly predict Y due to \(\epsilon\)
Confidence interval:
Prediction interval:
We will continue using the lab from Tuesday.
Syntax: lm(Y~X1+X2+X3,data=DataFrame): regression model y given x1, x2, and x3
Syntax: lm(y~.,data=DataFrame): regression model y given everything else
Syntax: lm(y~.-x3,data=DataFrame): regression model y given everything else except x3
Question 1 Fit a complete model (all columns) of the Boston Dataset with medv as the outcome (Y) variable. What are the coefficients?
Syntax: x1:x2, creates the interaction variable
Syntax: lm(y~x1*x2) models y given x1, x2, and x1:x2
Question 2 Again using the Boston Dataset
Question 3 Using the Carseats data.
Question 4 Using the Carseats data, fit the “best” regression you can. Explain your steps, justify why it is the best, and why you used the variables that you did.
Things to think about:
In this lab we practiced the following: