Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

Stata includes a a command (wntestq) that it calls the "portmanteau Q test for white noise." There seem to a variety of related tests in different packages in R. That said, most of these seem designed specifically for data in various time series formats and none that I could find that operate on a single variable.

like image 331
mako Avatar asked Mar 20 '23 01:03

mako


1 Answers

"Portmanteau" refers to a family of statistical tests. In time series analysis, portmanteau tests are used for testing for autocorrelation of residuals in a model. The most commonly used test is the Ljung-Box test. Although it's buried in a citation in the manual, it seems that is the test that the Stata command wntestq has implemented.

R implements the same test in a function called Box.test() which is in the stats package that comes included with R. As you can see in the documentation for that function, Box.test() actually implements two tests: the Ljung-Box text that Stata uses and the Box-Pierce test. According to some sources, Box-Pierce was found to include a seemingly trivial simplification which can lead to nasty effects.[1][2] For that reasons, and because the defaults are different in R and Stata, it is worth noting that the Box-Pierce version is default in R.

The test will consider a certain number of autocorrelation coefficients (i.e., up to lag h) and there is no obvious default to select (see this question on the statistics StackExchange for a much more detailed discussion). Another important difference that will lead to different results is that the default h or number of lags will be different in Stata and R. By default, R will set h to 1* while Stata will set h to [n/2]-2 or 40, whichever is smaller.

Although there are many reasons you might not want the default, the following R function will reproduce the default behavior of the Stata command:

q.test <- function (x) {
    Box.test(x, type="Ljung-Box", lag=min(length(x)/2-2, 40))
}
like image 64
mako Avatar answered Apr 25 '23 13:04

mako