Skip to content

Commit 2905c5f

Browse files
authored
Merge pull request #773 from bashtage/lm71-doc3-narrative-docs
DOC: Describe JIVE, the centered J and the C statistic in the IV math…
2 parents dee2e3f + 6b1a206 commit 2905c5f

19 files changed

Lines changed: 1963 additions & 88 deletions

‎doc/source/asset-pricing/introduction.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ prior to estimation.
6767
Covariance estimator:
6868
KernelCovariance, Kernel: bartlett, Bandwidth: 4
6969
See full_summary for complete results
70+
71+
Aligning Inputs
72+
===============
73+
The rows of ``portfolios`` and ``factors`` are paired by position. A ``factors``
74+
input that carries labels, such as a pandas ``DataFrame`` with a date index,
75+
must use the same labels, in the same order, as ``portfolios``. See
76+
:ref:`input-alignment`.

‎doc/source/index.rst‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ from `statsmodels <https://statsmodels.org>`_:
5555
* Linear factor model (1-step SUR, only for traded factors)
5656
(:class:`~linearmodels.asset_pricing.model.TradedFactorModel`)
5757

58+
**Specification Tests and Diagnostics**
59+
60+
* Cragg-Donald and Kleibergen-Paap tests of the rank of the first-stage
61+
coefficient matrix of IV models, and the F statistics that are compared with
62+
weak-instrument critical values (:ref:`testing identification
63+
<iv-testing-identification>`)
64+
* Classical and heteroskedasticity-robust tests of the exogeneity of the
65+
endogenous regressors and of the overidentifying restrictions of IV models, and
66+
the J, centered J and C statistics of IV-GMM models (:ref:`IV specification
67+
tests <iv-specification-tests>`)
68+
* Hausman test of random effects against fixed effects
69+
(:meth:`~linearmodels.panel.results.RandomEffectsResults.hausman`) and Pesaran CD
70+
test of cross-sectional dependence
71+
(:attr:`~linearmodels.panel.results.PanelResults.pesaran_cd`) for panel models
72+
(:ref:`panel specification tests <panel-specification-tests>`)
73+
5874
linearmodels
5975
============
6076

@@ -66,6 +82,7 @@ linearmodels
6682
panel/index
6783
asset-pricing/index
6884
system/index
85+
input-alignment
6986
utility
7087
compatibility
7188
types

‎doc/source/input-alignment.rst‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
.. _input-alignment:
2+
.. _iv-input-alignment:
3+
.. _system-input-alignment:
4+
.. _asset-pricing-input-alignment:
5+
6+
Aligning Inputs
7+
===============
8+
The rows of the inputs of a model are paired by position: the first row of
9+
``exog`` is attached to the first row of ``dependent``, and so on. To prevent a
10+
value from being attached to the wrong observation, an input that carries
11+
labels must use the same labels, in the same order, as ``dependent``. A
12+
``ValueError`` that names the input is raised if the labels differ, for example
13+
when the rows of a regressor have been sorted differently from the rows of the
14+
dependent variable. The inputs that carry labels are
15+
16+
* a pandas ``Series`` or ``DataFrame`` that has an index other than the default
17+
index, 0, 1, ..., n-1;
18+
* an xarray ``DataArray`` that has coordinates for its rows;
19+
* :class:`~linearmodels.iv.data.IVData` built from either.
20+
21+
To correct the error, reindex the input to the index of ``dependent``, e.g.,
22+
``exog = exog.reindex(dependent.index)``, or call ``to_numpy()`` to explicitly
23+
pair the values by position.
24+
25+
NumPy arrays, lists, tuples and pandas objects that only have the default index
26+
do not carry labels. They are always paired by position, and only need to have
27+
the right number of rows. This is also the case for a ``DataFrame`` with a
28+
default index and named columns, since the columns are variable names and are
29+
not compared. Index names are ignored.
30+
31+
If ``dependent`` does not carry labels, the first of the other inputs that
32+
carries labels is the reference instead, and each of the inputs after it that
33+
carries labels must use the same labels, in the same order. The error names both
34+
inputs, for example "The row index of instruments does not match exog." If none
35+
of the inputs carries labels, all of them are paired by position.
36+
37+
The order of the inputs, which decides which one is the reference when
38+
``dependent`` does not carry labels, and the inputs that are compared are
39+
40+
* **IV2SLS, IVLIML, IVGMM, IVGMMCUE, IVJIVE and OLS**: ``exog``, ``endog``,
41+
``instruments`` and ``weights``. ``clusters`` passed to ``fit``, or to the
42+
weight estimator of IVGMM and IVGMMCUE when ``weight_type`` is
43+
``"clustered"``, is compared in the same way.
44+
* **AbsorbingLS**: ``exog``, ``absorb``, ``interactions`` and ``weights``, and
45+
``clusters`` passed to ``fit``. If both ``cat`` and ``cont`` of an
46+
``Interaction`` carry labels, they must use the same labels.
47+
* **SUR, IV3SLS and IVSystemGMM**: the dependent variables that carry labels
48+
must use the same labels in every equation, since the residuals in a row of
49+
all of the equations are the errors of one observation. In each equation,
50+
``exog``, ``endog``, ``instruments`` and ``weights`` are compared with the
51+
``dependent`` of the equation. If the ``dependent`` of an equation does not
52+
carry labels, they are compared with the labels of the system. These are the
53+
labels of the first dependent variable that carries labels or, if none does,
54+
of the first input that carries labels, taking the equations in order and
55+
``exog``, ``endog``, ``instruments`` and ``weights`` within an equation. All of
56+
the equations must have the same number of observations. ``clusters`` passed
57+
to ``fit`` of SUR and IV3SLS is compared with the labels of the system.
58+
* **TradedFactorModel, LinearFactorModel and LinearFactorModelGMM**: ``factors``
59+
is compared with ``portfolios``. Nothing is compared if ``portfolios`` does not
60+
carry labels, since ``factors`` is the only other input with rows.
61+
* **The panel models** follow the same rule, see :ref:`panel-input-alignment`.
62+
63+
The labels are compared before observations that contain missing values are
64+
dropped. The observations that remain are the ones used in estimation, and so
65+
``clusters`` has to be provided for these observations. For the IV models,
66+
``model.notnull`` is a Boolean array that marks them, e.g.,
67+
``clusters[model.notnull]``. For the other models, drop the same rows from
68+
``clusters`` that were dropped from the data. ``clusters`` is compared with the
69+
labels of the remaining observations. ``clusters`` with the length of the
70+
original data raises the error about the wrong number of observations.
71+
72+
The formula interfaces take all of the variables from one ``DataFrame``, so they
73+
use the same labels. ``weights`` (and ``portfolios`` in the asset pricing
74+
models) that are passed separately to ``from_formula`` are compared in the same
75+
way.
76+
77+
Lists and tuples are accepted wherever a parameter is documented as
78+
``array_like``, for example ``dependent``, ``exog``, ``endog``, ``instruments``,
79+
``weights`` and ``clusters``. They are converted to arrays when the model is
80+
created, and give the same results as arrays. They do not carry labels.
81+
82+
.. code-block:: python
83+
84+
import numpy as np
85+
import pandas as pd
86+
from linearmodels.iv import IV2SLS
87+
88+
rng = np.random.default_rng(0)
89+
index = pd.Index([f"obs{i}" for i in range(100)])
90+
z = pd.Series(rng.standard_normal(100), index=index, name="z")
91+
x = pd.Series(z + rng.standard_normal(100), index=index, name="x")
92+
y = pd.Series(x + rng.standard_normal(100), index=index, name="y")
93+
const = pd.DataFrame({"const": np.ones(100)}, index=index)
94+
95+
IV2SLS(y, const, x, z) # same labels, same order
96+
try:
97+
IV2SLS(y, const, x, z.iloc[::-1]) # same labels, different order
98+
except ValueError as exc:
99+
print(exc)
100+
IV2SLS(y, const, x, z.reindex(y.index)) # reindexed to the labels of y
101+
IV2SLS(y, const, x, z.to_numpy()) # paired by position
102+
try:
103+
# dependent has no labels, so exog is the reference
104+
IV2SLS(y.to_numpy(), const, x, z.iloc[::-1])
105+
except ValueError as exc:
106+
print(exc)
107+
IV2SLS(y.to_list(), const.to_numpy(), x.to_list(), z.to_list()) # lists

0 commit comments

Comments
 (0)