Investigation: Selecting a model for a purpose#
Best is conditional on an objective, candidate set, and evaluation design. Predictive accuracy, explanatory adequacy, parsimony, and causal identification are distinct. Searching many models adds uncertainty that a single winning score conceals.
Question. Choose among nested feature sets with inner cross-validation, evaluate once on held-out data, and examine selection instability across repeated training samples.
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_features(n=180,p=10)
x <- as.data.frame(d$x); x$y <- d$y
s <- split_groups(seq_len(nrow(x)))
train <- x[s$train,]; test <- x[s$test,]
fold <- group_folds(seq_len(nrow(train)))
sizes <- 0:10
formula_for <- function(size) if(size==0) y~1 else reformulate(paste0("x",seq_len(size)),"y")
cv <- sapply(sizes,function(size) {
pred <- rep(NA_real_,nrow(train))
for(f in unique(fold)) pred[fold==f] <- predict(lm(formula_for(size),train[fold!=f,]),train[fold==f,])
mse(train$y,pred)
})
best_size <- sizes[which.min(cv)]
fit <- lm(formula_for(best_size),train)
data.frame(size=sizes,validation_mse=cv)
c(selected_size=best_size,test_mse=mse(test$y,predict(fit,test)))
| size | validation_mse |
|---|---|
| <int> | <dbl> |
| 0 | 5.088814 |
| 1 | 2.494300 |
| 2 | 2.021491 |
| 3 | 2.052381 |
| 4 | 2.103450 |
| 5 | 2.144666 |
| 6 | 1.963667 |
| 7 | 1.946369 |
| 8 | 1.953091 |
| 9 | 1.965791 |
| 10 | 1.984759 |
- selected_size
- 7
- test_mse
- 1.25107646406156
Make the search procedure explicit#
These are nested prespecified feature sets, not exhaustive subset selection. Exhaustive and stepwise procedures search different candidate sets; their search must also be repeated inside validation. AIC targets a penalized likelihood-based predictive criterion; BIC uses a different penalty and model-selection motivation. Neither proves a model true.
# Bootstrap the training data to examine variability in an AIC-selected size.
selected <- replicate(100,{
z <- train[sample.int(nrow(train),replace=TRUE),]
scores <- sapply(sizes,function(size) AIC(lm(formula_for(size),z)))
sizes[which.min(scores)]
})
table(factor(selected,levels=sizes))
plot(sizes,cv,type="b",xlab="Number of predictors",ylab="Inner-validation MSE")
Keep all simulation outcomes. An intercept-only or full-model winner is evidence about this experiment, not a reason to change the seed. Search instability does not by itself measure performance uncertainty; repeat the complete nested procedure for that target. AI-proposed formulas, transformations, and prompts all enlarge the search and must be recorded.
Interpret and verify#
Prespecify candidates and the decision rule. Keep every simulated outcome, including an intercept-only or full-model winner.
Failure to consider: Changing the random seed until an expected optimum appears selects favorable evidence. Reusing the test set for model search invalidates its role as a final assessment.
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.
Combine honest validation and simulation; regularization offers a structured alternative to discrete subset search.
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 authored by Ven Popov and edited by Krista Bond, Charles Wu, Patience Stevens, and Amy Sentis.
Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.