Skip to content

Commit adbe196

Browse files
committed
mini-meta delta
1 parent 5cda1a9 commit adbe196

26 files changed

Lines changed: 941 additions & 82 deletions

dabest/_api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def load(data, idx=None, x=None, y=None, paired=None, id_col=None,
88
ci=95, resamples=5000, random_seed=12345, proportional=False,
99
delta2 = False, experiment = None, experiment_label = None,
10-
x1_level = None):
10+
x1_level = None, mini_meta=False):
1111
'''
1212
Loads data in preparation for estimation statistics.
1313
@@ -55,6 +55,8 @@ def load(data, idx=None, x=None, y=None, paired=None, id_col=None,
5555
A list of String to specify the order of subplots for delta-delta plots.
5656
This can be expressed as a list of 2 elements if and only if 'delta2'
5757
is True; otherwise it can only be a string.
58+
mini_meta : boolean, default False
59+
Indicator of weighted delta calculation.
5860
5961
Returns
6062
-------
@@ -83,4 +85,5 @@ def load(data, idx=None, x=None, y=None, paired=None, id_col=None,
8385
'''
8486
from ._classes import Dabest
8587

86-
return Dabest(data, idx, x, y, paired, id_col, ci, resamples, random_seed, proportional, delta2, experiment, experiment_label, x1_level)
88+
return Dabest(data, idx, x, y, paired, id_col, ci, resamples, random_seed, proportional, delta2, experiment, experiment_label, x1_level, mini_meta)
89+

dabest/_classes.py

Lines changed: 591 additions & 54 deletions
Large diffs are not rendered by default.

dabest/_stats_tools/confint_1group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def create_bootstrap_indexes(array, resamples=5000, random_seed=12345):
1010
"""Given an array-like, returns a generator of bootstrap indexes
1111
to be used for resampling.
12-
"""i
12+
"""
1313
import numpy as np
1414
from numpy.random import PCG64, RandomState
1515
rng = RandomState(PCG64(random_seed))

dabest/_stats_tools/confint_2group_diff.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,20 @@ def compute_interval_limits(bias, acceleration, n_boots, ci=95):
257257
low = int(norm.cdf(low) * n_boots)
258258
high = int(norm.cdf(high) * n_boots)
259259
return low, high
260+
261+
262+
def calculate_group_var(control_var, control_N,test_var, test_N):
263+
return control_var/control_N + test_var/test_N
264+
265+
266+
def calculate_weighted_delta(group_var, differences, resamples):
267+
'''
268+
Compute the weighted deltas.
269+
'''
270+
import numpy as np
271+
272+
weight = 1/group_var
273+
denom = np.sum(weight)
274+
num = np.sum(weight[i] * differences[i] for i in range(0, len(weight)))
275+
276+
return num/denom

dabest/_stats_tools/effsize.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,10 @@ def _compute_hedges_correction_factor(n1, n2):
386386
out = numer / denom
387387

388388
return out
389+
390+
391+
def weighted_delta(difference, group_var):
392+
import numpy as np
393+
394+
weight = np.true_divide(1, group_var)
395+
return np.sum(difference*weight)/np.sum(weight)

dabest/plotter.py

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
1414
**plot_kwargs:
1515
color_col=None
1616
raw_marker_size=6, es_marker_size=9,
17-
swarm_label=None, contrast_label=None, delta_label=None,
18-
swarm_ylim=None, contrast_ylim=None,
17+
swarm_label=None, contrast_label=None, delta2_label=None,
18+
swarm_ylim=None, contrast_ylim=None, delta2_ylim=None,
1919
custom_palette=None, swarm_desat=0.5, halfviolin_desat=1,
2020
halfviolin_alpha=0.8,
2121
float_contrast=True,
@@ -66,6 +66,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
6666
yvar = EffectSizeDataFrame.yvar
6767
is_paired = EffectSizeDataFrame.is_paired
6868
delta2 = EffectSizeDataFrame.delta2
69+
mini_meta = EffectSizeDataFrame.mini_meta
6970
effect_size = EffectSizeDataFrame.effect_size
7071

7172
all_plot_groups = dabest_obj._all_plot_groups
@@ -79,8 +80,14 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
7980
show_delta2 = plot_kwargs["show_delta2"]
8081

8182

83+
if effect_size != "mean_diff" or not mini_meta:
84+
show_mini_meta = False
85+
else:
86+
show_mini_meta = plot_kwargs["show_mini_meta"]
8287

83-
88+
if show_delta2 and show_mini_meta:
89+
err0 = "`show_delta2` and `show_mini_meta` cannot be True at the same time."
90+
raise ValueError(err0)
8491

8592
# Disable Gardner-Altman plotting if any of the idxs comprise of more than
8693
# two groups or if it is a delta-delta plot.
@@ -92,7 +99,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
9299
if effect_size_type in ['cliffs_delta']:
93100
float_contrast = False
94101

95-
if show_delta2:
102+
if show_delta2 or show_mini_meta:
96103
float_contrast = False
97104

98105
if not is_paired:
@@ -236,7 +243,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
236243
if fig_size is None:
237244
all_groups_count = np.sum([len(i) for i in dabest_obj.idx])
238245
# Increase the width for delta-delta graph
239-
if show_delta2:
246+
if show_delta2 or show_mini_meta:
240247
all_groups_count += 2
241248
if is_paired and show_pairs is True:
242249
frac = 0.75
@@ -527,9 +534,42 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
527534
current_control))
528535

529536

537+
# Plot mini-meta violin
538+
if show_mini_meta:
539+
mini_meta_delta = EffectSizeDataFrame.mini_meta_delta
540+
weighted_deltas = mini_meta_delta.weighted_diff_from_boots
541+
difference = mini_meta_delta.difference
542+
ci_low = mini_meta_delta.bca_low
543+
ci_high = mini_meta_delta.bca_high
544+
545+
#Create the violinplot.
546+
#New in v0.2.6: drop negative infinities before plotting.
547+
position = max(rawdata_axes.get_xticks())+2
548+
v = contrast_axes.violinplot(weighted_deltas[~np.isinf(weighted_deltas)],
549+
positions=[position],
550+
**violinplot_kwargs)
551+
552+
fc = "grey"
553+
554+
halfviolin(v, fill_color=fc, alpha=halfviolin_alpha)
555+
556+
# Plot the effect size.
557+
contrast_axes.plot([position], difference, marker='o',
558+
color=ytick_color,
559+
markersize=es_marker_size)
560+
# Plot the confidence interval.
561+
contrast_axes.plot([position, position],
562+
[ci_low, ci_high],
563+
linestyle="-",
564+
color=ytick_color,
565+
linewidth=group_summary_kwargs['lw'])
566+
567+
contrast_xtick_labels.extend(["","Weighted delta"])
568+
569+
530570
# Make sure the contrast_axes x-lims match the rawdata_axes xlims,
531571
# and add an extra violinplot tick for delta-delta plot.
532-
if not show_delta2:
572+
if show_delta2 is False and show_mini_meta is False :
533573
contrast_axes.set_xticks(rawdata_axes.get_xticks())
534574
else:
535575
temp = rawdata_axes.get_xticks()
@@ -543,7 +583,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
543583

544584
if float_contrast is True:
545585
contrast_axes.set_xlim(0.5, 1.5)
546-
elif show_delta2:
586+
elif show_delta2 or show_mini_meta:
547587
# Increase the xlim of raw data by 2
548588
temp = rawdata_axes.get_xlim()
549589
if show_pairs:
@@ -712,8 +752,20 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
712752
# For Cumming Plots only.
713753

714754
# Set custom contrast_ylim, if it was specified.
715-
if plot_kwargs['contrast_ylim'] is not None:
716-
custom_contrast_ylim = plot_kwargs['contrast_ylim']
755+
if plot_kwargs['contrast_ylim'] is not None or (plot_kwargs['delta2_ylim'] is not None and show_delta2):
756+
757+
if plot_kwargs['contrast_ylim'] is not None:
758+
custom_contrast_ylim = plot_kwargs['contrast_ylim']
759+
if plot_kwargs['delta2_ylim'] is not None and show_delta2:
760+
custom_delta2_ylim = plot_kwargs['delta2_ylim']
761+
if custom_contrast_ylim!=custom_deltas_ylim:
762+
err1 = "Please check if `contrast_ylim` and `delta2_ylim` are assigned"
763+
err2 = "with same values."
764+
raise ValueError(err1 + err2)
765+
else:
766+
custom_delta2_ylim = plot_kwargs['delta2_ylim']
767+
custom_contrast_ylim = custom_delta2_ylim
768+
717769

718770
if len(custom_contrast_ylim) != 2:
719771
err1 = "Please check `contrast_ylim` consists of "
@@ -791,7 +843,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
791843
ax.set_ylim(ylim)
792844
del redraw_axes_kwargs['y']
793845

794-
if show_delta2 is True:
846+
if show_delta2 is True or show_mini_meta is True:
795847
ylim = contrast_axes.get_ylim()
796848
redraw_axes_kwargs['y'] = ylim[0]
797849
x_ticks = contrast_axes.get_xticks()
@@ -854,17 +906,17 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs):
854906

855907

856908
if show_delta2 is True:
857-
if plot_kwargs['delta_label'] is None:
858-
delta_label = "delta - delta"
909+
if plot_kwargs['delta2_label'] is None:
910+
delta2_label = "delta - delta"
859911
else:
860-
delta_label = plot_kwargs['delta_label']
861-
delta_axes = contrast_axes.twinx()
862-
delta_axes.set_frame_on(False)
863-
delta_axes.set_ylabel(delta_label)
912+
delta2_label = plot_kwargs['delta2_label']
913+
delta2_axes = contrast_axes.twinx()
914+
delta2_axes.set_frame_on(False)
915+
delta2_axes.set_ylabel(delta2_label)
864916
og_xlim_delta = contrast_axes.get_xlim()
865917
og_ylim_delta = contrast_axes.get_ylim()
866-
delta_axes.set_ylim(og_ylim_delta)
867-
delta_axes.vlines(og_xlim_delta[1],
918+
delta2_axes.set_ylim(og_ylim_delta)
919+
delta2_axes.vlines(og_xlim_delta[1],
868920
og_ylim_delta[0], og_ylim_delta[1],
869921
**redraw_axes_kwargs)
870922

23.2 KB
Loading
26.4 KB
Loading
23.2 KB
Loading
32.8 KB
Loading

0 commit comments

Comments
 (0)