Skip to main content

Instrumental Variable and Linear Panel models for Python

Project description

Build Status codecov

Linear (regression) models for Python. Extends statsmodels with Panel regression, instrumental variable estimators, system estimators and models for estimating asset prices:

  • Panel models:

    • Fixed effects (maximum two-way)

    • First difference regression

    • Between estimator for panel data

    • Pooled regression for panel data

    • Fama-MacBeth estimation of panel models

  • Instrumental Variable estimators

    • Two-stage Least Squares

    • Limited Information Maximum Likelihood

    • k-class Estimators

    • Generalized Method of Moments, also with continuously updating

  • Factor Asset Pricing Models:

    • 2- and 3-step estimation

    • Time-series estimation

    • GMM estimation

  • System Regression:

    • Seemingly Unrelated Regression (SUR/SURE)

    • Three-Stage Least Squares (3SLS)

    • Generalized Method of Moments (GMM) System Estimation

Designed to work equally well with NumPy, Pandas or xarray data.

Panel models

Like statsmodels to include, supports patsy formulas for specifying models. For example, the classic Grunfeld regression can be specified

import numpy as np
from statsmodels.datasets import grunfeld
data = grunfeld.load_pandas().data
data.year = data.year.astype(np.int64)
# MultiIndex, entity - time
data = data.set_index(['firm','year'])
from linearmodels import PanelOLS
mod = PanelOLS(data.invest, data[['value','capital']], entity_effect=True)
res = mod.fit(cov_type='clustered', cluster_entity=True)

Models can also be specified using the formula interface.

from linearmodels import PanelOLS
mod = PanelOLS.from_formula('invest ~ value + capital + EntityEffect', data)
res = mod.fit(cov_type='clustered', cluster_entity=True)

The formula interface for PanelOLS supports the special values EntityEffects and TimeEffects which add entity (fixed) and time effects, respectively.

Instrumental Variable Models

IV regression models can be similarly specified.

import numpy as np
from linearmodels.iv import IV2SLS
from linearmodels.datasets import mroz
data = mroz.load()
mod = IV2SLS.from_formula('np.log(wage) ~ 1 + exper + exper ** 2 + [educ ~ motheduc + fatheduc]', data)

The expressions in the [ ] indicate endogenous regressors (before ~) and the instruments.

Installing

The latest release can be installed using pip

pip install linearmodels

The master branch can be installed by cloning the repo and running setup

git clone https://github.com/bashtage/linearmodels
cd linearmodels
python setup.py install

Documentation

Stable Documentation is built on every tagged version using doctr. Development Documentation is automatically built on every successful build of master.

Plan and status

Should eventually add some useful linear model estimators such as panel regression. Currently only the single variable IV estimators are polished.

  • Linear Instrumental variable estimation - complete

  • Linear Panel model estimation - complete

  • Fama-MacBeth regression - complete

  • Linear Factor Asset Pricing - complete

  • System regression - complete

  • Linear IV Panel model estimation - not started

  • Dynamic Panel model estimation - not started

Requirements

Running

With the exception of Python 3.5+, which is a hard requirement, the others are the version that are being used in the test environment. It is possible that older versions work.

  • Python 3.5+: extensive use of @ operator

  • NumPy (1.11+)

  • SciPy (0.18+)

  • pandas (0.19+)

  • statsmodels (0.8+)

  • xarray (0.9+, optional)

Testing

  • py.test

Documentation

  • sphinx

  • guzzle_sphinx_theme

  • nbsphinx

  • nbconvert

  • nbformat

  • ipython

  • jupyter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linearmodels-4.6.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linearmodels-4.6-py2.py3-none-any.whl (907.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file linearmodels-4.6.tar.gz.

File metadata

  • Download URL: linearmodels-4.6.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for linearmodels-4.6.tar.gz
Algorithm Hash digest
SHA256 dfa63e14d53cfc0cfcf1e0e4e34b119437a0bd253c6af7b10f83a7f9112e002b
MD5 6b4c61a7b87c7b937d5d449d08ba9e58
BLAKE2b-256 bbd2f56da6293b9e1108e266c7950136d3993f64a1192d3d564edcc82e49c766

See more details on using hashes here.

File details

Details for the file linearmodels-4.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for linearmodels-4.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5c8ec29e60c703ff6770c28351df8b9b21a24bff704f8c05c6b446d16363a3b8
MD5 cefdb402f8d65b969c7af883e4041e2b
BLAKE2b-256 ee1cb0c5c00fc707cc322ad4914d3cf3cdb643d8bfac00c33cb583ff8122bd4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page