gpt4 book ai didi

r - 在 predict() 函数之后将预测值更改为响应比例,而不是通过 type = "response"

转载 作者:行者123 更新时间:2023-12-04 15:35:41 25 4
gpt4 key购买 nike

在 R 中工作。当我必须从预测中排除随机效应时,我在计算响应量表的预测值时遇到了麻烦。通过从预测中排除随机效应,我需要指定 type = "terms",从而无法包含 type = "response" 参数。有没有一种方法可以将预测值重新计算为响应量表(beta 回归)?或者是否可以在predict 函数中同时指定排除Areatype = "response"?请在下面查看我的代码。

str(data_re)
# 'data.frame': 35 obs. of 17 variables:
# $ ProportionBirdsScavenging: num 0.6619 0.4062 0.6943 0.0143 0.0143 ...
# $ OverheadCover : num 0.7 0.671 0.679 0.79 0.62 ...
# $ Area : Factor w/ 6 levels "Hamert","KempenBroek",..: 3 1 1 1 1 1 1 1 1 2 ...
# $ pointWeight : int 3 233 10 89 4 22 44 99 89 17 ...

mygam <- mgcv::gam(ProportionBirdsScavenging ~ OverheadCover + s(Area, bs="re"), family=betar(link="logit"), data = data_re, weights = pointWeight)
new.xgam <- expand.grid(OverheadCover = seq(0, 1, length.out = 1000))
new.xgam$Area <- "a" # pad new.xgam with an arbitrary value for variable Area -> https://stackoverflow.com/questions/54411851/mgcv-how-to-use-exclude-argument-in-predict-gam
new.ygam <- predict.gam(mygam, newdata = new.xgam, type = "terms", exclude = "s(Area)") # Because I have to specify type = "terms", I can't specify type = "response".
new.ygam <- data.frame(new.ygam)

head(new.ygam) # not on the response scale (0,1)
# OverheadCover
# 1 0.000000000
# 2 -0.004390776
# 3 -0.008781551
# 4 -0.013172327
# 5 -0.017563103
# 6 -0.021953878

最佳答案

您误读了参数exclude 的文档:

exclude: if type=="terms" or type="iterms" then terms (smooth or parametric) named in this array will not be returned. Otherwise any smooth terms named in this array will be set to zero. If NULL then no terms are excluded. Note that this is the term names as it appears in the model summary, see example. You can avoid providing the covariates for the excluded terms by setting newdata.guaranteed=TRUE, which will avoid all checks on newdata.

(强调我的)。

您可以使用 type = "response", exclude = "s(Area)") 并且应该忽略随机效应。您必须向 newdata 传递一些 Area 的值,否则这将不起作用;只需将newdata中的Area列设置为Area的第一层即可。

如果您非常小心,您也可以避免传入 ranef 变量。如果您确定传递给 newdata 的是为模型正确指定的一组变量,则可以省略 Area 并传递 newdata.guaranteed = TRUEpredict() 以阻止 predict() 检查您是否已正确传递模型所需的所有变量。

请参阅 ?mgcv::random.effects 中的示例以了解这两种行为。

关于r - 在 predict() 函数之后将预测值更改为响应比例,而不是通过 type = "response",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59858429/

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