在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
您正在误读参数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
中的示例。