def linear_rainbow(res, frac=0.5):
 nobs = res.nobs
 endog = res.model.endog
 exog = res.model.exog
 lowidx = np.ceil(((0.5 * (1 - frac)) * nobs)).astype(int)
 uppidx = np.floor((lowidx + (frac * nobs))).astype(int)
 mi_sl = slice(lowidx, uppidx)
 res_mi = OLS(endog[mi_sl], exog[mi_sl]).fit()
 nobs_mi = res_mi.model.endog.shape[0]
 ss_mi = res_mi.ssr
 ss = res.ssr
 fstat = ((((ss - ss_mi) / (nobs - nobs_mi)) / ss_mi) * res_mi.df_resid)
 from scipy import stats
 pval = stats.f.sf(fstat, (nobs - nobs_mi), res_mi.df_resid)
 return (fstat, pval)

def linear_rainbow ( res , frac = 0.5 ) : nobs = res . nobs endog = res . model . endog exog = res . model . exog lowidx = np . ceil ( ( ( 0.5 * ( 1 - frac ) ) * nobs ) ) . astype ( int ) uppidx = np . floor ( ( lowidx + ( frac * nobs ) ) ) . astype ( int ) mi_sl = slice ( lowidx , uppidx ) res_mi = OLS ( endog [ mi_sl ] , exog [ mi_sl ] ) . fit ( ) nobs_mi = res_mi . model . endog . shape [ 0 ] ss_mi = res_mi . ssr ss = res . ssr fstat = ( ( ( ( ss - ss_mi ) / ( nobs - nobs_mi ) ) / ss_mi ) * res_mi . df_resid ) from scipy import stats pval = stats . f . sf ( fstat , ( nobs - nobs_mi ) , res_mi . df_resid ) return ( fstat , pval )
rainbow test for linearity the null hypothesis is that the regression is correctly modelled as linear .