1.41 Multivariate Regression

Pirates data

if(!"yarrr" %in% installed.packages()) 
  install.packages("yarrr")
data(pirates, package = "yarrr")

head(pirates)[, c(1:5,8:10, 14:15)]
  id    sex age height weight tattoos tchests parrots sword.time beard.length
1  1   male  28    173   70.5       9       0       0       0.58           16
2  2   male  31    209  105.6       9      11       0       1.11           21
3  3   male  26    170   77.1      10      10       1       1.44           19
4  4 female  31    144   58.5       2       0       2      36.11            2
5  5 female  41    158   58.4       9       6       4       0.11            0
6  6   male  26    190   85.4       7      19       0       0.59           17

Question:

What attributes are most related to the number of treasure chests a pirate has acquired over their lifetime?

Pirate cartoon

Survey of 1000 pirates:

  • sex, age
  • height, weight
  • # tattoos, treasure chests, parrots
  • time to draw sword, beard length
  • mugs of grogg/day (avg)

Relationships between variables

The GGally package lets us make scatter plot matrices.

A scatterplot matrix showing the correlations between age, tattoos, treasure chests, parrots, sword draw time, and beard length among male pirates; sword draw time has been filtered to remove outliers with draw time higher than 5 seconds to facilitate visual display.

Pirate Accessories

A scatterplot matrix showing treasure chests, parrots, tattoos, and sword.time, with scatterplots on the lower left side, density plots on the diagonal, and correlation values on the upper right side.

pirate_sub <- filter(
  pirates, 
  sword.time < 5)
reg1 <- lm(
  tchests ~ sword.time + 
    parrots + tattoos, 
  data = pirate_sub)

Pirate Accessories


Call:
lm(formula = tchests ~ sword.time + parrots + tattoos, data = pirate_sub)

Residuals:
   Min     1Q Median     3Q    Max 
-38.04 -16.17  -7.69   7.64 123.02 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   16.903      2.927    5.77  1.1e-08 ***
sword.time     0.357      0.882    0.40   0.6861    
parrots        0.658      0.247    2.66   0.0079 ** 
tattoos        0.391      0.256    1.53   0.1264    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 24.4 on 917 degrees of freedom
Multiple R-squared:  0.00977,   Adjusted R-squared:  0.00653 
F-statistic: 3.02 on 3 and 917 DF,  p-value: 0.0292

Conclusion: each additional parrot owned over a lifetime increases the estimated number of treasure chests acquired by an average of 0.658. This difference is statistically significant when controlling for the effect of sword draw time and tattoos.

Let’s go back a minute…

A data frame containing 1,000 rows and 14 columns

id            An integer giving the pirate's id number
sex           A string with the pirate's self reported sex
age           An integer giving the age of the pirate in years
tattoos       An integer indicating the number of tattoos the pirate has
tchests       An integer indicating the number of treasure chests found by the pirate
parrots       An integer indicating the number of parrots owned by the pirate in his/her lifetime
sword.time    A number indicating how long it takes (in seconds) for the pirate to draw his/her sword. 
beard.length  A number indicating length of the pirate's beard in centimeters
grogg         How many mugs of grogg the pirate drinks a day on average.

Sex is related to

  • beard length
  • age
  • parrots

Male Pirate Success Factors

A parrot wearing a pirate hat, holding a telescope

male_pirates <- filter(pirates, sex=="male")
reg2 <- lm(tchests ~ tattoos + parrots + beard.length + grogg, 
           data = male_pirates)
summary(reg2)

Call:
lm(formula = tchests ~ tattoos + parrots + beard.length + grogg, 
    data = male_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-26.59 -14.22  -6.25   6.59 117.69 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept)   14.2673     4.9979    2.85   0.0045 **
tattoos       -0.0961     0.3515   -0.27   0.7848   
parrots        0.6865     0.3409    2.01   0.0446 * 
beard.length   0.4109     0.2056    2.00   0.0462 * 
grogg         -0.3035     0.3275   -0.93   0.3546   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 22.4 on 485 degrees of freedom
Multiple R-squared:  0.0198,    Adjusted R-squared:  0.0117 
F-statistic: 2.45 on 4 and 485 DF,  p-value: 0.0452

Let’s go back a minute…

A data frame containing 1,000 rows and 14 columns

id            An integer giving the pirate's id number
sex           A string with the pirate's self reported sex
age           An integer giving the age of the pirate in years
tattoos       An integer indicating the number of tattoos the pirate has
tchests       An integer indicating the number of treasure chests found by the pirate
parrots       An integer indicating the number of parrots owned by the pirate in his/her lifetime
sword.time    A number indicating how long it takes (in seconds) for the pirate to draw his/her sword. 
beard.length  A number indicating length of the pirate's beard in centimeters
grogg         How many mugs of grogg the pirate drinks a day on average.

Age is related to:

  • Number of parrots owned in his/her lifetime
  • Number of tattoos (once added, they don’t go away by themselves)
  • Number of treasure chests found

Male Pirate Success Factors

reg3 <- lm(tchests ~ age + parrots + beard.length, 
           data = male_pirates)
summary(reg3)

Call:
lm(formula = tchests ~ age + parrots + beard.length, data = male_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-27.96 -14.70  -6.04   6.43 118.71 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)    -5.943      5.841   -1.02  0.30937    
age             0.702      0.192    3.65  0.00029 ***
parrots         0.504      0.340    1.48  0.13879    
beard.length    0.372      0.169    2.20  0.02832 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 22.1 on 486 degrees of freedom
Multiple R-squared:  0.0442,    Adjusted R-squared:  0.0383 
F-statistic: 7.49 on 3 and 486 DF,  p-value: 6.56e-05

Once we account for age, parrots is no longer that useful!

Male Pirate Success Factors


Call:
lm(formula = tchests ~ age + beard.length, data = male_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-27.84 -14.37  -6.10   7.01 117.89 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)    -5.606      5.843   -0.96    0.338    
age             0.747      0.190    3.94  9.4e-05 ***
beard.length    0.365      0.169    2.16    0.032 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 22.1 on 487 degrees of freedom
Multiple R-squared:  0.0399,    Adjusted R-squared:  0.0359 
F-statistic: 10.1 on 2 and 487 DF,  p-value: 4.99e-05

Male Pirate Success Factors

male_pirates$resid <- resid(reg4)
male_pirates$pred <- predict(reg4)

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Male Pirate Success Factors

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Linear regression assumes

  • normally distributed residuals
  • identically distributed residuals
    • similar variance
    • similar mean
  • enough independence of predictors so that they aren’t perfectly predicting the response

Male Pirate Success Factors

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Linear regression assumes

  • normally distributed residuals ❌ ( a bit of a lean 🎋)
  • identically distributed residuals
    • similar variance predicted❌, age❌ , beard length✅
    • similar mean ✅ (close enough)
  • enough independence of predictors so that they aren’t perfectly predicting the response ✅

Female Pirate Success Factors

female_pirates <- filter(pirates, sex=="female")
reg5 <- lm(tchests ~ age, 
           data = female_pirates)
summary(reg5)

Call:
lm(formula = tchests ~ age, data = female_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-32.32 -16.79  -7.44   9.40 121.94 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    0.837      6.851    0.12  0.90287    
age            0.807      0.226    3.58  0.00038 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 25.5 on 462 degrees of freedom
Multiple R-squared:  0.027, Adjusted R-squared:  0.0249 
F-statistic: 12.8 on 1 and 462 DF,  p-value: 0.000379

Female Pirate Success Factors

reg6 <- lm(tchests ~ age + parrots, 
           data = female_pirates)
summary(reg6)

Call:
lm(formula = tchests ~ age + parrots, data = female_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-34.69 -16.86  -7.53   9.26 121.95 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    1.177      6.866    0.17  0.86395    
age            0.767      0.231    3.32  0.00096 ***
parrots        0.284      0.347    0.82  0.41329    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 25.5 on 461 degrees of freedom
Multiple R-squared:  0.0284,    Adjusted R-squared:  0.0242 
F-statistic: 6.74 on 2 and 461 DF,  p-value: 0.0013

Female Pirate Success Factors

reg7 <- lm(tchests ~ age + beard.length, 
           data = female_pirates)
summary(reg7)

Call:
lm(formula = tchests ~ age + beard.length, data = female_pirates)

Residuals:
   Min     1Q Median     3Q    Max 
-30.95 -17.08  -8.04   9.14 120.33 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)     2.384      6.868    0.35  0.72863    
age             0.809      0.225    3.60  0.00035 ***
beard.length   -4.032      1.955   -2.06  0.03970 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 25.4 on 461 degrees of freedom
Multiple R-squared:  0.0359,    Adjusted R-squared:  0.0317 
F-statistic: 8.58 on 2 and 461 DF,  p-value: 0.000219

Female Pirate Success Factors

female_pirates$resid <- resid(reg7)
female_pirates$pred <- predict(reg7)

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Diagnostics

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Residual plots. The first shows the residuals plotted against the predictions; it has a fan-shaped variance that is a violation of the assumption of equal variance. The second shows residuals plotted against age; a similar fan shape is found in this plot, and the residuals have a much longer tail on the positive side than the negative side. The third shows residuals plotted against beard length, and this plot has the same skewed behavior but does not have as much of a fan shape.

Linear regression assumes

  • normally distributed residuals ❌ ( a bit of a lean 🎋)
  • identically distributed residuals
    • similar variance predicted (maybe), age❌ , beard length✅
    • similar mean predicted ❌ , age ✅ (close enough), beard length✅
  • enough independence of predictors so that they aren’t perfectly predicting the response ✅