DPredict · Guide test 177
Rainbow test
Compares a central subsample’s linear fit with the full sample to detect nonlinearity.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Linear regression (OLS) — How does a numeric outcome depend on one or more predictors?
In Linear regression (OLS), choose Residual diagnostics = Full battery.
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Rainbow test
Adds curve × (x − 5)².
Error SD grows like exp(h · (x − 5)/5).
Errors in collection order carry φ × the previous error.
Your data
Simulated with a known ρIf H₀ were true
- The reference: how F varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .550).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- residual sum of squares of the central half (by x)
The verdict
F(20, 18) = 0.95, p = .550
p > α = .050: compatible with H₀
A curved relation looks linear in the middle: a much better central fit points to non-linearity.
- Statistic
- 0.947
- p-value
- 0.5495
Rainbow test on 40 (x, y) pairs: F(20, 18) = 0.95, p = .550. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: the same linear regression describes the full sample and its selected central subset.
Assumptions
Independent normal homoskedastic errors; subset selection based on a prespecified ordering/design rule, with enough central observations to fit all coefficients.
Formula
Parameters and notation
nc central subset size; p coefficients; RSScenter comes from refitting only the subset.
Structure of the method
Fit a full-sample regression and a second regression on a central subset defined by the chosen ordering or regressor geometry. Compare their residual sums of squares with degrees-of-freedom scaling. The procedure asks whether a middle region is substantially more compatible with the linear form than the entire sample.
Estimated and nuisance parameters
Full-sample and central-subset regression coefficients and error variances are separately estimated. The subset center may be defined from regressors, but the subset fraction and ordering rule are design choices rather than fitted slope parameters.
Settings and design choices
Specify central fraction, center location, ordering variable or Mahalanobis ordering, model matrix, and null calibration. Each subset must contain enough independent rows for full rank. A sweep over fractions or centers requires a joint selection-aware testing procedure.
Fitting and adaptation
Refit both regressions on the declared samples and preserve the same predictor specification. A design-conditional simulation should repeat subset selection and both fits. If a nonlinear alternative is fitted after rejection, assess prediction and residual behavior on held-out observations rather than treating a passing follow-up test as proof of correctness.
Sensitivity and robustness
Heteroskedasticity can make central and outer observations have different residual scales without a nonlinear conditional mean. Outliers, leverage, a poorly chosen ordering, or a tiny central subset also matter. The test may miss departures concentrated inside the selected region. Report the actual subset size, residual degrees of freedom, ordering rule, and whether the displayed F reference's assumptions are defensible.
How to use and calibrate
Use Fn−nc,nc−p. State fraction, ordering and center definition.
Example application
Detect departures from linearity that leave a central portion approximately linear.
Interpretation and pitfalls
Watch out
Data-chosen subsets and heteroskedasticity can cause misleading rejection.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Read the directional model comparison carefully: nonnested procedures can reject neither, one, or both models. A specification rejection does not identify the correct replacement model.
Validated — Executed successfully in the validation environment · needs numpy, scipy, statsmodels
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
import statsmodels.api as sm
n=100
x=rng.normal(size=n); z=rng.normal(size=n); y=1+.5*x+.3*z+rng.normal(size=n)
X=sm.add_constant(np.column_stack([x,z])); fit=sm.OLS(y,X).fit(); reduced=sm.OLS(y,X[:,:2]).fit()
from statsmodels.stats.diagnostic import linear_rainbow
print(linear_rainbow(fit))
Output
(np.float64(0.7321084017217783), np.float64(0.8604270277269974))
Validated — Executed successfully in the validation environment · needs lmtest
set.seed(2026)
n <- 100; x <- rnorm(n); z <- rnorm(n); y <- 1+.5*x+.3*z+rnorm(n)
d <- data.frame(y,x,z); fit <- lm(y~x+z,data=d); reduced <- lm(y~x,data=d)
print(lmtest::raintest(fit))
Output
Rainbow test data: fit Rain = 1.3883, df1 = 50, df2 = 47, p-value = 0.1295
Sources
- lmtest — lmtest · raintestAuthor documentation/source reviewed: lmtest 0.9-40.search.r-project.org
- raintestsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 177, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT177.