Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions statsmodels/discrete/discrete_margins.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def _margeff_cov_params_dummy(model, cov_margins, params, exog, dummy_ind,
K = dfdb.shape[1] // (J-1)
cov_margins[i::K, :] = dfdb
else:
cov_margins[i, :] = dfdb # how each F changes with change in B
# dfdb could be too short if there are extra params, k_extra > 0
cov_margins[i, :len(dfdb)] = dfdb # how each F changes with change in B
return cov_margins

def _margeff_cov_params_count(model, cov_margins, params, exog, count_ind,
Expand Down Expand Up @@ -247,7 +248,8 @@ def _margeff_cov_params_count(model, cov_margins, params, exog, count_ind,
K = dfdb.shape[1] / (J-1)
cov_margins[i::K, :] = dfdb
else:
cov_margins[i, :] = dfdb # how each F changes with change in B
# dfdb could be too short if there are extra params, k_extra > 0
cov_margins[i, :len(dfdb)] = dfdb # how each F changes with change in B
return cov_margins

def margeff_cov_params(model, params, exog, cov_params, at, derivative,
Expand Down Expand Up @@ -457,6 +459,9 @@ def summary_frame(self, alpha=.05):
'Conf. Int. Low', 'Cont. Int. Hi.']
ind = self.results.model.exog.var(0) != 0 # True if not a constant
exog_names = self.results.model.exog_names
k_extra = getattr(model, 'k_extra', 0)
if k_extra > 0:
exog_names = exog_names[:-k_extra]
var_names = [name for i,name in enumerate(exog_names) if ind[i]]

if self.margeff.ndim == 2:
Expand Down Expand Up @@ -541,6 +546,8 @@ def summary(self, alpha=.05):
_, const_idx = _get_const_index(model.exog)
if const_idx is not None:
exog_names.pop(const_idx[0])
if getattr(model, 'k_extra', 0) > 0:
exog_names = exog_names[:-model.k_extra]

J = int(getattr(model, "J", 1))
if J > 1:
Expand Down Expand Up @@ -661,6 +668,8 @@ def get_margeff(self, at='overall', method='dydx', atexog=None,
params = results.params
exog = model.exog.copy() # copy because values are changed
effects_idx, const_idx = _get_const_index(exog)
if hasattr(model, 'k_extra') and model.k_extra > 0:
effects_idx = np.concatenate((effects_idx, np.zeros(model.k_extra, np.bool_)))

if dummy:
_check_discrete_args(at, method)
Expand All @@ -674,6 +683,10 @@ def get_margeff(self, at='overall', method='dydx', atexog=None,
else:
count_idx = None

# attach dummy_idx and cout_idx
self.dummy_idx = dummy_idx
self.count_idx = count_idx

# get the exogenous variables
exog = _get_margeff_exog(exog, at, atexog, effects_idx)

Expand Down Expand Up @@ -710,6 +723,9 @@ def get_margeff(self, at='overall', method='dydx', atexog=None,
self.margeff_cov = margeff_cov[effects_idx][:, effects_idx]
else:
# don't care about at constant
# hack truncate effects_idx again if necessary
# if eyex, then effects is truncated to be without extra params
effects_idx = effects_idx[:len(effects)]
self.margeff_cov = margeff_cov[effects_idx][:, effects_idx]
self.margeff_se = margeff_se[effects_idx]
self.margeff = effects[effects_idx]
4 changes: 3 additions & 1 deletion statsmodels/discrete/discrete_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,9 @@ def _derivative_exog(self, params, exog=None, transform="dydx",
# group 3 poisson, nbreg, zip, zinb
if exog is None:
exog = self.exog
margeff = self.predict(params, exog)[:,None] * params[None,:]
k_extra = getattr(self, 'k_extra', 0)
params_exog = params if k_extra == 0 else params[:-k_extra]
margeff = self.predict(params, exog)[:,None] * params_exog[None,:]
if 'ex' in transform:
margeff *= exog
if 'ey' in transform:
Expand Down
Loading