Linear Models
Trent University
Chapter 7 of Introduction to Modern Statistics (2e).
Let’s load in the Zoom data:
Now the variable names are the whole question, so let’s shorten those:
And I do some final cleaning (details not important):
coffee sleep
Min. : 0.0 Min. : 60.0
1st Qu.: 0.0 1st Qu.:360.0
Median : 218.0 Median :420.0
Mean : 239.7 Mean :417.9
3rd Qu.: 355.0 3rd Qu.:465.0
Max. :3000.0 Max. :612.0
NA's :3
Everybody answered the coffee question, but 3 people left sleep blank. If we use the summarize() function, this could lead to issues:
We have some options to address this. Either, we can use the argument na.rm = TRUE for the mean and sd,
# A tibble: 1 × 2
avg_sleep sd_sleep
<dbl> <dbl>
1 418. 76.6
but not all functions have this argument, so we can also filter:
Now, let’s plot! Our choice of response/explanatory is kind of arbitrary here.
I’m not seeing much of a trend, so the correlation is likely close to zero.
There is a very weak negative correlation here.


Residuals are the leftovers from the model fit: Data = Fit + Residual

Formally, residuals are the difference between the observed values of the response (\(y_i\)) and predicted values of the response from the model (\(\hat{y}_i\)).
\[ e_i = y_i - \hat{y}_i \]

What is “close”?
\[ \hat{y} = \beta_0 + \beta_1 x \]
Notation:
Call:
lm(formula = Poverty ~ Graduates, data = poverty)
Residuals:
Min 1Q Median 3Q Max
-4.1624 -1.2593 -0.2184 0.9611 5.4437
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 64.78097 6.80260 9.523 9.94e-13 ***
Graduates -0.62122 0.07902 -7.862 3.11e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.082 on 49 degrees of freedom
Multiple R-squared: 0.5578, Adjusted R-squared: 0.5488
F-statistic: 61.81 on 1 and 49 DF, p-value: 3.109e-10
The slope of the regression can be calculated as \[ b_1 = \frac{s_y}{s_x} R \]
but we will just use R, and identify it from the summary table:
(so \(b_1 = -0.62\))
Interpretation For each additional % point in HS graduate rate, we would expect the % living in poverty to be lower on average by 0.62% points.
The intercept is where the regression line intersects the \(y\)-axis. The calculation of the intercept uses the fact the regression line always passes through \((\bar{x},\bar{y})\):
\[ b_0 = \bar{y} - b_1 \bar{x}, \]
but again, we will just use R and identify it:
(so \(b_0 = 64.78\)).

Which of the following is the correct interpretation of the intercept?
Since there are no states in the dataset with no HS graduates, the intercept is of no interest, not very useful, and also not reliable since the predicted value of the intercept is so far from the bulk of the data.
(see previous figure)

\(\widehat{\%~in~poverty} = 64.68 - 0.62~\%~HS~grad\)

These statements are not causal, unless the study is a randomized controlled experiment.






We now have our first statistical model. In the coming lectures, we will talk about what this model means, how we use it, and how we can determine uncertainty in the model (since it is statistical, after all!).
