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⋮1Xn]⏟X: Design Matrix[β0β1]⏟β: Vector of parameters+[ϵ1ϵ2⋮ϵn]
What are the dimensions of β?
[Y1Y2⋮Yn]=[1X11X2⋮⋮1Xn][β0β1]+[ϵ1ϵ2⋮ϵn]⏟ϵ: vector of error terms
[Y1Y2⋮Yn]=[1X11X2⋮⋮1Xn][β0β1]+[ϵ1ϵ2⋮ϵn]⏟ϵ: vector of error terms
What are the dimensions of ϵ?
[Y1Y2⋮Yn]⏟Y: Vector of responses=[1X11X2⋮⋮1Xn][β0β1]+[ϵ1ϵ2⋮ϵn]
[Y1Y2⋮Yn]⏟Y: Vector of responses=[1X11X2⋮⋮1Xn][β0β1]+[ϵ1ϵ2⋮ϵn]
What are the dimensions of Y?
[Y1Y2⋮Yn]=[1X11X2⋮⋮1Xn][β0β1]+[ϵ1ϵ2⋮ϵn]
Y=Xβ+ϵ
[ˆy1ˆy2⋮ˆyn]=[1x11x2⋮⋮1xn][ˆβ0 ˆβ1]
ˆyi=ˆβ0+ˆβ1xi
[ˆy1ˆy2⋮ˆyn]=[1x11x2⋮⋮1xn][ˆβ0 ˆβ1]
ˆyi=ˆβ0+ˆβ1xi
[ˆy1ˆy2⋮ˆyn]=[1x11x2⋮⋮1xn][ˆβ0 ˆβ1]
ˆyi=ˆβ0+ˆβ1xi
[ˆy1ˆy2⋮ˆyn]=[1x11x2⋮⋮1xn][ˆβ0 ˆβ1]
ˆyi=ˆβ0+ˆβ1xi
How are ˆβ0 and ˆβ1 chosen? What are we minimizing?
How are ˆβ0 and ˆβ1 chosen? What are we minimizing?
How are ˆβ0 and ˆβ1 chosen? What are we minimizing?
How could we re-write this with yi and xi?
How could we re-write this with yi and xi?
Let's put this back in matrix form:
∑ϵ2i=[ϵ1ϵ2…ϵn][ϵ1ϵ2⋮ϵn]=ϵTϵ
What can we replace ϵi with? (Hint: look back a few slides)
What can we replace ϵi with? (Hint: look back a few slides)
∑ϵ2i=(Y−Xβ)T(Y−Xβ)
OKAY! So this is the thing we are trying to minimize with respect to β:
(Y−Xβ)T(Y−Xβ)
In calculus, how do we minimize things?
OKAY! So this is the thing we are trying to minimize with respect to β:
(Y−Xβ)T(Y−Xβ)
In calculus, how do we minimize things?
OKAY! So this is the thing we are trying to minimize with respect to β:
(Y−Xβ)T(Y−Xβ)
In calculus, how do we minimize things?
OKAY! So this is the thing we are trying to minimize with respect to β:
(Y−Xβ)T(Y−Xβ)
In calculus, how do we minimize things?
OKAY! So this is the thing we are trying to minimize with respect to β:
(Y−Xβ)T(Y−Xβ)
In calculus, how do we minimize things?
XTY=XTXˆβ
[ˆβ0ˆβ1]=(XTX)−1XTY
ˆY=XˆβˆY=X(XTX)−1XTY
ˆY=XˆβˆY=X(XTX)−1XTY⏟ˆβ
ˆY=XˆβˆY=X(XTX)−1XT⏟hat matrixY
ˆY=XˆβˆY=X(XTX)−1XT⏟hat matrixY
Why do you think this is called the "hat matrix"
Linear Models
appex-01-linear-models
We can generalize this beyond just one predictor
[ˆβ0ˆβ1⋮ˆβp]=(XTX)−1XTY
We can generalize this beyond just one predictor
[ˆβ0ˆβ1⋮ˆβp]=(XTX)−1XTY
What are the dimensions of the design matrix, X now?
We can generalize this beyond just one predictor
[ˆβ0ˆβ1⋮ˆβp]=(XTX)−1XTY
What are the dimensions of the design matrix, (\mathbf{X})
now?
We can generalize this beyond just one predictor
[ˆβ0ˆβ1⋮ˆβp]=(XTX)−1XTY
What are the dimensions of the design matrix, X now?
X=[1X11X12…X1p1X21X22…X2p⋮⋮⋮⋮⋮1Xn1Xn2…Xnp]
The coefficient for x is ˆβ (95% CI: LBˆβ,UBˆβ). A one-unit increase in x yields an expected increase in y of ˆβ, holding all other variables constant.
Var(ˆβ)=σ2(XTX)−1
Var(ˆβ)=σ2(XTX)−1
Var(ˆβ)=σ2(XTX)−1
SE(ˆβ1)2=σ2∑ni=1(xi−ˉx)2
Var(ˆβ)=σ2(XTX)−1
SE(ˆβ1)2=σ2∑ni=1(xi−ˉx)2
Let's look at a sample of 116 sparrows from Kent Island. We are intrested in the relationship between Weight
and Wing Length
a quick pause for R
library(tidyverse)
lm
, and turns them into tidy data frames.library(broom)
park(drive(start_car(find("keys")), to = "campus"))
find("keys") %>% start_car() %>% drive(to = "campus") %>% park()
To send results to a function argument other than first one or to use the previous result for multiple arguments, use .
:
starwars %>% filter(species == "Human") %>% lm(mass ~ height, data = .)
## ## Call:## lm(formula = mass ~ height, data = .)## ## Coefficients:## (Intercept) height ## -116.58 1.11
How can we quantify how much we'd expect the slope to differ from one random sample to another?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
How do we interpret this?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
How do we interpret this?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
How do we know what values of this statistic are worth paying attention to?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
How do we know what values of this statistic are worth paying attention to?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
How do we know what values of this statistic are worth paying attention to?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy(conf.int = TRUE)
## # A tibble: 2 x 7## term estimate std.error statistic p.value conf.low conf.high## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1 -0.531 3.26 ## 2 WingLength 0.467 0.0347 13.5 2.62e-25 0.399 0.536
How are these statistics distributed under the null hypothesis?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
The distribution of test statistics we would expect given the null hypothesis is true, β1=0, is t-distribution with n-2 degrees of freedom.
How can we compare this line to the distribution under the null?
How can we compare this line to the distribution under the null?
The probability of getting a statistic as extreme or more extreme than the observed test statistic given the null hypothesis is true
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
The proportion of area less than 1.5
pt(1.5, df = 18)
## [1] 0.9245248
The proportion of area greater than 1.5
pt(1.5, df = 18, lower.tail = FALSE)
## [1] 0.07547523
The proportion of area greater than 1.5 or less than -1.5.
The proportion of area greater than 1.5 or less than -1.5.
pt(1.5, df = 18, lower.tail = FALSE) * 2
## [1] 0.1509505
The probability of getting a statistic as extreme or more extreme than the observed test statistic given the null hypothesis is true
If we use the same sampling method to select different samples and computed an interval estimate for each sample, we would expect the true population parameter ( β1 ) to fall within the interval estimates 95% of the time.
ˆβ1±t∗×SEˆβ1
(\Huge \hat\beta1 \pm t^∗ \times SE{\hat\beta_1})
(\Huge \hat\beta1 \pm t^∗ \times SE{\hat\beta_1})
lm(Weight ~ WingLength, data = Sparrows) %>% tidy(conf.int = TRUE)
## # A tibble: 2 x 7## term estimate std.error statistic p.value conf.low conf.high## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1 -0.531 3.26 ## 2 WingLength 0.467 0.0347 13.5 2.62e-25 0.399 0.536
lm(Weight ~ WingLength, data = Sparrows) %>% tidy(conf.int = TRUE)
## # A tibble: 2 x 7## term estimate std.error statistic p.value conf.low conf.high## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1 -0.531 3.26 ## 2 WingLength 0.467 0.0347 13.5 2.62e-25 0.399 0.536
If we use the same sampling method to select different samples and computed an interval estimate for each sample, we would expect the true population parameter ( β1 ) to fall within the interval estimates 95% of the time.
Using the information here, how could I predict a new sparrow's weight if I knew the wing length was 30?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
Using the information here, how could I predict a new sparrow's weight if I knew the wing length was 30?
lm(Weight ~ WingLength, data = Sparrows) %>% tidy()
## # A tibble: 2 x 5## term estimate std.error statistic p.value## <chr> <dbl> <dbl> <dbl> <dbl>## 1 (Intercept) 1.37 0.957 1.43 1.56e- 1## 2 WingLength 0.467 0.0347 13.5 2.62e-25
What is the residual sum of squares again?
What is the residual sum of squares again?
RSS=∑(yi−ˆyi)2
What is the residual sum of squares again?
RSS=∑(yi−ˆyi)2
TSS=∑(yi−ˉy)2
What could we use to determine whether at least one predictor is useful?
What could we use to determine whether at least one predictor is useful?
F=(TSS−RSS)/pRSS/(n−p−1)∼Fp,n−p−1 We can use a F-statistic!
lm(Weight ~ WingLength, data = Sparrows) %>% glance()
## # A tibble: 1 x 11## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC## <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>## 1 0.614 0.611 1.40 181. 2.62e-25 2 -203. 411. 419.## # … with 2 more variables: deviance <dbl>, df.residual <int>
Refer to Chapter 3 for more details on these topics if you need a refresher.
Linear Models
Linear Models
RStudio Cloud sessionlibrary(tidyverse)
then library(broom)
mpg
from am
tidy()
function to see the beta coefficientsglance()
function to see the model fit statistics01-hello-r.Rmd
)Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |