Interpolating timeseries

匆匆过客 提交于 2019-11-27 19:51:23

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

You can also use approx function like this and it will be much easier. Just make sure that you are working with data frames. Also, make sure that format of the column in calibration and sample data-set are the same by using as.POSIXct.

 calib <- data.frame(calib); sample <- data.frame(sample)

 IPcal <- data.frame(approx(calib$time,calib$value, xout = sample$time, 
                 rule = 2, method = "linear", ties = mean))

 head(IPcal)

#                x        y
#1 2017-03-22 01:00:52 252.3000
#2 2017-03-22 01:03:02 251.1142
#3 2017-03-22 01:05:23 249.9617
#4 2017-03-22 01:07:42 252.7707
#5 2017-03-22 01:10:12 255.6000

Read more about approx on approxfun documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!