ACCLAB / DABEST-python

Data Analysis with Bootstrapped ESTimation
https://acclab.github.io/DABEST-python/
Apache License 2.0
339 stars 47 forks source link

Delta delta & mini meta delta amendment #125

Closed LI-Yixuan closed 2 years ago

LI-Yixuan commented 3 years ago
LI-Yixuan commented 3 years ago

The plotting function of delta-delta is not changed. However, here is an updated way to get the numeric results (which is similar to that of the mini-meta delta):

# Data for tests.
# See: Asheber Abebe. Introduction to Design and Analysis of Experiments 
# with the SAS, from Example: Two-way RM Design Pg 137.
hr = [72, 78, 71, 72, 66, 74, 62, 69, 69, 66, 84, 80, 72, 65, 75, 71, 
      86, 83, 82, 83, 79, 83, 73, 75, 73, 62, 90, 81, 72, 62, 69, 70]

# Add experiment column
e1 = np.repeat("control", 8).tolist()
e2 = np.repeat("treatment 1", 8).tolist()
experiment = e1 + e2 + e1 + e2

# Add a `Drug` column as the first variable
d1 = np.repeat('AX23', 8).tolist()
d2 = np.repeat('CONTROL', 8).tolist()
drug = d1 + d2 + d1 + d2

# Add a `Time` column as the second variable
t1 = np.repeat('T1', 16).tolist()
t2 = np.repeat('T2', 16).tolist()
time = t1 + t2

# Add an `id` column for paired data plotting.
id_col = []
for i in range(1, 9):
    id_col.append(str(i)+"a")
for i in range(1, 9):
    id_col.append(str(i)+"c")
id_col.extend(id_col)

# Combine samples and gender into a DataFrame.
df_test = pd.DataFrame({'ID'   : id_col,
                   'Drug'      : drug,
                   'Time'      : time, 
                   'Experiment': experiment,
                   'Heart Rate': hr
                    })

df_test_control = df_test[df_test["Experiment"]=="Control"]
df_test_control = df_test_control.pivot(index="ID", columns="Time", values="Heart Rate")

df_test_treatment1 = df_test[df_test["Experiment"]=="Treatment1"]
df_test_treatment1 = df_test_treatment1.pivot(index="ID", columns="Time", values="Heart Rate")

# kwargs for Dabest class init.
dabest_default_kwargs = dict(ci=95, 
                            resamples=5000, random_seed=12345,
                            idx=None, proportional=False, mini_meta=False
                            )

# example of paired data with specified experiment/x1 level
paired_specified_level = dabest.load(data = df_test, x = ["Time", "Drug"], y = "Heart Rate", 
                  delta2 = True, experiment = "Experiment", paired="sequential", id_col="ID",
                  experiment_label=["control", "treatment 1"], x1_level=["T1", "T2"])
paired_specified_level.mean_diff

图片

paired_specified_level.mean_diff.delta_delta

图片

paired_specified_level.mean_diff.delta_delta.to_dict()

图片

For mini-meta delta:

N=20
seed=9999
import numpy as np
import pandas as pd
from scipy.stats import norm # Used in generation of populations.

np.random.seed(9999) # Fix the seed so the results are replicable.
# pop_size = 10000 # Size of each population.

# Create samples
c1 = norm.rvs(loc=3, scale=0.4, size=N)
c2 = norm.rvs(loc=3.5, scale=0.75, size=N)
c3 = norm.rvs(loc=3.25, scale=0.4, size=N)

t1 = norm.rvs(loc=3.5, scale=0.5, size=N)
t2 = norm.rvs(loc=2.5, scale=0.6, size=N)
t3 = norm.rvs(loc=3, scale=0.75, size=N)

# Add an `id` column for paired data plotting. 
id_col = pd.Series(range(1, N+1))

# Combine samples and gender into a DataFrame.
df = pd.DataFrame({'Control 1' : c1, 'Test 1' : t1,
                    'Control 2' : c2,     'Test 2' : t2,
                       'Control 3' : c3,     'Test 3' : t3,
                       'ID'  : id_col
                      })

unpaired = dabest.load(df, 
                       idx=(("Control 1", "Test 1"), ("Control 2", "Test 2"), ("Control 3", "Test 3")),
                       mini_meta=True)

unpaired.mean_diff

图片

unpaired.mean_diff.mini_meta_delta

图片