在 R、"seasonal" 和 "fpp3" 包中处理 tsibbles 中的日期
Working with dates in tsibbles in R, "seasonal" and "fpp3" packages
我一直在阅读 this book and been trying to use some of the same code, which require you to store your data as a tsibble. However when I try to use a specific code in chapter 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 认为数据存在差距。我认为这是因为日期问题,我浏览了互联网,包括 Whosebug,但我还没有找到任何有效的解决方案。在一个站点上,有人建议使用 fill_gaps(.full = TRUE)
,但这最终在我的数据中插入了大约 440000 个元素,这看起来很奇怪,因为我只有 86 个观察值。
如有任何帮助,我们将不胜感激。
您需要更改代码中的一些内容。
- 创建索引时删除键。你想预测 gdp 数字。它们不是关键值。 key要么自动选择,要么指定,需要唯一确定时间索引。
那么对于 1 个你:
GDP_tsbl <- as_tsibble(GDP,
index = Quarter)
正确设置季度。您需要使用函数 yearquarter
.
设置季度
x11_dcmp <- GDP_tsbl %>%
mutate(Quarter = yearquarter(Quarter)) %>%
model(x11 = X_13ARIMA_SEATS(GDPNumbers ~ x11())) %>%
components()
这些更改将确保您的自动绘图按预期工作。
我一直在阅读 this book and been trying to use some of the same code, which require you to store your data as a tsibble. However when I try to use a specific code in chapter 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 认为数据存在差距。我认为这是因为日期问题,我浏览了互联网,包括 Whosebug,但我还没有找到任何有效的解决方案。在一个站点上,有人建议使用 fill_gaps(.full = TRUE)
,但这最终在我的数据中插入了大约 440000 个元素,这看起来很奇怪,因为我只有 86 个观察值。
如有任何帮助,我们将不胜感激。
您需要更改代码中的一些内容。
- 创建索引时删除键。你想预测 gdp 数字。它们不是关键值。 key要么自动选择,要么指定,需要唯一确定时间索引。
那么对于 1 个你:
GDP_tsbl <- as_tsibble(GDP,
index = Quarter)
正确设置季度。您需要使用函数
设置季度yearquarter
.x11_dcmp <- GDP_tsbl %>% mutate(Quarter = yearquarter(Quarter)) %>% model(x11 = X_13ARIMA_SEATS(GDPNumbers ~ x11())) %>% components()
这些更改将确保您的自动绘图按预期工作。