# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # TASK 5 - MILESTONE 2
# ---

# # Packages used for analysis
# ---

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

# # Initial Hypothesis 
# ---

# **Health care remains a very important issue in todays world where the majority of the costs are thrown on patients. In the United States, health insurance is major concern given that public health insurance is not an option. This leaves millions of individuals without any health insurance if they are not able to afford insurance provided by the private sector. Could it be possible that different medical expenses are incurred based on geographic location, sex or age? Could individuals with different demographics be incurring different costs? These are all questions worth investigating and what we hope to answer through our analysis!**

# # DATASET
# ---

df1 = pd.read_csv('https://gist.githubusercontent.com/meperezcuello/82a9f1c1c473d6585e750ad2e3c05a41/raw/d42d226d0dd64e7f5395a0eec1b9190a10edbc03/Medical_Cost.csv')
df1

from project_functions import load_and_process
df2 = load_and_process('https://gist.githubusercontent.com/meperezcuello/82a9f1c1c473d6585e750ad2e3c05a41/raw/d42d226d0dd64e7f5395a0eec1b9190a10edbc03/Medical_Cost.csv')

# ---
# # QUESTIONS AND INQUIRIES 
# ---

# # What is the average medical cost of males and females in this set?

sns.barplot(x='sex', y='charges', data=df1, estimator=np.mean, palette='plasma')
plt.title('Medical Charges for Males and Females')

# **What this graph shows us is that the avergae paid on medical care is greater for men than for women**

# # What is the correlation between variables in this data set?

plt.figure(figsize=(8,6))
sns.set_context('paper', font_scale=1.4)
df_hm = df1.corr()
sns.heatmap(df_hm, annot=True, cmap='plasma', linecolor='white')
plt.title('Correlations Between Age, BMI, Number of Children, and Medical Charges')

# **This heatmap did not really help us much as the two variables we were concerned with didn't show up which were gender and whether the patient is a smoker or not**

# # Is there a correlation between BMI, SMOKING & MEDICAL CHARGES INCURRED? 

sns.set_context('paper', font_scale=1.4)
lm = sns.lmplot(x='bmi', y='charges', data=df1, col='sex', hue='smoker',
          height=8, aspect=0.6, palette='plasma')
fig = lm.fig 
fig.suptitle("Charges for Each Sex Based on BMI", fontsize=13)

# **<CENTER>From the graph above we can deduce that being smoker leads to greater medical charges & a higher BMI for both men and women</center>**

# # Is there a relationship between age and medical charges?  

sns.lineplot(data=df1, x='age', y='charges', hue='smoker')
plt.ylabel('Medical Charges')
plt.xlabel('Age')
plt.title('Relationship Between Medical Charges and Age for Smokers and Non-Smokers')

# **<center>This plot is gradually increasing with age. Therefore, there is a linear relationship between age and charges. The plot shows that there is a quite large difference between smokers and non-smokers. At most points, a smoker has almost double the charges compared to someone of the same age.</center>**

# # Is there a substantial difference in medical charges between males and females?

sns.catplot(data=df1, kind="bar", x="smoker", y="charges", estimator=np.mean, hue="sex", aspect=1.0)
plt.ylabel('Medical Charges')
plt.xlabel('Smoker')
plt.title('Medical Charges for Smokers Based on Sex')

# **<center>From the data, there isn't a substantial difference in medical charges between males and females. From the small differences between the sexes, males have higher medical expenses than females if they're a smoker but females have higher medical expenses when they are non-smokers. Though, there is quite a large difference between smokers and non-smokers.</center>** 

# # Is there an increase in medical charges, with an increase in children?

ax = sns.boxplot(x="children", y="charges", data=df1)
plt.title('Medical Charges Based on Number of Children Each Person Has')

# **The boxplot shows there is not a direct correlation between Medical Charges and amount of Children, in fact the lowest mean is that of the 5 children.**

# # Is there a correlation between BMI and Medical Charges?

sns.lmplot(x="bmi", y="charges", data=df1,
          ci=None, palette="muted", height=4,
           scatter_kws={"s": 50, "alpha": 1})
plt.title('Medical Charges Based on BMI')

# **Yes, as BMI increases, so too do the medical charges, at a near linear rate. Additionally, from the graph, the amount of lower medical charges appears fairly even for all BMI, but as the BMI increases, the higher medical charges become even more pronounced. For example at 20 BMI the highest charges appear to be just over 30,000, while above 30 BMI theres a few over 50,000.**

# # Conclusion

# **From the visualizations of the dataset, several conclusions can be made, regarding our initial inquiries. Firstly, the biggest factors that increase medical costs are smoking, age, and BMI, with smoking being the worst of the three. Secondly, there appears to be little to no correlation to medical charges based on sex or number of children. Males tend to have slightly higher medical charges, as do those with 2 children, but not substantially more. While not directly related to the hypothesis, there does also appear to be a correlation between smoking and BMI, and those that smoke and have a high BMI, tend to have much larger medical charges.**

StarcoderdataJupyterScriptsDedupFiltered