结果对比测试对比(first/second 差异)
Testing contrast of contrast (first/second difference) in outcome
我想进行对比的对比(即通过一阶/二阶差分来检验交互作用),其中对比指的是结果(预测概率)**
包括3个步骤:
(1) 估计预测概率(有几种方法;我从图中提取)
(2) 使用“对”计算预测概率(第一个差异)是否存在差异
(3) 使用对计算差值(第二个差值)是否存在差异。
我在步骤 (2) 和 (3) 中失败了。使用虚构数据在下面的 reprex 中查看我的代码。有更好的方法吗?
** 我最近的 S/O post 展示了 展示了如何处理“回归器对概率的边际效应的差异”。但这是“结果概率差异”的平行问题。
suppressPackageStartupMessages({
library(emmeans)})
# create ex. data set. 1 row per respondent (dataset shows 2 resp).
cedata.1 <- data.frame( id = c(1,1,1,1,1,1,2,2,2,2,2,2),
QES = c(1,1,2,2,3,3,1,1,2,2,3,3), # Choice set
Alt = c(1,2,1,2,1,2,1,2,1,2,1,2), # Alt 1 or Alt 2 in choice set
Choice = c(0,1,1,0,1,0,0,1,0,1,0,1), # Dep variable. if Chosen (1) or not (0)
LOC = c(0,0,1,1,0,1,0,1,1,0,0,1), # Indep variable per Choice set, binary categorical
SIZE = c(1,1,1,0,0,1,0,0,1,1,0,1), # Indep variable per Choice set, binary categorical
gender = c(1,1,1,1,1,1,0,0,0,0,0,0) # Indep variable per indvidual, binary categorical
)
# estimate model
glm.model <- glm(Choice ~ LOC*SIZE, data=cedata.1, family = binomial(link = "logit"))
# Plot interaction on response scale (i.e., predict prob)
zzz <- emmip(glm.model, LOC ~ SIZE, type = "response")
# (1) Estimate predicted prob (extract values where yvar=predicted prob)
zzz$data
#> LOC SIZE yvar SE df tvar xvar
#> 1 0 0 0.3333333 0.2721655 Inf 0 0
#> 2 1 0 0.5000000 0.3535534 Inf 1 0
#> 3 0 1 0.6666667 0.2721655 Inf 0 1
#> 4 1 1 0.5000000 0.2500000 Inf 1 1
# (2) calc 1st diff.
### I tried following, but got an error -> pairs(zzz$data, simple = "SIZE", by= NULL?
# (3) calc 2nd diff
### I tried following, but got an error -> pairs(pairs(zzz$data, simple = "SIZE"), by = NULL)
emm <- regrid(emmeans(glm.model, ~ SIZE | LOC))
pairs(pairs(emm), by = NULL)
emm
包含四个因素组合的预测概率。它们不是边际结果,因为我们没有对任何事情进行平均。 pairs(emm)
获取每个位置的 SIZE 差异,然后在下一次忽略 by 变量。
顺便说一句,你也可以这样做
contrast(emm, interaction = "pairwise")
我想进行对比的对比(即通过一阶/二阶差分来检验交互作用),其中对比指的是结果(预测概率)**
包括3个步骤:
(1) 估计预测概率(有几种方法;我从图中提取)
(2) 使用“对”计算预测概率(第一个差异)是否存在差异
(3) 使用对计算差值(第二个差值)是否存在差异。
我在步骤 (2) 和 (3) 中失败了。使用虚构数据在下面的 reprex 中查看我的代码。有更好的方法吗?
** 我最近的 S/O post 展示了
suppressPackageStartupMessages({
library(emmeans)})
# create ex. data set. 1 row per respondent (dataset shows 2 resp).
cedata.1 <- data.frame( id = c(1,1,1,1,1,1,2,2,2,2,2,2),
QES = c(1,1,2,2,3,3,1,1,2,2,3,3), # Choice set
Alt = c(1,2,1,2,1,2,1,2,1,2,1,2), # Alt 1 or Alt 2 in choice set
Choice = c(0,1,1,0,1,0,0,1,0,1,0,1), # Dep variable. if Chosen (1) or not (0)
LOC = c(0,0,1,1,0,1,0,1,1,0,0,1), # Indep variable per Choice set, binary categorical
SIZE = c(1,1,1,0,0,1,0,0,1,1,0,1), # Indep variable per Choice set, binary categorical
gender = c(1,1,1,1,1,1,0,0,0,0,0,0) # Indep variable per indvidual, binary categorical
)
# estimate model
glm.model <- glm(Choice ~ LOC*SIZE, data=cedata.1, family = binomial(link = "logit"))
# Plot interaction on response scale (i.e., predict prob)
zzz <- emmip(glm.model, LOC ~ SIZE, type = "response")
# (1) Estimate predicted prob (extract values where yvar=predicted prob)
zzz$data
#> LOC SIZE yvar SE df tvar xvar
#> 1 0 0 0.3333333 0.2721655 Inf 0 0
#> 2 1 0 0.5000000 0.3535534 Inf 1 0
#> 3 0 1 0.6666667 0.2721655 Inf 0 1
#> 4 1 1 0.5000000 0.2500000 Inf 1 1
# (2) calc 1st diff.
### I tried following, but got an error -> pairs(zzz$data, simple = "SIZE", by= NULL?
# (3) calc 2nd diff
### I tried following, but got an error -> pairs(pairs(zzz$data, simple = "SIZE"), by = NULL)
emm <- regrid(emmeans(glm.model, ~ SIZE | LOC))
pairs(pairs(emm), by = NULL)
emm
包含四个因素组合的预测概率。它们不是边际结果,因为我们没有对任何事情进行平均。 pairs(emm)
获取每个位置的 SIZE 差异,然后在下一次忽略 by 变量。
顺便说一句,你也可以这样做
contrast(emm, interaction = "pairwise")