I have dataset with multiple outcome variables that I would like to test against one predictor. on exploratory analysis I noted some of the relationships are polynomial to the degree 2 rather than linear. I would like to look at the BIC and AIC to make my decision of which is the best model to run.
I have lapply
function where I can iterate over multiple outcome variables but now I would like to add a second model and compare their fit. However when I run this function, it only saves the second model and I dont know how to get to 'outputs' to run through the next function. Can I have this within one function or do I need two?
Here is a example from the iris dataset
data(iris)
vars <- names(iris[2:4])
models2 <- lapply(vars, function(x) {
model_list=list(
mod1=lm(substitute(i ~ Sepal.Length, list(i=as.name(x))), data=iris),
mod2=lm(substitute(i ~ poly(Sepal.Length,2), list(i=as.name(x))), data=iris))
})
y <- lapply(models2, summary) #This only saves results from mod2
How do I then compare mod1
to mod2
fit and extract the following variable's?
data.frame(
do.call(merge, list(BIC(mod1, mod2), AIC(mod1, mod2))),
logLik=sapply(list(mod1, mod2), logLik),
anova(mod1, mod2, test='Chisq'))
compare_performance()
inlibrary(performance)
. Does a lot of the hard work in one line. – Griffey