简体   繁体   English

从 R gamlss 对象预测新拟合值时出错

[英]Error when predicting new fitted values from R gamlss object

I have a gamlss model that I'd like to use to make new y predictions (and confidence intervals) from in order to visualize how well the model fits the real data.我有一个 gamlss 模型,我想用它来做出新的 y 预测(和置信区间),以便可视化模型与真实数据的拟合程度。 I'd like to make predictions from a new data set of randomized predictor values (rather than the original data), but I'm running into an error message.我想从随机预测值的新数据集(而不是原始数据)中进行预测,但我遇到了错误消息。 Here's some example code:下面是一些示例代码:

library(gamlss)    

# example data
irr <- c(0,0,0,0,0,0.93,1.4,1.4,2.3,1.5)
lite <- c(0,1,2,2.5)
blck <- 1:8
raw <- data.frame(
   css =abs(rnorm(500, mean=0.5, sd=0.1)),
   nit =abs(rnorm(500, mean=0.72, sd=0.5)),
   irr =sample(irr, 500, replace=TRUE),
   lit =sample(lite, 500, replace=TRUE),
   block =factor(sample(blck, 500, replace=TRUE))
)

# the model
mod <- gamlss(css~nit + irr + lit + random(block), 
       sigma.fo=~irr*nit + random(block), data=raw, family=BE)

# new data (predictors) for making css predictions
pred <- data.frame(
nit =abs(rnorm(500, mean=0.72, sd=0.5)),
irr =sample(irr, 500, replace=TRUE),
lit =sample(lite, 500, replace=TRUE),
block =factor(sample(blck, 500, replace=TRUE))
)

# make predictions
predmu <- predict(mod, newdata=pred, what="mu", type="response")

This gives the following error:这给出了以下错误:

Error in data[match(names(newdata), names(data))] : 
  object of type 'closure' is not subsettable

When I run this on my real data, it gives this slightly different error:当我在我的真实数据上运行这个时,它给出了这个稍微不同的错误:

Error in `[.data.frame`(data, match(names(newdata), names(data))) : 
  undefined columns selected

When I use predict without newdata , it works fine making predictions on the original data, as in:当我在没有newdata情况下使用predict ,它可以很好地对原始数据进行预测,如下所示:

predmu <- predict(mod, what="mu", type="response")

Am I using predict wrong?我使用预测错误吗? Any suggestions are greatly appreciated!任何建议都非常感谢! Thank you.谢谢你。

No, you are not wrong.不,你没有错。 I have experienced the same issue.我也遇到过同样的问题。

The documentation indicates the implementation of predict is incomplete.文档表明 predict 的实现不完整。 this appears to be an example of an incomplete feature/function.这似乎是一个不完整的特性/功能的例子。

Hedgehog mentioned that predictions based on new-data is not possible yet. Hedgehog 提到基于新数据的预测尚不可能。 BonnieM therefore "moved the model" into lmer().因此,BonnieM 将模型“移动”到 lmer() 中。

I would like to further comment on this idea: BonniM tried to get predictions based on the object mod我想进一步评论这个想法:BonniM 试图根据对象mod预测

mod <- gamlss(css~nit + irr + lit + random(block), 
   sigma.fo=~irr*nit + random(block), data=raw, family=BE)

"Moving into lme()" in this scenario could look as follows:在这种情况下,“进入 lme()”可能如下所示:

mod2 <- gamlss(css~nit + irr + lit + re(random=~1|block), 
               sigma.fo=~irr*nit + re(random=~1|block), 
               data=raw, 
               family=BE)

Predictions on new-data based on mod2 are implemented within the gamlss2 package.基于mod2新数据预测在 gamlss2 包中实现。 Furthermore, mod and mod2 should be the same models.此外, modmod2应该是相同的模型。 See: Stasinopoulos, MD, Rigby, RA, Heller, GZ, Voudouris, V., & De Bastiani, F. (2017).参见:Stasinopoulos, MD, Rigby, RA, Heller, GZ, Voudouris, V., & De Bastiani, F. (2017)。 Flexible regression and smoothing: using GAMLSS in R. Chapman and Hall/CRC.灵活的回归和平滑:在 R. Chapman 和 Hall/CRC 中使用 GAMLSS。 Chapter 10.9.1第 10.9.1 章

Best regards Kai最好的问候凯

我在这个方向上遇到了很多随机问题,并发现使用权重参数进行拟合,并将一些额外的虚拟观察设置为权重为零(但我感兴趣的预测变量)作为一种解决方法。

通过确保 newdata 参数的新数据具有与运行 gamlss 模型时使用的列结构相同的结构,我能够克服 undefined columns selected 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM