[R] Connect observed values by a smooth curve, is there any method to get coordinate value of arbitrary point on the curve?

Duncan Murdoch murdoch.duncan at gmail.com
Wed Mar 30 13:33:34 CEST 2011


On 11-03-29 9:32 PM, Zhipeng Wang wrote:
> Dear all,
>
> I have the problem as the title shows. It is time series of measurements. I
> want to estimate the value at time point when we have no actual measured
> value (Only in the time range,  not for prediction in future time).
>
> Hope you could give me some suggestion or hint. Thank you so much.

If you want to interpolate the data, use splinefun.  For example,

x <- 1:5
y <- rnorm(5)

f <- splinefun(x,y)
curve(f, from=1, to=5)
points(x,y)


If you want to draw a smooth curve through the data, then use one of the 
model fitting functions, and then predict to compute values at new x 
values.  For example,

library(mgcv)
x <- 1:50      # Smoothing needs more data
y <- rnorm(50)
fit <- gam(y ~ s(x))
newx <- seq(1,50, len=1000)
newy <- predict(fit, newdata=data.frame(x=newx))

plot(x,y)
lines(newx, newy)

Duncan Murdoch



More information about the R-help mailing list