linearmodels.iv.absorbing.Interaction

class linearmodels.iv.absorbing.Interaction(cat: ndarray | DataArray | DataFrame | Series | None = None, cont: ndarray | DataArray | DataFrame | Series | None = None, nobs: int | None = None)[source]

Class that simplifies specifying interactions

Parameters:
cat: ndarray | DataArray | DataFrame | Series | None = None

Variables to treat as categoricals. Best format is a Categorical Series or DataFrame containing Categorical Series. Other formats are converted to Categorical Series, column-by-column. cats has shape (nobs, ncat).

cont: ndarray | DataArray | DataFrame | Series | None = None

Variables to treat as continuous, (nobs, ncont).

Notes

For each variable in cont, computes the interaction of the variable and the cartesian product of the categories.

Examples

>>> import numpy as np
>>> from linearmodels.iv.absorbing import Interaction
>>> rs = np.random.RandomState(0)
>>> n = 100000
>>> cats = rs.randint(2, size=n)  # binary dummy
>>> cont = rs.standard_normal((n, 3))
>>> interact = Interaction(cats, cont)
>>> interact.sparse.shape  # Get the shape of the dummy matrix
(100000, 6)
>>> rs = np.random.RandomState(0)
>>> import pandas as pd
>>> cats_df = pd.concat([pd.Series(pd.Categorical(rs.randint(5,size=n)))
...                     for _ in range(4)], axis=1)
>>> cats_df.describe()
             0       1       2       3
count   100000  100000  100000  100000
unique       5       5       5       5
top          3       3       0       4
freq     20251   20195   20331   20158
>>> interact = Interaction(cats_df, cont)
>>> interact.sparse.shape # Cart product of all cats, 5**4, times ncont, 3
(100000, 1875)

Methods

drop(locs)

from_frame(frame)

Convenience function the simplifies using a DataFrame

Properties

cat

Categorical Variables

cont

Continuous Variables

hash

Construct a hash that will be invariant for any permutation of inputs that produce the same fit when used as regressors

isnull

nobs

sparse

Construct a sparse interaction matrix