Investigation: Classifiers#

Classification separates estimated event probabilities from decisions made using a threshold. Logistic regression models log-odds; LDA and QDA model class-conditional distributions. Compare performance with a relevant baseline and a loss reflecting the intended use.

Question. Predict lexical-decision correctness for held-out participants. Compare majority prediction with logistic probabilities, confusion counts, Brier score, log loss, precision, and recall.

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()
s <- split_groups(d$subject)
train <- d[s$train,]; test <- d[s$test,]
fit <- glm(correct~frequency+length,train,family=binomial())
p <- predict(fit,test,type="response")
baseline <- rep(mean(train$correct),nrow(test))
rbind(logistic=binary_metrics(test$correct,p),baseline=binary_metrics(test$correct,baseline))
table(predicted=as.integer(p>=.5),observed=test$correct)
A matrix: 2 × 5 of type dbl
accuracybrierlog_lossprecisionrecall
logistic0.81666670.12749380.40229220.81666671
baseline0.81666670.15053850.47938750.81666671
         observed
predicted   0   1
        1  44 196

Probability, decision, and calibration#

The positive class is a correct response. A 0.5 threshold is prespecified here for illustration, not chosen from these test results. The event probability is inverse-logit(beta0 + beta1 × frequency + beta2 × length). The majority-class baseline can exceed 50% accuracy. Calibration compares predicted probabilities with observed event proportions.

bins <- cut(p,breaks=seq(0,1,.1),include.lowest=TRUE)
calibration <- aggregate(cbind(predicted=p,observed=test$correct),list(bin=bins),mean)
calibration
plot(calibration$predicted,calibration$observed,xlim=c(0,1),ylim=c(0,1),pch=16,xlab="Mean predicted probability",ylab="Observed fraction correct")
abline(0,1,lty=2)
# Fit discriminant models on the same training set, for a prespecified comparison.
lda <- MASS::lda(factor(correct)~frequency+length,train)
qda <- MASS::qda(factor(correct)~frequency+length,train)
rbind(LDA=binary_metrics(test$correct,predict(lda,test)$posterior[,"1"]),QDA=binary_metrics(test$correct,predict(qda,test)$posterior[,"1"]))
A data.frame: 4 × 3
binpredictedobserved
<fct><dbl><dbl>
(0.6,0.7]0.62586530.4687500
(0.7,0.8]0.75587850.7250000
(0.8,0.9]0.87247790.8250000
(0.9,1] 0.94086450.9772727
A matrix: 2 × 5 of type dbl
accuracybrierlog_lossprecisionrecall
LDA0.81666670.12662510.39985880.81666671
QDA0.81666670.12566310.39991960.81666671
../_images/cb6cf1100c8745d68778e1efbbe1d14e9ebe7c91dea91c78c3edc27cf56de028.png

For two predictors, each class has a 2 × 2 covariance matrix. LDA shares this covariance structure across classes; QDA estimates class-specific matrices. Multinomial logistic regression is another multiclass option. Do not select a winner and then report its same test score as an untouched evaluation. These trial-level uncertainty calculations would need participant-aware methods; the models here target new participants with familiar items.

Interpret and verify#

Specify the positive class and cost of errors, choose a threshold using training-side evidence, and evaluate once on held-out participants.

Failure to consider: High accuracy may reflect a dominant class. A probability threshold selected on the test set produces an optimistic evaluation.

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.

Extend continuous-outcome modeling to probabilities; mixed models then address the dependence within the trials.

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          MASS_7.3-57         IRdisplay_1.1      
 [7] repr_1.1.4          lifecycle_1.0.1     jsonlite_1.8.0     
[10] evaluate_0.15       pillar_1.8.0        rlang_1.0.6        
[13] cli_3.3.0           uuid_1.1-0          vctrs_0.4.1        
[16] IRkernel_1.3.0.9000 tools_4.2.1         glue_1.6.2         
[19] fastmap_1.1.0       compiler_4.2.1      base64enc_0.1-3    
[22] pbdZMQ_0.3-7        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.