<jupyter_start><jupyter_text>Observations and Insights  * Capomulin was effective in reducing the volume of the SCC tumor in a 45 day period when plotting data for one mouse [LineChart](Figures/LineChart.png)* Based on the average final SCC tumor volume for the four drug regimens of interest (i.e. Capomulin, Ramicane, Infubinol, and Ceftamin), Capomuline and Ramicane suggested are both the most effective.[Box and Whisker](Figures/BoxWhiskerPlot.png)* Mouse weight strongly correlates (R-squared is 0.84) with the average SCC tumor volume for mice on the the Capomulin treatment. Therefore, the mouse weight needs to be another controlled variable to prevent skewing the results when comparing treatments.[Regression](Figures/ScatterWeightTumorVolRegression.png)<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np

# Study data files
mouse_metadata_path = "data/Mouse_metadata.csv"
study_results_path = "data/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
combined_data = pd.merge(mouse_metadata,study_results,how='outer',on='Mouse ID')

# Display the data table for preview
combined_data
# Checking the number of mice.
total_mice = mouse_metadata['Mouse ID'].count()
total_mice
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
dup_data = combined_data.loc[combined_data.duplicated(subset=["Mouse ID","Timepoint"])]
# Optional: Get all the data for the duplicate mouse ID. 
dup_mouse = combined_data.loc[combined_data["Mouse ID"]=="g989"]
dup_mouse
# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data = combined_data.drop(index=dup_mouse.index)

# Checking the number of mice in the clean DataFrame.
total_mice = len(clean_data['Mouse ID'].unique())
total_mice<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code># Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen

# This method is the most straighforward, creating multiple series and putting them all together at the end.

# Initialize lists
regimens = clean_data["Drug Regimen"].unique()
mean_vol = []
median_vol = []
var_vol = []
std_vol = []
sems = []

# Iterate through the different treatments to calculate the summary statistics
for regimen in regimens:
    df = clean_data[clean_data["Drug Regimen"]==regimen]
    mean_vol.append(df["Tumor Volume (mm3)"].mean())
    median_vol.append(df["Tumor Volume (mm3)"].median())
    var_vol.append(np.var(df["Tumor Volume (mm3)"],ddof=0))
    std_vol.append(np.std(df["Tumor Volume (mm3)"],ddof=0))
    sems.append(st.sem(df["Tumor Volume (mm3)"]))

# Create a dataframe for the summary statistics
summ_stats_df = pd.DataFrame({
    "Drug Regimen":regimens,
    "Mean Volume": mean_vol,
    "Median Volume": median_vol,
    "Variance": var_vol,
    "Standard Deviation": std_vol,
    "Standard Error": sems
})

summ_stats_df
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen

# This method produces everything in a single groupby function

regimen_df = clean_data.groupby(['Drug Regimen'])

# Use the grouped object to calculate the summary statistics
mean_vol = regimen_df["Tumor Volume (mm3)"].mean()
median_vol = regimen_df["Tumor Volume (mm3)"].median()
var_vol = regimen_df["Tumor Volume (mm3)"].var(ddof=0)
std_vol = regimen_df["Tumor Volume (mm3)"].std(ddof=0)
sems = regimen_df["Tumor Volume (mm3)"].sem()

# Create a dataframe for the summary statistics
summ_stats_df = pd.DataFrame({
    "Mean Volume": mean_vol,
    "Median Volume": median_vol,
    "Variance": var_vol,
    "Standard Deviation": std_vol,
    "Standard Error": sems
})

summ_stats_df<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 

# Create a dataframe indexed by drug treatment to use pandas bar plotting
regimen_count = regimen_df['Mouse ID'].count()
bar_data_df = pd.DataFrame({
    "Count":regimen_count
})

bar_data_df.plot(kind='bar',figsize=(10,3),rot=45,width=0.5)
plt.title("Total number of mice for each treatment")

# Save the figure
plt.savefig("Figures/PandaBarChart.png")
plt.show()
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.

plt.figure(figsize=(10,3))
plt.bar(regimens,regimen_count,align='center',width=0.5)
plt.xticks(rotation=45)
plt.xlim(-0.75,len(regimens)-0.25)
plt.ylim(0, max(regimen_count)*1.05)

# Set title and labels
plt.title("Total number of mice for each treatment")
plt.xlabel("Drug Regimen")

#Save figure
plt.savefig("Figures/PyplotBarChart.png")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pandas

# Get count of female and male mice
gender_data = clean_data.groupby('Sex')
gender_count = gender_data['Sex'].count()

# Create dataframe to count the distribution of female and male mice
gender_count_df = pd.DataFrame(gender_count)

gender_count_df.plot(kind='pie', y="Sex", title= "Male versus Female Mice Distribution", autopct='%1.1f%%', startangle= 110,shadow=True, fontsize=14,legend =False)
plt.axis("equal")

# Save figure
plt.savefig("Figures/PandaPieChart.png")
plt.show()
# Generate a pie plot showing the distribution of female versus male mice using pyplot

# Get the count for male and female mice into an array
genders = gender_count_df.index.values

plt.pie(gender_count,labels=genders, autopct="%1.1f%%", shadow=True, startangle=110)
plt.rcParams['font.size'] = 14
plt.title("Male versus Female Mice Distribution")
plt.ylabel("Sex")
plt.axis("equal")

# Save figure
plt.savefig("figures/PyplotPieChart.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code># 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
mouseid_df = clean_data.groupby(["Mouse ID"])
tumor_data = mouseid_df.last()
tumor_data

# Put treatments into a list for for loop (and later for plot labels)
treatments = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]

# Create empty list to fill with tumor vol data (for plotting)
max_tumor_vol = []


for treatment in treatments:
    temp_df = tumor_data[tumor_data["Drug Regimen"]==treatment]["Tumor Volume (mm3)"]
    
    # Locate the rows which contain mice on each drug and get the tumor volumes
    max_tumor_vol.append(list(temp_df))
    
    # Calculate the quartiles
    quartiles =  temp_df.quantile([0.25,0.5,0.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    
    # Calculate the IQR and quantitatively determine if there are any potential outliers.
    iqr = upperq - lowerq
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    
    # Determine outliers using upper and lower bounds
    print(f"{treatment} potential outliers: {temp_df[(temp_df < lower_bound) | (temp_df > upper_bound)]}")
# 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 Volume by Drug Regimen')
ax1.set_ylabel('Tumor Volume (mm3)')
ax1.set_xlabel('Drug Regimen')
ax1.boxplot(max_tumor_vol)
plt.xticks([1,2,3,4],treatments)

# Save the figure
plt.savefig("Figures/BoxWhiskerPlot.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin

# Filter the original dataframe to only include Campomuline data
capomulin_data = clean_data[clean_data['Drug Regimen'] == 'Capomulin']

# Create capomuline dataframe that only has data for Mouse ID s185
capomulin_data_1 = capomulin_data[capomulin_data['Mouse ID']=="s185"]

# List comprehension to define the x and y axes
timepoints = [value for value in capomulin_data_1['Timepoint']]
tumor_vol = [value for value in capomulin_data_1['Tumor Volume (mm3)']]

plt.plot(timepoints,tumor_vol,marker='o',label='s185')
plt.legend(loc="best")
plt.title("Timepoints vs Tumor Volume")
plt.xlabel("Timepoint")
plt.ylabel("Tumor Volume (mm3)")
plt.grid(True)

plt.savefig("Figures/LineChart.png")
plt.show()
# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen

# Group by Mouse ID to collect weight and average tumor volume for each mouse on Capomulin
capomulin_mouse_id = capomulin_data.groupby(['Mouse ID'])
mouse_weight = capomulin_mouse_id["Weight (g)"].mean()
tumor_vol = round(capomulin_mouse_id["Tumor Volume (mm3)"].mean(),3)

plt.scatter(mouse_weight,
            tumor_vol,
            marker='o',
            facecolors='r',
            edgecolors='black')

plt.title("Mouse Weight vs. Avg. Tumor Volume")
plt.xlabel("Mouse weight (g)")
plt.ylabel("Tumor Volume (mm3)")

# Save the figure
plt.savefig("Figures/ScatterWeightTumorVol.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient and linear regression model 
# for mouse weight and average tumor volume for the Capomulin regimen
correlation = st.pearsonr(mouse_weight, tumor_vol)
print(f"The correlation between both factors is {round(correlation[0],2)}")


(slope, intercept, rvalue, pvalue, stderr) = st.linregress(mouse_weight,tumor_vol)
regress_values = mouse_weight * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(mouse_weight, tumor_vol)
plt.plot(mouse_weight,regress_values,"r-")
plt.annotate(line_eq,(17,37),fontsize=15,color="black")

plt.title("Mouse Weight vs Avg. Tumor Volume")
plt.xlabel("Mouse Weight (g)")
plt.ylabel("Average Tumor Volume (mm3)")
plt.grid(True)

# Save the figure
plt.savefig("Figures/ScatterWeightTumorVolRegression.png")
plt.show()<jupyter_output>The correlation between both factors is 0.84
{'fraction_non_alphanumeric': 0.10742434562615549, 'fraction_numerical': 0.011773863968084072, 'mean_word_length': 5.65242718446602, 'pattern_counts': {'":': 12, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '315769', 'license': 'ADSL', 'n_tokens_mistral': 3539, 'n_tokens_neox': 3258, 'n_words': 1003, 'path': 'Pymaceuticals/.ipynb_checkpoints/pymaceuticals_starter-checkpoint.ipynb', 'repo_name': 'joshuajonme/matplotlib-challenge', 'size': '10277'}