Comparing a baseline and a tree ensemble

Comparing a baseline and a tree ensemble#

Question: when does a nonlinear predictor improve an honestly evaluated scientific prediction?

We use a known nonlinear simulation, a mean baseline, linear regression, and bootstrap-aggregated regression trees. Bagging averages trees fitted to bootstrap samples; it is not the same as a random forest with random feature selection. All choices below are fixed in advance. Compare results on identical cases and retain unfavorable outcomes.

course_path <- if(file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
source(course_path)
set.seed(432)
d <- data.frame(x1=runif(240,-2,2),x2=runif(240,-2,2))
d$y <- d$x1^2+sin(2*d$x2)+rnorm(nrow(d),0,.4)
s <- split_groups(seq_len(nrow(d)))
train <- d[s$train,]; test <- d[s$test,]
linear <- lm(y~x1+x2,train)
B <- 40
trees <- lapply(seq_len(B),function(i) {
  rows <- sample.int(nrow(train),replace=TRUE)
  rpart::rpart(y~x1+x2,train[rows,],method="anova",control=rpart::rpart.control(cp=.001,minsplit=12,maxdepth=6,xval=0))
})
bag_predict <- function(newdata) rowMeans(sapply(trees,predict,newdata=newdata))
pred <- cbind(mean=rep(mean(train$y),nrow(test)),linear=predict(linear,test),bagged_trees=bag_predict(test))
apply(pred,2,function(p) mse(test$y,p))
mean
2.4205674783431
linear
2.27834399754666
bagged_trees
0.29296002005401
# Paired held-out loss difference, conditional on these fitted models.
difference <- (test$y-pred[,"linear"])^2-(test$y-pred[,"bagged_trees"])^2
boot <- replicate(1000,mean(sample(difference,length(difference),replace=TRUE)))
c(linear_minus_bagged_mse=mean(difference),quantile(boot,c(.025,.975)))
plot(test$y,pred[,"bagged_trees"],pch=16,xlab="Observed y",ylab="Bagged-tree prediction")
abline(0,1,lty=2)
linear_minus_bagged_mse
1.98538397749265
2.5%
1.37292171805486
97.5%
2.68016608669663
../_images/85d651a7469f110cfbc8cd3dc3eda26fa82626f22ce7a8134a84e797dadd625c.png

Challenge transport#

The conditional bootstrap interval does not include variability from collecting a new training sample. Nested/repeated evaluation is needed for that target. The following independent shifted sample probes extrapolation; do not use it to retune the original models and still call it a final test.

shift <- data.frame(x1=runif(100,2,4),x2=runif(100,-2,2))
shift$y <- shift$x1^2+sin(2*shift$x2)+rnorm(nrow(shift),0,.4)
c(linear_shift_mse=mse(shift$y,predict(linear,shift)),bagged_shift_mse=mse(shift$y,bag_predict(shift)))
linear_shift_mse
94.897919589956
bagged_shift_mse
60.045388780579

Optional pretrained-feature extension#

Use a documented, fixed feature matrix derived from permitted text or images. Record model/version, preprocessing, feature dimensions, source IDs, and whether extraction changes with context. Split by the independent source unit before tuning a downstream model. Compare a simple feature baseline, regularized regression on frozen features, and a prespecified nonlinear model with equal access to development data. Fit any scaling and PCA inside folds.

Do not claim that simulated vectors are pretrained embeddings. If no permitted frozen features are available, complete the runnable core comparison above and state that the embedding extension was not evaluated. Explain why predictive performance cannot by itself establish the scientific mechanism.

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          rpart_4.1.16        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