【发布时间】:2017-03-12 00:51:18
【问题描述】:
我有一个 gamlss 模型,我想用它来做出新的 y 预测(和置信区间),以便可视化模型与真实数据的拟合程度。我想根据随机预测值的新数据集(而不是原始数据)进行预测,但我遇到了错误消息。下面是一些示例代码:
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")
这会产生以下错误:
Error in data[match(names(newdata), names(data))] :
object of type 'closure' is not subsettable
当我在我的真实数据上运行它时,它会给出稍微不同的错误:
Error in `[.data.frame`(data, match(names(newdata), names(data))) :
undefined columns selected
当我使用 predict 而不使用 newdata 时,它可以很好地对原始数据进行预测,如下所示:
predmu <- predict(mod, what="mu", type="response")
我使用预测错误吗?任何建议都非常感谢!谢谢。
【问题讨论】: