Investigation: Linear models#

A linear model specifies a conditional mean that is linear in its coefficients. State the outcome, units, design matrix, and adjustment set before interpreting coefficients. An association or predictive coefficient is not automatically a causal effect.

Question. Fit reaction time as a function of word frequency and length, interpret coefficients in milliseconds, and inspect fitted values and residuals.

Before running: write the result you expect and what would challenge it. Data are simulated unless explicitly identified otherwise.

# Open this notebook from book/notebooks (or book/exercises).
course_path <- if (file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
stopifnot(file.exists(course_path))
source(course_path)
set.seed(432)
d <- simulate_trials()
fit <- lm(rt~frequency+length,d)
coef(summary(fit))
model.matrix(fit)[1:6,]
stopifnot(ncol(model.matrix(fit))==length(coef(fit)))
plot(d$frequency,d$rt,pch=16,col=adjustcolor("steelblue",.15),xlab="Word frequency",ylab="RT (ms)")
new <- data.frame(frequency=seq(1,7,length.out=50),length=mean(d$length))
lines(new$frequency,predict(fit,new),lwd=2)
A matrix: 3 × 4 of type dbl
EstimateStd. Errort valuePr(>|t|)
(Intercept)638.304519.7983151 65.144310.000000e+00
frequency-27.749491.3801472-20.106181.349260e-76
length 11.902380.9240493 12.880672.053572e-35
A matrix: 6 × 3 of type dbl
(Intercept)frequencylength
111.6240198
211.6240198
311.6240198
411.6240198
511.6240198
611.6240198
../_images/442ee24b543f86b4d7f4515f52ce6f123adf6cdae10c6fd426cca9fb43052565.png

Interpret the coefficient#

The frequency coefficient is a change in mean milliseconds per one-unit change in the simulated frequency measure, conditional on length. The line fixes length at its sample mean. The ordinary lm standard errors treat trials as independent; that assumption fails here because participants and items recur. Mixed-effects modeling will address this structure.

simple <- lm(rt~frequency,d)
rbind(unadjusted=coef(simple),adjusted=coef(fit)[1:2])
plot(fitted(fit),residuals(fit),xlab="Fitted RT (ms)",ylab="Residual (ms)")
abline(h=0,lty=2)
A matrix: 2 × 2 of type dbl
(Intercept)frequency
unadjusted742.7997-29.97062
adjusted638.3045-27.74949
../_images/d170ffadfd537c4e9cc3874489c44c25e978883ce58bab8dc0812ca33ae959ac.png

Real-data extension#

The bundled HCP exercise data can support an association between Flanker performance and brain-volume measures. Verify the data dictionary and sampling restrictions first; Flanker performance is not interchangeable with working-memory capacity. If family or site identifiers are unavailable, state the resulting limitation on independence and validation. The matrix refresher in Foundations explains notation and operations.

Interpret and verify#

Write an interpretation with units and an explicit population. Compare an unadjusted and adjusted association without causal overclaiming.

Failure to consider: Polynomial terms remain linear in coefficients but do not ensure independent errors, constant variance, or a correctly specified mean. Repeated trials need special attention.

Report the target, the numerical result with its units, the relevant uncertainty, and one limitation. Distinguish what is established by the simulation from what would require real data.

AI audit: critique the saved faulty claim in the companion exercise before using any live assistant. If you use AI, record the relevant suggestion, your independent check, and the final decision.

Connect the learning objective to a specified model; the next chapter shows how its coefficients are computed.

sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.7.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] fansi_1.0.3         crayon_1.5.1        digest_0.6.29      
 [4] utf8_1.2.2          IRdisplay_1.1       repr_1.1.4         
 [7] lifecycle_1.0.1     jsonlite_1.8.0      evaluate_0.15      
[10] pillar_1.8.0        rlang_1.0.6         cli_3.3.0          
[13] uuid_1.1-0          vctrs_0.4.1         IRkernel_1.3.0.9000
[16] tools_4.2.1         glue_1.6.2          fastmap_1.1.0      
[19] compiler_4.2.1      base64enc_0.1-3     pbdZMQ_0.3-7       
[22] htmltools_0.5.3    

Attribution#

Notebook by Krista Bond and Patience Stevens and edited by Charles Wu, Amy Sentis, and Fiona Horner.

Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.