-1
8,27268
17,7308
29,5468
59,4136
149,3741
299,1438
599,297
749,113
750,44

These are my data and I would like to find the polynomial fit and put it in a function.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • What order of polynomial? See [this post](http://stackoverflow.com/questions/3822535/fitting-polynomial-model-to-data-in-r). – jlhoward Aug 12 '14 at 19:44

1 Answers1

0

If this is your sample data

dd<-data.frame(
    x = c(8, 17, 29, 59, 149, 299, 599, 749, 750), 
    y = c(27268, 7308, 5468, 4136, 3741, 1438, 297, 113, 44)
)

You can fit a polynomial with any order using the poly() function. Here I fit a 3rd order polynomial

fit <- lm(y~poly(x,3),dd)

# Call:
# lm(formula = y ~ poly(x, 3), data = dd)
# 
# Coefficients:
# (Intercept)  poly(x, 3)1  poly(x, 3)2  poly(x, 3)3  
#        5535       -14073         8504        -6091  

Now I can plot the results

plot(dd)
x <- seq(min(dd$x), max(dd$x), length.out=100)
lines(x, predict(fit, newdata=data.frame(x=x)))

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295