简体   繁体   English

我如何在R中使用gam的样条曲线拟合所有变量而无需键入每个变量?

[英]How can I fit all variables using splines with gam in R without typing each one?

Lets say I have a data set with y and x1 , x2 , xp variables. 可以说我有一个数据集,其中包含yx1x2xp变量。 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)

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)

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

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