r - R中预测函数和零膨胀负二项式模型的错误
问题描述
我得到了一个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.
解决方案
predict
在我将输入变量分组为 a并在模型语句中data.frame
包含该data
选项后,该函数起作用:zeroinfl
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
推荐阅读
- c++ - 如何在两个双向链表之间完全交换节点
- javascript - createAsyncThunk 函数返回 userId 未定义的数据属性
- android - 单击图片后,图像捕获上的 onActivityResult 不显示确定按钮
- android - RecyclerView 中的 ExoPlayer 仍在播放不应播放的视频
- httpresponse - 解析多部分/混合响应
- bash - 如何在数组的每个项目上添加一个字符串?
- excel - Excel VBA 帮助 - 计算重复项并根据这些重复项填写数据
- discord - 自定义服务器前缀错误 discord.py bot
- .net - 真正微不足道的 WPF 绑定拒绝工作
- javascript - 如何将javascript添加到php中?