I generate this data in R:


When I perform Box-Ljung test to test the independency:


it gave me p-value=0.5957, which is reasonable. However, when I perform this:


it gave me p-value=0.00162045, which means the autocorrelation of this sub-sequence is significantly different from zero. This also happens for some other subsequences. However, I generated the time series using $rnorm$ function in R with 1000 data points. Could anybody please explain this? Thanks very much in advance.

For the convenience of you readers who are not using R, the sub-sequence ds[180:299] can be obtained here: http://ykang.hostoi.com/ds120.pdf

  • 1
    $\begingroup$ Welcome to the site. I removed your signature.... if you register, you will get one automatically. $\endgroup$ – Peter Flom Oct 5 '12 at 10:14
  • $\begingroup$ Small portions of your data may exhibit dependence, but overall (if long enough), rnorm will give you a non-dependent signal. $\endgroup$ – Roman Luštrik Oct 5 '12 at 10:22
  • 1
    $\begingroup$ 120 values is not small. This is intersting to me. Since I don't live in an R world, I can't duplicate the 100 values and thus explain this conumdrum. Please attach the 100 values to your post and I will put this under my microscope. $\endgroup$ – IrishStat Oct 5 '12 at 11:22
  • $\begingroup$ There is no problem with the test. You could see the significant correlation in 'acf' or 'pacf'(lag 4). It seems for small sample using 'rnorm' is really independent. $\endgroup$ – vinux Oct 5 '12 at 13:18
  • $\begingroup$ It wouldn't hurt for @IrishStat to do an analysis of this with autobox. I am just starting to use R today but he has tested software and is familiar with the Box-Ljung test which is not new! If the results don't match I would trust his results and start looking for bugs in your R program. Even if it can be explained theoreticlly that does not prove it is right and he will study your series thoroughly and quickly and for free and will probably post an answer here for all of us to see. $\endgroup$ – Michael R. Chernick Oct 5 '12 at 15:29

Let us do a simulation where we apply the test 1000 times for time series of length 120.

> fun <- function(n) {Box.test(rnorm(n),type="Ljung-Box",lag=log(n))$p.value}
> set.seed(111)
> mc <- sapply(rep(120,1000),fun)
> sum(mc<0.05)/length(mc)
[1] 0.039

So you get 39 cases out of 1000 for which the null is rejected if we assume threshold 0.05. This is perfectly normal.

The test statistic is a random variable which has a distribution. This means that it can get values which are unlikely, or likely with small probability. We reject the null hypothesis when the value is unlikely, yet by doing this we commit Type I error.

  • $\begingroup$ Type I error is a good explanation. Any test can't go with 100% sure. Totally agree. $\endgroup$ – Elaine Oct 8 '12 at 23:41
  • $\begingroup$ @IrishStat Agree with you that the test is clearly impacted by n. But when I tried to increase n to 10000 instead of 120 and do the simulation which Mpiktas did, the percentage of being rejected was even larger, which is not going well with my understandings... $\endgroup$ – Elaine Oct 9 '12 at 0:01
  • 1
    $\begingroup$ @Elaine, with set.seed(111) and n to 10000, I got 0.044. Which is again normal. By increasing n the percentage must go to 0.05, since then the finite sample test distribution converges to the true distribution. $\endgroup$ – mpiktas Oct 9 '12 at 6:41

This subsequence testing is actually related to the locally stationary extension of the Box test to detect time-varying dependencies (paper).

Under the null hypothesis of independent white noise you can compute the Box test on $N$ non-overlapping subwindows over your time series, add them all together, and then test the sum of these test statistics for significance. This works out since asymptotically the test statistic of window $i$ has a $\chi^2$ distribution with $k_i$ degrees of freedom, where $k_i$ is the lags tested in subwindow $i$, $i = 1, \ldots, N$. Under the null they are independent, and the sum of independent $\chi^2$ random variables has again a $\chi^2$ distribution with $K = \sum_{i=1}^{N} k_i$ degrees of freedom.

This should answer why one subwindow of your time series may have a low p-value; but when you add all sub-window test statistic, this one large test statistic in your particular window will be balanced with some extremely low test statistics in other subwindows. Overall, you will get again the right test coverage.

See the paper above for technical details. For R code and a data application see this blog post or the other stackoverflow post: ``Nice example where a series without a unit root is non stationary?''


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.