如何将多变量时间序列对象转换为数据框? [英] How to convert a multi variate time series object to a data frame?

查看:39
本文介绍了如何将多变量时间序列对象转换为数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这个问题中解释的反向操作:将带有日期列的数据框转换为时间序列.

I would like to do the reverse operation explained in this question: convert data frame with date column to time series.

例如co2数据的分解是一个多元时间序列:

For example the decomposition of the co2 data is a multi variate time series:

m <- decompose(co2)
> str(m)
List of 6
 $ x       : Time-Series [1:468] from 1959 to 1998: 315 316 316 318 318 ...
 $ seasonal: Time-Series [1:468] from 1959 to 1998: -0.0536 0.6106 1.3756 2.5168 3.0003 ...
 $ trend   : Time-Series [1:468] from 1959 to 1998: NA NA NA NA NA ...
 $ random  : Time-Series [1:468] from 1959 to 1998: NA NA NA NA NA ...
 $ figure  : num [1:12] -0.0536 0.6106 1.3756 2.5168 3.0003 ...
 $ type    : chr "additive"
 - attr(*, "class")= chr "decomposed.ts"

如何将这个多变量时间序列对象转换为带有日期列的数据框?

How can I convert this multi variate time series object to a data frame with a date column?

如果可能,无需安装新软件包.

If possible without installing a new package.

推荐答案

您可以尝试以下操作:

library(xts)

m <- decompose(co2)
str(m)
#> List of 6
#>  $ x       : Time-Series [1:468] from 1959 to 1998: 315 316 316 318 318 ...
#>  $ seasonal: Time-Series [1:468] from 1959 to 1998: -0.0536 0.6106 1.3756 2.5168 3.0003 ...
#>  $ trend   : Time-Series [1:468] from 1959 to 1998: NA NA NA NA NA ...
#>  $ random  : Time-Series [1:468] from 1959 to 1998: NA NA NA NA NA ...
#>  $ figure  : num [1:12] -0.0536 0.6106 1.3756 2.5168 3.0003 ...
#>  $ type    : chr "additive"
#>  - attr(*, "class")= chr "decomposed.ts"

df <- as.data.frame(m[c("x", "seasonal", "trend", "random")])
str(df)
#> 'data.frame':    468 obs. of  4 variables:
#>  $ x       : Time-Series  from 1959 to 1998: 315 316 316 318 318 ...
#>  $ seasonal: Time-Series  from 1959 to 1998: -0.0536 0.6106 1.3756 2.5168 3.0003 ...
#>  $ trend   : Time-Series  from 1959 to 1998: NA NA NA NA NA ...
#>  $ random  : Time-Series  from 1959 to 1998: NA NA NA NA NA ...

df2 <- data.frame(date = index(m$x), 
                  apply(df, 2, as.numeric))
str(df2)
#> 'data.frame':    468 obs. of  5 variables:
#>  $ date    : num  1959 1959 1959 1959 1959 ...
#>  $ x       : num  315 316 316 318 318 ...
#>  $ seasonal: num  -0.0536 0.6106 1.3756 2.5168 3.0003 ...
#>  $ trend   : num  NA NA NA NA NA ...
#>  $ random  : num  NA NA NA NA NA ...

reprex 包 (v0.3.0) 于 2020 年 3 月 13 日创建

Created on 2020-03-13 by the reprex package (v0.3.0)

你也可以试试tsibblefeasts

library(xts)
library(tsibble)
library(feasts)

m <- decompose(co2)

as_tsibble(co2) %>% 
  model(decomp = classical_decomposition(value, type = "additive")) %>%
  components() 
#> # A dable:                 468 x 7 [1M]
#> # Key:                     .model [1]
#> # Classical Decomposition: value = trend + seasonal + random
#>    .model    index value trend seasonal  random season_adjust
#>    <chr>     <mth> <dbl> <dbl>    <dbl>   <dbl>         <dbl>
#>  1 decomp 1959 Jan  315.   NA   -0.0536 NA               315.
#>  2 decomp 1959 Feb  316.   NA    0.611  NA               316.
#>  3 decomp 1959 Mär  316.   NA    1.38   NA               315.
#>  4 decomp 1959 Apr  318.   NA    2.52   NA               315.
#>  5 decomp 1959 Mai  318.   NA    3.00   NA               315.
#>  6 decomp 1959 Jun  318    NA    2.33   NA               316.
#>  7 decomp 1959 Jul  316.  316.   0.813  -0.284           316.
#>  8 decomp 1959 Aug  315.  316.  -1.25   -0.0170          316.
#>  9 decomp 1959 Sep  314.  316.  -3.05    0.758           317.
#> 10 decomp 1959 Okt  313.  316.  -3.25    0.362           316.
#> # … with 458 more rows

reprex 包 (v0.3.0) 于 2020 年 3 月 13 日创建

Created on 2020-03-13 by the reprex package (v0.3.0)

这篇关于如何将多变量时间序列对象转换为数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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