linearmodels.panel.model.PanelOLS.fit

PanelOLS.fit(*, use_lsdv: bool = False, use_lsmr: bool = False, low_memory: bool | None = None, cov_type: str = 'unadjusted', debiased: bool = True, auto_df: bool = True, count_effects: bool = True, **cov_config: bool | float | str | ndarray[tuple[int, ...], dtype[int64]] | DataFrame | PanelData) → PanelEffectsResults[source]

Estimate model parameters.

Parameters:
use_lsdv: bool = False

Flag indicating to use the Least Squares Dummy Variable estimator to eliminate effects. The default is False, which uses only means and does not require constructing dummy variables for each effect.

use_lsmr: bool = False

Flag indicating to use LSDV with the Sparse Equations and Least Squares estimator to eliminate the fixed effects. It takes precedence over use_lsdv. The default is False.

low_memory: bool | None = None

Flag indicating whether to use a low-memory algorithm when a model contains two-way fixed effects. If not provided, the choice is taken automatically, and the low memory algorithm is used if the required dummy variable array is both larger than the array of regressors in the model and requires more than 1 GiB.

cov_type: str = 'unadjusted'

Name of covariance estimator. The default is “unadjusted”. The names are not case sensitive. Supported covariance estimators are (see Notes):

  • ”unadjusted”, “conventional”, “homoskedastic” - Homoskedastic estimator

  • ”robust”, “heteroskedastic” - Heteroskedastic estimator

  • ”clustered” - Clustered estimator

  • ”kernel”, “driscoll-kraay”, “dk” - Driscoll-Kraay estimator

  • ”ac”, “autocorrelated” - Autocorrelation robust estimator

debiased: bool = True

Flag indicating whether to debias the covariance estimator using a degree of freedom adjustment. The default is True. The estimated effects are counted in the degrees of freedom whether or not debiased is True. See Notes.

auto_df: bool = True

Flag indicating that the treatment of estimated effects in degree of freedom adjustment is automatically handled. This is useful since clustered standard errors that are clustered using the same variable as an effect do not require degree of freedom correction while other estimators such as the unadjusted covariance do. With two cluster variables, the effect has to be nested in both of them, which means that each of its groups is in one group of each variable. The default is True.

count_effects: bool = True

Flag indicating that the covariance estimator should be adjusted to account for the estimation of effects in the model. Only used if auto_df=False. The default is True.

**cov_config: bool | float | str | ndarray[tuple[int, ...], dtype[int64]] | DataFrame | PanelData

Additional covariance-specific options. The options that each covariance estimator supports are listed in Notes. A key that the estimator does not support raises a ValueError.

Returns:

Estimation results.

Return type:

linearmodels.panel.results.PanelEffectsResults

Raises:
  • UnknownOptionError – If cov_type is not one of the supported names, or if it is “kernel” or “ac” and kernel is not one of the supported kernels. It is both a ValueError and a KeyError.

  • AbsorbingEffectError – If the effects absorb one or more variables of exog, or make them collinear, and drop_absorbed is False in the model.

  • ValueError – If a key of cov_config is not supported by the covariance estimator (this is checked before the model is estimated), if clusters does not have the shape of the data, if clusters has a missing value for an observation that the model uses, if bandwidth is not below the number of time periods, if the model has no residual degrees of freedom, or if all of the variables of exog are absorbed.

  • TypeError – If the value of a key of cov_config does not have the type that the key needs, e.g., if kernel is not a str.

Warns:
  • AbsorbingEffectWarning – If variables are dropped since they are absorbed by the effects, which happens if drop_absorbed is True in the model.

  • MemoryWarning – If low_memory is not provided and the low-memory algorithm is used for a model with entity and time effects.

Notes

Five covariance estimators are supported:

  • “unadjusted”, “conventional”, “homoskedastic” - Assume the residuals are homoskedastic

  • “robust”, “heteroskedastic” - Control for heteroskedasticity using White’s estimator

  • “clustered” - One- or two-way clustering. The configuration options are:

    • clusters - Input containing 1 or 2 variables. This can be a pandas object or PanelData with the index of dependent, a 1-d array or list with a value for each row of dependent in the order of its rows, or an array or list with the layout of dependent (time by entity, or variable by time by entity). An array or list with the layout, or a DataFrame with the time periods as its index and the entities as its columns, has a value for each time period and entity. It is placed on the rows of dependent, which need not be balanced: the values of the combinations that dependent does not have are not used. See Aligning Inputs. Clusters should be integer valued, although other types will be coerced to integer values by treating as categorical variables.

    • cluster_entity - Boolean flag indicating to use entity clusters. The default is False.

    • cluster_time - Boolean flag indicating to use time clusters. The default is False.

    • group_debias - Boolean flag indicating whether to apply an adjustment for the number of clusters. It multiplies the covariance by g / (g - 1) * (n - 1) / n, where g is the number of distinct clusters and n is the number of observations. With two-way clustering, it is applied to each of the three one-way covariances that are combined. The default is False.

  • “kernel”, “driscoll-kraay”, “dk” - Driscoll-Kraay HAC estimator. The configuration options are:

    • kernel - One of the supported kernels: “bartlett”, “newey-west”, “parzen”, “gallant”, “qs”, “quadratic-spectral” or “andrews”. The names are not case sensitive. The default is “newey-west”, which is Bartlett’s kernel.

    • bandwidth - Bandwidth to use when computing the kernel. If not provided, a naive default is used. It must be below the number of time periods, and a ValueError is raised otherwise.

  • “ac”, “autocorrelated” - Autocorrelation robust estimator, which allows for autocorrelation of the errors of an entity and not for correlation between entities. The configuration options are kernel and bandwidth, as for “kernel”. The lags of an entity are the positions of its observations, not calendar periods, and the bandwidth can be larger than the number of observations of an entity. See ACCovariance.

debiased=True, which is the default, scales the covariance by n / (n - k), where k is the number of regressors, and uses the t and F distributions for inference.

debiased=True and group_debias=True give the clustered covariance of Stata’s xtreg, fe vce(cluster ...). See Small-sample Adjustments.

PanelOLS counts the effects that it estimates in the degrees of freedom, which is extra_df of the covariance estimator. If d is the number of effects, the covariance is scaled by n / (n - d - k) if debiased is True and by n / (n - d) if it is False. If auto_df is True, the effects are not counted when the model has a single effect that is nested in the clusters. Use auto_df=False and count_effects to choose. Since extra_df is set from the effects, it is not a keyword argument of fit.

Changed in version 7.1: extra_df raises a ValueError at the start of fit. It raised a TypeError after the parameters were estimated.

An effect is nested in a cluster variable if each of its groups is in one group of the variable, for example entity effects with cluster_entity=True, or entity effects with clusters that are the industries of the entities. It is nested in the clusters if it is nested in every one of the cluster variables, so that the covariance does not depend on the order of the variables. Entity effects are nested in the two-way clusters (entity, industry) if every entity is in one industry, and they are not nested in the clusters (entity, time), as an entity has observations in several periods. They are counted in the degrees of freedom then. The effects are always counted if the model has more than one effect or the covariance is not clustered. See Small-sample Adjustments.

Changed in version 7.1: With two cluster variables the effect has to be nested in every one of them. Earlier versions kept the result for the last variable only, so that the standard errors depended on the order of the variables, and the effects were not counted if they were nested in the last variable and not in the first.

Changed in version 7.1: A kernel that is not one of the supported names raises an UnknownOptionError when the model is fit, for “kernel” and “ac”. Earlier versions raised it when the covariance was first used, for example when std_errors of the results was read, so that fit returned results that raised when the covariance was used.

Changed in version 7.1: clusters with the layout of dependent (time by entity, or variable by time by entity) can be used if dependent is not balanced. It raised IndexError: Boolean index has wrong length. It is placed on the rows of dependent by time period and entity. Earlier versions paired it by position with the rows, which is right only if the rows are sorted by entity and then time: if they were sorted by time and then entity, an array was attached to the wrong observations without an error, and a pandas object with labels was rejected.

Changed in version 7.1: A ValueError is raised if clusters has a missing value for an observation that the model uses, as in the IV and system models. Earlier versions made the observations with a missing value a cluster of their own, so that the standard errors changed without an error. The clusters of the observations that are dropped because a value of dependent, exog, weights or other_effects is missing are not used, and can be missing.

Examples

>>> from linearmodels import PanelOLS
>>> from linearmodels.datasets import wage_panel
>>> data = wage_panel.load().set_index(["nr", "year"])
>>> y = data["lwage"]
>>> x = data[["married", "union"]]
>>> mod = PanelOLS(y, x, entity_effects=True)
>>> res = mod.fit(cov_type="clustered", cluster_entity=True)