插入时间序列 [英] Interpolating timeseries

查看:31
本文介绍了插入时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间序列问题,希望有人能帮忙解决!

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.

你们有没有做过类似的事情?是否有一个 chron 或 zoo 函数可以做我想要的(将低频数据插入到两个 ts 都是随机的高频数据上)?

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)?

推荐答案

我会使用 zoo(或 xts)并这样做:

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

这篇关于插入时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆