gpt4 book ai didi

r - mgcv:如何在 predict.gam 中使用 'exclude' 参数?

转载 作者:行者123 更新时间:2023-12-02 02:46:34 26 4
gpt4 key购买 nike

我有一个结构如下的模型,我想在忽略随机效应的同时提取预测值。如 ?predict.gam 中所述和 here ,我正在使用 exclude参数,但我收到一个错误。我的错误在哪里?

dt <- data.frame(n1 = runif(500, min=0, max=1),
n2 = rep(1:10,50),
n3 = runif(500, min=0, max=2),
n4 = runif(500, min=0, max=2),
c1 = factor(rep(c("X","Y"),250)),
c2 = factor(rep(c("a", "b", "c", "d", "e"), 100)))

mod = gam(n1 ~
s(n2, n3, n4, by=c1) +
s(c2, bs="re"),
data=dt)

newd=data.table(expand.grid(n1=seq(min(dt$n1), max(dt$n1), 0.5),
n2=1:10,
n3=seq(min(dt$n3), max(dt$n3), 0.5),
n4=seq(min(dt$n4), max(dt$n4), 0.5),
c1=c("X", "Y")))
newd$pred <- predict.gam(mod, newd, exclude = "s(c2)")

In predict.gam(mod, newd, exclude = "s(c2)"): not all required variables have been supplied in newdata!

最佳答案

exclude不像你想象的那样工作。您仍然需要在 newd 中提供所有变量为 predict.gam .看我的 this answer后面是什么predict.gam .

以下是您需要做的:

## pad newd with an arbitrary value for variable c2
newd$c2 <- "a"
## termwise prediction
pt <- predict.gam(mod, newd, type = "terms", exclude = "s(c2)")
## linear predictor without random effect
lp_no_c2 <- rowSums(pt) + attr(pt, "constant")

关于r - mgcv:如何在 predict.gam 中使用 'exclude' 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54411851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com