1

The code below displays the problem. I ran a degree-four-polynomial regression lm on the data.frame dfto obtain model4. Then I createD the regression function fhat4. This works as intended.

I want to generalize this to any degree of polynomial. So, I use poly to create modeln. This matches model4. But I am not able to create the appropriate function fhatn. Perhaps this is something to do with the for loop?

df <- structure(list(x = c(0.3543937637005, 0.674911001464352, 0.21966037643142, 
0.14723521983251, 0.36166316177696, 0.975983075099066, 0.539355604210868, 
0.294046462047845, 0.853777077747509, 0.634912414476275), y = c(0.0120776002295315, 
0.655085238162428, 0.310665819328278, 0.525274415733293, 0.938241509487852, 
0.520828885724768, 0.241615766659379, 0.724816955626011, 0.808277940144762, 
0.358921303786337)), .Names = c("x", "y"), row.names = c(NA, 
-10L), class = "data.frame")

############################################# 
model4 <- lm(y~x+I(x^2)+I(x^3)+I(x^4), data=df)

fhat4 <- function (x) {
  model4$coefficients[1]+
  model4$coefficients[2]*x+
  model4$coefficients[3]*x^2+
  model4$coefficients[4]*x^3+
  model4$coefficients[5]*x^4
  }

fhat4(2)

############################################# 
modeln <- lm(y~poly(x,4,raw=TRUE), data=df)

fhatn <- function (x) {
  fn <- 0
  for (i in 0:5){
    fn <- fn + modeln$coefficients[i+1]*x^i
  }
}

fhatn(4)
Cettt
  • 11,460
  • 7
  • 35
  • 58
Geoff
  • 925
  • 4
  • 14
  • 36

1 Answers1

2

your for loop should only go from 0 to 4 not till 5. Also your function does not return anything so you could add a return(fn) at the end.

Anyway, you can implement the same function without any loops:

modeln <- lm(y ~ poly(x, 4, raw = TRUE), data = df)

fhatn <- function (x) {
  sum(x^(seq_along(coef(modeln)) - 1) * coef(modeln))
}

fhatn(2)
[1] -150.6643

Note that coef(modeln) is an alternative to modeln$coefficients.

Or as Vincent said in the comments you can use the predict function:

predict(modeln, newdata = data.frame(x = 2))
-150.6643 
Cettt
  • 11,460
  • 7
  • 35
  • 58
  • The key was simply the lack of return(fn). Many thanks. You're right of course that `i` need only go up to 4. Since that was a typo, one could edit that bit out of the original question. – Geoff Nov 10 '19 at 11:35
  • Note that a function without any return value still returns something but that something is not printed out in the console. You can try `print(fhatn(2))` for your old function `fhatn` and it should also work. Check also [this question](https://stackoverflow.com/questions/21541012/r-function-with-no-return-value). – Cettt Nov 10 '19 at 11:36
  • `coef(fm)` and `fm$coefficients` are not always equivalent . In general, `coef(fm)` should be used. – G. Grothendieck Nov 10 '19 at 13:39
  • @G.Grothendieck good to know, thank you. Can you elaborate a bit more or do you know a good source? – Cettt Nov 10 '19 at 13:43
  • 1
    Look at the source. You can see that it does give the same result as `fm$coefficients` in some cases and not in others. Also `fm$coefficients` depends on the particular data structure that `lm` uses internally whereas if that were to change in the future then `coef` would presumably change in a corresponding way but `fm$coefficients` might break. Also see: https://en.wikipedia.org/wiki/Information_hiding – G. Grothendieck Nov 10 '19 at 14:04