Skip to content

Commit

Permalink
BUG/ENH: fix RLM start_params closes #9233. enable RLMDetS for 0 or 1…
Browse files Browse the repository at this point in the history
… start_exog
  • Loading branch information
josef-pkt committed Apr 26, 2024
1 parent c79c89d commit a3e739a
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 @@ -1288,6 +1288,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)

Check warning on line 1289 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1287-L1289

Added lines #L1287 - L1289 were not covered by tests

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

Check warning on line 1300 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1293-L1300

Added lines #L1293 - L1300 were not covered by tests

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

Check warning on line 1303 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1303

Added line #L1303 was not covered by tests

idx_all = []

Check warning on line 1305 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1305

Added line #L1305 was not covered by tests
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

Check warning on line 40 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L40

Added line #L40 was not covered by tests

# TODO: detect constant
if col_indices is None:
exog_start = self.exog[:, 1:]

Check warning on line 44 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L44

Added line #L44 was not covered by tests
else:
Expand All @@ -51,6 +52,13 @@ def __init__(self, endog, exog, norm=None, breakdown_point=0.5,

def _get_start_params(self, h):

Check warning on line 53 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L53

Added line #L53 was not covered by tests
# 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])

Check warning on line 57 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L57

Added line #L57 was not covered by tests
start_params_all = [np.atleast_1d([q]) for q in quantiles]
return start_params_all

Check warning on line 59 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L59

Added line #L59 was not covered by tests


starts = _get_detcov_startidx(

Check warning on line 62 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L62

Added line #L62 was not covered by tests
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 a3e739a

Please sign in to comment.