[英]How can I plot an interaction in R when using a negative binomial model?
I'm new to R.我是 R 的新手。 I used a negative binomial model to test the effects of 2 variables (1 binary variable and 1 continuous variable) and a count response variable.
我使用负二项式 model 来测试 2 个变量(1 个二进制变量和 1 个连续变量)和计数响应变量的影响。 I also added their interaction to the model.
我还将它们的交互添加到 model。
Since the results from the glm.nb
is minimal, I would like to plot the result somehow, especially the interaction.由于
glm.nb
的结果很小,我想 plot 以某种方式得到结果,尤其是交互。
I've done this to run the model:我这样做是为了运行 model:
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 *
---
Then I tried to use plot(allEffects(Model8))
from the effects
package to plot the interaction, but this is the message I received:然后我尝试使用
plot(allEffects(Model8))
从effects
package 到 plot 交互,但这是我收到的消息:
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
What am I missing?我错过了什么?
Again, I'm very new to R.同样,我对 R 还是很陌生。 Sorry in advance if this sounds silly.
如果这听起来很愚蠢,请提前道歉。
Most likely you have some other variables in your environment that is throwing the error.您的环境中很可能还有其他一些引发错误的变量。 It would be useful if you place all your variables into a data.frame.
如果您将所有变量放入 data.frame 中,这将很有用。 It works for me with an example dataset:
它适用于我的示例数据集:
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)
The interaction term is specified using :
and this *
is used for factor crossing.交互项使用
:
指定,此*
用于因子交叉。 See help page if you are unclear.如果您不清楚,请参阅帮助页面。 Below I can do the summary and we get a similar output to yours:
下面我可以做总结,我们得到一个与你类似的 output:
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
Plotting it works:绘制它有效:
plot(allEffects(fit))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.