statsmodels.genmod.generalized_linear_model.GLM.fit_regularized#
- GLM.fit_regularized(method='elastic_net', alpha=0.0, start_params=None, refit=False, opt_method='bfgs', **kwargs)[source]#
Return a regularized fit to a linear regression model.
- Parameters:
- method{‘elastic_net’, ‘l1_slsqp’},
optional ‘elastic_net’ uses coordinate descent and supports the full elastic net penalty. ‘l1_slsqp’ solves a smooth constrained reformulation of the L1 problem with slsqp, an interior point style method, and only supports the lasso penalty (L1_wt must be 1).
- alphascalar or array_like,
optional The penalty weight. If a scalar, the same penalty weight applies to all variables in the model. If a vector, it must have the same length as params, and contains a penalty weight for each coefficient. The default is 0, which applies no penalty and gives the unpenalized maximum likelihood estimate.
- start_paramsarray_like,
optional Starting values for params.
- refitbool,
optional If True, the model is refit using only the variables that have non-zero coefficients in the regularized fit. The refitted model is not regularized. The default is False.
- opt_method
str,optional The method used for numerical optimization.
- **kwargs
Additional keyword arguments used when fitting the model.
- method{‘elastic_net’, ‘l1_slsqp’},
- Returns:
GLMResultsAn array or a GLMResults object, same type returned by fit.
Notes
The penalty is the
elastic netpenalty, which is a combination of L1 and L2 penalties.The function that is minimized is:
\[-loglike/n + alpha*((1-L1\_wt)*|params|_2^2/2 + L1\_wt*|params|_1)\]where \(|*|_1\) and \(|*|_2\) are the L1 and L2 norms.
Post-estimation results are based on the same data used to select variables, hence may be subject to overfitting biases.
The elastic_net method uses the following keyword arguments. The defaults given below apply to the coordinate descent algorithm that is used when L1_wt is not 0. If L1_wt is 0, the ridge problem is solved using opt_method instead, and maxiter, cnvrg_tol, zero_tol and refit are ignored.
- maxiterint, optional
Maximum number of iterations (default 50).
- L1_wtfloat, optional
Must be in [0, 1]. The L1 penalty has weight L1_wt and the L2 penalty has weight 1 - L1_wt (default 1).
- cnvrg_tolfloat, optional
Convergence threshold for maximum parameter change after one sweep through all coefficients (default 1e-10).
- zero_tolfloat, optional
Coefficients below this threshold are treated as zero (default 1e-10).
The l1_slsqp method uses the following keyword arguments:
- maxiterint, optional
Maximum number of iterations (default 1000).
- trim_mode{‘auto’, ‘size’, ‘off’}, optional
If not ‘off’, trim (set to zero) parameters that would have been zero if the solver reached the theoretical minimum. If ‘auto’, trim params using the theoretical optimality conditions. If ‘size’, trim params if they have very small absolute value.
- size_trim_tolfloat, optional
Tolerance used when trim_mode is ‘size’.
- auto_trim_tolfloat, optional
Tolerance used when trim_mode is ‘auto’.
- qc_tolfloat, optional
Print warning and do not allow auto trim when the optimality conditions are violated by this much.
- qc_verbosebool, optional
If True, print out a full QC report upon failure.
- accfloat, optional
Requested accuracy as used by slsqp (default 1e-10).