From lexical records to a qualified prediction

From lexical records to a qualified prediction#

This investigation uses the bundled English Lexicon Project teaching extract rather than the shared simulation. Source: Balota et al. (2007), The English Lexicon Project, Behavior Research Methods, 39, 445–459. The extract is a teaching subset; inspect its documentation before interpreting it as representative of all English words or readers.

Question: can word length and log frequency predict response correctness for held-out participants within the population represented by this extract? We preserve trial identities locally, audit the join, document exclusions, and evaluate a probability model. We do not print participant identifiers or send records to an external service.

course_path <- if(file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
source(course_path)
set.seed(432)
data_dir <- if(dir.exists("../exercises/lexDat")) "../exercises/lexDat" else "book/exercises/lexDat"
raw <- read.csv(file.path(data_dir,"LexicalData_withIncorrect.csv"),stringsAsFactors=FALSE)
items <- read.csv(file.path(data_dir,"Items.csv"),stringsAsFactors=FALSE)
stopifnot(!anyDuplicated(items$Word),!anyDuplicated(raw[c("Sub_ID","Trial")]))
raw$row_id <- seq_len(nrow(raw))
joined <- merge(raw,items[c("Word","Length","Log_Freq_HAL")],by.x="D_Word",by.y="Word",all.x=TRUE,sort=FALSE)
stopifnot(nrow(joined)==nrow(raw),!anyDuplicated(joined$row_id))
joined$reason <- ifelse(is.na(joined$Length)|is.na(joined$Log_Freq_HAL),"no usable item metadata",ifelse(!joined$Correct %in% c(0,1),"invalid response code","retained"))
table(joined$reason)
d <- joined[joined$reason=="retained",]
stopifnot(all(is.finite(d$Length)),all(is.finite(d$Log_Freq_HAL)))
c(raw_trials=nrow(raw),retained_trials=nrow(d),participants=length(unique(d$Sub_ID)))
no usable item metadata                retained 
                   4280                   70589 
raw_trials
74869
retained_trials
70589
participants
299

Define what remains after the join#

Some strings have no matching usable word metadata. Excluding them changes the target to the retained subset; investigate whether nonwords or other item types are involved rather than calling them random missingness. Correctness is the outcome, so missing or formatted RT is not an exclusion criterion for this model. For an RT question, parse and audit RT separately.

s <- split_groups(d$Sub_ID,seed=432)
train <- d[s$train,]; test <- d[s$test,]
fit <- glm(Correct~Length+Log_Freq_HAL,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))
stopifnot(length(intersect(train$Sub_ID,test$Sub_ID))==0)
# Participant-level loss: give each sampled participant equal weight.
loss <- aggregate((test$Correct-p)^2,list(participant=test$Sub_ID),mean)$x
boot <- replicate(1000,mean(sample(loss,length(loss),replace=TRUE)))
c(mean_participant_brier=mean(loss),quantile(boot,c(.025,.975)))
A matrix: 2 × 5 of type dbl
accuracybrierlog_lossprecisionrecall
logistic0.88653870.095459780.32763680.88660080.999921
baseline0.88660880.100533860.35354830.88660881.000000
mean_participant_brier
0.0963728050357595
2.5%
0.085648121182913
97.5%
0.107755195907542
library(ggplot2)
plot_data <- data.frame(probability=p,correct=test$Correct)
plot_data$bin <- cut(plot_data$probability,breaks=seq(0,1,.1),include.lowest=TRUE)
cal <- aggregate(cbind(probability,correct)~bin,plot_data,mean)
ggplot(cal,aes(probability,correct))+geom_point(size=2)+geom_abline(slope=1,intercept=0,linetype=2)+coord_equal(xlim=c(0,1),ylim=c(0,1))+labs(x="Mean predicted probability",y="Observed fraction correct",caption="Held-out participants; bin means are descriptive, without uncertainty intervals.")+theme_minimal()

Interpret and extend#

The participant bootstrap conditions on the fitted model and observed item setting. Repeated model training and sampling new items add uncertainty not represented here. Compare trial-weighted and participant-weighted losses; they answer different averaging questions. This simple logistic model does not model all participant/item heterogeneity.

For the next step, prespecify a crossed mixed-effects model or a new-item holdout. Do not select it by reusing this final test set and claim the score remains independent. Write a limitation about unmatched strings, population sampling, and the meaning of the recorded outcome. An AI summary must not turn this prediction into a causal claim about frequency.

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     

other attached packages:
[1] ggplot2_3.3.6

loaded via a namespace (and not attached):
 [1] pillar_1.8.0        compiler_4.2.1      base64enc_0.1-3    
 [4] tools_4.2.1         digest_0.6.29       uuid_1.1-0         
 [7] jsonlite_1.8.0      evaluate_0.15       lifecycle_1.0.1    
[10] tibble_3.1.8        gtable_0.3.0        pkgconfig_2.0.3    
[13] rlang_1.0.6         IRdisplay_1.1       cli_3.3.0          
[16] DBI_1.1.3           IRkernel_1.3.0.9000 fastmap_1.1.0      
[19] withr_2.5.0         repr_1.1.4          dplyr_1.0.9        
[22] generics_0.1.3      vctrs_0.4.1         grid_4.2.1         
[25] tidyselect_1.1.2    glue_1.6.2          R6_2.5.1           
[28] fansi_1.0.3         pbdZMQ_0.3-7        farver_2.1.1       
[31] purrr_0.3.4         magrittr_2.0.3      scales_1.2.0       
[34] htmltools_0.5.3     assertthat_0.2.1    colorspace_2.0-3   
[37] labeling_0.4.2      utf8_1.2.2          munsell_0.5.0      
[40] crayon_1.5.1