【问题标题】:Interpolate data for irregular time series为不规则时间序列插入数据
【发布时间】:2019-03-18 15:43:24
【问题描述】:

我尝试在此处插入这个meterValue,完整的csv:https://drive.google.com/open?id=18cwtw-chAB-FqqCesXZJ-6NB6eHFJlgQ

localminute,dataid,meter_value
2015-10-03 09:51:53,6578,157806
2015-10-13 13:41:49,6578,158086
:
:
2016-01-17 16:00:33,6578,164544  #end of meter_value data for ID=6578

基于@G. Grothendieck,建议,我在 z.interpolate(合并数据)时遇到错误

D6578z <- read.csv.zoo("test_6578.csv")[,2]
D6578zd <- to.daily(D6578z)[,4]
#Warning messages:
                #1: In zoo(xx, order.by = index(x), ...) : some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
                #2: In zoo(rval, index(x)[i]) :some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique

     test_6578t <- time(D6578zd)

     plot(D6578zd,type="p",xaxt="n", pch=19, col="blue",cex=1.5)

     diff(test_6578t) 

     t.daily6578 <- seq(from =min(test_6578t),to=max(test_6578t),by="1 day")

     dummy6578 <- zoo(,t.daily6578) 

     z.interpolated <- merge(D6578zd,dummy6578,all=TRUE)
        *#Error in merge.zoo(D6578zd, dummy6578, all = TRUE) :  series cannot be merged with non-unique index entries in a series*

@G提供的插值数据中R代码一小时时差的解决方法。格洛腾迪克,如下。

嗨@G。 Grothendieck,感谢您提供解决方案代码。关于您的代码,我有一些问题要向您澄清。

  `line1: to.hour <- function(x) as.POSIXct(trunc(as.POSIXct(x, origin = "1970-01-01"), "hour"))

    line2: z <- read.csv.zoo("test_6578.csv", FUN = to.hour, aggregate = function(x) tail(x, 1))`

         `line3: zz <- na.approx(as.zoo(as.ts(z)))`

        `line4: time(zz) <- as.POSIXct(time(zz), origin = "1970-01-01")`

在第 1 行,为什么在 `trunc(as.POSIXct(x,origin =”1970-01-01”)?
我知道“trunc”函数会四舍五入日期时间值。

在第 2 行中,这段代码是什么意思“FUN=to.hour, aggregate =函数(x)尾(x,1)”工作?

因为我不明白什么是tail(x,1)。我提取了z csv文件中的函数,我观察到只有dataid和meter_value 使用“read.csv.zoo”函数时会生成列。

在第 3 行,我了解到,zz 函数提供了插值数据 但我没有完全理解代码“na.approx(as.zoo(as.ts(z)))”, 因为z已经是read.csv.zoo之后的动物园系列,为什么我们还有 在“na.approx”行中使用“as.zoo”和“as.ts”?

zoo 和 zooreg 系列有什么区别?

第4行,“time(zz)”是“zz”函数的索引?

提前感谢您的解释。

我可以绘制时差 = 1 小时的插值数据。

【问题讨论】:

    标签: r time-series forecasting


    【解决方案1】:

    在使用read.csv.zoo 转换为Date 类中读取文件,该类聚合重复日期,以便使用最后一个日期。然后转换为ts 并返回动物园,这将用 NA 填补空白的日子。现在使用na.approx 填写NA 值。由于ts 不能代表Date 类,因此生成的系列将包含代表日期的数字,因此请将它们转换回来。

    library(zoo)
    z <- read.csv.zoo("test_6578.csv", FUN = as.Date, aggregate = function(x) tail(x, 1))
    zz <- na.approx(as.zoo(as.ts(z)))
    time(zz) <- as.Date(time(zz))
    

    在 cmets 中,有人声称输出中存在漏洞,但事实并非如此。连续时间之差为 1 且不存在 NA。

    table(diff(time(zz)))
    ##   1 
    ## 106 
    
    any(is.na(zz)) 
    ## [1] FALSE
    
    any(is.na(time(zz)))
    ## [1] FALSE
    

    这里是一个一小时而不是一天的例子。

    to.hour <- function(x) as.POSIXct(trunc(as.POSIXct(x, origin = "1970-01-01"), "hour"))
    z <- read.csv.zoo("test_6578.csv", FUN = to.hour, aggregate = function(x) tail(x, 1))
    zz <- na.approx(as.zoo(as.ts(z)))
    time(zz) <- as.POSIXct(time(zz), origin = "1970-01-01")
    
    plot(zz[, 2], type = "p", pch = ".")
    

    【讨论】:

    • 感谢您的回复。别介意,我不明白这两行是如何工作的。请纠正我,如果我错了。 zd &lt;- to.daily(z)[, 4] z <- read.csv.zoo("test_2034.csv")[, 2] # extract only column name =mtervalue?
    • 我尝试了另一个 dataID csv 文件(drive.google.com/open?id=18cwtw-chAB-FqqCesXZJ-6NB6eHFJlgQRead.csv.zoo 没有错误,但我在转换常规系列时出错。当我运行此代码时:D6578zd &lt;- to.daily(D6578z)[,4] 警告消息:1:在 zoo(xx, order.by = index(x), ...) 中:如果 'order.by' 中的索引条目不是唯一的,则“zoo”对象的某些方法不起作用 2:在 zoo(rval, index (x)[i]) : 如果 'order.by' 中的索引条目不是唯一的,则“zoo”对象的某些方法不起作用
    • 我插入另一个数据集的代码流在我上面的问题中。我在合并时遇到错误
    • @G. Grothendieck,如果你不介意的话,我可以知道,这段代码在聚合函数中是什么意思 function(x) tail(x, 1))[, 2]。 z function(x) tail(x, 1))[, 2] 在 R 控制台中给出错误
    • 如您在此链接中看到的图:drive.google.com/open?id=1lVVtCphuGhnO7kzHSl76u8Q0u03U_Qxf,似乎 z.interpolated 数据点未完全填充现有数据(图中的红点)。 dd hh:mm:ss 有什么可能吗?这样所有的数据都可以插值。 diff(time_zooNA) 的时差是 15 分钟间隔还是 30 分钟?
    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2014-09-02
    • 2020-07-19
    • 1970-01-01
    • 2021-09-10
    • 2020-06-06
    • 2020-09-21
    相关资源
    最近更新 更多