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
# Import packages
import numpy as np
import pandas as pd
from scipy.stats import norm
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")
# 1. Printando as primeiras linhas da tabela
print(drug_safety.head())
# Start coding here...
# 2. Plotagem da qtde do número de efeitos
plot_num_eff = sns.countplot(data=drug_safety, x='num_effects')
plot_num_eff.set(xlabel='Number of Adverse Effects')
plot_num_eff.set_title("Distribution of the Number of Effects Between the Groups")
plt.show()
# 3. two-sample Z-test verificando se a proporção de efeitos adverso varia entre os grupos de droga e placebo
'''
n = drug_safety.groupby('trx')['adverse_effects'].count()
n_drug = n['Drug']
n_placebo = n['Placebo']
#print(n)
p = drug_safety.groupby('trx')['adverse_effects'].value_counts(normalize=True)
p_drug = p['Drug','Yes']
p_placebo = p['Placebo','Yes']
#print(p)
SE = np.sqrt((p_drug*(1-p_drug))/n_drug + (p_placebo*(1-p_placebo))/n_placebo)
two_samp_z_stat = round((p_drug - p_placebo)/SE, 3)
two_samp_z_p_value = round(2*(1 - norm.cdf(two_samp_z_stat, loc=0, scale=1)), 3)
print(two_samp_z_stat)
print(two_samp_z_p_value)
'''
n_by_trx = drug_safety.groupby('trx')['adverse_effects'].count().values # valores totais
n_yes_by_trx = drug_safety.groupby('trx')['adverse_effects'].value_counts()[:,'Yes'].values # valores 'Yes'
two_samp_z_stat, two_samp_z_p_value = map(
lambda x: round(x, 3), # Função para arredondamento
proportions_ztest(count=n_yes_by_trx, nobs=n_by_trx, alternative='two-sided')
)
print(two_samp_z_stat)
print(two_samp_z_p_value) # H0 é confirmado, não há diferença entre o grupo placebo e o de teste
# 4. Chi-square para testar a independencia entre grupos:
expected, observed, stats = pingouin.chi2_independence(
data = drug_safety,
x = 'trx',
y = 'num_effects'
)
pearson_num_effect_trx = round(stats[stats['test'] == 'pearson'], 3) # para alpha=0.1, logo H0 não é refutado
print(pearson_num_effect_trx)
# 5. Look at the distribution of age in the Drug and Placebo groups
sns.histplot(data=drug_safety, x='age')
# 6. Verificar se há diferença significante entre as idades entre o grupo de drogas e o placebo
two_samp_ind_results = pingouin.mwu(
drug_safety.loc[drug_safety['trx'] == 'Drug', 'age'],
drug_safety.loc[drug_safety['trx'] == 'Placebo', 'age']
).round(3) # Para alpha = 0.2, H0 é refutado, significando que há diferença de idade
print(two_samp_ind_results)