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:
| Column | Description |
|---|---|
sex | The gender of the individual |
age | The age of the individual |
week | The week of the drug testing |
trx | The treatment (Drug) and control (Placebo) groups |
wbc | The count of white blood cells |
rbc | The count of red blood cells |
adverse_effects | The presence of at least a single adverse effect |
num_effects | The 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.
Step 1: Import Libraries and Load Data
# 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")Explanation:
Libraries are imported for data manipulation, statistical tests, and visualization. The dataset is loaded into drug_safety.
Step 2: Test for Differences in Adverse Effects (Two-Proportion Z-Test)
# Count values and totals
adv_eff_by_trx = drug_safety.groupby("trx").adverse_effects.value_counts()
adv_eff_by_trx_totals = adv_eff_by_trx.groupby("trx").sum()
# Create arrays for counts and totals
yeses = [adv_eff_by_trx["Drug"]["Yes"], adv_eff_by_trx["Placebo"]["Yes"]]
n = [adv_eff_by_trx_totals["Drug"], adv_eff_by_trx_totals["Placebo"]]
# Perform Z-test
two_sample_results = proportions_ztest(yeses, n)
two_sample_p_value = two_sample_results[1]Explanation:
Counts and totals are calculated for each group. The two-proportion z-test determines if the proportion of adverse effects is significantly different between Drug and Placebo groups.
Step 3: Test for Independence of Number of Effects (Chi-square Test)
num_effects_groups = pingouin.chi2_independence(
data=drug_safety, x="num_effects", y="trx")
num_effects_p_value = num_effects_groups[2]["pval"][0]
# Create a histogram with Seaborn
sns.histplot(data=drug_safety, x="age", hue="trx")Explanation:
The Chi-square test assesses whether the number of adverse effects is independent of the treatment group.
Step 4: Test for Age Differences (Mann-Whitney U Test)
# Select age groups
age_trx = drug_safety.loc[drug_safety["trx"] == "Drug", "age"]
age_placebo = drug_safety.loc[drug_safety["trx"] == "Placebo", "age"]
# Visualize data distribution
sns.histplot(data=drug_safety, x="age", hue="trx")
# Perform Mann-Whitney U test
age_group_effects = pingouin.mwu(age_trx, age_placebo)
age_group_effects_p_value = age_group_effects["p-val"]Explanation:
A histogram visualizes the age distribution. The Mann-Whitney U test is used since the data is not normally distributed, assessing differences in age between Drug and Placebo groups.
Summary of Key Results
✅ two_sample_p_value: Difference in adverse effects between groups ✅ num_effects_p_value: Independence of number of effects and treatment ✅ age_group_effects_p_value: Difference in ages between groups