将预测值更改为predict()函数之后的响应标度,而不是通过类型=“ response”

问题描述 投票:2回答:1

在R中工作。当我不得不从预测中排除随机效应时,我难以计算出我的预测值到响应等级。通过从预测中排除随机效应,我需要指定type = "terms",从而无法包含type = "response"参数。有没有一种方法可以将预测值重新计算为响应量表(β回归)?还是可以在Area功能中同时指定type = "response"predict的排除?请在下面查看我的代码。

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
r predict gam
1个回答
0
投票

您正在误读参数exclude的文档:

排除:如果type=="terms"type="iterms"则表示(平滑或参数)将不会返回此数组中命名的参数。Otherwise在此数组中命名的任何平滑术语都将设置为零。如果NULL,则不排除任何术语。注意这个是模型摘要中显示的术语名称,请参见例。您可以避免提供以下变量的协变量通过设置newdata.guaranteed=TRUE排除条款,将避免对newdata进行所有检查。

(重点是我的。)>

您可以使用type = "response", exclude = "s(Area)"),应忽略随机效果。您必须将newdata的一些值传递给Area,否则将无法使用;只需将Area中的newdata列设置为Area的所有第一级即可。

如果您非常小心

,您也可以避免传入ranef变量。如果您确定传递给newdata的是模型的正确指定的变量集,则可以省略Area并将newdata.guaranteed = TRUE传递给predict()以阻止predict()检查是否具有正确传递了模型所需的所有变量。

关于两种类型的行为,请参见?mgcv::random.effects中的示例。

© www.soinside.com 2019 - 2024. All rights reserved.