# R Tutorial Series: Basic Polynomial Regression

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Often times, a scatterplot reveals a pattern that seems not so linear. Polynomial regression can be used to explore a predictor at different levels of curvilinearity. This tutorial will demonstrate how polynomial regression can be used in a hierarchical fashion to best represent a dataset in R.**R Tutorial Series**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

### Tutorial Files

Before we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. Note that all code samples in this tutorial assume that this data has already been read into an R variable and has been attached. This dataset contains hypothetical student data that uses practice exam scores to predict final exam scores.### Scatterplot

### Creating The Higher Order Variables

A two step process, identical to the one used to create interaction variables, can be followed to create higher order variables in R. First, the variables must be centered to mitigate multicollinearity. Second, the predictor must be multiplied by itself a certain number of times to create each higher order variable. In this tutorial, we will explore the a linear, quadratic, and cubic model. Therefore, the predictor will need to be squared to create the quadratic model and cubed to create the cubic model.#### Step 1: Centering

To center a variable, simply subtract its mean from each data point and save the result into a new R variable, as demonstrated below.

- > #center the independent variable
- > FinalC <- Final - mean(Final)
- > #center the predictor
- > PracticeC <- Practice - mean(Practice)

#### Step 2: Multiplication

Once the input variable has been centered, the higher order terms can be created. Since a higher order variable is formed by the product of a predictor with itself, we can simply multiply our centered term from step one and save the result into a new R variable, as demonstrated below.

- > #create the quadratic variable
- > PracticeC2 <- PracticeC * PracticeC
- > #create the cubic variable
- > PracticeC3 <- PracticeC * PracticeC * PracticeC

### Creating The Models

Now we have all of the pieces necessary to assemble our linear and curvilinear models.

- > #create the models using lm(FORMULA, DATAVAR)
- > #linear model
- > linearModel <- lm(FinalC ~ PracticeC, datavar)
- > #quadratic model
- > quadraticModel <- lm(FinalC ~ PracticeC + PracticeC2, datavar)
- > #cubic model
- > cubicModel <- lm(FinalC ~ PracticeC + PracticeC2 + PracticeC3, datavar)

### Evaluating The Models

As is the case in other forms of regression, it can be helpful to summarize and compare our potential models using the summary(MODEL) and anova(MODEL1, MODEL2,… MODELi) functions.- > #display summary information about the models
- > summary(linearModel)
- > summary(quadraticModel)
- > summary(cubicModel)
- #compare the models using ANOVA
- anova(linearModel, quadraticModel, cubicModel)

At this point we can compare the models. In this case, the quadratic and cubic terms are not statistically significant themselves nor are their models statistically significant beyond the linear model. However, in a real research study, there would be other practical considerations to make before deciding on a final model.

### More On Interactions, Polynomials, and HLR

Certainly, much more can be done with these topics than I have covered in my tutorials. What I have provided is a basic discussion with guided examples. The regression topics covered in these tutorials can be mixed and matched to create exceedingly complex models. For example, multiple interactions and higher order variables could be contained in a single model. The good news is that more complex models can be created using the same techniques covered here. The basic principles remain the same.### Complete Polynomial Regression Example

To see a complete example of how polynomial regression models can be created in R, please download the polynomial regression example (.txt) file.To

**leave a comment**for the author, please follow the link and comment on their blog:**R Tutorial Series**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.