我正在使用该emmeans软件包,但无法理解为什么它会起作用
library(emmeans)\nmod <- lm(mpg ~ disp + hp, data = mtcars)\nl1 <- emmeans(mod, list("disp","hp"))\n\nfor (x in l1) {print(data.frame(x))}\n\n disp emmean SE df lower.CL upper.CL\n1 230.7219 20.09062 0.5527102 29 18.96021 21.22104\n hp emmean SE df lower.CL upper.CL\n1 146.6875 20.09062 0.5527102 29 18.96021 21.22104\nRun Code Online (Sandbox Code Playgroud)\n但这不是吗?
\nlapply(l1, function(x) data.frame(x))\n\n Error in as.data.frame.default(x[[i]], optional = TRUE) : \ncannot coerce class \xe2\x80\x98"function"\xe2\x80\x99 to a data.frame\nRun Code Online (Sandbox Code Playgroud)\n使用普通列表,我得到预期的输出(列表中的数据帧):
\nl2 <- list(a=matrix(c(1,2,3,4)), b=matrix(c(5,6,7,8)))\nlapply(l2, function(x) data.frame(x))\n$a\n x\n1 1\n2 2\n3 1\n4 3\n\n$b\n x\n1 …Run Code Online (Sandbox Code Playgroud) 我可以通过下面的代码得到两两比较的意义
m <- lmer(angle ~ recipe*temp + (1|replicate), data=cake)
emtrends(m, pairwise~recipe, var="temp")
$emtrends
recipe temp.trend SE df lower.CL upper.CL
A 0.1537143 0.02981898 250 0.09498586 0.2124427
B 0.1645714 0.02981898 250 0.10584300 0.2232999
C 0.1558095 0.02981898 250 0.09708110 0.2145379
$contrasts
contrast estimate SE df t.ratio p.value
A - B -0.010857143 0.0421704 250 -0.257 0.9641
A - C -0.002095238 0.0421704 250 -0.050 0.9986
B - C 0.008761905 0.0421704 250 0.208 0.9765
Run Code Online (Sandbox Code Playgroud)
但是,如果我对每个趋势recipe本身是否重要感兴趣怎么办?我怎样才能得到 的意义$emtrends?
dat <- data.frame(nitrogen = runif(50, 0, 10), temperature= rnorm(50, 10, 3))
modmat <- model.matrix(~ nitrogen * temperature, dat)
coeff <- c(1, 2, -1, 1.5)
dat$soil <- rnorm(50, mean = modmat %*% coeff, sd = 0.5)
Run Code Online (Sandbox Code Playgroud)
我试图绘制回归平面,显示了如何temperature温和派之间的关系nitrogen和soil。
library(plot3D)
x <- dat$nitrogen
y <- dat$temperature
z <- dat$soil
fit <- lm(z ~ x*y)
grid.lines = 26
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, …Run Code Online (Sandbox Code Playgroud)