-2

To do a [muliple] linear regression model, one uses lm

Is it possible to derive a multiple polynomial regression model? Where each coefficient is a polynomial function?

thistleknot
  • 1,098
  • 16
  • 38
  • 1
    See this for an example: https://stats.stackexchange.com/questions/280344/multiple-polynomial-regression-versus-gam – Phil Sep 14 '18 at 03:36
  • Could you post some code to support your question? – Artem Sep 14 '18 at 06:04
  • 1
    Do you mean `lm(y~poly(x1,2)+poly(x2,3))`? – user2974951 Sep 14 '18 at 06:18
  • yep, something like this. and it would be nice if you transfer information from the comment in the body of quesion by editing. Moreover to post some [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be great. – Artem Sep 14 '18 at 07:13

1 Answers1

1

You can do it, please see an example below. Just add in poly function argument raw = TRUE to get easily interpretable coefficients:

set.seed(123)

x <- seq(-10, 10, by = 0.1)
y <- 0.5 * x ^ 3 + rnorm(length(x), 0, sd = 10)

df <- data.frame(x, y)

m <- lm(y ~ poly(x,3, raw = TRUE), data = df)
summary(m)

# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)             -0.337708   1.015189  -0.333    0.740    
# poly(x, 3, raw = TRUE)1 -0.156641   0.291625  -0.537    0.592    
# poly(x, 3, raw = TRUE)2  0.010747   0.022476   0.478    0.633    
# poly(x, 3, raw = TRUE)3  0.501871   0.004411 113.783   <2e-16 ***
#  ---
#  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

plot(df$x, df$y, col = "blue", xlab = "x", ylab = "y")
df$fitted <- fitted(m, data.frame(x))
lines(df$x, df$fitted, col = "red", lwd = 2)

Output (red line is fitted data, blue dots are initial data):

enter image description here

Artem
  • 3,304
  • 3
  • 18
  • 41
  • thank you. I intend on using this on ogive's generally, but it would be nice to have a function that dynamically set's # of co-efficients, or allows me to set a static # per column in a dataframe. But I might be asking for too much. I know the danger of overfitting and negative slopes (on an ogive!) from polynomial equations can lead to incorrect interpolated values. I was thinking maybe a method that literally uses interpolation [triangulation to avoid negative slopes on an ogive], but I assume that would simply be MR with a lot of coefficients. – thistleknot Sep 15 '18 at 14:35