I have a dataframe called data. I have created a function that loop thorugh a list of variables and creates a linear model for each of them using lapply. This method is based on this post.
library(datasets)
testDF <- data.frame(Salaries)
#creates list of variables
varListTest <- names(testDF)[3:4]
#creates a model for each of the variables in question
model<- lapply(varListTest, function(x) {
lm(substitute(i~Rank, list(i = as.name(x))), data = testDF)})
#output model
lapply(model, summary)
This works great. However, I would also like to run post-hoc tests in the same fashion, normally i would do this by running:
TukeyHSD(model)
This obviously won't work in this example, but I thought this would:
lapply(model, TukeyHSD)
But this returns:
no applicable method for 'TukeyHSD' applied to an object of class "lm"
What am I missing to make this work?