linearmodels.iv.absorbing.Interaction¶
-
class linearmodels.iv.absorbing.Interaction(cat: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None, cont: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =None, nobs: int | None =None)[source]¶ Class that simplifies specifying interactions.
- Parameters:¶
- cat: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | 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. cat has shape (nobs, ncat).
- cont: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None¶ Variables to treat as continuous, (nobs, ncont). A
TypeErroris raised if the values are complex or cannot be converted to float64.- nobs: int | None =
None¶ Number of observations. It is required if both
catandcontare not provided, and it is otherwise the number of rows of the inputs.
- cat: ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
- Raises:¶
TypeError – If
catorcontis complex or cannot be converted to float64.ValueError – If
nobsis not provided whencatandcontare not provided, if both are empty, if they have different numbers of rows, or if they carry labels that differ.
Notes
For each variable in cont, computes the interaction of the variable and the cartesian product of the categories.
If both
catandcontcarry labels (a pandas Series or DataFrame with an index other than the default 0, 1, …, n-1, or an xarray DataArray with coordinates), they must use the same index, in the same order.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)Drop observations.
from_frame(frame)Create an Interaction from the columns of a DataFrame.
Properties
Categorical variables.
Continuous variables.
Construct a hash that is invariant to the order of the inputs.
Return a Series that is True for the rows that contain a missing value.
Number of observations, which drop reduces.
Construct a sparse interaction matrix.