linearmodels.iv.absorbing.AbsorbingLS¶
-
class linearmodels.iv.absorbing.AbsorbingLS(dependent: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...], exog: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None, *, absorb: DataFrame | Interaction | None =None, interactions: DataFrame | Interaction | Iterable[DataFrame | Interaction] | None =None, weights: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =None, drop_absorbed: bool =False)[source]¶ Linear regression with high-dimensional effects.
- Parameters:¶
- dependent: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...]¶
Dependent variable (nobs by 1).
- exog: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None¶ Exogenous regressors (nobs by nexog).
- absorb: DataFrame | Interaction | None =
None¶ The effects or continuous variables to absorb. When using a DataFrame, effects must be categorical variables. Other variable types are treated as continuous variables that should be absorbed, so they must be numeric, and a
TypeErroris raised for columns of strings, which have to be converted to categorical. When using an Interaction, variables in the cat argument are treated as effects and variables in the cont argument are treated as continuous.- interactions: DataFrame | Interaction | Iterable[DataFrame | Interaction] | None =
None¶ Interactions containing both categorical and continuous variables. Each interaction is constructed using the Cartesian product of the categorical variables to produce the dummy, which are then separately interacted with each continuous variable. The columns of a DataFrame follow the rules of
absorb.- weights: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None¶ Observation weights used in estimation. The weights must be strictly positive and finite. They are rescaled to have a mean of 1. A NaN weight is missing, and its observation is dropped. The default is to weight all observations equally.
- drop_absorbed: bool =
False¶ Flag indicating whether to drop absorbed variables. The default is False.
- Raises:¶
TypeError – If
absorborinteractionsis not a DataFrame or an Interaction (a list of them forinteractions), or if a column that is not categorical is not numeric.ValueError – If
dependent,exog,absorb,interactionsandweightshave different numbers of observations or labels that do not match (see Notes), if all observations have a missing value, or ifweightsare not strictly positive and finite.
See also
InteractionSpecify interactions between categorical and continuous variables.
linearmodels.iv.model.IVLIMLLimited information ML and k-class estimation of IV models.
linearmodels.iv.model.IV2SLSEstimation of IV models using two-stage least squares.
scipy.sparse.linalg.lsmrIterative solver for sparse least squares problems.
Notes
Capable of estimating models with millions of effects. The model is not a panel estimator. See Absorbing Least Squares for when to use it and when to use
PanelOLS.Estimates models of the form
\[y_i = x_i \beta + z_i \gamma + \epsilon_i\]where \(\beta\) are parameters of interest and \(\gamma\) are not. z may be high-dimensional, although must have fewer variables than the number of observations in y.
The syntax simplifies specifying high-dimensional z when z consists of categorical (factor) variables, also known as effects, or when z contains interactions between continuous variables and categorical variables, also known as fixed slopes.
The high-dimensional effects are fit using LSMR which avoids inverting or even constructing the inner product of the regressors. This is combined with Frish-Waugh-Lovell to orthogonalize x and y from z.
z can contain factors that are perfectly linearly dependent. LSMR estimates a particular restricted set of parameters that captures the effect of non-redundant components in z.
A variable in
exogis absorbed if the effects explain all of it. The algorithms that absorb the effects stop with a relative error of about 1e-8, so a variable is treated as absorbed if less than 1e-5 of its norm is left after the effects are removed, whatever its units. It is treated in the same way if it is collinear with the variables before it once the effects are removed. AnAbsorbingEffectErroris raised for such a variable. Ifdrop_absorbedis True, the variable is dropped and anAbsorbingEffectWarningis raised, and the constant is never dropped. The norm includes the mean of the variable, so a variable with a large mean and little other variation, less than 1e-5 of its norm, is also treated as absorbed. Subtract its mean to avoid this.Rows are paired by position. An input that carries labels (a pandas Series or DataFrame with an index other than the default 0, 1, …, n-1, or an xarray DataArray with coordinates) must use the same index, in the same order, as
dependent, so that a value cannot be attached to the wrong observation. AValueErroris raised if the index ofexog,absorb,interactionsorweightsdiffers from the index ofdependent. Either reindex the input to the index ofdependent, or callto_numpy()to pair its values by position. NumPy arrays and pandas objects with a default index do not carry labels and are always paired by position. Ifdependentdoes not carry labels, the inputs that do must use the same index as the first ofexog,absorb,interactionsandweightsthat does. The same rule applies toclusterspassed tofit. Dates and time deltas are compared by value, so that the same dates at different resolutions, e.g.,datetime64[us]anddatetime64[ns], are the same labels.Examples
Estimate a model by absorbing 2 categoricals and 2 continuous variables
>>> import numpy as np >>> import pandas as pd >>> from linearmodels.iv import AbsorbingLS, Interaction >>> dep = np.random.standard_normal((20000,1)) >>> exog = np.random.standard_normal((20000,2)) >>> cats = pd.DataFrame({i: pd.Categorical(np.random.randint(1000, size=20000)) ... for i in range(2)}) >>> cont = pd.DataFrame({i+2: np.random.standard_normal(20000) for i in range(2)}) >>> absorb = pd.concat([cats, cont], axis=1) >>> mod = AbsorbingLS(dep, exog, absorb=absorb) >>> res = mod.fit()Add interactions between the cartesian product of the categorical and each continuous variables
>>> iaction = Interaction(cat=cats, cont=cont) >>> absorb = Interaction(cat=cats) # Other encoding of categoricals >>> mod = AbsorbingLS(dep, exog, absorb=absorb, interactions=iaction)Methods
fit(*[, cov_type, debiased, method, ...])Estimate model parameters.
resids(params)Compute model residuals.
wresids(params)Compute weighted model residuals.
Properties
Dependent variable with effects absorbed.
Exogenous variables with effects absorbed.
Return the dependent variable.
Return the exogenous regressors.
Flag indicating whether the model has a constant or equivalent.
Return the instruments, of which there are none.
Return the weights, which have a mean of 1.