import pandas as pd
import statsmodels.formula.api as smf
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt

"""
Explanation: 1. Import the necessary packages to read in the data, plot, and create a linear regression model
End of explanation
"""


df = pd.read_csv('../data/hanford.csv')

df.head()

"""
Explanation: 2. Read in the hanford.csv file
End of explanation
"""


df.mean()

"""
Explanation: <img src="images/hanford_variables.png">
3. Calculate the basic descriptive statistics on the data
Central Tendency:

Mean
End of explanation
"""


df.median()

"""
Explanation: Median
End of explanation
"""


df.mode()

"""
Explanation: Mode
End of explanation
"""


max(df['Exposure']) - min(df['Exposure'])

max(df['Mortality']) - min(df['Mortality'])

"""
Explanation: Spread:

Range
End of explanation
"""


df['Exposure'].quantile(q=0.75)  - df['Exposure'].quantile(q=0.25) 

df['Mortality'].quantile(q=0.75)  - df['Mortality'].quantile(q=0.25) 

"""
Explanation: Interquartile Range
End of explanation
"""


df.std()

"""
Explanation: Standard Deviation
End of explanation
"""


df.corr()

df.plot(kind = 'scatter', x = 'Exposure', y = 'Mortality')

"""
Explanation: 4. Calculate the coefficient of correlation (r) and generate the scatter plot. Does there seem to be a correlation worthy of investigation?
End of explanation
"""


lm = smf.ols(formula = 'Mortality~Exposure', data = df).fit()

b, m = lm.params

def predicted_mortality_rate(exposure):
    y = m * exposure + b
    return y

"""
Explanation: Yes.
5. Create a linear regression model based on the available data to predict the mortality rate given a level of exposure
End of explanation
"""


df.plot(kind = 'scatter', x = 'Exposure', y = 'Mortality')
plt.plot(df['Exposure'], m * df['Exposure'] + b, '-', color = 'red')

"""
Explanation: 6. Plot the linear regression line on the scatter plot of values. Calculate the r^2 (coefficient of determination)
End of explanation
"""


predicted_mortality_rate(10)

"""
Explanation: 7. Predict the mortality rate (Cancer per 100,000 man years) given an index of exposure = 10
End of explanation
"""


