在 R 中拟合 ARIMA 时的置信带?

机器算法验证 r 时间序列 自相关 阿玛
2022-04-18 00:14:47

我想查看我的数据的 acf 和 pacf,以确定我的平均方程的模型,所以我想为我的平均方程拟合一个 ARMA,然后通过 ARCH/GARCH 对条件方差进行建模(我知道我有做联合模型估计)。在第一步中,我想查看 ACF 和 PACF 进行识别,我使用了预测库的标准 Acf,但我注意到,这些图中的置信带是为了测试随机性而不是为了拟合 ARMA 而给出的。

正如维基百科所说:

相关图也用于模型识别阶段以拟合 ARIMA 模型。在这种情况下,假设数据采用移动平均模型,应生成以下置信区间:

±z1α/21N(1+2i=1kyi2) 我怎样才能得到这些置信区间随着滞后的增加而增加?

2个回答

使用acfstats包(或Acfforecast包)与ci.type="ma". 请注意,有些人一直使用更简单的近似值 - 这只是为了让您了解哪些模型可能值得考虑,因此准确性并不那么重要。

Bartlett 的近似值(您从 Wikipedia 引用的那个)仅与检查自相关函数有关:滞后的置信区间是假设作为零假设的,即阶的移动平均过程;它取决于所有先前滞后的估计自相关。(因此请注意,它与在 ARMA(1,1) 和 ARMA (1,2) 之间做出决定并不特别相关。)qq1

您可能会假设偏自相关的置信区间有一个类似的公式,已做必要的修改;但你错了:如果你假设一个自回归过程,偏自相关的标准误差是渐近的,其中是观察的数量。Quenouille (1949),“时间序列中相关性的近似检验”,JRSS B11,1p11nn

我在下面包含了一个示例,以展示如何计算 Bartlett 近似值并将它们添加到自相关函数图中的一种方法。

在示例中,我是手动完成的,而不是依赖特定的包,因此代码比可能需要的要长。我不声称代码是有效的,但它确实得到了正确的结果。

我的示例中的结果可以与图 C1.2 的准确性进行比较。在Pankratz (1983)的案例 1 中,它使用了我使用过的相同数据集。如果您没有这本书,请不要担心,因为案例 1 的内容可以免费下载。

请注意,稍作修改,下面的代码可用于绘制 pacf 上的标准误差,Scortchi 已正确指出应该等于n1/2

# Import data from the web
inventories <- scan("http://robjhyndman.com/tsdldata/books/pankratz.dat", skip=5, nlines=5, sep="")
# Calculate sample size and mean
n <- length(inventories)
mean.inventories <- sum(inventories)/n
# Express the data in deviations from the mean
z.bar <- rep(mean.inventories,n)
deviations <- inventories - z.bar
# Calculate the sum of squared deviations from the mean
squaredDeviations <- deviations^2
sumOfSquaredDeviations <-sum(squaredDeviations)
# Create empty vector to store autocorrelation coefficients
r <- c()
# Use a for loop to fill the vector with the coefficients
for (k in 1:n) {
  ends <- n - k
  starts <- 1 + k
  r[k] <- sum(deviations[1:(ends)]*deviations[(starts):(n)])/sumOfSquaredDeviations
}
# Create empty vector to store Bartlett's standard errors
bart.error <- c()
# Use a for loop to fill the vector with the standard errors
for (k in 1:n) {
  ends <- k-1
  bart.error[k] <- ((1 + sum((2*r[0:(ends)]^2)))^0.5)*(n^-0.5)
}
# Plot the autocorrelation function
plot(r[1:(n/4)], 
     type="h", 
     main="Autocorrelation Function", 
     xlab="Lag", 
     ylab="ACF",
     ylim=c(-1,1),
     las=1)
abline(h=0)
# Add Bartlett's standard errors to the plot
lines(2*bart.error[1:(n/4)])
lines(2*-bart.error[1:(n/4)])

运行代码后,您应该看到以下图表:

带有 Bartlett 错误的 ACF