如何更改零膨胀回归模型的 na.action? [英] How to change na.action for zero-inflated regression model?
问题描述
我正在使用 pscl
包中的函数 zeroinfl
运行零膨胀负二项式回归模型.
I am running a zero-inflated negative binomial regression model using the function zeroinfl
from the pscl
package.
我需要从模型中排除 NA,以便能够在稍后的分析中针对因变量绘制残差.
I need to exclude NA's from the model in order to be able to plot the residuals against the dependent variable later in the analysis.
因此,我想设置na.action="na.exclude"
.对于非零膨胀负二项式回归模型(使用 glm
包中的 glm.nb
),我可以毫无问题地做到这一点,例如.
Therefore, I want to set na.action="na.exclude"
. I can do this without any problem for a non-zero-inflated negative binomial regression model (using glm.nb
from the glm
package), eg.
fm_nbin <- glm.nb(DV ~ factor(IDV) + contr1
+contr2 + contr3, data=df,
subset=(df$var<500), na.action="na.exclude")
fm_nbin.res = resid(fm_nbin)
plot(fm_nbin.res~df$var)
工作正常.但是,当我对零膨胀模型执行相同操作时,它不起作用:
works fine. However, when I do the same for a zero-inflated model, it does not work:
zinfl <- zeroinfl(DV ~ factor(IDV) + contr1
+contr2 + contr3 | factor(IDV) + contr1
+contr2 + contr3, data=df,
subset=(df$var<500), na.action="na.exclude")
zinfl.res = resid(zinfl)
plot(zinfl.res~df$var)
给出错误
Error in function (formula, data = NULL, subset = NULL, na.action = na.fail, :
variable lengths differ (found for 'df$var')
是否还有其他命令可用于从回归中排除 NA?
Is there any other command I should use to exclude NA's from my regression?
这是我能找到的最接近的答案.它可以以某种方式应用于我的问题吗?另外,可以以某种方式应用 naresid
吗?
This is the nearest of an answer I could find. Can it in some way be applied to my problem?
Also, can naresid
in some way be applied?
推荐答案
由于可以选择使用 na.omit(df)
或 na.action="na.exclude"
似乎在 zeroinfl
回归模型中不起作用,我发现了另一种(间接)实现该 NA
的方法被排除在回归中.
Since both the option of using na.omit(df)
or na.action="na.exclude"
don't seem to work in a zeroinfl
regression model, I found another (indirect) way of achieving that NA
's are excluded in the regression.
首先,由于我的原始数据集包含的变量远不止回归量和结果变量,因此我创建了一个新数据集,仅包含我在回归模型中使用的变量;并在 var
的值上设置一个条件,以在回归中包含观察:
First, since my original dataset contains far more variables than only the regressors and outcome variable, I created a new dataset including only the variables I use in the regression model; and also set a condition on the value of var
to include observations in the regression:
df1 <- subset(df, var<500, select=c("DV", "IDV", "contr1", "contr2", "contr3"))
df1 <- na.omit(df1)
然后我使用新的数据集 df1
运行与上面相同的代码,效果很好:
I then run the same code as above using the new dataset df1
, which works perfectly:
zinfl <- zeroinfl(DV ~ factor(IDV) + contr1
+contr2 + contr3 | factor(IDV) + contr1
+contr2 + contr3, data=df1)
zinfl.res = resid(zinfl)
plot(zinfl.res~df1$DV)
这篇关于如何更改零膨胀回归模型的 na.action?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!