我已经得到了一个 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
函数在我将输入变量分组为 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