简体   繁体   中英

Predict Command with GAM package in R

I am trying to use the predict command for gam functions in the "gam" package. I find that when I have two or more splines that I cannot predict onto a new sample, even if the new sample is the original data or a subset of the original data. The last two lines in the code below gives an error.

EDIT: gam.data is a dataset that comes with the gam package and used in the examples for gam and predict.gam. It's a data.frame with columns: x, y, z, f, probf, ybin, and ybin 2 -- all numeric.

require(gam)

data(gam.data)
Gam.object <- gam(y ~ s(x,z,df=6)+s(f,probf,df=6),data=gam.data)
summary(Gam.object)
predict(Gam.object)
predict(Gam.object,newdata=gam.data)
predict(Gam.object,newdata=gam.data[1:20,])

predict(Gam.object,newdata=gam.data)

Error in gam.s(data[["s(f, probf, df = 6)"]], z, w, spar = probf, df = 6, : object 'probf' not found

predict(Gam.object,newdata=gam.data[1:20,])

Error in gam.s(data[["s(f, probf, df = 6)"]], z, w, spar = probf, df = 6, : object 'probf' not found

The answer seems to be missing a call to terms="response" (or some other terms call) in the predict command.

The following runs:

data(gam.data)
Gam.object <- gam(y ~ s(x,z,df=6)+s(f,probf,df=6),data=gam.data)
summary(Gam.object)
predict(Gam.object)
predict(Gam.object,newdata=gam.data,terms="response")
predict(Gam.object,newdata=gam.data[1:20,],terms="response")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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