Here's some examples that will generate your polynomials.
# Simulate some data
ind1 <- rnorm(100)
ind2 <- rnorm(100)
ind3 <- rnorm(100)
ind4 <- rnorm(100)
ind5 <- rnorm(100)
dep <- rnorm(100, mean=ind1)
Polynomials can be defined manually using the I
function. For example a polynomial of degree 3 for ind1
will be
lm(dep ~ ind1 + I(ind1^2) + I(ind1^3))
You can also use the poly
function to generate the polynomials for you, e.g.,
lm(dep ~ poly(ind1, degree=3, raw=TRUE))
The argument raw=TRUE
is needed to get raw and not orthogonal polynomials. It doesn't impact the predictions or the fit but it does ensure that the parameter estimates are comparable.
Thus, you can fit your desired model with
lm(dep ~ poly(ind1, degree=3, raw=TRUE) +
poly(ind2, degree=3, raw=TRUE) +
poly(ind3, degree=3, raw=TRUE) +
poly(ind4, degree=3, raw=TRUE) +
poly(ind5, degree=3, raw=TRUE))
Note that it may be necessary to scale your predictors. If you measure something that results in large values then ind^3
may give you numerical problems.