def test_score_test(self): res1 = self.res1 modr = self.klass(endog, exog.values[:, :-1]) resr = modr.fit(method="newton", maxiter=300) params_restr = np.concatenate( [resr.params[:-1], [0], resr.params[-1:]]) r_matrix = np.zeros((1, len(params_restr))) r_matrix[0, -2] = 1 exog_extra = res1.model.exog[:, -1:] from statsmodels.base._parameter_inference import score_test sc1 = score_test(res1, params_constrained=params_restr, k_constraints=1) sc2 = score_test(resr, exog_extra=(exog_extra, None)) assert_allclose(sc2[:2], sc1[:2]) sc1_hc = score_test(res1, params_constrained=params_restr, k_constraints=1, r_matrix=r_matrix, cov_type="HC0") sc2_hc = score_test(resr, exog_extra=(exog_extra, None), cov_type="HC0") assert_allclose(sc2_hc[:2], sc1_hc[:2])
def test_wald_score(self): mod_full = self.model_full mod_drop = self.model_drop restriction = 'x5=0, x6=0' res_full = mod_full.fit() res_constr = mod_full.fit_constrained('x5=0, x6=0') res_drop = mod_drop.fit() wald = res_full.wald_test(restriction) lm_constr = np.hstack(score_test(res_constr)) lm_extra = np.hstack(score_test(res_drop, exog_extra=self.exog_extra)) res_wald = np.hstack([wald.statistic.squeeze(), wald.pvalue, [wald.df_denom]]) assert_allclose(lm_constr, res_wald, rtol=self.rtol_ws, atol=self.atol_ws) assert_allclose(lm_extra, res_wald, rtol=self.rtol_ws, atol=self.atol_ws) assert_allclose(lm_constr, lm_extra, rtol=1e-12, atol=1e-14) # regression number assert_allclose(lm_constr[1], self.res_pvalue[0], rtol=1e-12, atol=1e-14) cov_type='HC0' res_full_hc = mod_full.fit(cov_type=cov_type, start_params=res_full.params) wald = res_full_hc.wald_test(restriction) lm_constr = np.hstack(score_test(res_constr, cov_type=cov_type)) lm_extra = np.hstack(score_test(res_drop, exog_extra=self.exog_extra, cov_type=cov_type)) res_wald = np.hstack([wald.statistic.squeeze(), wald.pvalue, [wald.df_denom]]) assert_allclose(lm_constr, res_wald, rtol=self.rtol_ws, atol=self.atol_ws) assert_allclose(lm_extra, res_wald, rtol=self.rtol_ws, atol=self.atol_ws) assert_allclose(lm_constr, lm_extra, rtol=1e-13) # regression number assert_allclose(lm_constr[1], self.res_pvalue[1], rtol=1e-12, atol=1e-14) if not self.skip_wooldridge: # compare with Wooldridge auxiliary regression # does not work for Poisson, even with family attribute # diao.lm_test_glm assumes fittedvalues is mean (not linear pred) lm_wooldridge = diao.lm_test_glm(res_drop, self.exog_extra) assert_allclose(lm_wooldridge.pval1, self.res_pvalue[0], rtol=1e-12, atol=1e-14) assert_allclose(lm_wooldridge.pval3, self.res_pvalue[1], rtol=self.rtol_wooldridge) # smoke test lm_wooldridge.summary()
def test_score_test(self): res1 = self.res1 resr = self.resr params_restr = np.concatenate([resr.params, [0]]) r_matrix = np.zeros((1, len(params_restr))) r_matrix[0, -1] = 1 exog_prec_extra = res1.model.exog_precision[:, 1:] from statsmodels.base._parameter_inference import score_test sc1 = score_test(res1, params_constrained=params_restr, k_constraints=1) sc2 = score_test(resr, exog_extra=(None, exog_prec_extra)) assert_allclose(sc2[:2], sc1[:2]) sc1_hc = score_test(res1, params_constrained=params_restr, k_constraints=1, r_matrix=r_matrix, cov_type="HC0") sc2_hc = score_test(resr, exog_extra=(None, exog_prec_extra), cov_type="HC0") assert_allclose(sc2_hc[:2], sc1_hc[:2])
def test_score_test_alpha(self): # this is mostly sanity check # we need heteroscedastic model, i.e. exog_dispersion as comparison # res1 = self.res1 modr = self.klass(endog, exog.values[:, :-1]) resr = modr.fit(method="newton", maxiter=300) params_restr = np.concatenate([resr.params[:], [0]]) r_matrix = np.zeros((1, len(params_restr))) r_matrix[0, -1] = 1 # use random expg_extra, then we don't reject null # exog_extra = res1.model.exog[:, 1:2] np.random.seed(987125643) exog_extra = 0.01 * np.random.randn(endog.shape[0]) from statsmodels.base._parameter_inference import (score_test, _scorehess_extra) # note: we need params for the restricted model here # if params is not given, then it will be taked from results instance sh = _scorehess_extra(resr, exog_extra=None, exog2_extra=exog_extra, hess_kwds=None) assert not np.isnan(sh[0]).any() # sc1 = score_test(res1, params_constrained=params_restr, # k_constraints=1) sc2 = score_test(resr, exog_extra=(None, exog_extra)) assert sc2[1] > 0.01 # sc1_hc = score_test(res1, params_constrained=params_restr, # k_constraints=1, r_matrix=r_matrix, cov_type="HC0") sc2_hc = score_test(resr, exog_extra=(None, exog_extra), cov_type="HC0") assert sc2_hc[1] > 0.01