r语言 - 使用emmeans与"interaction"参数对比时,有什么方法可以反转比较的方向?



我正在尝试使用emmeans来测试"对比度的对比度";自定义正交对比度应用于零膨胀负二项模型。研究设计有4组(研究组:grp1、grp2、grp3、grp4(,每组在3个时间点(时间:时间1、时间2、时间3(进行评估。

有了下面的代码,我可以非常接近但不完全是我想要的。出现的对比度用诸如grp1/grp2、grp1/grp3、…、,。。。,grp3/grp4("越高越低";参见下面代码的输出(。

有一种方法可以将这些比率翻转为grp2/grp1,grp3/grp1,。。。,grp4/grp3("高过低"(。我试过把reverse=TRUE粘在不同的地方,但没有效果。

除了重新调整研究组因素外,emmeans中还有什么可以做的吗?

谢谢!

library(glmmTMB)
library(emmeans)
set.seed(3456)
# Building grid for study design: 4 groups of 3 sites, 
#  each with 20 participants observed 3 times 
site <- rep(1:12, each=60)
pid <- 1000*site+10*(rep(rep(1:20,each=3),12))
study_group <- c(rep("grp1",180), rep("grp2",180), rep("grp3",180), rep("grp4",180))
grp_num <- c(rep(0,180), rep(1,180), rep(2,180), rep(3,180))
time <- c(rep(c("Time1", "Time2", "Time3"),240))
time_num <- c(rep(c(0:2),240))
# Site-level random effects (intercepts)
site_eff_count = rep(rnorm(12, mean = 0, sd = 0.5), each = 60)
site_eff_zeros = rep(rnorm(12, mean = 0, sd = 0.5), each = 60)
# Simulating a neg binomial outcome
y_count <- rnbinom(n = 720, mu=exp(3.25 + grp_num*0.15 + time_num*-0.20 + grp_num*time_num*0.15 + site_eff_count), size=0.8)
# Simulating some extra zeros
log_odds = (-1.75 + grp_num*0.2 + time_num*-0.40 + grp_num*time_num*0.50 + site_eff_zeros)
prob_1 = plogis(log_odds)
prob_0 = 1 - prob_1
y_zeros <- rbinom(n = 720, size = 1, prob = prob_0) 
# Building datasest with ZINB-ish outcome
data_ZINB <- data.frame(site, pid, study_group, time, y_count, y_zeros)
data_ZINB$y_obs <- ifelse(y_zeros==1, y_count, 0)
# Estimating ZINB GLMM in glmmTMB
mod_ZINB <- glmmTMB(y_obs ~ 1 
+ study_group + time + study_group*time
+ (1|site),
family=nbinom2,
zi = ~ .,
data=data_ZINB)
#summary(mod_ZINB)
# Getting model-estimated "cell" means for conditional (non-zero) sub-model
#  in response (not linear predictor) scale
count_means <- emmeans(mod_ZINB, 
pairwise ~ time | study_group, 
component="cond", 
type="response", 
adjust="none")
# count_means
# Defining custom contrast function for orthogonal time contrasts
#  contr1 = Time 2 - Time 1
#  contr2 = Time 3 - Times 1 and 2
compare_arms.emmc <- function(levels) {
k <- length(levels)
contr1 <- c(-1,1,0)
contr2 <- c(-1,-1,2)
coef <- data.frame()
coef <- as.data.frame(lapply(seq_len(k - 1), function(i) {
if(i==1) contr1 else contr2
}))
names(coef) <- c("T1vT2", "T1T2vT3")
attr(coef, "adjust") = "none"
coef
}
# Estimating pairwise between-group "contrasts of contrasts"
#  i.e., testing if time contrasts differ across groups
compare_arms_contrast <- contrast(count_means[[1]], 
interaction = c("compare_arms", "pairwise"), 
by = NULL)
compare_arms_contrast

如上所述应用emmeans::contrast函数得到的结果是:

time_compare_arms study_group_pairwise ratio    SE  df null t.ratio p.value
T1vT2             grp1 / grp2          1.091 0.368 693    1   0.259  0.7957
T1T2vT3           grp1 / grp2          0.623 0.371 693    1  -0.794  0.4276
T1vT2             grp1 / grp3          1.190 0.399 693    1   0.520  0.6034
T1T2vT3           grp1 / grp3          0.384 0.241 693    1  -1.523  0.1283
T1vT2             grp1 / grp4          0.664 0.245 693    1  -1.108  0.2681
.
.
.
T1T2vT3           grp3 / grp4          0.676 0.556 693    1  -0.475  0.6346
Tests are performed on the log scale

Russ Lenth在注释和emmeans文档中为contrast函数提供的答案是在contrast函数调用中将pairwise替换为revpairwise

相关内容

最新更新