Functions to perform the Ljung-Box test for autocorrelation in residuals.
The test statistic is
$$Q := n(n+2)\sum_{k=1}^{\ell} \frac{\hat{r}_k^2}{n-k}$$
Where:
$$\hat{r}_k := \frac{\sum_{i=k}^{n-1}(v_i-\bar{v})(v_{i-k} - \bar{v})}{\sum_{i=0}^{n-1}(v_i-\bar{v})^2}$$
Where: \(n\) is the sample size (length of \(v\)) and \(\ell\) is the number of lags
See also
Boost Documentation for more details on the mathematical background.
Examples
# Ljung-Box test for autocorrelation
ljung_box(c(1, 2, 3, 4, 5), lags = 2, fit_dof = 0)
#> [1] 1.5166667 0.4684465