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)
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| (Intercept) | 638.30451 | 9.7983151 | 65.14431 | 0.000000e+00 |
| frequency | -27.74949 | 1.3801472 | -20.10618 | 1.349260e-76 |
| length | 11.90238 | 0.9240493 | 12.88067 | 2.053572e-35 |
| (Intercept) | frequency | length | |
|---|---|---|---|
| 1 | 1 | 1.624019 | 8 |
| 2 | 1 | 1.624019 | 8 |
| 3 | 1 | 1.624019 | 8 |
| 4 | 1 | 1.624019 | 8 |
| 5 | 1 | 1.624019 | 8 |
| 6 | 1 | 1.624019 | 8 |
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.
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.