【问题标题】:Subsetting only the workdays from a time-series仅对时间序列中的工作日进行子集化
【发布时间】:2020-02-03 21:23:20
【问题描述】:

我只想在forecast 包中对时间序列taylor 中的工作日进行子集化。

help(taylor)
Half-hourly electricity demand in England and Wales from Monday 5 June 2000 to 
Sunday 27 August 2000. Discussed in Taylor (2003), and kindly provided by James
W Taylor. Units: Megawatts

但时间序列的时间不是日期,它们是从 1 开始的数字,表示从序列开始的天数:

time(head(taylor))  
Time Series:
Start = c(1, 1) 
End = c(1, 6) 
Frequency = 336 
[1] 1.000000 1.002976 1.005952 1.008929 1.011905 1.014881

如何将这些转换为日期,仅提取工作日样本,并创建频率为 5*24*2(而不是原始频率为 7*24*2)的新时间序列?

【问题讨论】:

    标签: r date time-series subset forecasting


    【解决方案1】:

    我们可以使用taylor 的描述中给出的开始和结束日期创建一个“半小时” 日期序列。

    data("taylor", package="forecast")
    
    dates <- seq(as.POSIXct("2000-06-05"), as.POSIXct("2000-08-28"), "30 min")
    dates <- dates[-length(dates)]  # exclude "2000-08-28 00:00:00"
    

    现在,使用substr(),我们可以排除以"S" 开头的weekdays()(可能不适用于其他语言),并创建一个具有特定开始、结束、频率值的新“ts”对象。

    taylor2 <- ts(taylor[!substr(weekdays(dates), 1, 1) == "S"], start=1, end=12, frequency=240)
    

    不过,最好使用forecast 库创建一个"msts" 对象以保持相同的季节性。

    library(forecast)
    taylor3 <- msts(taylor[!substr(weekdays(dates), 1, 1) == "S"], seasonal.periods=c(24*2, 24*2*5))
    

    检查

    op <- par(mfrow=c(3, 1))
    plot(taylor)
    plot(taylor2)
    plot(taylor3)
    par(op)
    

    【讨论】:

    • 为什么我们需要使用循环?在没有[cycle(taylor)] 部分的情况下使用您的确切答案有什么问题?
    • @ihadanny 好点。这些值与ts 对象不匹配,我首先认为它们需要排序。实际上,从逻辑上讲,我们需要将序列延长一天并排除最后一个午夜,因此它给出了准确的 length(taylor) 值。查看更新!
    • 谢谢,还有一件事 - end 是什么,为什么将其设置为 12?我认为正确的命令是taylor2 &lt;- msts(taylor[!substr(weekdays(dates), 1, 1) == "S"], seasonal.periods = c(24*2, 24*2*5))
    • @ihadanny end == 12 指的是周数(如之前设置的那样),请查看 length(taylor)/(2*24*7)table(cycle(taylor))
    • 啊!我知道了。我检查了,它也适用于msts 调用,我将编辑你的答案,因为没有这个新的taylor2 系列失去了原始taylor 系列的多季节性。
    【解决方案2】:

    您可以考虑将时间序列转换为xts-对象,以便更轻松地进行数据操作。例如,我们可以使用.indexwkdayxts-object 中提取工作日:

    library(xts)
    
    ## load data
    data(taylor, package = "forecast")
    
    ## convert to xts
    taylor_xts <- xts(
        x = taylor,
        order.by = seq(from = as.POSIXct("2000-06-05"), length = length(taylor), by = "30 min")
    )
    
    ## extract weekdays
    taylor_wk <- taylor_xts[.indexwday(taylor_xts) %in% 1:5]
    
    head(taylor_wk); tail(taylor_wk)
    #>                      [,1]
    #> 2000-06-05 00:00:00 22262
    #> 2000-06-05 00:30:00 21756
    #> 2000-06-05 01:00:00 22247
    #> 2000-06-05 01:30:00 22759
    #> 2000-06-05 02:00:00 22549
    #> 2000-06-05 02:30:00 22313
    #>                      [,1]
    #> 2000-08-25 21:00:00 33064
    #> 2000-08-25 21:30:00 31953
    #> 2000-08-25 22:00:00 30548
    #> 2000-08-25 22:30:00 29236
    #> 2000-08-25 23:00:00 27623
    #> 2000-08-25 23:30:00 26063
    

    或者,如果我们希望仅提取办公时间(工作日上午 9 点至下午 6 点)的数据:

    ## extract office hours
    taylor_offh <- taylor_xts[.indexwday(taylor_xts) %in% 1:5 & .indexhour(taylor_xts) >= 9 & .indexhour(taylor_xts) < 18]
    
    head(taylor_offh); tail(taylor_offh)
    #>                      [,1]
    #> 2000-06-05 09:00:00 36834
    #> 2000-06-05 09:30:00 37296
    #> 2000-06-05 10:00:00 37338
    #> 2000-06-05 10:30:00 37608
    #> 2000-06-05 11:00:00 37692
    #> 2000-06-05 11:30:00 37944
    #>                      [,1]
    #> 2000-08-25 15:00:00 35067
    #> 2000-08-25 15:30:00 34928
    #> 2000-08-25 16:00:00 34738
    #> 2000-08-25 16:30:00 35004
    #> 2000-08-25 17:00:00 34748
    #> 2000-08-25 17:30:00 34090
    

    注意:使用plot.xts 绘制子采样时间序列会在 x 轴上显示日期时间,因此包括周末的间隙,(因为不再定期对时间序列进行采样)。要将数据绘制为串联系列,请使用plot.default(或转换回ts 对象后的plot.ts)。

    ## plot time-series along time
    plot(taylor_wk)
    

    ## plot time-series along index 
    plot.default(taylor_wk, type = "l")    ## equivalently `plot(coredata(taylor_wk), type = "l")`
    

    【讨论】:

    • 看起来非常优雅,但不是我所期望的...请绘制taylor_wk 并亲自查看:这不是没有周末的串联系列,它是相同的时间线,带有 2-每个周末的天间隔
    • @ihadanny: plot(taylor_wk) 调用 plot.xts,它使用时间序列的时间戳作为 x 轴。如果您想将数据绘制为串联系列,请尝试使用plot.ts(taylor_wk)plot(ts(taylor_wk, start = 1, frequency = 240))(也称为plot.ts)以获得更合理的x 轴。
    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多