Runs Test for Residual Randomness
do_runs_test.RdPerforms a nonparametric runs test to evaluate whether a sequence of residuals is randomly distributed around a reference mean. The function also computes three–sigma control limits used to identify potential residual outliers.
Arguments
- x
Numeric vector of residuals.
- type
Character string specifying the assumed mean of the residuals. If
"resid"(default), the residual mean is assumed to be zero. Otherwise, the empirical mean ofxis used.- mixing
Character string specifying the alternative hypothesis for the runs test:
"two.sided"– tests for both positive and negative autocorrelation (default)."less"– left-tailed test detecting positive autocorrelation.
Value
A list containing:
sig3lim– Numeric vector of length two giving the lower and upper three–sigma control limits for the residuals.p.runs– P-value from the runs test for randomness.
A small p-value (e.g., < 0.05) indicates evidence that the residual
sequence is not random and may exhibit autocorrelation or other systematic
patterns.
Details
This implementation is adapted from diagnostics used in Stock Synthesis (SS3). The runs test evaluates whether residuals exhibit non-random structure (e.g., positive or negative autocorrelation).