linearmodels

repository·main·Indexed 21 days ago

https://github.com/bashtage/linearmodels

A Python library extending statsmodels with advanced linear regression capabilities. It provides tools for Panel models (PanelOLS), Instrumental Variable (IV) estimators (IV2SLS, IVLIML, IVGMM, IVGMMCUE), System regression (SUR), and Asset Pricing models (TradedFactorModel, LinearFactorModel, LinearFactorModelGMM). The library is designed for seamless integration with NumPy, Pandas, and xarray, and supports high-dimensional regression via AbsorbingLS.

Tokens
25.1K
Snippets
96
Records
131
Agent score
77%

What's inside linearmodels

  1. Overview of linearmodels capabilities

    main
    The linearmodels library provides estimation and inference for several linear model types that are not available in statsmodels. It is primarily used for panel data analysis, high-dimensional regression, instrumental variables, system regression, and asset pricing models.
  2. Explore Panel Data Model Estimation

    main

    The linearmodels panel module provides various estimators for panel data analysis. To get started, you can explore the following resources:

    • Estimators: Refer to the module reference for a complete list of available estimation methods.
    • Results: After estimation, the returned results classes provide model specification tests and covariance estimates.
    • Basic Usage: Use the Basic Examples (based on Wooldridge's textbooks) to see standard model implementations.
    • Formula Specification: Models can be specified using R-like formulas via the formulaic library.
    • Data Formats: The module supports multiple data formats for model specification.
    • Mathematical Foundation: Documentation is available regarding the mathematical notation used for parameter estimation, covariance, and hypothesis testing.
  3. Explore Instrumental Variable (IV) Estimation in linearmodels

    main

    The linearmodels package provides a suite of Instrumental Variable (IV) estimators. To use IV estimation, you can explore the following resources:

    • Estimators: Refer to the module reference for a complete list of available estimation methods.
    • Results: After estimation, the package returns results classes that include model specification tests and parameter estimates.
    • Mathematical Foundation: The package provides documentation on the specific formulas used for parameter estimation, covariance estimation, and hypothesis testing.
    • Learning via Examples:
      • Use Basic Examples (based on Wooldridge) for introductory usage.
      • Use Advanced Examples (based on Cameron and Trivedi) for more complex scenarios.
      • See using-formulas.ipynb for direct formula application.
      • See absorbing-regression.ipynb for absorbing regression techniques.
  4. Choose the appropriate linear factor asset pricing estimator

    main

    The linearmodels.asset_pricing module provides three estimators for linear factor asset pricing models, depending on the nature of your factors:

    1. TradedFactorModel: Use this when all factors are traded assets. The model is estimated using regressions as a Seemingly Unrelated Regression (SUR).
    2. LinearFactorModel: A general-purpose 2-step estimator suitable for both traded and non-traded factors. The first step estimates factor loadings, and the second step estimates risk premia.
    3. LinearFactorModelGMM: A version of the 2-step model using Generalized Method of Moments (GMM). This is generally more efficient and is the preferred choice. You can enable the continuously updating version by setting use_cue=True during the .fit() call.

    All estimators support standard heteroskedasticity robust inference by default, as well as kernel-based HAC (Heteroskedasticity and Autocorrelation Consistent) estimators using the Bartlett (Newey-West), Parzen, or Quadratic-Spectral kernels.

  5. Using NumPy 2 with linearmodels v6.1

    main

    Version 6.1 provides compatibility with NumPy 2.0. Wheels are built using NumPy 2.0.0rc1 (or later) and are compatible with any NumPy version from 1.22.3 up to and including NumPy 2.0.0.

    Important: To use NumPy 2, your entire environment must consist of packages that have been built against NumPy 2.0.0rc1 or later to ensure binary compatibility.

  6. Understand R-squared (rsquared) definitions in linearmodels

    main

    The reported rsquared in linearmodels results represents the $R^2$ from the actual model fit after adjusting the data for specific transformations. This differs from Stata, which uses a correlation-based measure that may ignore the estimated intercept or allow for affine adjustments.

    Depending on the estimator used, the rsquared value accounts for:

    • Weights: All estimators.
    • Effects: PanelOLS.
    • Re-centering: RandomEffects.
    • Within entity aggregation: BetweenOLS.
    • Differencing: FirstDifferenceOLS.
  7. How SUR and 3SLS differ in system models

    main

    The linearmodels.system module provides two primary estimators for system regression:

    1. SUR (Seemingly Unrelated Regression): Used when all variables are exogenous. It is a special case of 3SLS.
    2. IV3SLS (Three-stage Least Squares): A generalization of SUR that allows for endogenous variables and the use of instruments. It enables the joint estimation of systems of Instrumental Variable (IV) equations.

    Use SUR for simple simultaneous regressions and IV3SLS when your system includes endogenous variables that require instrumentation.

  8. Understand Panel Data Models in linearmodels

    main

    Panel data consists of observations on multiple entities (e.g., individuals, firms, countries) over multiple time periods. The models in linearmodels.panel are designed to estimate parameters while addressing entity-specific components ($\alpha_i$).

    All panel models require two primary inputs:

    • dependent: The variable to be modeled ($y_{it}$).
    • exog: The regressors ($x_{it}$).

    Available estimators include:

    • PanelOLS: Uses fixed effects (entity effects) to eliminate entity-specific components. It is more general and can model multiple effects (e.g., both entity and time effects).
    • BetweenOLS: Regresses time-averaged values using OLS.
    • FirstDifferenceOLS: Uses first differences to eliminate entity-specific effects.
    • RandomEffects: Efficiently estimates parameters when the entity effect is independent of regressors (but inconsistent if they are dependent).
    • PooledOLS: Ignores entity effects; consistent but inefficient if the effect is independent of regressors.
  9. Use Instrumental Variable (IV) models for endogenous regressors

    main

    Instrumental variable models are used when regressors are endogenous (correlated with the model error) or when there is measurement error. These models use instruments that are correlated with the endogenous variable but not with the model error.

    All IV models in linearmodels require four primary inputs:

    • dependent: The variable to be modeled ($y_i$).
    • exog: The exogenous regressors ($x_{1i}$). Note that exogenous variables can also be used as instruments for projecting endogenous variables.
    • endog: The endogenous regressors ($x_{2i}$).
    • instruments: The instruments ($z_{2i}$).

    To perform a standard Two-Stage Least Squares (2SLS) estimation, pass these variables to the IV2SLS class and call .fit() on the model instance.

    import pandas as pd
    import numpy as np
    import statsmodels.api as sm
    from linearmodels.iv import IV2SLS
    from linearmodels.datasets import wage
    
    data = wage.load()
    dependent = np.log(data.wage)
    exog = sm.add_constant(data.exper)
    endog = data.educ
    instruments = data.sibs
    
    mod = IV2SLS(dependent, exog, endog, instruments)
    res = mod.fit(cov_type='unadjusted')
    print(res)
  10. Estimate a linear factor asset pricing model

    main

    To estimate a model, you need two primary inputs:

    • portfolios: A $T imes P$ array of portfolio returns.
    • factors: A $T imes K$ array of factor returns or shocks.

    Note: Portfolios should be transformed into excess returns (subtracting the risk-free rate) prior to estimation.

    Example using LinearFactorModel with kernel-based covariance:

    from linearmodels.datasets import french
    from linearmodels.asset_pricing import LinearFactorModel
    
    # Load data
    data = french.load()
    
    # Prepare factors (e.g., Market, Size, Value)
    factors = data[['MktRF', 'SMB', 'HML']]
    
    # Prepare portfolios and transform to excess returns
    portfolios = data[['S1V1','S1V3','S1V5','S5V1','S5V3','S5V5']].copy()
    portfolios.loc[:,:] = portfolios.values - data[['RF']].values
    
    # Initialize and fit the model
    mod = LinearFactorModel(portfolios, factors)
    res = mod.fit(cov_type='kernel')
    print(res)
    from linearmodels.datasets import french
    data = french.load()
    factors = data[['MktRF', 'SMB', 'HML']]
    portfolios = data[['S1V1','S1V3','S1V5','S5V1','S5V3','S5V5']].copy()
    portfolios.loc[:,:] = portfolios.values - data[['RF']].values
    from linearmodels.asset_pricing import LinearFactorModel
    mod = LinearFactorModel(portfolios, factors)
    res = mod.fit(cov_type='kernel')
    print(res)