0

This is my data:

y<-c(1.8, 2, 2.8, 2.9, 2.46, 1.8,0.3,1.1,0.664,0.86,1,1.9)
x<- c(1:12)
data<-as.data.frame(cbind(y,x))
plot(data$y ~ data$x)

enter image description here

I want to fit a curve through these points so that I can generate the intermediate predicted values. I need a curve that goes through the points. I don't care what function it fits.

I consulted this link.

Fitting a curve to specific data

 install.packages("rgp")
 library(rgp)

 result <- symbolicRegression(y ~ x,data=data,functionSet=mathFunctionSet,
                         stopCondition=makeStepsStopCondition(2000))

 # inspect results, they'll be different every time...
 (symbreg <- result$population[[which.min(sapply(result$population, 
 result$fitnessFunction))]])

  function (x) 
   exp(sin(sqrt(x)))


# inspect visual fit

 ggplot() + geom_point(data=data, aes(x,y), size = 3) +
  geom_line(data=data.frame(symbx=data$x, symby=sapply(data$x, symbreg)), 
  aes(symbx, symby), colour = "red")

enter image description here

If I repeat this analysis again, every time the function above produces a different curve. Does anyone know why is this happening and whether this is a right way to fit a curve in these points? Also this function does not go through each points therefore I cannot obtain the intermediates points.

user53020
  • 889
  • 2
  • 10
  • 33
  • There is an important clarification you need to make: do you need a curve that goes *through* the points (and don't care about the concrete function) - or do you need to estimate the parameters in a known function (and don't care whether the points are on the function)? – bdecaf Jul 21 '17 at 11:13
  • I need a curve that goes through the points. I don't care what function it fits. – user53020 Jul 21 '17 at 11:15

1 Answers1

4

A standard approach is to fit a spline, this gives a nice curve that goeas through all points. See spline. Concretely you would use a call like:

spline(x = myX, y = myY, xout=whereToInterpolate)

or just calculating 100 points to your example:

ss <- spline(x,y, n=100)
plot(x,y)
lines(ss)

output

Note there is also a smoothing spline which may help for noisy data.

If the curve doesn't need to be smooth there is the simpler approx which does linear interpolation.

approx(x = myX, y = myY, xout=whereToInterpolate) 
bdecaf
  • 4,652
  • 23
  • 44