7

I generate this data in R:

set.seed(111)
ds=rnorm(1000)

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

Box.test(ds,type='Ljung',lag=log(length(ds)))

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

Box.test(ds[180:299],type='Ljung',lag=log(length(ds[180:299])))

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

Elaine
  • 153
  • 1
  • 1
  • 6
  • 1
    Welcome to the site. I removed your signature.... if you register, you will get one automatically. – Peter Flom Oct 05 '12 at 10:14
  • Small portions of your data may exhibit dependence, but overall (if long enough), `rnorm` will give you a non-dependent signal. – Roman Luštrik Oct 05 '12 at 10:22
  • 1
    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. – IrishStat Oct 05 '12 at 11:22
  • 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. – vinux Oct 05 '12 at 13:18
  • 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. – Michael R. Chernick Oct 05 '12 at 15:29
  • Could you possibly post the data in a columnar format as I am having some issues with the way you presented it. Thanks – IrishStat Oct 08 '12 at 01:16
  • @IrishStat Thanks very much for your help. I have edited the question again. Please see the data here: http://ykang.hostoi.com/ds120.pdf – Elaine Oct 08 '12 at 01:22
  • @Michael You can start looking for bugs right away: just type `Box.test` in `R` to see the code. It's short and sweet--I doubt you'll find any bugs. Mpiktas has more than adequately explained the phenomenon described here: when you fish through hundreds of p-values, you are bound to find some small ones by chance alone. – whuber Oct 08 '12 at 16:59
  • @whuber a p value of .5957 is kind of large by chance, particularly when the acf has values that I listed. Perhaps this test is not as robust as might be desired as this looks like a false positive. The test Q= n (n+2 ) Σ (1/n-j) ρ2 j – IrishStat Oct 08 '12 at 17:33
  • The test Q= n (n+2 ) Σ (1/n-j) ρ2 j uses n observations and j lags. It clearly is impacted by n and consequently could be overstating the test statistic. – IrishStat Oct 08 '12 at 17:40
  • @Irish I don't follow: a p-value of 0.5957 is precisely what one would expect "by chance"; there's no evidence anything is wrong with this series of 1000 pseudorandom normal variates. The crux of the matter here is that more than chance is operating with the second p-value: the selection of 180 as the starting index in the series was purposeful. It begins a subsequence of length 120 having the smallest p-value for the Box-Ljung test. – whuber Oct 08 '12 at 18:19
  • oops I misread . Sorry .. – IrishStat Oct 08 '12 at 18:29

2 Answers2

7

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.

mpiktas
  • 33,140
  • 5
  • 82
  • 138
  • Type I error is a good explanation. Any test can't go with 100% sure. Totally agree. – Elaine Oct 08 '12 at 23:41
  • @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... – Elaine Oct 09 '12 at 00:01
  • 1
    @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. – mpiktas Oct 09 '12 at 06:41
1

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?''

Georg M. Goerg
  • 2,364
  • 20
  • 21