Skip to content

GoodnessOfFitCharts

GoodnessOfFitCharts

Bases: SingleChart

Visualizes the evolution of goodness-of-fit metrics across the steps of a recursive parameter elimination.

The GoodnessOfFitCharts class takes the goodness-of-fit table collected during LinearModel.recursive_elimination and plots R², adjusted R², the p-value of the least significant term and the min-max normalized Akaike information criterion (AIC) against the elimination step. A dashed line marks the significance level used to decide whether a term should be eliminated, and a shaded area highlights the elimination steps at which the model was heterarchical, i.e. not hierarchical.

Once a preferred epoch has been identified visually, use formula_at_epoch or refit_at_epoch to extract the formula used at that step and refit the underlying LinearModel with it.

PARAMETER DESCRIPTION
df_gof

The goodness-of-fit table collected during recursive elimination, i.e. the concatenated result of LinearModel.recursive_elimination. Must contain the columns ANOVA.EPOCH, ANOVA.FORMULA, ANOVA.R2, ANOVA.R2_ADJ, ANOVA.P_LEAST, ANOVA.AIC and ANOVA.HIERARCHICAL.

TYPE: DataFrame

linear_model

The linear regression model the given df_gof table was generated from. Used to draw the significance threshold line and to refit via refit_at_epoch.

TYPE: LinearModel

stretch_figsize

If True, the height and width of the figure are stretched based on the number rows and columns in the axes grid. If a float is provided, the figure size is stretched by the given factor. If a tuple of two floats is provided, the figure size is stretched by the given factors for the x and y axis, respectively. by default False.

TYPE: bool | float | tuple[float, float] DEFAULT: False

Examples:

import pandas as pd
import daspi as dsp

df = dsp.load_dataset('painkillers-dissolution')
lm = dsp.LinearModel(
    source=df,
    target='dissolution',
    factors=['employee', 'stirrer', 'brand', 'catalyst', 'water'],
    covariates=['temperature', 'preparation'],
    order=2)
df_gof = pd.concat(lm.recursive_elimination())
chart = dsp.GoodnessOfFitCharts(df_gof, lm).plot().stripes().label(info=True)

GoodnessOfFitChartsDocstringExample

Once a good epoch has been picked by eye, e.g. epoch 5, refit the model with the formula recorded at that step:

chart.refit_at_epoch(5)
dsp.ParameterRelevanceCharts(lm).plot().stripes().label()

lm = linear_model instance-attribute

The linear regression model the goodness-of-fit table was generated from.

df_gof = df_gof.sort_values(ANOVA.EPOCH).reset_index(drop=True) instance-attribute

The goodness-of-fit table used to build this chart.

metric_labels = {ANOVA.R2: '$R^2$', ANOVA.R2_ADJ: '$R^2_{adj}$', ANOVA.P_LEAST: '$p_{least}$', self._AIC_NORM: '$AIC_{norm}$'} instance-attribute

Mapping of the goodness-of-fit column names to the mathtext label used for them in the legend.

plot

Plot each goodness-of-fit metric as a line against the elimination step.

RETURNS DESCRIPTION
Self

The GoodnessOfFitCharts instance, for method chaining.

stripes

Adds a dashed line at the significance level used for the elimination and a shaded area marking the elimination steps at which the model was not hierarchical.

RETURNS DESCRIPTION
Self

The GoodnessOfFitCharts instance, for method chaining.

formula_at_epoch

Get the model formula recorded at the given elimination step.

PARAMETER DESCRIPTION
epoch

The elimination step whose formula should be extracted.

TYPE: int

RETURNS DESCRIPTION
str

The model formula used at the given epoch.

refit_at_epoch

Refit the underlying LinearModel with the formula recorded at the given elimination step.

Use this once you have picked a preferred epoch by inspecting the chart produced by plot(), e.g. where the significance threshold and the hierarchy of the model satisfy you best.

PARAMETER DESCRIPTION
epoch

The elimination step whose formula should be used to refit the model.

TYPE: int

**kwds

Additional keyword arguments for ols function of statsmodels.formula.api, passed to LinearModel.fit.

DEFAULT: {}

RETURNS DESCRIPTION
LinearModel

The linear_model passed to this chart, refitted with the formula of the given epoch.

label

Adds titles and labels to the chart generated by the plot() method.

PARAMETER DESCRIPTION
info

If True, the method will add an informative subtitle to the chart. If a string is provided, it will be used as the subtitle, by default False.

TYPE: bool | str DEFAULT: False

**kwds

Additional keyword arguments to be passed to the label() method of the SingleChart instance.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Self

The GoodnessOfFitCharts instance, for method chaining.