user2472273
user2472273

Reputation: 125

Interpolation in R

I have hourly time series and would like to interpolate sub-hourly values like every 15 min. Linear interpolation will do. But if there is any way to specify Gaussian, Polynomial, that would be great.

For example if I have

a<-c(4.5,7,3.3) which is the first three hour data. How can I get 15 min sub-hourly data, total of 9 values in this case? I have been using approx function and studying zoo package and still don't know how I can do it. Thank you very much!

Upvotes: 2

Views: 4389

Answers (2)

agstudy
agstudy

Reputation: 121568

Within xts package, you can either na.approx or na.spline.

  1. Coerce you times series to an xts object
  2. Create a new index having 15 minutes intervals
  3. Use this new index to create a NULL xts object that you merge with your object
  4. Approximate missing values using na.approx for linear/constant approx or na.spline for polynomial one.

here a complete example:

library(xts)
set.seed(21)
## you create the xts object
x <- xts(rnorm(10),
         seq(from=as.POSIXct(Sys.Date()),
             length.out=10,
             by=as.difftime(1,units='hours')))
## new index to be used 
new.index <- 
  seq(min(index(x)),max(index(x)), by=as.difftime(15,units='mins'))
## linear approx
na.approx(merge(x,xts(NULL,new.index)))
## polynomial approx
na.spline(merge(x,xts(NULL,new.index)))

Upvotes: 1

Doctor Dan
Doctor Dan

Reputation: 771

How about this:

b<-xts(c(4.5,7,3.3), order.by=as.POSIXct(c('2013-07-26 0:00', 
                                           '2013-07-26 2:00', 
                                           '2013-07-26 3:00')))
approx(b, n=13)   ,

adjusting n for the appropriate time interval?

Upvotes: 4

Related Questions