An ABA Ljung-Box calculator summarizes several autocorrelations from one ordered residual sequence. Use it only after the team has fitted and documented a model, preserved its residuals in their real observation order, and stated the multi-lag question. This is not a calculator for the original behavior series, and it does not fit or select a model.
Clinicians & ABA Professionals / Data, Outcomes and Clinical Decision-Making.
Ask whether dependence remains across several residual lags
Use the ABA Ljung-Box calculator as a descriptive portmanteau check, with the ordered residual record available for review.
The output is a descriptive portmanteau statistic. Because signed autocorrelations are squared before they enter Q, the total can flag accumulated structure without showing its direction. No value proves randomness, validates a model, identifies a clinical cause, or demonstrates a functional relation. Keep the residual plot, signed lag table, and autocorrelation plot beside the arithmetic.
Fix the model, order, lag horizon, and intended decision first
The worksheet begins with a signed scope record. The analysis owner identifies what produced the residuals and why the selected maximum lag belongs to the question before viewing the result.
Scope fieldRequired entryStop signalResponse and unitObservable measure and unitMixed or changing unitsSource modelFormula, predictors, phase, versionResiduals have no documented modelResidual conventionObserved minus fitted, or another named conventionConvention cannot be reconstructedOrdering keyDate, session, opportunity, or other fixed indexOrder is uncertain or was sorted post hocSpacingPlanned adjacency and known gapsSpacing changes the lag meaningMaximum lag hInteger chosen before calculationh < 1 or h >= nParameter adjustmentDocumented model_dfAdjustment chosen after seeing QIntended useSpecific diagnostic reviewCausal, treatment-effect, or pass/fail claim
Record phase changes, interruptions, setting changes, staffing changes, missing observations, and other events that can make adjacency misleading. A single pooled statistic should not conceal those features.
Preserve the ordered residual ledger
Copy the residuals without sorting them by value. Keep the original observation, fitted value, residual, sequence key, phase, and inclusion decision available under authorized access. The calculator may use a minimum necessary analysis table, but the provenance record must let a reviewer trace each row.
Index tAuthorized sequence keyResidual e_tPhase or contextIncluded?Reason123...
Do not interpolate a missing residual merely to preserve spacing. Retain the gap and stop if it changes what lag one or lag two means. Do not combine residuals from different fitted models as if they came from one sequence.
Calculate autocorrelation and the Ljung-Box sum transparently
For n residuals, first apply the declared centering convention. This page uses the sequence mean e_bar and the NIST-style denominator across all centered residuals:
rk = sum from t=1 to n-k of ((et - ebar)(e(t+k) - ebar)) / sum from t=1 to n of (et - e_bar)^2
For prespecified lags k = 1, ..., h, compute:
QLB = n(n + 2) * sum from k=1 to h of (rk^2 / (n - k))
The statsmodels Ljung-Box documentation states that the series is demeaned and reports results through selected lags. The NIST Box-Ljung definition displays the same finite-sample weighting form. Software conventions, lag defaults, parameter adjustments, and inference settings still require explicit versioned review.
Copy the blank multi-lag worksheet
Lag kPaired products sumr_kr_k^2n-kWeighted term r_k^2/(n-k)Cumulative term12...h
Result fieldValueAudit notenCount after prespecified inclusion rulese_barCentering valueTotal centered sum of squaresMust be positivehChosen before result reviewmodel_dfDocumented fitted-parameter adjustmenth - model_dfMust be positive for a referenced chi-square comparisonQ_LBDescriptive statistic from full precision
Carry full precision through the calculations. Rounding is for display only. Preserve the code or spreadsheet version, analyst, reviewer, calculation date, and any software cross-check.
Work a fictional eight-residual example
Suppose a fictional review starts with ordered residuals [1, 0, -1, -2, -1, 0, 1, 2]. The mean is 0, the centered sum of squares is 12, n = 8, and the prespecified lag horizon is h = 2.
LagProduct sumr_kr_k^2n-kWeighted term160.50000000000.250000000070.03571428572-1-0.08333333330.006944444460.0011574074
The weighted sum is 0.0368716931. Because n(n+2) = 80, the fictional result is Q_LB = 2.9497354497. This worksheet reports the arithmetic and the chosen lag horizon. It does not attach an automatic p-value or a universal pass/fail label.
Audit each arithmetic step
For lag one, align rows 1 through 7 with rows 2 through 8. The centered cross-product sum is 6, so r1 = 6/12 = 0.5. For lag two, align rows 1 through 6 with rows 3 through 8. The product sum is -1, so r2 = -1/12 = -0.0833333333.
Square each autocorrelation only after preserving its signed value. Divide by that lag's n-k term, sum the weighted terms, and multiply once by n(n+2). A spreadsheet should expose the lag table instead of returning only Q. A second implementation should reproduce every intermediate value before interpretation begins.
Keep the lag horizon and degrees of freedom visible
Ljung-Box accumulates information through h; it does not inspect a single adjacency only. For the same fictional sequence, using only lag one gives QLB = 2.8571428571. Extending the calculation through lag three, where r3 = -0.5, gives Q_LB = 6.9497354497. The difference is a lag-choice sensitivity, not evidence that one result is preferable.
The NIST description adjusts the reference degrees of freedom for fitted ARMA parameters. Statsmodels exposes modeldf and returns an undefined p-value when lag - modeldf <= 0. This page records h - model_df but does not supply a critical value, p-value, or inferential decision. Model-specific adjustment requires qualified methods review.
Run invariance and boundary checks
CheckExpected calculation behaviorWhat it does not showReverse the sequenceAutocorrelations and Q remain the same for this conventionReversal is clinically or temporally validMultiply every residual by a positive constantQ remains the sameUnits or model are interchangeableMultiply every residual by -1Q remains the sameResidual sign convention is irrelevant to provenanceConstant residual sequenceCentered denominator is zero; stopConstant error means a valid modelSet h >= nInvalid lag horizon; stopShort sequences can support arbitrary lagsChange h after viewing QPreserve as an unauthorized post hoc analysisThe preferred result is credible
These checks verify implementation properties. They do not verify the source model, measurement system, sampling plan, or clinical meaning.
Interpret Q as a review prompt, not a verdict
The NIST autocorrelation plot guidance describes autocorrelation across lags and warns that lack of autocorrelation does not establish full randomness. A cumulative Q can flag residual dependence somewhere in the selected lag set, but it does not identify which environmental, measurement, phase, modeling, or operational feature produced the pattern.
Review the signed lag-specific autocorrelations and plots. Compare them with phase boundaries, missing sessions, schedule changes, treatment modifications, data-collection changes, and model misspecification. Escalate a plausible pattern to qualified review rather than deleting rows or changing the model until Q looks smaller.
Stop when the estimand or sequence is not defensible
Stop if residuals do not come from one documented model, or if the response definition or unit changed. Also stop when order or spacing is uncertain; gaps alter adjacency; phases were pooled without justification; or the centered denominator is zero. Stop as well when h is not prespecified; h >= n; adjusted degrees of freedom are nonpositive; or repeated, clustered, seasonal, or irregularly spaced observations require another method.
Also stop for a request to prove independence, establish treatment effect, certify experimental control, or convert the statistic into a clinical rule. This calculator does not rescue weak measurement, a poor graph, an unsuitable model, or an unrepresentative sample.
Preserve clinical, ethical, privacy, and implementation review
The BACB Ethics Codes page is the current professional source, and the BCBA Test Content Outline includes measurement, data display, interpretation, and experimental design content. The Standards for Educational and Psychological Testing provide broader testing principles. None certifies this calculator, creates competence, or supplies ABA-specific Ljung-Box thresholds.
Use synthetic or appropriately de-identified sequences for training and testing. When identifiable information is necessary in an authorized workflow, apply minimum-necessary access, approved storage, retention controls, and traceable versions. The HHS Privacy Rule summary and Security Rule summary describe federal requirements for regulated entities. Applicability and safeguards still require organizational and legal review.
Related resources
- ABA Durbin-Watson Ordered-Residual Autocorrelation Diagnostic Calculator for Clinicians
- ABA Ordinary Least-Squares Fixed-X Regression Diagnostic Calculator for Clinicians
- ABA Pearson Product-Moment Correlation Linearity and Outlier Sensitivity Calculator for Clinicians
- ABA Phase Level, Trend, Variability and Immediacy Calculator for Clinicians