-
Notifications
You must be signed in to change notification settings - Fork 30
/
自回归均移模型最优值.R
46 lines (43 loc) · 2.44 KB
/
自回归均移模型最优值.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
自回归均移模型最优值 <- function(样本, 季节差分的次数 = NULL, 季节性 = '勾', 规律极限值 = 10, 自回归均移模型值 = FALSE, 外因 = NULL) {
# 通过设置并迭代筹算自回归均移模型中不同`p,d,q`的规律值,来比较并筛选出最低或最大负数的赤池信息量准则,也就是最优统计模型。
#
# 《预测:方法与实践(第三版)》第九章第九节 - 季节性ARIMA模型
# https://otexts.com/fpp3cn/seasonal-arima-cn.html
if (!季节性 %in% c('勾', '有', '叉', '冇')) stop('请选择季节性:"勾"或"有"或"是",或者"叉"或"冇"或"否"。')
if (季节性 %in% c('勾', '有')) 季节性 <- TRUE
if (季节性 %in% c('叉', '冇')) 季节性 <- FALSE
# 季节差分的次数,一般上使用到的数值是零到二。
半成品 <- auto.arima(样本, D = 季节差分的次数, seasonal = 季节性,
max.order = 规律极限值, xreg = 外因)
if (自回归均移模型值 == FALSE) {
成果 <- arimaorder(半成品)
} else {
#https://stats.stackexchange.com/questions/178577/how-to-read-p-d-and-q-of-auto-arima
成果 <- 半成品$arma
#https://stackoverflow.com/questions/23617662/extract-arima-specificaiton
names(成果) <- c('p', 'q', 'P', 'Q', 's', 'd', 'D')
成果 %<>% .[c(1, 6, 2, 3, 7, 4, 5)]
#(p,d,q) and (P,D,Q) and seasonal period
} #范例:`s` seasonal period = 12 表示十二个月
return(成果)
#https://onlinecourses.science.psu.edu/stat510/node/67/
# https://stackoverflow.com/questions/23617662/extract-arima-specificaiton
#
#function (object) {
# order <- object$arma[c(1, 6, 2, 3, 7, 4, 5)]
# result <- paste("ARIMA(", order[1], ",", order[2], ",", order[3],
# ")", sep = "")
# if (order[7] > 1 & sum(order[4:6]) > 0)
# result <- paste(result, "(", order[4], ",", order[5],
# ",", order[6], ")[", order[7], "]", sep = "")
# if (is.element("constant", names(object$coef)) | is.element("intercept",
# names(object$coef)))
# result <- paste(result, "with non-zero mean")
# else if (is.element("drift", names(object$coef)))
# result <- paste(result, "with drift ")
# else if (order[2] == 0 & order[5] == 0)
# result <- paste(result, "with zero mean ")
# else result <- paste(result, " ")
# return(result)
#}
}