【问题标题】:Error with predict function and zero-inflated negative binomial model in RR中预测函数和零膨胀负二项式模型的错误
【发布时间】:2020-04-27 19:25:18
【问题描述】:

我收到了一个 Rdata 文件,其中包含来自回归模型的大量输入和输出。我已经能够提取模型分析的数据并重现参数估计。但是,当我尝试使用原始的 predict 语句时,我收到一个错误,即使 predict 语句在应用于存储在 Rdata 文件中的模型时没有返回错误。

我希望下面提供了足够的信息,即使我没有提供功能可重现的示例,也可能有人能够告诉我如何更正我的预测语句 my.probs。我认为,这是我第一次在这里发布问题而没有提供这样的例子。该数据集包含超过 100,000 个观察值,有点敏感,我不确定如何重现 Rdata 文件。

library(msm)
library(MASS)
library(pscl)

# model output returned when extracting the model name from the `Rdata` file
original.model
# Call:
# zeroinfl(formula = AA ~ log(BB) + CC + DD + CC:DD | log(BB) + DD, data = original.data, 
#     offset = log(EE), dist = "negbin")
# 
# Count model coefficients (negbin with log link):
#           (Intercept)  log(BB)      CC3      CC4      CC5 DDPrivate CC3:DDPrivate CC4:DDPrivate CC5:DDPrivate  
#              -2.05317  0.31178 -0.41402 -0.71208 -0.92290   0.17878      -0.18476      -0.18674       0.07307  
# Theta = 0.8551 
# 
# Zero-inflation model coefficients (binomial with logit link):
#           (Intercept)  log(BB)        DDPrivate  
#                1.6724 -0.5022            0.9742  
#  
# Warning message:
# In deparse(x$call, width.cutoff = floor(getOption("width") * 0.85)) :
#   invalid 'cutoff' value for 'deparse', using default

# data for new observation for use in the predict statement
new.data
#         DD      EE   CC               BB
# 1  Private       1    4         1118.948

str(new.data)
#'data.frame': 1 obs. of  4 variables:
# $ DD       : Factor w/ 2 levels "Public","Private": 2
# $ EE       : num 1
# $ CC       : Factor w/ 4 levels "2","3","4","5": 3
# $ BB: num 1119

original.probs <- predict(original.model, new.data, type='prob')
original.probs
# truncated probabilities returned by the predict statement.  These sum to one if vector not truncated
c(0.7534319,    0.1552296,    0.05681916,   0.02133936,   0.008116065,  0.003110019,  0.001197667)

# reproduce the original model
my.version <- zeroinfl(formula = AA ~ log(BB) + CC + DD + CC:DD | log(BB) + DD, offset = log(EE), dist = "negbin")

# Error returned by the predict statement
my.probs <- predict(my.version, new.data, type='prob')
my.probs
# Error in exp(X %*% object$coefficients$count + offsetx)[, 1] : 
#   incorrect number of dimensions
# In addition: Warning message:
# In X %*% object$coefficients$count + offsetx :
#   Recycling array of length 1 in array-vector arithmetic is deprecated.
#   Use c() or as.vector() instead.

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。
  • 是的,我知道。我在这里已经 10 年了。我只是不知道如何重现 Rdata 文件,这将是执行您建议的第一步。
  • 嗯,目前还不清楚我们如何为您提供帮助。也许尝试为preoduct() 函数打开调试,并在每种情况下逐步执行它,看看何时发生不同的事情。

标签: r regression predict


【解决方案1】:

predict 函数在我将输入变量分组到 data.frame 并在 zeroinfl 模型语句中包含 data 选项后起作用:

my.data <- data.frame(AA = AA,
                      BB = BB,
                      CC = CC,
                      DD = DD,
                      EE = EE)

my.version <- zeroinfl(formula = AA ~ log(BB) + CC + DD + CC:DD | log(BB) + DD, 
                               offset = log(EE), dist = "negbin", data = my.data)

summary(my.version)

my.probs <- predict(my.version, new.data, type='prob')
my.probs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2020-10-05
    • 2020-06-12
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多