【问题标题】:tsibble: Extract two keys from a ts objecttsibble:从 ts 对象中提取两个键
【发布时间】:2021-02-15 13:39:27
【问题描述】:

我正在使用 Seatbelts ts 数据集,并希望将其转换为 tsibble,我可以在其中使用按 law 分组的 DriversKilled,或者包括 law 作为稍后使用的密钥。

使用

head(as_tsibble(Seatbelts))

按预期创建带有日期索引的 tsibble,但只有第一个 ts 变量 (DriversKilled) 作为键。

尝试

head(as_tsibble(Seatbelts, key=c(DriversKilled, law))

给了我同样的东西。如何从安全带 ts 对象中提取两个键?

【问题讨论】:

    标签: tsibble


    【解决方案1】:

    Seatbelts 对象是一个多变量 ts 对象 (mts)。

    class(Seatbelts)
    #> [1] "mts" "ts"
    

    reprex package (v0.3.0) 于 2021-02-17 创建

    key 选项在 as_tsibble(<mts>) 方法中不存在,就像在 as_tsibble(<tibble>) 方法中一样。您可以通过以下方式详细了解如何将as_tsibble() 用于mts 对象:

    ?as_tsibble.mts
    

    将此数据转换为 tsibble 的最合适方法是:

    library(tsibble)
    as_tsibble(Seatbelts, pivot_longer = FALSE)
    #> # A tsibble: 192 x 9 [1M]
    #>       index DriversKilled drivers front  rear   kms PetrolPrice VanKilled   law
    #>       <mth>         <dbl>   <dbl> <dbl> <dbl> <dbl>       <dbl>     <dbl> <dbl>
    #>  1 1969 Jan           107    1687   867   269  9059       0.103        12     0
    #>  2 1969 Feb            97    1508   825   265  7685       0.102         6     0
    #>  3 1969 Mar           102    1507   806   319  9963       0.102        12     0
    #>  4 1969 Apr            87    1385   814   407 10955       0.101         8     0
    #>  5 1969 May           119    1632   991   454 11823       0.101        10     0
    #>  6 1969 Jun           106    1511   945   427 12391       0.101        13     0
    #>  7 1969 Jul           110    1559  1004   522 13460       0.104        11     0
    #>  8 1969 Aug           106    1630  1091   536 14055       0.104         6     0
    #>  9 1969 Sep           107    1579   958   405 12106       0.104        10     0
    #> 10 1969 Oct           134    1653   850   437 11372       0.103        16     0
    #> # … with 182 more rows
    

    reprex package (v0.3.0) 于 2021-02-17 创建


    tsibble 中的“关键”变量应唯一标识时间序列。对于每个组合键,不能有任何时间重复。

    在这个数据集中,有来自单个时间序列的 8 个不同的测量变量(随时间变化的事物)。因此,此数据集不应包含关键变量。

    此数据的关键变量示例可能是“国家/地区”。 Seatbelts 数据集描述了英国的道路伤亡情况,但是如果它包含多个国家/地区,您可以考虑包含每个国家/地区的时间序列的数据。这将保证将“国家”指定为关键变量。更多关于设置 tsibble 的细节可以在这里找到:https://otexts.com/fpp3/tsibbles.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2016-05-25
      • 2019-02-24
      • 1970-01-01
      相关资源
      最近更新 更多