R 如何找出变量子集对因变量的统计意义是否大于同一变量的另一个水平

R 如何找出变量子集对因变量的统计意义是否大于同一变量的另一个水平,r,R,我有一个假设:对欧洲货币联盟内部的人来说,欧洲货币联盟的支持比其外部的人更重要 变量如下: 动车组:1,动车组内;0,在EMU之外 欧盟支持:1、支持;0,不支持 我的代码如下所示: EB734_May_2010_model_u <- glm(trust ~ D_economy + (Support_EMU*EMU), family = binomial, data = EB734_May_2010) EB734\u May\u 2010\u model\u我编造了一些虚假数据来帮助说明

我有一个假设:对欧洲货币联盟内部的人来说,欧洲货币联盟的支持比其外部的人更重要

变量如下: 动车组:1,动车组内;0,在EMU之外 欧盟支持:1、支持;0,不支持

我的代码如下所示:

EB734_May_2010_model_u <- glm(trust ~ D_economy + (Support_EMU*EMU), family = binomial, data = EB734_May_2010)

EB734\u May\u 2010\u model\u我编造了一些虚假数据来帮助说明这一点。以下是数据和模型:

df <- tibble(
  EMU = sample(c(0,1), 1000, prob=c(.75,.25), replace=TRUE), 
  Support_EMU = sample(c(0,1), 1000, replace=TRUE), 
  z = rnorm(1000), 
  eta = EMU + .25*Support_EMU + 2*Support_EMU*EMU + z, 
  p = plogis(eta), 
  y = rbinom(1000, 1, p)
)

mod <- glm(y ~ EMU*Support_EMU + z, data=df, family=binomial)
summary(mod)

# Call:
#   glm(formula = y ~ EMU * Support_EMU + z, family = binomial, data = df)
# 
# Deviance Residuals: 
#   Min       1Q   Median       3Q      Max  
# -2.7554  -0.9769   0.3717   0.9175   2.3001  
# 
# Coefficients:
#                 Estimate Std. Error z value Pr(>|z|)    
# (Intercept)      -0.0601     0.1115  -0.539  0.58977    
# EMU               1.3788     0.2661   5.183 2.19e-07 ***
# Support_EMU       0.3237     0.1582   2.046  0.04080 *  
# z                 0.9455     0.0878  10.768  < 2e-16 ***
# EMU:Support_EMU   1.4732     0.4897   3.008  0.00263 ** 
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# (Dispersion parameter for binomial family taken to be 1)
# 
# Null deviance: 1344.4  on 999  degrees of freedom
# Residual deviance: 1090.0  on 995  degrees of freedom
# AIC: 1100
# 
# Number of Fisher Scoring iterations: 5

这意味着使用MER方法,平均而言,当EMU为1时,S从0变为1的概率的第一个差异比EMU为0时大0.087。然而,置信区间表明,这并不是统计上的显著差异。或者,您可以使用“平均边际效应”(AME)方法。这里,我们分别计算每个观测值的第二个差值,将所有其他变量保持在数据集中的观测值。这给了我们第二个不同的观察。然后,我们将这些第二个差异的平均值作为效果。这是它的样子

s2 <- secondDiff(mod, c("EMU", "Support_EMU"), df, method="AME")
summary(s2)

# Second Difference Using the Average Marginal Effect Approach
# 
# Overall: 
#   Average Second Difference: 0.114, 95% CI: (0.005,0.224)
# 
# Individual:
#   Significant Negative Individual Second Differences: 0 
#   Significant Positive Individual Second Differences: 369 
#   Insignificant Individual Second Differences: 631 

关于AME和MER方法之间的区别,迈克尔·汉默和奥桑·卡尔坎写了一篇关于这个主题的有趣文章。

你真是个天才!您好,谢谢您的评论-我收到了以下代码:数据中的错误[[x]]:类型为“closure”的对象不可再附加@Dave-您知道为什么会这样吗?@Anrisakakikibara通常这种类型的错误意味着您在工作区中不存在这些对象时指定了类似于
data=data
data=df
的内容。我怀疑这是因为您没有用数据帧替换
secondDiff()
函数中的
df
。您好,戴夫,再次感谢您的回复。情况就是这样,但后来我遇到了这个错误消息:error in UseMethod(“central”):在使用“MER”时,没有适用于“central”类对象的“central”方法“方法。如何解决这个问题?AnriSakakibara你可以通过将模型中的变量改为数值来解决这个问题。例如,
library(dplyr)
tmp%
`select(trust,Support\u EMU,EMU,D\u economy)%%`mutate\u all(.funs=las.numeric)`然后,在模型和后续的
secondDiff()函数中将
EB734\u May\u 2010
替换为
tmp
s2 <- secondDiff(mod, c("EMU", "Support_EMU"), df, method="AME")
summary(s2)

# Second Difference Using the Average Marginal Effect Approach
# 
# Overall: 
#   Average Second Difference: 0.114, 95% CI: (0.005,0.224)
# 
# Individual:
#   Significant Negative Individual Second Differences: 0 
#   Significant Positive Individual Second Differences: 369 
#   Insignificant Individual Second Differences: 631 
remotes::install_github("davidaarmstrong/damisc")