I'm doing a one-way anova and post-Hoc multiple comparison. Using mtcars dataset as an example:
mtcars$cyl <- as.factor(mtcars$cyl)
aov<- aov(mpg~cyl, data=mtcars)
summary(multcomp::glht(aov, linfct=mcp(cyl='Dunnet')))
However, I do not want to hardcode the variable as cyl. so I create a variable var='cyl':
var <- 'cyl'
aov <- aov(as.formula(paste('mpg~', var)), data=mtcars)
summary(multcomp::glht(aov, linfct=mcp( var='Dunnet')))
I got error message:
Error in mcp2matrix(model, linfct = linfct) : Variable(s) ‘var’ have been specified in ‘linfct’ but cannot be found in ‘model’!
I think the problem comes from passing var in the mcp function. How can I fix this? I tried: as.name(var) , eval(quote(var))... But no luck.. Thanks a lot for help.