【发布时间】: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