R:计算lm()函数中的预测平均值

时间:2016-11-28 23:41:52

标签: r lm

我试图计算具有分类和连续变量的给定简单线性回归模型的结果的预测/调整平均值。一个简单的例子类似于以下内容。

dat <- data.frame(value = c(5,8,41,25,23,56,58,54,51,52,56,59),
                  x = c("A","A","A","A","A","A", "B","B","B", "B","B","G"),
                  y=c("C","C","C","D","D","D", "D","D","D", "D","E","E"), 
                  z = c(34,56,25,35,54,67,43,73,52,78,15,38))
m <- lm(value ~ x + y + z, dat)

如果x =“A”,我如何计算输出的调整平均值,以及该值的置信区间。特别是当模型中存在另一个分类变量时。

谢谢!!

2 个答案:

答案 0 :(得分:2)

我认为包lsmeans解决了这个问题。

答案 1 :(得分:0)

我想这就是你要找的 -

dat$predicted <- m$fitted.values
adj_mean <- aggregate(predicted ~ x, data = dat, FUN = mean)