statsmodels.stats.anova.anova_lm#
- statsmodels.stats.anova.anova_lm(*args, **kwargs)[source]#
Anova table for one or more fitted linear models
- Parameters:
- *args
fittedlinearmodelresultsinstance One or more fitted linear models
- scale
floatorNone,optional Estimate of variance, If None, will be estimated from the largest model. Default is None.
- test{“F”, “Chisq”, “Cp”,
None},optional Test statistics to provide. Default is “F”.
- typ{1, 2, 3, “I”, “II”, “III”},
optional The type of Anova test to perform. Default is I, more see notes.
- robust{
None, “hc0”, “hc1”, “hc2”, “hc3”},optional Use heteroscedasticity-corrected coefficient covariance matrix. If robust covariance is desired, it is recommended to use hc3.
- *args
- Returns:
- anova
DataFrame When args is a single model, return is DataFrame with columns:
- sum_sqfloat64
Sum of squares for model terms.
- dffloat64
Degrees of freedom for model terms.
- Ffloat64
F statistic value for significance of adding model terms.
- PR(>F)float64
P-value for significance of adding model terms.
When args is multiple models, return is DataFrame with columns:
- df_residfloat64
Degrees of freedom of residuals in models.
- ssrfloat64
Sum of squares of residuals in models.
- df_difffloat64
Degrees of freedom difference from previous model in args
- ss_dfffloat64
Difference in ssr from previous model in args
- Ffloat64
F statistic comparing to previous model in args
- PR(>F): float64
P-value for significance comparing to previous model in args
- anova
- Raises:
ValueErrorIf a single model with a rank-deficient design matrix is passed, for any type of Anova table. Remove redundant columns, empty cells and unused factor levels before fitting the model.
See also
statsmodels.regression.linear_model.RegressionResults.compare_f_testNested model comparrison using an F-test
statsmodels.regression.linear_model.RegressionResults.compare_lm_testNested model comparrison using an LM test
Notes
Model statistics are given in the order of args. Models must have been fit using the formula api.
The tables of a single model assume that the design matrix has full column rank. This fails if a term duplicates or is a linear combination of other terms, if a factor level or a cell of an interaction has no observations, or if there are more columns than observations. A model with a redundant term has the same fitted values as the model without it, but the redundant term would be assigned an arbitrary sum of squares, degrees of freedom and p-value, so the model is rejected. A factor
Bthat is nested in a factorAcan be used in the formulaA/Bif the same labels ofBare used within every level ofA. If each level ofAhas its own labels ofB, the cells that combine a label ofBwith the other levels ofAare empty.Type I: Sequential sums of squares. Each term is tested after the terms that precede it in the model. Consequently, the results depend on the order of the terms when the design is unbalanced.
Type II: Each term is tested after all other terms except higher-order terms that contain it. Thus, main effects are not adjusted for interactions involving them. Type II tests respect the principle of marginality and are generally most appropriate when interactions are absent or are not of primary interest.
Type III: Each term is tested after all other terms in the model, including higher-order terms that contain it. This permits testing main effects in models containing interactions, but such tests can be difficult to interpret and may depend on the contrast coding used for categorical factors.
Examples
>>> import statsmodels.api as sm >>> from statsmodels.formula.api import ols >>> moore = sm.datasets.get_rdataset("Moore", "carData", cache=True) # load >>> data = moore.data >>> data = data.rename(columns={"partner.status" : ... "partner_status"}) # make name pythonic >>> moore_lm = ols('conformity ~ C(fcategory, Sum)*C(partner_status, Sum)', ... data=data).fit() >>> table = sm.stats.anova_lm(moore_lm, typ=2) # Type 2 Anova DataFrame >>> print(table)