r - 使用负二项式模型时,如何在 R 中绘制交互作用?
问题描述
我是 R 新手。我使用负二项式模型来测试 2 个变量(1 个二元变量和 1 个连续变量)和计数响应变量的影响。我还将他们的交互添加到模型中。
由于结果glm.nb
很少,我想以某种方式绘制结果,尤其是交互。
我这样做是为了运行模型:
Y<- cbind(N_Cooperations)
Model8 <- glm.nb(Y ~ Condition + NR + Condition*NR)
summary(Model8)
Call:
glm.nb(formula = Y ~ Condition + NR + Condition * NR, init.theta = 2.012332023,
link = log)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.2063 -0.9508 -0.1757 0.3389 2.5682
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.5379 0.6920 2.222 0.02626 *
Condition -2.9514 1.0876 -2.714 0.00665 **
NR -0.1470 0.2065 -0.712 0.47654
Condition:NR 0.7771 0.3170 2.451 0.01423 *
---
然后我尝试使用plot(allEffects(Model8))
from effects
package 来绘制交互,但这是我收到的消息:
plot(allEffects(Model8))
Error in mod.matrix[, components] : subscript out of bounds
In addition: Warning messages:
1: In factor.cols & stranger.cols :
longer object length is not a multiple of shorter object length
2: In (!factor.cols) & stranger.cols :
longer object length is not a multiple of shorter object length
我错过了什么?
再说一次,我对 R 很陌生。如果这听起来很傻,请提前道歉。
解决方案
您的环境中很可能还有其他一些引发错误的变量。如果您将所有变量放入 data.frame 中,这将很有用。它适用于我的示例数据集:
library(MASS)
library(effects)
set.seed(111)
df = data.frame(Y = rnbinom(100,mu=10,size=1),
NR = runif(100),Condition=rbinom(100,1,0.5))
fit = glm.nb(Y ~ Condition*NR,data=df)
交互项使用指定:
,*
用于因子交叉。如果您不清楚,请参阅帮助页面。下面我可以做总结,我们得到与你类似的输出:
summary(fit)
Call:
glm.nb(formula = Y ~ Condition * NR, data = df, init.theta = 1.19503151,
link = log)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.3798 -1.0970 -0.2340 0.4323 2.0848
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.4744 0.2978 4.952 7.36e-07 ***
Condition 0.9414 0.3973 2.369 0.0178 *
NR 1.0294 0.5003 2.058 0.0396 *
Condition:NR -0.9773 0.6809 -1.435 0.1512
绘制它有效:
plot(allEffects(fit))
推荐阅读
- reactjs - 将 Jest 和 Enzyme 添加到 React 样板文件中
- javascript - 为什么只有在重新加载页面后才会加载语言?i18n
- r - 在“df1”中添加一个新变量(标准偏差),用于依赖“df2”中的多行并以“日期时间”和其他两个变量为条件的行
- spring - Spring WebFlux:如何根据查询参数路由到不同的处理函数?
- c# - 表中的数据未出现在 ASP.NET 页面上
- python - 有没有办法让 Python 中的 Selenium 检查下载窗口的内容?
- amazon-web-services - API 网关调用我的 lambda 函数的旧版本
- python - 如果没有,则获得一个值,如果不是,则获得另一个?
- keras - 如何为 keras 层保存输入内存?
- matlab - 如何在 Matlab 中将 2.499858675195129e+02 数字设为 2.4998?