How to convert a multi variate time series object to a data frame?

烈酒焚心 提交于 2021-02-10 22:41:33

问题


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

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.


回答1:


You can try the following:

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 ...

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

You can also try tsibble and feasts

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

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



来源:https://stackoverflow.com/questions/60670097/how-to-convert-a-multi-variate-time-series-object-to-a-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!