时间序列插值

时间:2017-03-21 20:58:37

标签: r time-series interpolation zoo

我有两个系列的数据(校准和样本),我试图将每月的校准数据插入样本的频率,该频率在每分钟到第二个之间随机变化。

我试过了这个(Interpolating timeseries),这是我的代码:

     require(zoo)

     zc <- zoo(calib$MW2, calib$Date)    
     zs <- zoo(sample$MW.2, sample$DateMW.2)

     z <- merge(zc, zs)

     zc <- zoo(calib$MW2, calib$Date)   
     zs <- zoo(sample$MW.2, sample$DateMW.2)  

     # "merge" gets data frames only
     zc <- data.frame(zc)
     zs <- data.frame(zs)

     z <- merge(zc, zs)

     z$zc <- na.approx(z$zc, rule=2)

     df <- z[index(zs),]

注意: 在合并之前将zoo的输出转换为data.framezczs)。< / p>

问题在于它不是插值,而是重复校准数据集;您可以查看所谓插值df的部分,并将其与上面的原始数据进行比较,以确认我说的内容;

     > df
        zc       zs                date     
     1     60.84440 61.40373 2016-06-02 18:15:00
     2     58.85957 61.40373 2016-06-02 18:30:00
     3     57.49543 61.40373 2016-06-02 18:45:00
     4     56.32829 61.40373 2016-06-02 19:00:00
     5     56.84261 61.40373 2016-06-02 19:15:00
     6     57.76762 61.40373 2016-06-02 19:30:00
     7     59.58310 61.40373 2016-06-02 19:45:00
     8     59.95826 61.40373 2016-06-02 20:00:00
     9     60.84440 61.41549 2016-06-02 20:15:00
     10    58.85957 61.41549 2016-06-02 20:30:00
     11    57.49543 61.41549 2016-06-02 20:45:00
     12    56.32829 61.41549 2016-06-02 21:00:00

数据:

     sample <- structure(list(DateMW.2 = structure(1:15, .Label = c("6/2/2016 18:15:00", 
     "6/2/2016 18:30:00", "6/2/2016 18:45:00", "6/2/2016 19:00:00", 
     "6/2/2016 19:15:00", "6/2/2016 19:30:00", "6/2/2016 19:45:00", 
     "6/2/2016 20:00:00", "6/2/2016 20:15:00", "6/2/2016 20:30:00", 
     "6/2/2016 20:45:00", "6/2/2016 21:00:00", "6/2/2016 21:15:00", 
     "6/2/2016 21:30:00", "6/2/2016 21:45:00"), class = "factor"), 
     MW.2 = c(61.40373, 61.41549, 61.41549, 61.42451, 61.42752, 
     61.42478, 61.43107, 61.42369, 61.40564, 61.41056, 61.40592, 
     61.39416, 61.38432, 61.3753, 61.3753)), .Names = c("DateMW.2", 
     "MW.2"), row.names = c(NA, 15L), class = "data.frame")


     calib <- structure(list(Date = structure(c(4L, 5L, 6L, 7L, 8L, 1L, 2L, 
     3L), .Label = c("10/31/2016 12:00:00", "11/30/2016 12:00:00", 
     "12/31/2016 12:00:00", "5/31/2016 12:00:00", "6/30/2016 12:00:00", 
     "7/31/2016 12:00:00", "8/31/2016 12:00:00", "9/30/2016 12:00:00"
     ), class = "factor"), MW2 = c(60.844402, 58.859566, 57.495434, 
     56.328285, 56.842606, 57.76762, 59.583103, 59.958263)), .Names = c("Date",     
     "MW2"), class = "data.frame", row.names = c(NA, -8L))

1 个答案:

答案 0 :(得分:0)

如果您的数据集已经格式化为日期时间,则无需使用zoo。在这里,我只使用approx函数,它给了我我想要的东西。您可以从问题中获取数据集以重现代码。

       ipc <- approx(calib$Date,calib$MW2, xout = sample$`DateMW-2`, 
       rule = 1, method = "linear", ties = mean)

您可以看到数据在给定数据点之间线性插值。

enter image description here

感谢您的深刻见解。