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")

# Getting the count of successes and total counts
# Drug
drug = drug_safety[(drug_safety['adverse_effects']=='Yes') & (drug_safety['trx']=='Drug')]
Drug = drug.groupby('trx')['adverse_effects'].value_counts()
# Placebo
placebo = drug_safety[(drug_safety['adverse_effects']=='Yes') & (drug_safety['trx']=='Placebo')]
Placebo = placebo.groupby('trx')['adverse_effects'].value_counts()

# Calculate the number of adverse effects in each group
num_adverse_drug = Drug.sum()
num_adverse_placebo = Placebo.sum()

# Calculate the total number of observation(rows) of participants in each group
total_drug = drug_safety[drug_safety['trx'] == 'Drug'].shape[0]
total_placebo = drug_safety[drug_safety['trx'] == 'Placebo'].shape[0]

# Perform the two-sample z-test for proportions
count = np.array([num_adverse_drug, num_adverse_placebo]) # Array of the sum of the Yes counts
nobs = np.array([total_drug, total_placebo]) # Array of the total no of rows in each group
stat, two_sample_p_value = proportions_ztest(count, nobs)
print('two_sample_p_value:',two_sample_p_value)

# Association between adverse effects and the groups:
# Find out if the number of adverse effects is independent of the treatment and control groups
# Performing a Chi-square test of independence to determine if num_effects and trx are independent
num_eff_by_trx = pingouin.chi2_independence(data=drug_safety, x='num_effects', y='trx')
num_effects_p_value = num_eff_by_trx[2]['pval'][0] # Extracting the p_val
print('num_effects_pval:',num_effects_p_value) 

# Inspecting whether age is normally distributed:
# To determine what test to use to confirm whether age differs significantly between the trx groups, you need to check if age is normally distributed in the trx groups

# Creating a histogram with seaborn
colors = ['#FFCC33', '#7C4F2A']
sns.set_style('darkgrid')
sns.set_palette(colors)
sns.histplot(data=drug_safety, x='age', hue='trx')
plt.show()
plt.clf()

# Testing for normality 
# Optionally - confirm the histogram's output by conducting a normality test
# To choose between unpaired t-test and Wilcoxon-Mann-Whitney test
normality_test = pingouin.normality(data=drug_safety, dv='age', group='trx', method='shapiro', alpha=0.05)
print(normality_test)

# Significant difference between the ages of both groups
# To ensure age wasn't a confounder, conduct a Mann-Whitney test to determine if age differed significantly between the trx groups.

# Select the age of the Drug group
age_trx = drug_safety[drug_safety['trx']=='Drug']['age']

# Select the age of the Placebo group
age_placebo = drug_safety[drug_safety['trx']=='Placebo']['age']

# Since the data distribution is not normal
# Conduct a two-sided Mann-Whitney U test
age_group_effects = pingouin.mwu(x=age_trx, y=age_placebo)

#Extract the p_val
age_group_effects_p_value = age_group_effects['p-val']
print(age_group_effects_p_value)