使用来自 emmeans::ref_grid 的两个变量的组合重建参考网格

Reconstruct a reference grid with the combination of two variables from emmeans::ref_grid

我的真实数据与emmeans:MOats想要传达的想法具有相似的复杂性。我正在使用 MOats 作为练习示例。

library(emmeans)


MOats.lm = lm(yield ~ Block + Variety, data = MOats)
    ref_grid(MOats.lm)
'emmGrid' object with variables:
    Block = VI, V, III, IV, II, I
    Variety = Golden Rain, Marvellous, Victory
    rep.meas = multivariate response levels: 0, 0.2, 0.4, 0.6
    # Silly illustration of how to use 'mult.levs' to make comb's of two factors
    ref_grid(MOats.lm, mult.levs = list(T=LETTERS[1:2], U=letters[1:2]))

假设MOats.lm中的Block因子不是实验设计中流行的blocking factor,而是Oat的一个特性。

主要问题:我想通过 VarietyBlock 的组合创建一个新变量,使用 add_grouping 语法调用 eater,这样如果 Variety = Golden Rain x Block = I then eater = fox, if Variety = Golden Rain x Block = II then eater = fox, if Variety = Marvellous x Block = II then eater = cat, 依此类推,做出12种组合(12只是任意的,有些动物吃更多种类,有些只吃一种)。我想我需要制作一个 Block x Variety 的虚拟变量,然后分配所需的 eater。最后,我想对每个品种的食客进行对比。

eater <- factor(c("fox", "cat","mouse","frog"), levels = c("fox", "cat","frog", "mouse"))

我该如何进行? add_grouping 示例只有单因素重建。如果 Block 的水平不能被 Variety 的水平整除怎么办?例如,Block 有 9 个级别,Variety 有 4 个级别。https://rdrr.io/cran/emmeans/man/add_grouping.html

fiber.lm <- lm(strength ~ diameter + machine, data = fiber)
( frg <- ref_grid(fiber.lm) )

# Suppose the machines are two different brands
brands <- factor(c("FiberPro", "FiberPro", "Acme"), levels = c("FiberPro", "Acme"))
( gfrg <- add_grouping(frg, "brand", "machine", brands) )

附带问题:rep.meas = multivariate response levels: 0, 0.2, 0.4, 0.6 是从哪里来的? View(MOats).

中没有这样的栏目

我还没弄清楚如何从这里的源代码中以 Factor1 = Factor2*Factor3 的形式构造一个新变量 https://rdrr.io/github/rvlenth/emmeans/src/R/ref-grid.R。非常感谢任何线索。

更新:以下行添加了新的分组变量但删除了原始分组变量,VarietyBlock

eater <- rep(LETTERS[1:3],6)
RG_add2 <- add_grouping(RG, "eater", "BV", eater)
RG_add2
'emmGrid' object with variables:
    BV = 6 G, 5 G, 3 G, 4 G, 2 G, 1 G, 6 M, 5 M, 3 M, 4 M, 2 M, 1 M, 6 V, 5 V, 3 V, 4 V, 2 V, 1 V
    rep.meas = multivariate response levels: 0.0, 0.2, 0.4, 0.6
    eater = A, B, C
Nesting structure:  BV %in% eater


RG_add <- add_grouping(RG, "eater", "BVlev", eater)  
Error in add_grouping(RG, "eater", "BVlev", eater) : 
  Length of 'newlevs' doesn't match # levels of 'BVlev'

我不明白错误,因为

length(BV)
[1] 18
 length(eater)
[1] 18
BV
 [1] "6 G" "5 G" "3 G" "4 G" "2 G" "1 G" "6 M" "5 M" "3 M" "4 M" "2 M" "1 M"
[13] "6 V" "5 V" "3 V" "4 V" "2 V" "1 V"
BVlev
 [1] "6 G" "5 G" "3 G" "4 G" "2 G" "1 G" "6 M" "5 M" "3 M" "4 M" "2 M" "1 M"
[13] "6 V" "5 V" "3 V" "4 V" "2 V" "1 V"

最后,我想做emmeans(RG_add, ~ Variety|eater)

add_grouping() 函数目前需要单个嵌套因子。所以你需要创造那个因素。这可以使用 levels<- 方法完成:

library(emmeans)
MOats.lm = lm(yield ~ Block + Variety, data = MOats)

RG = ref_grid(MOats.lm)
RG
## 'emmGrid' object with variables:
##     Block = VI, V, III, IV, II, I
##     Variety = Golden Rain, Marvellous, Victory
##     rep.meas = multivariate response levels: 0, 0.2, 0.4, 0.6

BVlev = do.call(paste, expand.grid(c(6, 5, 3, 4, 2, 1), c("G", "M", "V")))

levels(RG) = list(BV = BVlev, rep.meas = c(0, 0.2, 0.4, 0.6))
RG
## 'emmGrid' object with variables:
##     BV = 6 G, 5 G, 3 G, 4 G, 2 G, 1 G, 6 M, 5 M, 3 M, 4 M, 2 M, 1 M, 6 V, 5 V, 3 V, 4 V, 2 V, 1 V
##     rep.meas = multivariate response levels: 0.0, 0.2, 0.4, 0.6

reprex package (v2.0.0)

于 2021-08-17 创建

现在您可以继续 add_grouping(RG, "eater", "VB", eaters)eaters 的长度必须为 18,以便每个元素指定与 BV.

的每个级别关联的食客

替换水平时,需要注意保持水平列表中因子的相对顺序。合并的因素需要是连续的。