从 R gamlss 对象预测新拟合值时出错
Error when predicting new fitted values from R gamlss object
我有一个 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 的实现不完整。这似乎是不完整 feature/function.
的示例
Hedgehog 提到基于新数据的预测尚不可能。
BonnieM 因此 "moved the model" 进入 lmer().
我想进一步评论这个想法:
BonniM 尝试根据对象 mod
进行预测
mod <- gamlss(css~nit + irr + lit + random(block),
sigma.fo=~irr*nit + random(block), data=raw, family=BE)
"Moving into lme()" 在这种情况下可能如下所示:
mod2 <- gamlss(css~nit + irr + lit + re(random=~1|block),
sigma.fo=~irr*nit + re(random=~1|block),
data=raw,
family=BE)
基于 mod2
的新数据预测在 gamlss2 包中实现。
此外,mod
和 mod2
应该是相同的模型。
看:
Stasinopoulos, M. D.、Rigby, R. A.、Heller, G. Z.、Voudouris, V. 和 De Bastiani, F.(2017 年)。灵活的回归和平滑:在 R. Chapman 和 Hall/CRC 中使用 GAMLSS。第10.9.1章
此致
凯
我在这方面遇到了很多随机问题,发现使用权重参数进行拟合,一些额外的虚拟观察设置为权重零(但我感兴趣的预测变量)是一种解决方法。
通过确保 newdata 参数的新数据具有与 运行 gamlss 模型时使用的完全相同的列结构,我能够克服未定义的列选择错误。
我有一个 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 的实现不完整。这似乎是不完整 feature/function.
的示例Hedgehog 提到基于新数据的预测尚不可能。 BonnieM 因此 "moved the model" 进入 lmer().
我想进一步评论这个想法:
BonniM 尝试根据对象 mod
mod <- gamlss(css~nit + irr + lit + random(block),
sigma.fo=~irr*nit + random(block), data=raw, family=BE)
"Moving into lme()" 在这种情况下可能如下所示:
mod2 <- gamlss(css~nit + irr + lit + re(random=~1|block),
sigma.fo=~irr*nit + re(random=~1|block),
data=raw,
family=BE)
基于 mod2
的新数据预测在 gamlss2 包中实现。
此外,mod
和 mod2
应该是相同的模型。
看:
Stasinopoulos, M. D.、Rigby, R. A.、Heller, G. Z.、Voudouris, V. 和 De Bastiani, F.(2017 年)。灵活的回归和平滑:在 R. Chapman 和 Hall/CRC 中使用 GAMLSS。第10.9.1章
此致 凯
我在这方面遇到了很多随机问题,发现使用权重参数进行拟合,一些额外的虚拟观察设置为权重零(但我感兴趣的预测变量)是一种解决方法。
通过确保 newdata 参数的新数据具有与 运行 gamlss 模型时使用的完全相同的列结构,我能够克服未定义的列选择错误。