0

我一直在阅读这本书并尝试使用一些相同的代码,这些代码要求您将数据存储为tsibble但是,当我尝试对我自己的数据使用第 3 章中的特定代码时,我无法让它工作。

首先我加载自己的数据,然后将其存储在 tsibble 中并尝试将其转换为 tsibble(我不知道这是否是最佳方式),但似乎在此过程中的某个时刻,我不'不澄清季度日期是正确的。

#The packages i have 
library(seasonal)
library(tsibble)
library(feasts)
library(ggplot2)
library(fpp3)
library(dplyr)
library(lubridate)

#My data x=values and y=dates
x <- c(-4.2,3.1,-1.3,6.3,-6.5,2.6,-0.7,5.1,-6.5,4.2,-2.1,4.6,
       -5.2,1.8,-1.3,6.8,-5.3,3.6,-1.6,5.9,-6.7,6.9,-2.6,5,
       -4.4,6.2,-2.4,4.1,-5.2,2.6,-0.5,5.1,-6.1,3.5,-2.5,1.5,
       -6.6,0.7,-0.7,3.8,-4.6,3.9,0.2,3.5,-4.8,3.6,-2.2,4.4,
       -4.9,2.6,-1,3.4,-5,4.3,-1.2,3.5,-4.6,3,0.3,3.7,-4.2,
       3.9,-1.3,3.5,-4.1,5.9,-1.6,4.1,-4.1,4.8,-2.5,4.4,-4.8,
       5.1,-2.1,4.4,-4.4,4.4,-2,3.9,-5.7,-2.8,3.4,4.9,-5.5,7.3)

y <- c("2000 Q1",   "2000 Q2",  "2000 Q3",  "2000 Q4",  "2001 Q1",  "2001 Q2",  
       "2001 Q3",   "2001 Q4",  "2002 Q1",  "2002 Q2",  "2002 Q3",  "2002 Q4",  "2003 Q1",  
       "2003 Q2",   "2003 Q3",  "2003 Q4",  "2004 Q1",  "2004 Q2",  "2004 Q3",  "2004 Q4",  
       "2005 Q1",   "2005 Q2",  "2005 Q3",  "2005 Q4",  "2006 Q1",  "2006 Q2",  "2006 Q3",  
       "2006 Q4",   "2007 Q1",  "2007 Q2",  "2007 Q3",  "2007 Q4",  "2008 Q1",  "2008 Q2",  
       "2008 Q3",   "2008 Q4",  "2009 Q1",  "2009 Q2",  "2009 Q3",  "2009 Q4",  "2010 Q1",  
       "2010 Q2",   "2010 Q3",  "2010 Q4",  "2011 Q1",  "2011 Q2",  "2011 Q3",  "2011 Q4",  
       "2012 Q1",   "2012 Q2",  "2012 Q3",  "2012 Q4",  "2013 Q1",  "2013 Q2",  "2013 Q3",  
       "2013 Q4",   "2014 Q1",  "2014 Q2",  "2014 Q3",  "2014 Q4",  "2015 Q1",  "2015 Q2",  
       "2015 Q3",   "2015 Q4",  "2016 Q1",  "2016 Q2",  "2016 Q3",  "2016 Q4",  "2017 Q1",
       "2017 Q2",   "2017 Q3",  "2017 Q4",  "2018 Q1",  "2018 Q2",  "2018 Q3",  "2018 Q4",  
       "2019 Q1",   "2019 Q2",  "2019 Q3",  "2019 Q4",  "2020 Q1",  "2020 Q2",  "2020 Q3",  
       "2020 Q4",   "2021 Q1",  "2021 Q2")

#Convert to a tibble (i couldn't get the dates to work, so i created a sequence)
GDP <- tibble(
  GDPNumbers = x,
  Quarter = seq(as.Date("2000-01-01"), as.Date("2021-05-05"), by = "1 quarter")
)

#Convert to a tsibble
GDP_tsbl <- as_tsibble(GDP,
                       key = GDPNumbers,
                       index = Quarter) 

# Then i want to use two codes, which doesn't work for me
x11_dcmp <- GDP_tsbl %>%
  model(x11 = X_13ARIMA_SEATS(x ~ x11())) %>%
  components()

autoplot(x11_dcmp) + 
  labs(tittle =
         "Decomposition using X-11.")

当我到达最后两个代码时,我收到错误消息。显然最后一个给出了错误消息,因为以前没有工作。

Error: Problem with `mutate()` input `cmp`.
x no applicable method for 'components' applied to an object of class "null_mdl"
i Input `cmp` is `map(.fit, components)`.
Run `rlang::last_error()` to see where the error occurred.
In addition: Advarselsbesked:
57 errors (4 unique) encountered for x11
[6] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.
[15] Internal error in `df_slice()`: Columns must match the data frame size.
[11] X-13 run failed

Errors:
- 1. Check input file and format.
- Time series could not be read due to previously found errors
- Specify series before user-defined adjustments
- Need to specify a series to identify outliers

Notes:
- Correct input errors in the order they are detected since the first one or two may be
  responsible for the others (especially if there are errors in the SERIES or COMPOSITE
  spec).
[25] X-13 run failed

Errors:
- Seasonal period must be 4 or 12 if a seasonal adjustment is done.

Notes:
- Correct input errors in the order they are detected since the first one or two may be
  responsible for the others (especially if there are errors in the SERIES or COMPOSITE
  spec).

据我所知,R 认为数据存在差距。我认为这是由于日期的原因,我浏览了互联网,包括 stackoverflow,但我没有找到任何有效的解决方案。在一个站点上,有人建议使用fill_gaps(.full = TRUE),但这最终在我的数据中插入了大约 440000 个元素,这看起来很奇怪,因为我只有 86 个观察值。

任何帮助,将不胜感激。

4

1 回答 1

0

您需要更改代码中的一些内容。

  1. 创建索引时删除键。您想预测 gdp 数字。它们不是关键值。键将被自动选择,或者如果您想指定它,它需要唯一地确定时间索引。

所以对于 1 个你:

GDP_tsbl <- as_tsibble(GDP,
                       index = Quarter)
  1. 正确设置四分之一。您需要使用函数设置季度yearquarter

    x11_dcmp <- GDP_tsbl %>% 
      mutate(Quarter = yearquarter(Quarter)) %>% 
      model(x11 = X_13ARIMA_SEATS(GDPNumbers ~ x11())) %>%
      components()
    

这些更改将确保您的自动绘图按预期工作。

于 2021-11-09T19:08:55.447 回答