Interpolating timeseries

Alex Archibald picture Alex Archibald · Oct 25, 2012 · Viewed 18.6k times · Source

I have a time series problem which I hope someone can help with!

The problem revolves around two sets of data with different time stamps. One set of data contains calibration data, the other contains sample data. The calibration is much less frequent than the samples.

What I would like to do is interpolate the calibration data (low freq) onto the sample time series (high freq).

sam <- textConnection("time, value
01:00:52, 256
01:03:02, 254
01:05:23, 255
01:07:42, 257
01:10:12, 256")

cal <- textConnection("time, value
01:01:02, 252.3
01:05:15, 249.8
01:10:02, 255.6")

sample <- read.csv(sam)

sample$time <- as.POSIXct(sample$time, format="%H:%M:%S")

calib <- read.csv(cal)

calib$time <- as.POSIXct(calib$time, format="%H:%M:%S")

The big problem (that I see) is that the freq of the data changes randomly.

Have any of you had to do similar things? Is there a chron or zoo function which would do what I want (interpolate low freq data onto higher freq data where both ts are random)?

Answer

Joshua Ulrich picture Joshua Ulrich · Oct 25, 2012

I would use zoo (or xts) and do it like this:

library(zoo)
# Create zoo objects
zc <- zoo(calib$value, calib$time)    # low freq
zs <- zoo(sample$value, sample$time)  # high freq
# Merge series into one object
z <- merge(zs,zc)
# Interpolate calibration data (na.spline could also be used)
z$zc <- na.approx(z$zc, rule=2)
# Only keep index values from sample data
Z <- z[index(zs),]
Z
#                      zs       zc
# 2012-10-25 01:00:52 256 252.3000
# 2012-10-25 01:03:02 254 251.1142
# 2012-10-25 01:05:23 255 249.9617
# 2012-10-25 01:07:42 257 252.7707
# 2012-10-25 01:10:12 256 255.6000