How to model polynomial regression in R?

£可爱£侵袭症+ 提交于 2019-12-01 08:45:21

You could paste the formula, if all variables are named systematically:

form <- as.formula(paste("y~", paste0("poly(var", 1:10, ")", collapse="+")))

or (for polynomial of 3rd degree):

form <- as.formula(paste("y~", paste0("poly(var", 1:10, ", degree=3)", collapse="+")))

Also, if you have only the dependent variable y and covariates of interest (that have non-systematic names) in your dataset df, you can try

ind.y <- grep("y", colnames(df))
form <- as.formula(paste("y~", paste0("poly(", colnames(df[, -ind.y]), ", degree=3)", collapse="+")))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!