Hi, I'm Mike Marin and in this video we'll discuss the idea of polynomial
regression and how to fit and assess these models in R. Polynomial
regression is a special case of linear regression where the relationship
between x and y is modeled using a polynomial rather than a line. It can be
used when the relationship between x and y is nonlinear although this is still
considered to be a special case of multiple linear regression; we will be
working with a different version of the lung capacity data as was used in other
videos. A link to download this data can be found in the video description below.
You can also download the R script used in this video there as well. I've already
imported the data into RStudio and attached it. As an example we'll model
the relationship between lung capacity and height. Let's begin by looking at a
scatter plot and let's fit a simple linear regression model and look at a
summary of that model. Here we'll model the relationship between lung capacity
and height, we can take note that the r-squared is about 75% and the residual
standard error is 1.292; we can also add the line for this model
to the plot using the abline command (function); visually we can see that the
relationship between lung capacity and height looks a bit curved or non linear;
a reminder that we can also use residual plots to help with assessing linearity
and checking model assumptions. for a more thorough discussion of this topic
you can refer to one of our earlier videos on checking assumptions for
linear regression. There are many approaches to dealing with
nonlinearities, one we will discuss in this video is including polynomial terms
in our model we will start with including height squared in our model
first let's take a look at the wrong way to do this while it may seem like
including height squared directly in the model call will work, R will not include
height squared in the model if entered this way let's take a look at that here
we can see if we enter height squared directly in the model call, in the model
summary you can see height squared is left out, R has just ignored height
squared. It is important to take note of this because R does not give us a
warning or an error message with this. Now let's take a look at the right way
to do this: to do so we'll use a capital I and then include height squared within
the parentheses and if we ask for a summary of the model we can now see that
height squared has been included in the model we'll get to talking about this
model in a moment but first let's take a look at a few other ways to get the same
result instead of using the approach we just
showed we could instead first create a new variable called height squared and
then include this variable in the model here we can see we're creating this
height square variable and including that in our model and if we ask for the
summary you can see that this produces the exact same model and results as the
previous set of commands, we could also make use of the Poly command (function) in R,
here we would let R know that we would like to include polynomial terms for the
height variable and we set the degree argument to the degree of polynomial
we'd like, in this case setting the degree equal 2 will include height and
height squared and if instead of setting the raw argument to TRUE we set it to
FALSE, R would fit a model that used orthogonal polynomials so let's fit that
model and again you can take the time to verify that this produces the exact same
results as the earlier two ways we looked at so let's just give ourselves a
quick reminder of the model that we fit we can see here that with this
polynomial including height squared the R squared is about 77% and the residual
standard error is 1.238, you may recall that for the model
with only height the R squared was about 75% and the residual standard error was
1.292, it looks like height squared may be improving the
model let's take a look at this visually we can add the polynomial model to the
plot using the lines command, so let's add this line using a thick blue line
subjectively it looks like the model that includes height squared may provide
a better fit to the data than the model that does not include height squared
let's compare these two models formally using the partial F test. for a more
detailed discussion of the partial F test you can see one of our earlier
videos where we discuss this test; this test has a null hypothesis that there is
no significant difference between the two models and an alternative hypothesis
that's a full model, the model that includes height squared, is significantly
better we can run the test in R, using the ANOVA command we can see that with
such a small p-value we will reject the null hypothesis and conclude that we
have evidence to believe the model including height squared provides a
statistically significantly better fit than the model without height squared
most often we won't want to include polynomial terms much beyond x squared
or x cubed let's explore a model that includes X cubed as well and it's worth
noting that you must always include all lower order terms in a model. If we
include height cubed we must also include height squared and height in the
model let's fit this model including height squared and height cubed and
let's ask for a summary we can add this model to the plot using the lines
command and we will do this as a thick green dashed line let's also add a
legend to the plot to help remind ourselves which model is which we can
see visually that there's almost no difference between the model that
includes height cubed and the model that does not as before we can use the
partial F test to help us decide if height cubed improves the model here
when we conduct the partial F test we can see that the p-value is large and
there is not a statistically significant difference in the models we can decide
that including height cubed is not necessary in our model before finishing
off we should add a note that there are other approaches to dealing with
nonlinearities some of which include transforming the x or y variable
converting x to a categorical variable or factor or using nonlinear regression
methods instead. All of these different approaches have their pros and cons!
thanks for watching this video make sure to subscribe to marinstatslectures, like us on
Facebook visit our website (statslectures.com)