def linear_lm(resid, exog, func=None):
 from scipy import stats
 if (func is None):
  func = (lambda x: np.power(x, 2))
 exog_aux = np.column_stack((exog, func(exog[:, 1:])))
 (nobs, k_vars) = exog.shape
 ls = OLS(resid, exog_aux).fit()
 ftest = ls.f_test(np.eye((k_vars - 1), ((k_vars * 2) - 1), k_vars))
 lm = (nobs * ls.rsquared)
 lm_pval = stats.chi2.sf(lm, (k_vars - 1))
 return (lm, lm_pval, ftest)

def linear_lm ( resid , exog , func = None ) : from scipy import stats if ( func is None ) : func = ( lambda x : np . power ( x , 2 ) ) exog_aux = np . column_stack ( ( exog , func ( exog [ : , 1 : ] ) ) ) ( nobs , k_vars ) = exog . shape ls = OLS ( resid , exog_aux ) . fit ( ) ftest = ls . f_test ( np . eye ( ( k_vars - 1 ) , ( ( k_vars * 2 ) - 1 ) , k_vars ) ) lm = ( nobs * ls . rsquared ) lm_pval = stats . chi2 . sf ( lm , ( k_vars - 1 ) ) return ( lm , lm_pval , ftest )
lagrange multiplier test for linearity against functional alternative limitations: assumes currently that the first column is integer .