def _endogeneity_setup(self, vars=None): """Setup function for some endogeneity iv""" if vars is not None and not isinstance(vars, list): vars = [vars] nobs = self.model.dependent.shape[0] e2 = self.resids.values nendog, nexog = self.model.endog.shape[1], self.model.exog.shape[1] if vars is None: assumed_exog = self.model.endog.ndarray aug_exog = c_[self.model.exog.ndarray, assumed_exog] still_endog = empty((nobs, 0)) else: assumed_exog = self.model.endog.pandas[vars].values ex = [c for c in self.model.endog.cols if c not in vars] still_endog = self.model.endog.pandas[ex].values aug_exog = c_[self.model.exog.ndarray, assumed_exog] ntested = assumed_exog.shape[1] from linearmodels.iv import IV2SLS mod = IV2SLS(self.model.dependent, aug_exog, still_endog, self.model.instruments) e0 = mod.fit().resids.values[:, None] z2 = c_[self.model.exog.ndarray, self.model.instruments.ndarray] z1 = c_[z2, assumed_exog] e1 = proj(e0, z1) e2 = proj(e2, self.model.instruments.ndarray) return e0, e1, e2, nobs, nexog, nendog, ntested
def wooldridge_overid(self): r""" Wooldridge's score test of overidentification Returns ------- t : WaldTestStatistic Object containing test statistic, p-value, distribution and null Notes ----- Wooldridge's test examines whether there is correlation between the model residuals and the component of the instruments that is orthogonal to the endogenous variables. Define :math:`\tilde{z}` to be the residuals of the instruments regressed on the exogenous variables and the first-stage fitted values of the endogenous variables. The test is computed as a regression .. math :: 1 = \gamma_1 \hat{\epsilon}_i \tilde{z}_{i,1} + \ldots + \gamma_q \hat{\epsilon}_i \tilde{z}_{i,q} where :math:`q = n_{instr} - n_{endog}`. The test is a :math:`n\times R^2 \sim \chi^2_{q}`. The order of the instruments does not affect this test. """ from linearmodels.iv.model import _OLS exog, endog = self.model.exog, self.model.endog instruments = self.model.instruments nobs, nendog = endog.shape ninstr = instruments.shape[1] if ninstr - nendog == 0: import warnings warnings.warn( 'Test requires more instruments than ' 'endogenous variables', UserWarning) return WaldTestStatistic(0, 'Test is not feasible.', 1, name='Infeasible test.') endog_hat = proj(endog.ndarray, c_[exog.ndarray, instruments.ndarray]) q = instruments.ndarray[:, :(ninstr - nendog)] q_res = annihilate(q, c_[self.model.exog.ndarray, endog_hat]) test_functions = q_res * self.resids.values[:, None] res = _OLS(ones((nobs, 1)), test_functions).fit('unadjusted') stat = res.nobs * res.rsquared df = ninstr - nendog null = 'Model is not overidentified.' name = 'Wooldridge\'s score test of overidentification' return WaldTestStatistic(stat, null, df, name=name)