harrelfe / rms

Regression Modeling Strategies
https://hbiostat.org/R/rms
Other
170 stars 48 forks source link

Anova command does not run with lapply #134

Open SilviaMorgenstern opened 10 months ago

SilviaMorgenstern commented 10 months ago

I am trying to run RMS's anova on a list of models, but to my surprise it does not work, while the same syntax works with other rms commands. More specifically, after having created a list of models (allfits), including allfits$model1, allfits$model2, and allfits$model3, I then want to see each model's summary, anova, plots etc and I use lapply to apply the same commands to my list.

allsummary <- as.list(lapply(allfits, summary)) ## summarize each model
allanovas <- as.list(lapply(allfits, anova)) ## test main hypotheses of each model (it does not work)
allpred <- as.list(lapply(allfits, Predict)) ## predict values for each model
allplots <- as.list(lapply(allpred, ggplot)) ## plot those values
allnomograms <- as.list(lapply(allfits, nomogram)) ## calculate monogram
allnomoplots <- as.list(lapply(allnomograms, plot)) ## plot monogram   

Interestingly, each of these commands runs smoothly, except for the anova command. I get error "Error in anova.rms(X[[i]], ...) : factor names not in design: ...". I tried also with "anova.rms" and "rms::anova", but none of these works. How am I supposed to correctly call for your anova command in this context? The same command works if I run it on a single model at a time, i.e., anova(allfits$model1)