Skip to content
New Workbook
Sign up
Project: Hypothesis Testing in Healthcare

Hypothesis Testing in Healthcare: Drug Safety

A pharmaceutical company GlobalXYZ has just completed a randomized controlled drug trial. To promote transparency and reproducibility of the drug's outcome, they (GlobalXYZ) have presented the dataset to your organization, a non-profit that focuses primarily on drug safety.

The dataset provided contained five adverse effects, demographic data, vital signs, etc. Your organization is primarily interested in the drug's adverse reactions. It wants to know if the adverse reactions, if any, are of significant proportions. It has asked you to explore and answer some questions from the data.

The dataset drug_safety.csv was obtained from Hbiostat courtesy of the Vanderbilt University Department of Biostatistics. It contained five adverse effects: headache, abdominal pain, dyspepsia, upper respiratory infection, chronic obstructive airway disease (COAD), demographic data, vital signs, lab measures, etc. The ratio of drug observations to placebo observations is 2 to 1.

For this project, the dataset has been modified to reflect the presence and absence of adverse effects adverse_effects and the number of adverse effects in a single individual num_effects.

The columns in the modified dataset are:

ColumnDescription
sexThe gender of the individual
ageThe age of the individual
weekThe week of the drug testing
trxThe treatment (Drug) and control (Placebo) groups
wbcThe count of white blood cells
rbcThe count of red blood cells
adverse_effectsThe presence of at least a single adverse effect
num_effectsThe number of adverse effects experienced by a single individual

The original dataset can be found here.

Your organization has asked you to explore and answer some questions from the data collected. See the project instructions.

# Import packages
import numpy as np
import pandas as pd
from statsmodels.stats.proportion import proportions_ztest
import pingouin
import seaborn as sns
import matplotlib.pyplot as plt

# Load the dataset
drug_safety = pd.read_csv("drug_safety.csv")

# Start coding here...
effects_by_trx_group = drug_safety.groupby('trx')['adverse_effects'].value_counts()

adverse_counts = np.array([effects_by_trx_group[('Drug','Yes')], effects_by_trx_group[('Placebo','Yes')]])

n = np.array([effects_by_trx_group[('Drug','Yes')]+effects_by_trx_group[('Drug','No')], effects_by_trx_group[('Placebo','Yes')]+effects_by_trx_group[('Placebo','No')]])

two_sample_results = proportions_ztest(adverse_counts,n,alternative='two-sided')
print(two_sample_results)
drug_safety.groupby('trx')['num_effects'].value_counts(normalize=True)
num_effects_groups = pingouin.chi2_independence(data=drug_safety,x='num_effects',y='trx')
#print(stats)
#print(stats[stats['test'] == 'pearson']['chi2'].values[0], stats[stats['test'] == 'pearson']['pval'].values[0])
#num_effects_groups = stats[stats['test'] == 'pearson']['chi2'].values[0], stats[stats['test'] == 'pearson']['pval'].values[0]
print(num_effects_groups)
summary_stat = drug_safety.groupby('trx')['age'].agg(['mean', 'std','count'])
print(summary_stat)
# Calculate the numerator of the test statistic
numerator = summary_stat.loc['Drug','mean'] - summary_stat.loc['Placebo','mean']
print(numerator)

# Calculate the denominator of the test statistic
std_drug = summary_stat.loc['Drug','std']
std_placebo = summary_stat.loc['Placebo','std']
n_drug = summary_stat.loc['Drug','count']
n_placebo = summary_stat.loc['Placebo','count']
denominator = np.sqrt(std_drug**2/n_drug+std_placebo**2/n_placebo)

# Calculate the test statistic
t_stat = numerator/denominator

# Print the test statistic
print(t_stat)

#Calculate the degrees of freedom
degrees_of_freedom = n_drug+n_placebo-2

# Calculate the p-value from the test stat

age_group_effects = pingouin.mwu(x=drug_safety[drug_safety['trx']=="Drug"]['age'], 
                              y=drug_safety[drug_safety['trx']=="Placebo"]['age'], 
                              alternative="two-sided")
#age_group_effects = ttest_results['T'].values[0], ttest_results['p-val'].values[0]
print(age_group_effects)