Classes
UnivarStats
Perform univariate statistical analysis and visualization using Plotly.
Parameters
dfpd.DataFrameInput DataFrame containing the measurement and group columns.
x_colstrColumn name for the grouping variable.
y_colstrColumn name for the measurement variable.
group_orderlist of str, optionalCustom group plotting order.
custom_colorsdict of str -> str, optionalMapping from group name to color.
stats_optionslist of str, optionalSupported["t-test", "anova", "nonparametric", "effect-size"].p_value_thresholdfloat, default=0.05Significance threshold.
annotate_style{'value', 'symbol'}, default='value'Annotation stylenumeric or stars.y_offset_factorfloat, default=0.35Vertical spacing factor for annotations.
show_non_significantbool, default=TrueWhether to display 'ns'.
correct_pstr or None, default='bonferroni'Method for multiple testing correction. Supported: - 'bonferroni', 'holm', 'hochberg', 'hommel' - 'fdr_bh', 'fdr_by', 'fdr_tsbh', 'fdr_tsbky' - None or 'none' = no correction
title_str, optionalPlot title.
y_labelstr, optionalY-axis label.
x_labelstr, optionalX-axis label.
fig_heightint, default=800Figure height.
fig_widthint, default=600Figure width.
plot_type{'box', 'violin'}, default='box'Plot type.
show_axis_linesbool, default=TrueWhether to show axis lines.
Examples: >>> import pandas as pd >>> import numpy as np >>> import metbit >>> np.random.seed(0) >>> df = pd.DataFrame({ ... "group": ["A"] * 20 + ["B"] * 20, ... "value": np.concatenate([np.random.normal(0, 1, 20), np.random.normal(1, 1, 20)]) ... }) >>> us = metbit.stats.UnivarStats(df, x_col="group", y_col="value", stats_options=["t-test"]) >>> fig = us.plot() >>> fig.show() >>> table = us.get_stats_table() >>> print(table)
Methods
__init__(self, df: pd.DataFrame, x_col: str, y_col: str, group_order: Optional[List[str]]=None, custom_colors: Optional[Dict[str, str]]=None, stats_options: Optional[List[str]]=None, p_value_threshold: float=0.05, annotate_style: str='value', y_offset_factor: float=0.35, show_non_significant: bool=True, correct_p: Optional[str]='bonferroni', title_: Optional[str]=None, y_label: Optional[str]=None, x_label: Optional[str]=None, fig_height: int=800, fig_width: int=600, plot_type: str='box', show_axis_lines: bool=True)
compute_effsize(a, b, eftype: str='cohen')
Compute effect size between two groups.
Parameters
aarray-likeObservations for group A.
barray-likeObservations for group B.
eftypestr, default='cohen'Effect size type. Currently only 'cohen' (Cohen's d) is supported.
Returns
float Computed effect size.
Examples: >>> import numpy as np >>> import metbit >>> a = np.random.normal(0, 1, 30) >>> b = np.random.normal(1, 1, 30) >>> d = metbit.stats.UnivarStats.compute_effsize(a, b) >>> print(f"Cohen's d: {d:.3f}")
plot(self, show_description: bool=True)
Generate an annotated box or violin plot with statistical comparisons.
Parameters
show_descriptionbool, default=TrueIf True and annotate_style is 'symbol', adds a legend annotation explaining the significance symbols.
Returns
go.Figure Plotly figure with significance annotations.
Examples: >>> import pandas as pd >>> import numpy as np >>> import metbit >>> np.random.seed(42) >>> df = pd.DataFrame({ ... "group": ["A"] * 20 + ["B"] * 20, ... "value": np.concatenate([np.random.normal(0, 1, 20), np.random.normal(2, 1, 20)]) ... }) >>> us = metbit.stats.UnivarStats(df, x_col="group", y_col="value") >>> fig = us.plot() >>> fig.show()
get_stats_table(self)
Return a DataFrame of statistical results.
Returns
pd.DataFrame
Table with columnsComparison, Raw P-Value, Corrected P-Value,and Effect Size (Cohen's d).
Examples: >>> import pandas as pd >>> import numpy as np >>> import metbit >>> np.random.seed(0) >>> df = pd.DataFrame({ ... "group": ["A"] * 20 + ["B"] * 20, ... "value": np.concatenate([np.random.normal(0, 1, 20), np.random.normal(1, 1, 20)]) ... }) >>> us = metbit.stats.UnivarStats(df, x_col="group", y_col="value") >>> _ = us.plot() >>> table = us.get_stats_table() >>> print(table)