Skip to main content

Linear Models

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)

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

Release files for linearmodels 4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for linearmodels 4.1
File Size Uploaded
linearmodels-4.1.tar.gz 1.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for linearmodels 4.1
File Interpreter ABI Platform
linearmodels-4.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 2.3 MB

Release files / linearmodels-4.1.tar.gz

Download URL linearmodels-4.1.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
1412d508ab850585769772bc4a4cbbb205abb493c2bc7580d7f8c7dc88f5c7c6
BLAKE2b-256 checksum
How to use checksums
35f098a9f07c9ed242c18461e7c48206b744772a383314774e52ca370d723828
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / linearmodels-4.1-py2.py3-none-any.whl

Download URL linearmodels-4.1-py2.py3-none-any.whl
Size 740.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8ad650fe4b428247ecfadab78d01ce232d5111ba73468fbda384474b89eb314f
BLAKE2b-256 checksum
How to use checksums
bd68fba57074df6545433d498d1da036658db136db61c5044be7dc7cfa3f1de1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

7.0

31 release files

6.1

30 release files

6.0

25 release files

5.4

25 release files

5.3

29 release files

5.2

17 release files

5.1

17 release files

5.0

11 release files

4.31

23 release files

4.30

23 release files

4.29

23 release files

4.28

23 release files

4.27

18 release files

4.26

18 release files

4.25

23 release files

4.24

16 release files

4.23

16 release files

4.22

16 release files

4.21

16 release files

4.20

16 release files

4.19

16 release files

4.18

7 release files

4.17

16 release files

4.16

16 release files

4.15.2

1 release file

4.15

3 release files

4.14

2 release files

4.13

1 release file

4.12

1 release file

4.11

1 release file

4.10

2 release files

4.9

2 release files

4.8

2 release files

4.7

2 release files

4.6

2 release files

4.5

2 release files

4.2

2 release files

This release

4.1 This release

2 release files

3.5

2 release files

3.0

1 release file

2.0

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page