简体   繁体   English

如何用独立数据预测具有平滑项和基本函数的 GAM?

[英]How to predict GAM with smooth terms and basic functions with independent data?

I attempt to fit a GAM model with interactions between days (tt variable) and lagged predictors (k=2) using k basis functions.我尝试使用 k 个基函数拟合 GAM model 与天数(tt 变量)和滞后预测变量(k=2)之间的交互。

library(mgcv)
# Example data
data=data.frame(
tt=1:107, # days
pol=(sample.int(101,size=107,replace=TRUE)-1)/100,
at_rec=sample.int(101,size=107,replace=TRUE),
w_cas=sample.int(2000,size=107,replace=TRUE)
)

# model
gam1<-gam(pol ~ s(tt, k = 10) + 
            s(tt, by = Lag(at_rec, k = 2), k = 10)+
            s(tt, by = Lag(w_cas, k = 2), k = 10), 
          data=data,method="GACV.Cp")
summary(gam1)

# while making newdata
> newdata=data.frame(tt=c(12,22),at_rec=c(44,34), w_cas=c(2011,2455))
# and prediction
> predict(gam1,newdata=newdata,se.fit=TRUE)

I got this error "Error in PredictMat(object$smooth[[k]], data): Can't find by variable"我收到此错误“PredictMat 中的错误(object$smooth[[k]],data):无法通过变量找到”

How to predict such a model with new data?如何用新数据预测这样的 model?

I'm 99.9% sure that the predict method can't find the by terms because they are functions of variables and it's looking for variables with exactly the names you provided: "Lag(at_rec, k = 2)".我 99.9% 确信predict方法找不到by项,因为它们是变量的函数,并且它正在寻找与您提供的名称完全相同的变量:“Lag(at_rec, k = 2)”。

Try adding those lagged variables to your data frame as explicit variables and refit the model and it should work:尝试将这些滞后变量作为显式变量添加到您的数据框中并重新安装 model,它应该可以工作:

data <- transform(data,
                  lag_at_rec = Lag(at_rec, k=2),
                  lag_w_cas = Lag(w_cas, k=2))
gam1 <- gam(pol ~ s(tt, k = 10) + 
              s(tt, by = lag_at_rec, k = 10)+
              s(tt, by = lag_w_cas, k = 10), 
            data = data, method = "GACV.Cp")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM