[英]Error with predict function and zero-inflated negative binomial model in R
我得到了一個Rdata
文件,其中包含來自回歸 model 的大量輸入和輸出。 我已經能夠提取 model 分析的數據並重現參數估計。 但是,當我嘗試使用原始predict
語句時,我收到一個錯誤,即使predict
語句在應用於存儲在Rdata
文件中的 model 時沒有返回錯誤。
我希望下面提供了足夠的信息,即使我沒有提供功能可重現的示例,也有人可以告訴我如何更正我的預測語句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.
在我將輸入變量分組到data.frame
並將data
選項包含在zeroinfl
model 語句中之后, predict
function 起作用:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.