linearmodels.iv.model.IV2SLS.from_formula¶
-
static IV2SLS.from_formula(formula: str, data: DataFrame, *, weights: IVData | ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None) IV2SLS[source]¶ Create a model from a formula.
- Parameters:¶
- formula: str¶
Formula modified for the IV syntax described in the notes section.
- data: DataFrame¶
DataFrame containing the variables used in the formula.
- weights: IVData | ndarray | DataArray | DataFrame | Series | list[Any] | tuple[Any, ...] | None =
None¶ Observation weights used in estimation. The weights must be strictly positive and finite. They are rescaled to have a mean of 1. A NaN weight is missing, and its observation is dropped. The default is to weight all observations equally.
- Returns:¶
Model instance.
- Return type:¶
- Raises:¶
ValueError – If the formula is not an IV formula, or if the model that it describes is not valid. See the class for the errors that the model raises.
Notes
The IV formula modifies the standard formula syntax to include a block of the form [endog ~ instruments] which is used to indicate the list of endogenous variables and instruments. The general structure is dependent ~ exog [endog ~ instruments] and it must be the case that the formula expressions constructed from blocks dependent ~ exog endog and dependent ~ exog instruments are both valid formulas.
A constant must be explicitly included using “1 +” if required. Use I(exper ** 2) for the square of a variable, since exper ** 2 is the power of a set of terms in a formula, which is exper.
Examples
>>> import numpy as np >>> from linearmodels.datasets import wage >>> from linearmodels.iv import IV2SLS >>> data = wage.load() >>> formula = 'np.log(wage) ~ 1 + exper + I(exper ** 2) + brthord + [educ ~ sibs]' >>> mod = IV2SLS.from_formula(formula, data)