R中的季节性调整包(人口普查局)仅调整一半数据

时间:2019-11-12 10:24:56

标签: r date time-series arima

我已经设法使library(seasonal)程序包在我的一半数据(第2列和第8列)上完美运行,但它拒绝季节性调整另一半。我真的很困惑,因为代码可以正常运行并且没有错误显示,所以我想知道这是否是我的数据的功能(我是时间序列的新手)还是我的代码中存在隐藏的错误? >

head(x)
        Date European Maori Pacific Peoples Asian MELAA Other Ethnicity Total
1 2004-09-30      7.9  17.9            14.8  15.4  13.4            14.1   9.7
2 2004-12-31      7.9  18.6            13.3  20.9  14.3            14.9   9.9
3 2005-03-31      7.8  17.8            14.6  21.1  12.6            16.1   9.8
4 2005-06-30      7.6  18.1            11.9  20.4  12.6            13.7   9.4
5 2005-09-30      7.1  17.4            10.5  16.1   9.4            19.2   9.0
6 2005-12-31      7.6  15.9             9.8  16.6  11.1            12.0   9.3

代码如下:

*请注意,季节调整在sub1和sub2中运行平稳。 sub3 sub4

中没有发生季节性调整
library(tidyverse)
library(seasonal)

x <- read.csv("HLF524501_20191112_083700_50.csv", header = TRUE, check.names = FALSE, stringsAsFactors = FALSE)
x <- x %>% 
        mutate(Date = ifelse(substring(Date, 5, nchar(x)) == "Q3", paste(substring(Date, 0, 4), "09-30", sep = "-"),
                             ifelse(substring(Date, 5, nchar(x)) == "Q4", paste(substring(Date, 0, 4), "12-31", sep = "-"),
                                    ifelse(substring(Date, 5, nchar(x)) == "Q1", paste(substring(Date, 0, 4), "03-31", sep = "-"),
                                           paste(substring(Date, 0, 4), "06-30", sep = "-")))))
x$Date <- as.Date(x$Date)

sub1 <- x[,c(1,8)]
season0<-ts(sub1[,-1],frequency=4,start=c(2004,3))
sea1 <- seas(season0)
plot(sea1)

sub2 <- x[,c(1,2)]
season0<-ts(sub2[,-1],frequency=4,start=c(2004,3))
sea2 <- seas(season0)
plot(sea2)

sub3 <- x[,c(1,3)]
season0<-ts(sub3[,-1],frequency=4,start=c(2004,3))
sea3 <- seas(season0)
plot(sea3)

sub4 <- x[,c(1,4)]
season0<-ts(sub4[,-1],frequency=4,start=c(2004,3))
sea4 <- seas(season0)
plot(sea4)

数据如下:

dput(x)
structure(list(Date = c("2004-09-30", "2004-12-31", "2005-03-31", 
"2005-06-30", "2005-09-30", "2005-12-31", "2006-03-31", "2006-06-30", 
"2006-09-30", "2006-12-31", "2007-03-31", "2007-06-30", "2007-09-30", 
"2007-12-31", "2008-03-31", "2008-06-30", "2008-09-30", "2008-12-31", 
"2009-03-31", "2009-06-30", "2009-09-30", "2009-12-31", "2010-03-31", 
"2010-06-30", "2010-09-30", "2010-12-31", "2011-03-31", "2011-06-30", 
"2011-09-30", "2011-12-31", "2012-03-31", "2012-06-30", "2012-09-30", 
"2012-12-31", "2013-03-31", "2013-06-30", "2013-09-30", "2013-12-31", 
"2014-03-31", "2014-06-30", "2014-09-30", "2014-12-31", "2015-03-31", 
"2015-06-30", "2015-09-30", "2015-12-31", "2016-03-31", "2016-06-30", 
"2016-09-30", "2016-12-31", "2017-03-31", "2017-06-30", "2017-09-30", 
"2017-12-31", "2018-03-31", "2018-06-30", "2018-09-30", "2018-12-31", 
"2019-03-31", "2019-06-30", "2019-09-30"), European = c(7.9, 
7.9, 7.8, 7.6, 7.1, 7.6, 7.7, 6.7, 7.2, 8.5, 8.1, 7.8, 7.2, 7.7, 
9, 8.1, 8.9, 9.6, 10.8, 11.5, 12.1, 12.5, 11.4, 11.3, 10.9, 11.4, 
11.8, 11.7, 10.9, 11, 11.6, 11.8, 12.6, 12.4, 11.1, 10.7, 11.2, 
12.2, 11.2, 10.8, 10.3, 11.7, 11.3, 10.9, 11, 10.7, 11.4, 10.9, 
10.4, 11.1, 10.6, 10.1, 10, 11.1, 11, 10.4, 9.5, 11.4, 10.2, 
9.5, 8.7), Maori = c(17.9, 18.6, 17.8, 18.1, 17.4, 15.9, 17.3, 
16.1, 15.6, 18.1, 19.3, 17, 16.8, 15.9, 18.9, 16.2, 17.5, 18.9, 
21.7, 22.4, 24.9, 26.3, 25, 25.5, 25.6, 25.1, 25.8, 25.5, 25.1, 
24.5, 25, 25, 28.1, 28.4, 24, 24.7, 24.8, 25.7, 24.5, 22.9, 23.1, 
25.1, 24.4, 24, 25.1, 23.5, 25.2, 22.8, 22.9, 23.1, 22.3, 21.4, 
20.6, 21.7, 20.9, 20.3, 19, 20.9, 19.3, 17.6, 17.6), `Pacific Peoples` = c(14.8, 
13.3, 14.6, 11.9, 10.5, 9.8, 15, 12.3, 12.3, 12.6, 11.6, 15, 
9.5, 12.8, 17.6, 13.2, 15.6, 16.1, 21.3, 23.4, 25.1, 25.8, 24.4, 
25.8, 26.1, 24.8, 24.2, 25.1, 25.2, 24.2, 27.6, 27.8, 29.6, 29.2, 
27.7, 27.7, 27.2, 26.2, 24.8, 21.6, 22.4, 22.9, 23.7, 20.8, 23.9, 
21.7, 21.8, 18.8, 18.7, 19.8, 19.9, 18.9, 18.4, 18.5, 16.9, 17.4, 
14.8, 20.4, 18, 15.5, 14.1), Asian = c(15.4, 20.9, 21.1, 20.4, 
16.1, 16.6, 11.1, 12.5, 8.9, 19.6, 17.3, 14.6, 15.5, 13.3, 13.9, 
14.6, 13.4, 15.2, 16, 17, 20.6, 19.4, 18.2, 18.7, 17.9, 17.8, 
18.2, 15.2, 15.3, 19, 18.4, 18.3, 18.5, 17.8, 14, 14.7, 14.4, 
14.5, 14.9, 14.9, 13, 13.7, 15.6, 14.9, 13.7, 14.5, 17.5, 14.6, 
13.4, 12.5, 13.1, 11.3, 11.1, 13.1, 11.9, 11.1, 11.1, 12.5, 11.1, 
10.5, 9.5), MELAA = c(13.4, 14.3, 12.6, 12.6, 9.4, 11.1, 18.6, 
7, 13.7, 15.4, 19.7, 20.5, 12.1, 15.9, 25.8, 21.7, 25.6, 26.4, 
26.5, 26.9, 19.1, 28.5, 23, 21.4, 24.2, 14.5, 19.8, 25, 27.1, 
14.2, 23.7, 22.4, 23.8, 21.8, 18.3, 14.5, 21.7, 20.7, 25.5, 21.4, 
20.9, 26.2, 21.9, 25.9, 19.9, 18.7, 21.7, 19, 15.8, 23.9, 17.4, 
18.3, 17, 20.3, 17.5, 17.9, 10.9, 16.1, 14.9, 22.6, 13.5), `Other Ethnicity` = c(14.1, 
14.9, 16.1, 13.7, 19.2, 12, 9.8, 13.4, 10.1, 16.6, 10, 16.1, 
18.5, 9.2, 9.2, 13, 8.2, 9.8, 6.7, 8.6, 10.2, 9.1, 12.6, 10.6, 
8.8, 10.6, 11.1, 12.5, 12, 13.7, 15, 16.8, 12.2, 12.7, 7.9, 10.8, 
12.5, 13.2, 12.1, 12.4, 8.5, 9.5, 13.1, 12.6, 10.2, 7.8, 9.4, 
11.8, 11.9, 14.2, 13, 13.4, 12.3, 12.6, 13.4, 11.6, 13.7, 11.4, 
11.2, 13.6, 9.3), Total = c(9.7, 9.9, 9.8, 9.4, 9, 9.3, 9.4, 
8.5, 8.6, 10.3, 10.3, 9.7, 8.8, 9.2, 10.8, 9.8, 10.3, 11.2, 12.6, 
13.4, 14.6, 14.9, 13.9, 13.8, 13.7, 13.9, 14.3, 13.8, 13.4, 13.6, 
14.3, 14.4, 15.3, 14.9, 13.2, 13, 13.5, 14.4, 13.4, 12.9, 12.3, 
13.7, 13.8, 13.2, 13.2, 12.8, 13.8, 12.7, 12.2, 12.8, 12.5, 11.6, 
11.7, 12.6, 12.1, 11.7, 10.9, 12.8, 11.5, 10.8, 9.9)), class = "data.frame", row.names = c(NA, 
-61L))

非常感谢您的帮助:) 谢谢!

1 个答案:

答案 0 :(得分:2)

如果seas函数未在数据中检测到季节性,则不包括季节性。这基于QS统计信息,在此进行详细讨论:https://stats.stackexchange.com/questions/148573/the-results-and-specifics-from-the-qs-function-in-r

您可以将summary(sea1)qs(sea1)summary(sea3)qs(sea3)进行比较,以查看ARIMA建模详细信息以及数据季节性检验。更广泛地说,建议您在所有4个模型上使用summary,以查看数据各部分的建模详细信息。