从emmeans R包的emmGrid中提取元素

Extracting elements from emmGrid of emmeans R package

我想知道如何从 emmeans R 包的 emmGrid 中提取 emmeanSE 列。 MWE在下面给出。

library(emmeans)
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
Test <- emmeans(warp.lm,  specs = "wool")

Test
wool   emmean       SE df lower.CL upper.CL
 A    31.03704 2.105459 48 26.80373 35.27035
 B    25.25926 2.105459 48 21.02595 29.49257

Results are averaged over the levels of: tension 
Confidence level used: 0.95 

class(Test)
[1] "emmGrid"
attr(,"package")
[1] "emmeans"

summary(Test) 改为 data.frame。

class(summary(Test))
[1] "summary_emm" "data.frame" 

所以可以这样做:

summary(Test)$emmean
[1] 31.03704 25.25926

summary(Test)$SE
[1] 2.105459 2.105459

要真正获得新的子集 data.frame,您需要显式强制转换为 class data.frame:

as.data.frame(summary(Test))[c('emmean', 'SE')]
    emmean       SE
1 31.03704 2.105459
2 25.25926 2.105459