#Ramicane appears to be the most effective treatment given the smaller mean and lower-leaning distrubtion of final tumor volumes. Correlation with weight differences between regimen samples should be considered.
#Ketapril appears to be the least effective treament; boasting the highest mean tumor volume and largest variance of tumor volume.
#Positive correlation exists between Capomulion mices' weight and mean tumor volume
# Dependencies and Setup
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
# Study data files
mouse_metadata_path = "Resources/Mouse_metadata.csv"
study_results_path = "Resources/Study_results.csv"
# Read the mouse data and the study results
mouse_metadata = pd.read_csv(mouse_metadata_path)
study_results = pd.read_csv(study_results_path)
# Combine the data into a single dataset
pyma = pd.merge(mouse_metadata,study_results,on="Mouse ID")
# Display the data table for preview
pyma.head()
# Checking the number of mice.
pd.DataFrame({"Total Subjects":[len(pyma["Mouse ID"].unique())]})
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint.
pyma_dupes = pyma[pyma.duplicated(["Mouse ID","Timepoint"])]
pyma_dupes = pd.DataFrame({"Mouse ID":pyma_dupes["Mouse ID"].unique()})
pyma_dupes
# Optional: Get all the data for the duplicate mouse ID.
pyma.loc[pyma["Mouse ID"]==str(pyma_dupes.iloc[0,0]),:]
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
drop_index = pyma[pyma["Mouse ID"]==str(pyma_dupes.iloc[0,0])].index
pyma_clean = pyma.drop(drop_index)
pyma_clean.reset_index(inplace=True)
# Checking the number of mice in the clean DataFrame.
pd.DataFrame({"Total Subjects":[len(pyma_clean["Mouse ID"].unique())]})
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
mean_regimen = pyma_clean.groupby("Drug Regimen")[["Tumor Volume (mm3)"]].mean()
med_regimen = pyma_clean.groupby("Drug Regimen")[["Tumor Volume (mm3)"]].median()
var_regimen = pyma_clean.groupby("Drug Regimen")[["Tumor Volume (mm3)"]].var()
stdev_regimen = pyma_clean.groupby("Drug Regimen")[["Tumor Volume (mm3)"]].std()
sem_regimen = pyma_clean.groupby("Drug Regimen")[["Tumor Volume (mm3)"]].sem()
regimen_summ = pd.merge(mean_regimen,med_regimen,on="Drug Regimen")
regimen_summ = pd.merge(regimen_summ,var_regimen,on="Drug Regimen")
regimen_summ.rename(columns={
"Tumor Volume (mm3)_x":"Mean Tumor Vol",
"Tumor Volume (mm3)_y":"Median of Tumor Vol"
},inplace=True)
regimen_summ = pd.merge(regimen_summ,stdev_regimen,on="Drug Regimen")
regimen_summ = pd.merge(regimen_summ,sem_regimen,on="Drug Regimen")
regimen_summ.rename(columns={
"Tumor Volume (mm3)_x":"Var of Tumor Vol",
"Tumor Volume (mm3)_y":"Std of Tumor Vol",
"Tumor Volume (mm3)":"SEM of Tumor Vol"
},inplace=True)
regimen_summ
# This method is the most straighforward, creating multiple series and putting them all together at the end.
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.
mice_count = pyma_clean.groupby("Drug Regimen")[["Mouse ID"]].nunique()
mice_count = mice_count.iloc[:,0]
x_axis = np.arange(len(mice_count))
tick_locations = [value for value in x_axis]
plt.xticks(tick_locations, mice_count.index,rotation="vertical")
plt.bar(x_axis, mice_count, color='purple', alpha=0.5, align="center")
plt.title("# of Mice per Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("# of Mice")
plt.show()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using Pandas.
mice_count_chart = mice_count.plot(kind="bar", title="# of Mice per Regimen",legend=False,color="purple")
mice_count_chart.set_ylabel("# of Mice")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas
mice_sex = pyma_clean.groupby("Sex")[["Mouse ID"]].nunique()
mice_sex_chart = mice_sex["Mouse ID"].plot(
kind="pie",label="", title="Mice per Sex", labels=mice_sex.index, autopct='%1.1f%%',legend=False,startangle=90,colors=["maroon","teal"])
# Generate a pie plot showing the distribution of female versus male mice using pyplot
fig1, ax1 = plt.subplots()
ax1.pie(mice_sex["Mouse ID"], labels=mice_sex.index, autopct='%1.1f%%',startangle=90,colors=["maroon","teal"])
plt.title("Mice per Sex")
plt.show()
# Calculate the final tumor volume of each mouse across four of the treatment regimens:
# Capomulin, Ramicane, Infubinol, and Ceftamin
# Start by getting the last (greatest) timepoint for each mouse
mouse_ids = list(pyma_clean["Mouse ID"].unique())
max_times = []
for mouse in mouse_ids:
maxidx = pyma_clean.loc[pyma_clean["Mouse ID"]==mouse,:]
max_times.append(maxidx["Timepoint"].idxmax())
max_times = list(pyma_clean.iloc[i,[1,2,7]] for i in max_times)
max_times = pd.DataFrame(data=max_times)
max_times.rename(columns={
"Tumor Volume (mm3)":"Final Tumor Vol"
},inplace=True)
max_times
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
# Put treatments into a list for for loop (and later for plot labels)
regimenz = ["Capomulin","Ramicane","Infubinol","Ceftamin"]
# Create empty list to fill with tumor vol data (for plotting)
final_vol = []
# Calculate the IQR and quantitatively determine if there are any potential outliers.
for drug in regimenz:
# Locate the rows which contain mice on each drug and get the tumor volumes
max_drug = max_times.loc[max_times["Drug Regimen"]==drug,:]
quartiles = max_drug["Final Tumor Vol"].quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq
lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"The lower quartile of Final Tumor Vol for {drug} is: {lowerq}")
print(f"The upper quartile of Final Tumor Vol for {drug} is: {upperq}")
print(f"The interquartile range of Final Tumor Vol for {drug} is: {iqr}")
print(f"The the median of Final Tumor Vol for {drug} is: {quartiles[0.5]} ")
print(f"\nValues below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# Determine outliers using upper and lower bounds
outlier_vol = max_drug.loc[(max_drug['Final Tumor Vol'] <
lower_bound) | (max_drug['Final Tumor Vol'] > upper_bound)]
if len(outlier_vol) == 0:
print(f"\n{drug} has no potential outliers\n")
print("-------------------------------------")
else:
print(f"\nPotential outliers for {drug}")
print(outlier_vol.to_string(index=False))
print("-------------------------------------")
# add subset
max_drug = max_drug.loc[:,"Final Tumor Vol"]
final_vol.append(max_drug)
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig1, ax1 = plt.subplots()
ax1.set_title('Final Tumor Volumes')
ax1.set_ylabel('Tumor Volume (mm3)')
ax1.set_xticklabels(regimenz)
outlier = dict(markerfacecolor='aqua')
bp = ax1.boxplot(final_vol, flierprops=outlier)
plt.show()
# Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
capomulin_mouse = pyma_clean.loc[pyma_clean["Mouse ID"]==pyma_clean.loc[pyma_clean["Drug Regimen"]=="Capomulin",:].iloc[0][1],:]
capo_vol = list(capomulin_mouse["Tumor Volume (mm3)"])
fig2, ax2 = plt.subplots()
ax2.set_title(f"{capomulin_mouse.iloc[0][1]}'s Tumor Vol Over Time")
ax2.set_ylabel('Tumor Volume (mm3)')
ax2.set_xlabel('Timepoint')
ax2.plot(capomulin_mouse["Timepoint"],capomulin_mouse["Tumor Volume (mm3)"],color="blue")
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
capo_regime = pyma_clean.loc[pyma_clean["Drug Regimen"]=="Capomulin",:].groupby("Mouse ID")[["Weight (g)","Tumor Volume (mm3)"]].mean()
plt.scatter(capo_regime["Weight (g)"],capo_regime["Tumor Volume (mm3)"],color="g")
plt.title("Weight vs Avg Tumor Vol")
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Avg Tumor Volume (mm3)")
plt.show()
# Calculate the correlation coefficient and linear regression model
# for mouse weight and average tumor volume for the Capomulin regimen
capo_slope, capo_int, capo_r, capo_p, capo_std_err = st.linregress(capo_regime["Weight (g)"],capo_regime["Tumor Volume (mm3)"])
capo_fit = capo_slope * capo_regime["Weight (g)"] + capo_int
plt.scatter(capo_regime["Weight (g)"],capo_regime["Tumor Volume (mm3)"],color="g")
plt.plot(capo_regime["Weight (g)"],capo_fit,"--",color="b")
plt.xticks(capo_regime["Weight (g)"], rotation=90)
plt.title("Weight vs Avg Tumor Vol")
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Avg Tumor Volume (mm3)")
plt.show()