我想對模型的不同變量運行多重比較分析。我的想法是:如何使用字符作爲函數的屬性
library(multcomp)
set.seed(123)
x1 <- gl(4,10)
x2 <- gl(5,2,40)
y <- rnorm(40)
fm1 <- lm(y ~ x1 + x2)
for(var in c('x1', 'x2'))
{
mc1 <- glht(fm1, linfct=mcp(var='Tukey'))
print(summary(mc1))
}
當我跑,我得到以下錯誤:
Error en mcp2matrix(model, linfct = linfct) :
Variable(s) ‘var’ have been specified in ‘linfct’ but cannot be found in ‘model’!
That is, it is not possible to use a character to specify an attribute of the mcp function. Anyone knows a solution?
+1好手法。謝謝喬希。 –
幾乎總是更好地使用替代而不是文字操作。 – hadley
雖然可能會很棘手。另一種選擇是do.call和setNames – hadley