statsmodels.robust.robust_linear_model.RLM.fit#

RLM.fit(maxiter=50, tol=1e-08, scale_est='mad', cov='H1', update_scale=True, conv='dev', start_params=None, start_scale=None)[source]#

Fit the model using iteratively reweighted least squares

The IRLS routine runs until the specified objective converges to tol or maxiter has been reached.

Parameters:
conv{“coefs”, “dev”, “sresid”, “weights”}, optional

Indicates the convergence criteria. Available options are “coefs” (the coefficients), “weights” (the weights in the iteration), “sresid” (the standardized residuals), and “dev” (the un-normalized log-likelihood for the M estimator). The default is “dev”.

cov{“H1”, “H2”, “H3”}, optional

Indicates how the covariance matrix is estimated. Default is ‘H1’. See rlm.RLMResults for more information.

maxiterint, optional

The maximum number of iterations to try. Default is 50.

scale_est{“mad”}, HuberScale or callable, optional

Indicates the estimate to use for scaling the weights in the IRLS. The default is ‘mad’ (median absolute deviation). Other options are ‘HuberScale’ for Huber’s proposal 2. Huber’s proposal 2 has optional keyword arguments d, tol, and maxiter for specifying the tuning constant, the convergence tolerance, and the maximum number of iterations. Custom callables can accept either resid or (model, resid) and must return the scale estimate. The model object provides useful afftributes like nobs and df_reside that may be needed in scale estimation. Due to backward compatability issues, single- argument callables use the same degrees-of-freedom correction as the built-in non-Huber scale estimators (nobs/df_resid). Scale estimates from two argument functions are used without modification. See statsmodels.robust.scale for more information or the examples below.

tolfloat, optional

The convergence tolerance of the estimate. Default is 1e-8.

update_scalebool, optional

If update_scale is False then the scale estimate for the weights is held constant over the iteration. Otherwise, it is updated for each fit in the iteration. Default is True.

start_paramsarray_like, optional

Initial guess of the solution of the optimizer. If not provided, the initial parameters are computed using OLS.

start_scalefloat, optional

Initial scale. If update_scale is False, then the scale will be fixed at this level for the estimation of the mean parameters. during iteration. If not provided, then the initial scale is estimated from the OLS residuals

Returns:
resultsstatsmodels.robust.robust_linear_model.RLMResults

Results instance

Examples

>>> import statsmodels.api as sm
>>> data = sm.datasets.stackloss.load()
>>> data.exog = sm.add_constant(data.exog)
>>> rlm_model = sm.RLM(data.endog, data.exog, M=sm.robust.norms.HuberT())
>>> rlm_results_mad = rlm_model.fit(scale_est="mad")
>>> from statsmodels.robust import HuberScale
>>> hs = HuberScale(d=1.345, tol=1e-6, maxiter=100)
>>> rlm_results_mad = rlm_model.fit(scale_est=hs)

Next we use a custom callable to estimate the scale. The callable can either accept a single argument (the residuals) or two arguments (the model and the residuals). In this example, we use a callable that computes the average absolute deviation of the residuals. Note that the scale estimate from this one-input callable is adjusted for degrees of freedom, so it will be different than the two-input callable version below.

>>> import numpy as np
>>> def avg_abs_deviation(resid):
...     median = np.median(resid)
...     c = np.sqrt(np.pi / 2)
...     return c * np.mean(np.abs(resid - median))
>>> rlm_results_custom = rlm_model.fit(scale_est=avg_abs_deviation)
>>> print(f"{rlm_results_custom.scale:.4f}")
3.0997

This second version accepts two versions. While the function value is the same it will behave differently in practice since the scale estimate is not adjusted for degrees of freedom.

>>> def avg_abs_deviation_with_model(model, resid):
...     median = np.median(resid)
...     c = np.sqrt(np.pi / 2)
...     return c * np.mean(np.abs(resid - median))
>>> rlm_results_custom_2 = rlm_model.fit(scale_est=avg_abs_deviation_with_model)
>>> print(f"{rlm_results_custom_2.scale:.4f}")
2.7686