L中的Ljung-Box测试 - 出乎意料的结果

时间:2016-04-03 14:20:08

标签: r statistics correlation

我正在研究模拟时间序列z_t的属性。简而言之,z_t生成如下:y_t是具有创新的AR(1)过程e_t; z_t是y_t * e_t。我希望z_t能显示滞后1相关性。

我在R中对z_t进行了Ljung-Box测试,发现我的结果很大程度上依赖于种子值(对于某些种子,我得到p值<0.001,对于其他种子我得到的p值接近1;我测试了10粒种子)。我还尝试根据LB测试的可能定义明确计算p值,发现它总是很小。在Python中完成的相同分析提供了低p值。我希望我可能错误地使用了Box.test函数。

以下是一些重现问题的代码;最后3行是我亲自动手的LB测试:

n <- 1000; phi = 0.9; set.seed(9)
errors <- rnorm(n); Y <- rep(0, times <- n)

for (k in 2:1000){
    Y[k] <- phi * Y[k-1] + errors[k]
}
y <- ts(Y)

z <- y * errors
Box.test(z, lag=1, type="Ljung-Box", fitdf = 0) 
# note: result doesn't depend very much on number of lags
# sometimes result > 0.1.

r1 <- sum(z*lag(z,-1)) / sum(z**2) #1st order autocorrelation
LB <- n * ((n+2)/(n-1))*r1**2      #LB statistic for lag = 1.
1 - pchisq(LB, 1) # p-value of the LB test; always << 1.

0 个答案:

没有答案