When reading ?Box.test, I learned about a rarely mentioned parameter fitdf, which specified the "number of degrees of freedom to be subtracted if x is a series of residuals". It seems that when applying this function on residuals, we should pass this parameter to Box.test.
Details
These tests are sometimes applied to the residuals from an
ARMA(p, q)fit, in which case the references suggest a better approximation to the null-hypothesis distribution is obtained by settingfitdf = p+q, provided of course thatlag > fitdf.
However, many appear to ignore this suggestion. Here is the source code of an implementation of the S3 method tsdiag. Note that Box.test didn't get fitdf.
> getS3method("tsdiag", "Arima")
function (object, gof.lag = 10, ...)
{
oldpar <- par(mfrow = c(3, 1))
on.exit(par(oldpar))
rs <- object$residuals
stdres <- rs/sqrt(object$sigma2)
plot(stdres, type = "h", main = "Standardized Residuals",
ylab = "")
abline(h = 0)
acf(object$residuals, plot = TRUE, main = "ACF of Residuals",
na.action = na.pass)
nlag <- gof.lag
pval <- numeric(nlag)
for (i in 1L:nlag) pval[i] <- Box.test(rs, i, type = "Ljung-Box")$p.value
plot(1L:nlag, pval, xlab = "lag", ylab = "p value", ylim = c(0,
1), main = "p values for Ljung-Box statistic")
abline(h = 0.05, lty = 2, col = "blue")
}
<bytecode: 0x7fad483ad6c8>
<environment: namespace:stats>
Is this OK?