Skip to content

Commit

Permalink
BUG/ENH: fix RLM start_params closes statsmodels#9233. enable RLMDetS…
Browse files Browse the repository at this point in the history
… for 0 or 1 start_exog
  • Loading branch information
josef-pkt committed May 15, 2024
1 parent f529a4c commit 4520796
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions statsmodels/robust/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,18 @@ def _get_detcov_startidx(z, h, options_start=None, methods_cov="all"):
scale_func = options_start.get("scale_func", mad)
z = (z - loc_func(z)) / scale_func(z)

if np.squeeze(z).ndim == 1:
# only one random variable
z = np.squeeze(z)
nobs = z.shape[0]
idx_sel = np.argpartition(np.abs(z), h)[:h]
idx_all = [(idx_sel, "abs-resid")]
idx_sorted = np.argsort(z)
h_tail = (nobs - h) // 2
idx_all.append((idx_sorted[h_tail : h_tail + h], "trimmed-tail"))
return idx_all

# continue if more than 1 random variable
cov_all = _cov_starting(z, standardize=False, quantile=0.5)

idx_all = []
Expand Down
8 changes: 8 additions & 0 deletions statsmodels/robust/resistant_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, endog, exog, norm=None, breakdown_point=0.5,
# data for robust mahalanobis distance of starting sets
self.breakdown_point = breakdown_point

# TODO: detect constant
if col_indices is None:
exog_start = self.exog[:, 1:]
else:
Expand All @@ -51,6 +52,13 @@ def __init__(self, endog, exog, norm=None, breakdown_point=0.5,

def _get_start_params(self, h):
# I think we should use iterator with yield

if self.data_start.shape[1] == 0 and self.exog.shape[1] == 1:
quantiles = np.quantile(self.endog, [0.25, 0.5, 0.75])
start_params_all = [np.atleast_1d([q]) for q in quantiles]
return start_params_all


starts = _get_detcov_startidx(
self.data_start, h, options_start=None, methods_cov="all")

Expand Down
1 change: 1 addition & 0 deletions statsmodels/robust/robust_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def fit(self, maxiter=50, tol=1e-8, scale_est='mad', init=None, cov='H1',
wls_results = lm.WLS(self.endog, self.exog).fit()
else:
start_params = np.asarray(start_params, dtype=np.double).squeeze()
start_params = np.atleast_1d(start_params)
if (start_params.shape[0] != self.exog.shape[1] or
start_params.ndim != 1):
raise ValueError('start_params must by a 1-d array with {} '
Expand Down

0 comments on commit 4520796

Please sign in to comment.