簡體   English   中英

使用負二項式 model 時,如何在 R 中進行 plot 交互?

[英]How can I plot an interaction in R when using a negative binomial model?

我是 R 的新手。 我使用負二項式 model 來測試 2 個變量(1 個二進制變量和 1 個連續變量)和計數響應變量的影響。 我還將它們的交互添加到 model。

由於glm.nb的結果很小,我想 plot 以某種方式得到結果,尤其是交互。

我這樣做是為了運行 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 * 
---

然后我嘗試使用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

我錯過了什么?

同樣,我對 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)

交互項使用:指定,此*用於因子交叉。 如果您不清楚,請參閱幫助頁面 下面我可以做總結,我們得到一個與你類似的 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   

繪制它有效:

plot(allEffects(fit))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM