Search code examples
rlinear-regressionpolynomials

How to model polynomial regression in R?


I've a dataset with 70 variables, and I want to try polynomial regression on it. If the number of columns were three/four I could just hand code something like this --

 model <- lm(y ~ poly(var1,3) + poly(var2,3) + poly(var4,4)

How would we go about this, if we have 70 variables? Should we type in manually names of all the variables or is there a easier method?


Solution

  • 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="+")))