Processing math: 0%
+ - 0:00:00
Notes for current slide
Notes for next slide

The General Linear Model - Mixing in Categories, Nonlinearities, and All That

1 / 45

The General Linear Model

\Large \boldsymbol{Y} \sim \mathcal{N}(\boldsymbol{Y}, \sigma^2\boldsymbol{I}) \Large \boldsymbol{\hat{Y}} = \boldsymbol{\beta X}

  • This is the Matrix formulation of \sum\beta_jx_j

  • This equation is huge. X can be anything - categorical, continuous, squared, sine, etc.

  • There can be straight additivity, or interactions

  • So far, the only model we've used with >1 predictor is ANOVA

2 / 45

So Many Ways to Build a Model

  1. Mixing Categorical and Continuous Variables

  2. Nonlinearities

  3. Interaction Effects

3 / 45

Combining Categorical Variables and Continuous Variables (Analysis of Covariance)

\Large \boldsymbol{Y} = \boldsymbol{\beta X}

translates to

\widehat{y_i} = \beta_0 + \beta_{1}x_1 + \sum_{j=2}^{k}\beta_{ij}x_{ij}
x_{ij} = 0,1

  • Here, we have many levels of a group in x_{ij}

  • Often used to correct for a gradient or some continuous variable affecting outcome

  • OR used to correct a regression due to additional groups that may throw off slope estimates
    • e.g. Simpson's Paradox: A positive relationship between test scores and academic performance can be masked by gender differences
4 / 45

Neanderthals and Mixing Categorical and Continuous Variables

Who had a bigger brain: Neanderthals or us?

5 / 45

The Means Look the Same...

6 / 45

But there appears to be a Relationship Between Body and Brain Mass

7 / 45

And Mean Body Mass is Different

8 / 45

9 / 45

Mixing Categories and Continuous Variables: Analysis of Covariance

lnbrain_i \sim \mathcal{N}(\widehat{lnbrain_i}, \sigma_2) \widehat{lnbrain_i} = \beta_0 + \beta_{1}lnmass_1 + \sum_{j=2}^{k}\beta_{ij}species_{ij}
species_{ij} = 0\; or\; 1\; if\; neandertal

Evaluate a categorical effect(s), controlling for a covariate (parallel lines)

Groups modify the intercept.

10 / 45

Fit with Our Engine: Ordinary Least Squares

neand_lm <- lm(lnbrain ~ species + lnmass,
data=neand)
neand_dat <- augment(neand_lm)
11 / 45

Does Your Model Match Your Data?

12 / 45

Are There Weird Patterns in Your Residuals?

13 / 45

Did You Modelboss Too Close to the Correlated Suns?

14 / 45

Did Our Predictors Matter?

term sumsq df statistic p.value
species 0.028 1 6.204 0.017
lnmass 0.130 1 29.276 0.000
Residuals 0.160 36 NA NA
15 / 45

What are the Association Between Predictors and Response?

term sumsq df statistic p.value
species 0.028 1 6.204 0.017
lnmass 0.130 1 29.276 0.000
Residuals 0.160 36 NA NA
16 / 45

What Does it Look Like to See the Unique Effect of One Driver?

term estimate std.error statistic p.value
(Intercept) 5.188 0.395 13.126 0.000
speciesrecent 0.070 0.028 2.491 0.017
lnmass 0.496 0.092 5.411 0.000
17 / 45

What Does it Look Like to See the Unique Effect of One Driver?

term estimate std.error statistic p.value
(Intercept) 5.188 0.395 13.126 0.000
speciesrecent 0.070 0.028 2.491 0.017
lnmass 0.496 0.092 5.411 0.000
  • Intercept is the brain mass of a neanderthal with 0 body mass
17 / 45

What Does it Look Like to See the Unique Effect of One Driver?

term estimate std.error statistic p.value
(Intercept) 5.188 0.395 13.126 0.000
speciesrecent 0.070 0.028 2.491 0.017
lnmass 0.496 0.092 5.411 0.000
  • Intercept is the brain mass of a neanderthal with 0 body mass

  • speciesrecent is the change in brain mass for a recent human, still assuming 0 body mass

17 / 45

What Does it Look Like to See the Unique Effect of One Driver?

term estimate std.error statistic p.value
(Intercept) 5.188 0.395 13.126 0.000
speciesrecent 0.070 0.028 2.491 0.017
lnmass 0.496 0.092 5.411 0.000
  • Intercept is the brain mass of a neanderthal with 0 body mass

  • speciesrecent is the change in brain mass for a recent human, still assuming 0 body mass

  • lnmass is the association of log body mass with log brain mass

17 / 45

What Does it Mean Visually?

18 / 45

We can run comparisons at equal body mass

19 / 45

So Many Ways to Build a Model

  1. Mixing Categorical and Continuous Variables

  2. Nonlinearities

  3. Interaction Effects

20 / 45

Five year study of wildfires & recovery in Southern California shrublands in 1993. 90 plots (20 x 50m)

(data from Jon Keeley et al.)
21 / 45

What is the effect of cover on richness?

22 / 45

The Linear Effect of Cover on Richness

23 / 45

But... What's Up With That Bend?

24 / 45

How Do We Add Nonlinearities to our Linear Model?

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \sum \beta_{j}x_{ij}

25 / 45

How Do We Add Nonlinearities to our Linear Model?

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \sum \beta_{j}x_{ij}

  • What if x_1 is a linear term and x_2 = x_1^2?
25 / 45

How Do We Add Nonlinearities to our Linear Model?

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \sum \beta_{j}x_{ij}

  • What if x_1 is a linear term and x_2 = x_1^2?

  • Adding nonlinear terms is just adding more predictors to a multiple linear regression model

25 / 45

How Do We Add Nonlinearities to our Linear Model?

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \sum \beta_{j}x_{ij}

  • What if x_1 is a linear term and x_2 = x_1^2?

  • Adding nonlinear terms is just adding more predictors to a multiple linear regression model

  • Sometimes to reduce collinearity/make our model make more sense, we need to center x - i.e. use x-\bar{x} - to reduce SE in parameters

25 / 45

A Nonlinear Effect of Cover?

mod_sq <- lm(rich ~ cover + I(cover^2), data = keeley)

26 / 45

Which is Better? Maybe A Cubic? Cross-Validate with LOO!

order mse
1 210.27
2 200.16
3 208.85
4 196.36
5 211.24
6 1323.11
27 / 45

2 or 4? Or Other Nonlinear Terms?

28 / 45

Cool story. Other than nonlinear transformations of y or x, are there other forms of nonlinearity we should watch for?

29 / 45

So Many Ways to Build a Model

  1. Mixing Categorical and Continuous Variables

  2. Nonlinearities

  3. Interaction Effects

30 / 45

What if Fire Severity Only Affects Species Richness in Old Stands?

31 / 45

Interaction Effects

  • The effect of one predictor cannot be known without knowing the level of another
32 / 45

Interaction Effects

  • The effect of one predictor cannot be known without knowing the level of another

  • Common in nature!

32 / 45

Interaction Effects

  • The effect of one predictor cannot be known without knowing the level of another

  • Common in nature!

  • Some are inevitable (e.g., the effects of disturbance depend on something being there to disturb)

32 / 45

Interaction Effects

  • The effect of one predictor cannot be known without knowing the level of another

  • Common in nature!

  • Some are inevitable (e.g., the effects of disturbance depend on something being there to disturb)

  • Some are.... quite tricky, but the spice of biological life!

32 / 45

Interaction Effects

  • The effect of one predictor cannot be known without knowing the level of another

  • Common in nature!

  • Some are inevitable (e.g., the effects of disturbance depend on something being there to disturb)

  • Some are.... quite tricky, but the spice of biological life!

  • Exercise: Think of an interaction effect you have encountered in biology or elsewhere!

32 / 45

Interaction Effects in Models

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \beta_{1}x_{1i} + \beta_{2}x_{2i} + \beta_{3}x_{1i}x_{2i} x_{2i} = 0,1

33 / 45

Interaction Effects in Models

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \beta_{1}x_{1i} + \beta_{2}x_{2i} + \beta_{3}x_{1i}x_{2i} x_{2i} = 0,1 If we consider x_1 * x_2 a variable in it's own right - i.e. x_3 then..... this is just.......

33 / 45

Interaction Effects in Models

y_{i} \sim N(\widehat{y_{i}}, \sigma^{2} ) \widehat{y_{i}} = \beta_{0} + \beta_{1}x_{1i} + \beta_{2}x_{2i} + \beta_{3}x_{1i}x_{2i} x_{2i} = 0,1 If we consider x_1 * x_2 a variable in it's own right - i.e. x_3 then..... this is just.......

\widehat{y_{i}} = \beta_{0} + \sum\beta_{j}x_{ij}

IT'S ALL THE SAME THING!

33 / 45

Interaction Effects in Models - and F-Tests!

mod_int <- lm(rich ~ firesev * age_break, data = keeley)
term sumsq df statistic p.value
firesev 1918.314 1 10.122 0.002
age_break 202.176 1 1.067 0.305
firesev:age_break 1060.005 1 5.593 0.020
Residuals 16298.526 86 NA NA
34 / 45

Interaction Effects What does it mean?

term estimate std.error statistic p.value
(Intercept) 53.464 6.581 8.123 0.000
firesev 0.117 1.626 0.072 0.943
age_breakold stand 16.348 8.961 1.824 0.072
firesev:age_breakold stand -4.730 2.000 -2.365 0.020
  • The intercept is the number of species when fire severity is 0 and stand age is young
35 / 45

Interaction Effects What does it mean?

term estimate std.error statistic p.value
(Intercept) 53.464 6.581 8.123 0.000
firesev 0.117 1.626 0.072 0.943
age_breakold stand 16.348 8.961 1.824 0.072
firesev:age_breakold stand -4.730 2.000 -2.365 0.020
  • The intercept is the number of species when fire severity is 0 and stand age is young

  • The firesev effect is the effect of fire for young stands

35 / 45

Interaction Effects What does it mean?

term estimate std.error statistic p.value
(Intercept) 53.464 6.581 8.123 0.000
firesev 0.117 1.626 0.072 0.943
age_breakold stand 16.348 8.961 1.824 0.072
firesev:age_breakold stand -4.730 2.000 -2.365 0.020
  • The intercept is the number of species when fire severity is 0 and stand age is young

  • The firesev effect is the effect of fire for young stands

  • The age effect is the increase in species in an old stand - but only if fire severity is 0

35 / 45

Interaction Effects What does it mean?

term estimate std.error statistic p.value
(Intercept) 53.464 6.581 8.123 0.000
firesev 0.117 1.626 0.072 0.943
age_breakold stand 16.348 8.961 1.824 0.072
firesev:age_breakold stand -4.730 2.000 -2.365 0.020
  • The intercept is the number of species when fire severity is 0 and stand age is young

  • The firesev effect is the effect of fire for young stands

  • The age effect is the increase in species in an old stand - but only if fire severity is 0

  • The interaction is the change in the fire severity slope when the stand is old

35 / 45

This is Why I Hate Staring at Coefficients

36 / 45

Maybe if Age Was Continuous....

37 / 45

Models with Continuous Interactions - The Same Model!

y_{i} \sim \mathcal{N}(\widehat{y_{i}}, \sigma^2) \widehat{y_{i}} = \beta_{0} + \beta_{1}x_{1i} + \beta_{2}x_{2i} + \beta_{3}x_{1i}x_{2i}

38 / 45

Fit and Go!

mod_int_c <- lm(rich ~ firesev * age, data = keeley)
term sumsq df statistic p.value
firesev 1361.509 1 7.877 0.006
age 466.926 1 2.701 0.104
firesev:age 2228.687 1 12.894 0.001
Residuals 14865.094 86 NA NA
39 / 45

Did You Make Sure your Train Wasn't On Fire?

40 / 45

But Was This a Good Idea? AIC Edition

Modnames K AIC Delta_AIC AICWt
fire*age 5 725.035 0.000 0.991
fire + age 4 735.608 10.573 0.005
age 3 736.034 10.998 0.004
fire 3 740.506 15.470 0.000
41 / 45

Coefficients are Clearer

term estimate std.error statistic p.value
(Intercept) 42.935 7.851 5.469 0.000
firesev 3.106 1.863 1.667 0.099
age 0.827 0.313 2.641 0.010
firesev:age -0.230 0.064 -3.591 0.001
  • Intercept is when all predictors are 0

  • Additive slopes are when the OTHER predictor is 0

  • Interaction is how they modify each other
42 / 45

What if a Predictor Value of 0 Makes No Sense: Centering

X_i - \bar{X}

  • Additive coefficients are now the effect of a predictor at the mean value of the other predictors

  • Intercepts are at the mean value of all predictors

  • Visualization will keep you from getting confused!

43 / 45

Model Visualization: The Effect of One Predictor at Different Levels of the Other

44 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!
45 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!

  • Note, we are still thinking in terms of normal error - and an additive model

45 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!

  • Note, we are still thinking in terms of normal error - and an additive model

  • There are plenty of extensions

    • nonlinear non-additive models
    • Generalized Linear Models with Non-Normal Error
45 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!

  • Note, we are still thinking in terms of normal error - and an additive model

  • There are plenty of extensions

    • nonlinear non-additive models
    • Generalized Linear Models with Non-Normal Error
  • But, additive Gaussian models are a safe starting point
45 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!

  • Note, we are still thinking in terms of normal error - and an additive model

  • There are plenty of extensions

    • nonlinear non-additive models
    • Generalized Linear Models with Non-Normal Error
  • But, additive Gaussian models are a safe starting point

  • AND extensions.... really all just have linear models at their core

45 / 45

Final Thoughts

  • Whew! That's a lot of ways to build a model!

  • Note, we are still thinking in terms of normal error - and an additive model

  • There are plenty of extensions

    • nonlinear non-additive models
    • Generalized Linear Models with Non-Normal Error
  • But, additive Gaussian models are a safe starting point

  • AND extensions.... really all just have linear models at their core

  • It's the BIOLOGY and your intuition of what model to build that should always be the core of your modeling process

45 / 45

The General Linear Model

\Large \boldsymbol{Y} \sim \mathcal{N}(\boldsymbol{Y}, \sigma^2\boldsymbol{I}) \Large \boldsymbol{\hat{Y}} = \boldsymbol{\beta X}

  • This is the Matrix formulation of \sum\beta_jx_j

  • This equation is huge. X can be anything - categorical, continuous, squared, sine, etc.

  • There can be straight additivity, or interactions

  • So far, the only model we've used with >1 predictor is ANOVA

2 / 45
Paused

Help

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