How to transform an as_tibble() format time series data into an as_tsibble() format in R?

591 Views Asked by At

so far the result I get is as below, which is a 'tibble' format.

However, as this is a group of time series, I wish to transform this result x into a 'tsibble' format afterwords.

Anyone know what further step I need to do?

enter image description here

I tried using

x <- function_name(n.ts = 3, freq = 12, nComp = 2, n = 120,output_format = "list")
as_tsibble(x)

And the error said enter image description here

Another trying & error enter image description here The content should display something similar to this enter image description here

1

There are 1 best solutions below

17
On BEST ANSWER

The 'x' is a list with multiple components. We can loop over the list with map, extract the 'x' component and convert it to tsibble

library(dplyr)
library(purrr)
x1 <- map(x,  ~ as_tsibble(.x$x))
x1
#$N1
# A tsibble: 120 x 2 [1M]
#      index value
#      <mth> <dbl>
# 1 0001 Jan  186.
# 2 0001 Feb  184.
# 3 0001 Mar  189.
# 4 0001 Apr  188.
# 5 0001 May  193.
# 6 0001 Jun  189.
# 7 0001 Jul  192.
# 8 0001 Aug  194.
# 9 0001 Sep  194.
#10 0001 Oct  196.
# … with 110 more rows

#$N2
# A tsibble: 120 x 2 [1M]
#      index value
#      <mth> <dbl>
# 1 0001 Jan  18.4
# 2 0001 Feb  16.0
# 3 0001 Mar  16.9
# ...

Here, the index part is created by the column name ('Month') and as there was no information regarding the 'year', it started with '0001'

data

library(gratis)
x <- generate_ts(n.ts = 3, freq = 12, nComp = 2, n = 120)