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

Changing predicted values to response scale after predict() function, not via type = "response"

在 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 -> 
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 = TRUE 传递给 predict() 以停止predict() 检查您是否正确传递了模型所需的所有变量。

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