ProportionTest
ProportionTest
¶
Bases: ConfidenceInterval
Class for creating plotters with error bars representing confidence intervals for proportion (events/observation).
This class is specifically designed for testing the statistical significance of proportions. It uses confidence intervals to visually represent the uncertainty in the variation estimates and allows for a quick assessment of whether the intervals overlap or not.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Pandas long format DataFrame containing the data source for the plot.
TYPE:
|
target
|
Internal bookkeeping column name only, used to satisfy the
base class's single-target interface; it holds
TYPE:
|
n_groups
|
Number of groups (variable combinations) for the Bonferroni
adjustment. A good way to do this is to pass
TYPE:
|
events
|
Column name containing the values of counted events for each feature.
TYPE:
|
observations
|
Column name containing the values of counted observations for each feature.
TYPE:
|
feature
|
Column name of the feature variable for the plot, by default ''.
TYPE:
|
method
|
A pandas Series method to use for aggregating target values within each feature level. This method is only required if there is more than one value for number of observations and number of events for each factor level. Otherwise it is ignored, by default 'sum'.
TYPE:
|
show_center
|
Flag indicating whether to show the center points, by default True.
TYPE:
|
bars_same_color
|
Flag indicating whether to use same color for error bars as markers for center. If False, the error bars are black, by default False
TYPE:
|
target_on_y
|
Flag indicating whether the target variable is plotted on the y-axis, by default True.
TYPE:
|
confidence_level
|
Confidence level for the confidence intervals, by default 0.95.
TYPE:
|
color
|
Color to be used to draw the artists. If None, the first color is taken from the color cycle, by default None.
TYPE:
|
marker
|
The marker style for the center points. Available markers see: https://matplotlib.org/stable/api/markers_api.html, by default None
TYPE:
|
ax
|
The axes object for the plot. If None, the current axes is
fetched using
TYPE:
|
visible_spines
|
Specifies which spines are visible, the others are hidden. If 'none', no spines are visible. If None, the spines are drawn according to the stylesheet. Defaults to None.
TYPE:
|
hide_axis
|
Specifies which axes should be hidden. If None, both axes are displayed. Defaults to None.
TYPE:
|
**kwds
|
Additional keyword arguments that have no effect and are only used to catch further arguments that have no use here (occurs when this class is used within chart objects).
DEFAULT:
|
Notes
Unlike the other plotters in this module, ProportionTest cannot
work off a single target column alone. A proportion needs two
numbers per feature level, so this class requires both:
events: the count of the thing you are testing the proportion of (e.g. number of out-of-spec parts, number of failures, ...).observations: the total count the events were drawn from (e.g. total number of parts inspected, total trials, ...).
How target, events/observations and feature play together:
featuregroups the rows exactly like in every other plotter (the categories shown on the feature axis). For each feature level,eventsandobservationsare aggregated withmethod(relevant only if a level has more than one row) and passed toproportion_cito compute the center and confidence bounds.eventsandobservationsare the real inputs to the statistics; they are consumed internally and never drawn directly.targetis only a bookkeeping column required by the baseTransformPlotter/ConfidenceIntervalmachinery, which expects a single target column. Internally it is filled with(events, observations)tuples, not with the proportion value. If you pass an empty string, a name is generated automatically as'{events}/{observations}'. This means thetargetyou pass here is independent of anytarget(e.g. a precomputedproportioncolumn) you use for another plotter in the same chart, such asBar.
Because target is this internal placeholder, ProportionTest
does not visualize a proportion on its own. If you want to draw
the proportion itself (e.g. as bars) in addition to its confidence
interval, precompute the proportion yourself and pass it as the
target of a separate plotter such as Bar, while passing
events/observations to ProportionTest as shown below.
Examples:
Step by step: visualize the proportion of out-of-spec parts per
group as bars, with ProportionTest adding the confidence
interval on top.
- Build sample data and compute
events(out-of-spec count) andobservations(sample size) per group, plus the proportion to plot as bars:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from daspi import ProportionTest, ProcessEstimator, SpecLimits, Bar
df = pd.DataFrame(dict(
x = ['first'] * 100 + ['second'] * 100 + ['third'] * 100,
y = (list(np.random.normal(loc=3, scale=1, size=100))
+ list(np.random.normal(loc=4, scale=1, size=100))
+ list(np.random.normal(loc=2, scale=1, size=100)))))
spec_limits = SpecLimits(upper=4.3)
data = pd.DataFrame()
for name, group in df.groupby('x'):
y = ProcessEstimator(group['y'], spec_limits=spec_limits)
temp = pd.DataFrame(dict(
x = [name],
proportion = y.n_nok / y.n_samples,
events = y.n_nok,
observations = y.n_samples))
data = pd.concat([data, temp], ignore_index=True, axis=0)
- Draw the precomputed
proportionas bars on an Axes:
- Add
ProportionTeston the same Axes. Note thattarget=''is passed explicitly here, so the bookkeeping column name is auto-generated, whileeventsandobservationsprovide the actual counts used for the confidence interval:
test = ProportionTest(
source=data, target='', feature='x', events='events',
observations='observations', show_center=False,
n_groups=data.x.nunique(), confidence_level=0.95,
bars_same_color=True, ax=ax)
test()
test.label_feature_ticks()
test.target_as_percentage()
Equivalent, using the plot method of a DaSPi Chart object. The
chart's own target='proportion' only drives the Bar plotter;
ProportionTest still relies on its own events/observations:
import daspi as dsp
chart = dsp.SingleChart(
source=data,
target='proportion',
feature='x',
categorical_feature=True, # needed to label the feature tick labels
).plot(
dsp.Bar,
).plot(
dsp.ProportionTest,
events='events',
observations='observations',
show_center=False,
n_groups=data.x.nunique(),
confidence_level=0.95,
bars_same_color=True,
).label() # needed to label the feature tick labels
method = method
instance-attribute
¶
The provided Pandas Series method for aggregating events and observations (if there are multiple) per feature level.
transform
¶
Perform the transformation on the target data by using the
given function ci_func and return the transformed data.
| PARAMETER | DESCRIPTION |
|---|---|
feature_data
|
Base location (offset) of feature axis coming from
TYPE:
|
target_data
|
Feature grouped target data used for transformation, coming
from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
data
|
The transformed data source for the plot.
TYPE:
|