Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
a 256 bit value. This allows variables to be reused in different
models if the set of absorbing variables and interactions is held
constant.
See also
--------
linearmodels.iv.covariance.HomoskedasticCovariance
linearmodels.iv.covariance.HeteroskedasticCovariance
linearmodels.iv.covariance.KernelCovariance
linearmodels.iv.covariance.ClusteredCovariance
"""
if self._absorbed_dependent is None:
self._first_time_fit(use_cache, lsmr_options)
self._x = exog_resid = to_numpy(self.absorbed_exog)
dep_resid = to_numpy(self.absorbed_dependent)
if self._exog.shape[1] == 0:
params = empty((0, 1))
else:
if exog_resid.shape[1]:
check_absorbed(exog_resid, self.exog.cols)
params = lstsq(exog_resid, dep_resid)[0]
self._num_params += exog_resid.shape[1]
cov_estimator = COVARIANCE_ESTIMATORS[cov_type]
cov_config['debiased'] = debiased
cov_config['kappa'] = 0.0
cov_config_copy = {k: v for k, v in cov_config.items()}
if 'center' in cov_config_copy:
del cov_config_copy['center']
cov_estimator = cov_estimator(exog_resid, dep_resid, exog_resid, params, **cov_config_copy)
Parameters
----------
cat : Series
Categorical series to convert to dummy variables
cont : {Series, DataFrame}
Continuous variable values to use in the dummy interaction
precondition : bool
Flag whether dummies should be preconditioned
Returns
-------
interact : csc_matrix
Sparse matrix of dummy interactions with unit column norm
"""
codes = get_codes(category_product(cat).cat)
interact = csc_matrix((to_numpy(cont).flat, (arange(codes.shape[0]), codes)))
if not precondition:
return interact
else:
return preconditioner(interact)[0]
weps = self.wresids(params)
cov = cov_estimator.cov
debiased = cov_estimator.debiased
residual_ss = (weps.T @ weps)[0, 0]
w = self.weights.ndarray
root_w = sqrt(w)
e = self._dependent.ndarray * root_w
if self.has_constant:
e = e - root_w * average(self._dependent.ndarray, weights=w)
total_ss = float(e.T @ e)
r2 = max(1 - residual_ss / total_ss, 0.0)
e = to_numpy(self._absorbed_dependent) # already scaled by root_w
# If absorbing contains a constant, but exog does not, no need to demean
if self._const_col is not None:
col = self._const_col
x = to_numpy(self._absorbed_exog)[:, col:col + 1]
mu = (lstsq(x, to_numpy(e))[0]).squeeze()
e = e - x * mu
aborbed_total_ss = float(e.T @ e)
r2_absorbed = max(1 - residual_ss / aborbed_total_ss, 0.0)
fstat = self._f_statistic(params, cov, debiased)
out = {'params': Series(params.squeeze(), columns, name='parameter'),
'eps': Series(eps.squeeze(), index=index, name='residual'),
'weps': Series(weps.squeeze(), index=index, name='weighted residual'),
'cov': DataFrame(cov, columns=columns, index=columns),
's2': float(cov_estimator.s2),
Parameters
----------
params : ndarray
Model parameters (nvar by 1)
Returns
-------
wresids : ndarray
Weighted model residuals
Notes
-----
Uses weighted versions of data instead of raw data. Identical to
resids if all weights are unity.
"""
return to_numpy(self._absorbed_dependent) - to_numpy(self._absorbed_exog) @ params
def _drop_missing(self) -> ndarray:
missing = to_numpy(self.dependent.isnull)
missing |= to_numpy(self.exog.isnull)
missing |= to_numpy(self._absorb_inter.cat.isnull().any(1))
missing |= to_numpy(self._absorb_inter.cont.isnull().any(1))
for interact in self._interaction_list:
missing |= to_numpy(interact.isnull)
if npany(missing):
self.dependent.drop(missing)
self.exog.drop(missing)
self._absorb_inter.drop(missing)
for interact in self._interaction_list:
interact.drop(missing)
missing_warning(missing)
return missing
def _drop_missing(self) -> ndarray:
missing = to_numpy(self.dependent.isnull)
missing |= to_numpy(self.exog.isnull)
missing |= to_numpy(self._absorb_inter.cat.isnull().any(1))
missing |= to_numpy(self._absorb_inter.cont.isnull().any(1))
for interact in self._interaction_list:
missing |= to_numpy(interact.isnull)
if npany(missing):
self.dependent.drop(missing)
self.exog.drop(missing)
self._absorb_inter.drop(missing)
for interact in self._interaction_list:
interact.drop(missing)
missing_warning(missing)
return missing
def _post_estimation(self, params: ndarray, cov_estimator, cov_type: str):
columns = self._columns
index = self._index
eps = self.resids(params)
fitted = DataFrame(self._dependent.ndarray - eps, index=self._dependent.rows,
columns=['fitted_values'])
absorbed_effects = DataFrame(to_numpy(self._absorbed_dependent) - to_numpy(fitted),
columns=['absorbed_effects'], index=self._dependent.rows)
weps = self.wresids(params)
cov = cov_estimator.cov
debiased = cov_estimator.debiased
residual_ss = (weps.T @ weps)[0, 0]
w = self.weights.ndarray
root_w = sqrt(w)
e = self._dependent.ndarray * root_w
if self.has_constant:
e = e - root_w * average(self._dependent.ndarray, weights=w)
total_ss = float(e.T @ e)
r2 = max(1 - residual_ss / total_ss, 0.0)