4

Lets say I have a data set with y and x1, x2, xp variables. I want to fit all of my predictors with splines.

For example :

gam_object = gam(y ~ s(x1) + s(x2) + s(xp), data)

How can I do this without typing every single variable? If I would like to fit the module without the first two without using splines. How can I do that?

gam_object2 =  gam(y ~ x1 + x2 + s(x1) + s(x2), data)
etienne
  • 3,648
  • 4
  • 23
  • 37
Jixxi
  • 111
  • 1
  • 11

1 Answers1

4

Maybe this could help you:

p<-10
as.formula(paste0("y~",paste0("s(x",1:p,")",collapse="+")))

If you don't want to use first two or more generally don't use splines on some specific variables use:

data<- #yours data
use<-c(3:6,9:10)
dontuse<-c(1:2,7:8)
form<-as.formula(
 paste0("y~",paste0("s(x",use,")",collapse="+"),"+",paste0("x",dontuse,collapse="+"),collapse=""))

And then run model:

gam(data=data,formula=form,family=gaussian)
Maju116
  • 1,607
  • 1
  • 15
  • 30