Exercises for 2.4: Linear models

Preliminaries

  • load the driving.Rdata file.
  • type head( driving ) to look at the first few observations
  • load the following packages: lsr, car
load( "~/Work/Research/Rbook/workshop_dsto/datasets/driving.Rdata")
head( driving )
##    id gender age distractor peak.hour errors_time1 errors_time2 rt_time1
## 1 s.1   male  19      radio       yes            7            7      346
## 2 s.2 female  42    toddler        no           15           16      424
## 3 s.3   male  27       none        no           10            7      415
## 4 s.4 female  22      radio       yes            5            1      266
## 5 s.5 female  33       none        no            4            9      302
## 6 s.6 female  35    toddler       yes           15           12      423
##   rt_time2
## 1      636
## 2      787
## 3      580
## 4      459
## 5      513
## 6      767
library(lsr)
library(car)

Exercise 2.4.1: Multiple regression

  • use lm() to fit a regression model with the RT at time 1 as the outcome variable, including age and errors at time 1 as predictors. Save the results to a variable called mod.1
  • use summary() to run the hypothesis tests etc associated with mod.1
  • use standardCoefs() to extract standardised regression coefficients

Solution 2.4.1:

mod.1 <- lm( 
  formula = rt_time1 ~ errors_time1 + age, 
  data = driving
  )
summary( mod.1 )
## 
## Call:
## lm(formula = rt_time1 ~ errors_time1 + age, data = driving)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -81.501 -24.829  -4.703  26.731 117.324 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   209.033     53.092   3.937  0.00149 **
## errors_time1    8.196      2.661   3.080  0.00814 **
## age             2.782      1.917   1.451  0.16878   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 49.78 on 14 degrees of freedom
## Multiple R-squared:  0.566,  Adjusted R-squared:  0.504 
## F-statistic: 9.127 on 2 and 14 DF,  p-value: 0.002902
standardCoefs( mod.1 )
##                     b      beta
## errors_time1 8.196376 0.5937550
## age          2.782137 0.2797177

Exercise 2.4.2: Regression diagnostic plots

  • use plot() to draw the standard regression diagnostic plots associated with mod.1

Solution 2.4.2:

plot( mod.1, which = 1 )