Use regression when one variable is an outcome (response, \(y\)).
See if/how response depends on other variable(s), explanatory, \(x_1, x_2,\ldots\).
Can have one or more than one explanatory variable, but always one response.
Assumes a straight-line relationship between response and explanatory.
Ask:
sleep.txt, ATST then age, values separated by a space.Make scatter plot of ATST (response) vs. age (explanatory).
(over)
Call:
lm(formula = atst ~ age, data = sleep)
Residuals:
Min 1Q Median 3Q Max
-23.011 -9.365 2.372 6.770 20.411
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 646.483 12.918 50.05 2.49e-14 ***
age -14.041 1.368 -10.26 5.70e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 13.15 on 11 degrees of freedom
Multiple R-squared: 0.9054, Adjusted R-squared: 0.8968
F-statistic: 105.3 on 1 and 11 DF, p-value: 5.7e-07
The relationship appears to be a straight line, with a downward trend.
\(F\)-tests for model as a whole and \(t\)-test for slope (same) both confirm this (P-value \(5.7\times 10^{-7}=0.00000057\)).
Slope is \(-14\), so a 1-year increase in age goes with a 14-minute decrease in ATST on average.
Here R-squared is 0.9054, pleasantly high.
Output from regression (and eg. \(t\)-test) is all right to look at, but hard to extract and re-use information from.
Package broom extracts info from model output in way that can be used with pipe (later):
augmentUseful for plotting residuals:
.hat say how much leverage an observation has, based on its explanatory variable values: that is, how much potential it has to affect the fit..hat value bigger than \(2p/n\) is considered unusually large, where:
.cooksdOnce useful regression exists, use it for prediction:
To get a single number for prediction at a given \(x\), substitute into regression equation, eg. age 10: predicted ATST is \(646.48-14.04(10)=506\) minutes.
To express uncertainty of this prediction, CI for mean response expresses uncertainty about mean ATST for all children aged 10, based on data.
Also do above for a child aged 5.
marginaleffects package 1/2To get predictions for specific values, set up a dataframe with those values first:
Any variables in the dataframe that you don’t specify are set to their mean values (quantitative) or most common category (categorical).
marginaleffects package 2/2Then feed into newdata in predictions. This contains a lot of columns, so you probably want only to display the ones you care about:
The confidence limits are a 95% confidence interval for the mean response at that age.
Marks confidence interval for mean for all \(x\):
How to tell whether a straight-line regression is appropriate?
Before: check scatterplot for straight trend.
After: plot residuals (observed minus predicted response) against predicted values. Aim: a plot with no pattern.
Not much pattern here — regression appropriate.
Change \(x\) (adding \(x^2\))
Another way: change \(y\) (transformation).
Can guess how to change \(y\), or might be theory:
example: relationship \(y=ae^{bx}\) (exponential growth):
take logs to get \(\ln y=\ln a + bx\).
Taking logs has made relationship linear (\(\ln y\) as response).
Or, estimate transformation, using Box-Cox method.
MASS, load with library(MASS, exclude = "select")Ice crystals are introduced into a chamber that is kept at a fixed temperature of \(-5\)°C. The growth of the crystals over time is observed. Our data are measurements of mass m of an ice crystal in nanograms at time t in seconds after being added to the chamber. Each ice crystal was measured once, at a “random” time.
Model relationship between mass and time.
It is suggested by the scientist that m is actually related to log(t) in some way.
Suggestion of curve and maybe fanning out.
Output on next slide.
Call:
lm(formula = log(m) ~ log(t), data = ice_crystal)
Residuals:
Min 1Q Median 3Q Max
-0.6662 -0.2347 -0.0412 0.2735 0.4703
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -5.7278 0.7381 -7.76 1.42e-09 ***
log(t) 2.0315 0.1558 13.04 3.59e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.3091 on 41 degrees of freedom
Multiple R-squared: 0.8056, Adjusted R-squared: 0.8009
F-statistic: 169.9 on 1 and 41 DF, p-value: 3.589e-16
log(m) and log(t) mean?\[\ln(m) = a + b \ln(t)\]
Exp both sides:
\[ m = e^{a + b \ln(t)}\]
Simplify the right:
\[ m = e^a e^{b \ln(t)} \] \[ m = A t^b \]
where \(A = \exp(a)\).
What if more than one \(x\)? Extra issues:
Now one intercept and a slope for each \(x\): how to interpret?
Which \(x\)-variables actually help to predict \(y\)?
Different interpretations of “global” \(F\)-test and individual \(t\)-tests.
R-squared no longer correlation squared, but still interpreted as “higher better”.
In lm line, add extra \(x\)s after ~.
Interpretation not so easy (and other problems that can occur).
Data set punting.txt contains 4 variables for 13 right-footed football kickers (punters): left leg and right leg strength (lbs), distance punted (ft), average leg strength avg. Predict punting distance from other variables.
Call:
lm(formula = punt ~ left + right + avg, data = punting)
Residuals:
Min 1Q Median 3Q Max
-14.9325 -11.5618 -0.0315 9.0415 20.0886
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -4.6855 29.1172 -0.161 0.876
left 0.2679 2.1111 0.127 0.902
right 1.0524 2.1477 0.490 0.636
avg -0.2672 4.2266 -0.063 0.951
Residual standard error: 14.68 on 9 degrees of freedom
Multiple R-squared: 0.7781, Adjusted R-squared: 0.7042
F-statistic: 10.52 on 3 and 9 DF, p-value: 0.00267
Overall regression strongly significant, R-sq high.
None of the \(x\)’s significant! Why?
\(t\)-tests only say that you could take any one of the \(x\)’s out without damaging the fit; doesn’t matter which one.
Explanation: look at correlations.
left right punt avg
left 1.0000000 0.8957224 0.8117368 0.9722632
right 0.8957224 1.0000000 0.8805469 0.9728784
punt 0.8117368 0.8805469 1.0000000 0.8679507
avg 0.9722632 0.9728784 0.8679507 1.0000000
All correlations are high: \(x\)’s with punt (good) and with each other (bad, at least confusing).
What to do? Probably do just as well to pick one variable, say right since kickers are right-footed.
If we take out just the least significant variable, the two explanatory variables that remain are still going to be correlated with each other.
rightNo significant loss by dropping other two variables.
right:right significant:
Call:
lm(formula = punt ~ right, data = punting)
Residuals:
Min 1Q Median 3Q Max
-15.7576 -11.0611 0.3656 7.8890 19.0423
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.6930 25.2649 -0.146 0.886
right 1.0427 0.1692 6.162 7.09e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 13.36 on 11 degrees of freedom
Multiple R-squared: 0.7754, Adjusted R-squared: 0.7549
F-statistic: 37.97 on 1 and 11 DF, p-value: 7.088e-05
Maybe we got the form of the relationship with left wrong.
Check: plot residuals from previous regression (without left) against left.
Residuals here are “punting distance adjusted for right leg strength”.
If there is some kind of relationship with left, we should include in model.
Plot of residuals against original variable: augment from broom.
leftThere is a curved relationship with left.
We should add left-squared to the regression (and therefore put left back in when we do that):
left-squared
Call:
lm(formula = punt ~ left + I(left^2) + right, data = punting)
Residuals:
Min 1Q Median 3Q Max
-11.3777 -5.3599 0.0459 4.5088 13.2669
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -4.623e+02 9.902e+01 -4.669 0.00117 **
left 6.888e+00 1.462e+00 4.710 0.00110 **
I(left^2) -2.302e-02 4.927e-03 -4.672 0.00117 **
right 7.396e-01 2.292e-01 3.227 0.01038 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 7.931 on 9 degrees of freedom
Multiple R-squared: 0.9352, Adjusted R-squared: 0.9136
F-statistic: 43.3 on 3 and 9 DF, p-value: 1.13e-05
This was definitely a good idea (R-squared has clearly increased).
We would never have seen it without plotting residuals from punting.2 (without left) against left.
Negative slope for left-squared means that increased left-leg strength only increases punting distance up to a point: beyond that, it decreases again.
Comments