我有一个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
当我在没有使用 newdata
的情况下使用 predict
时,它可以很好地对原始数据进行预测,例如:
predmu <- predict(mod, what="mu", type="response")
我使用predict方法的方式有误吗?非常感谢任何建议!谢谢。