<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 1. Import the necessary packages to read in the data, plot, and create a linear regression model
Step1:  2. Read in the hanford.csv file
Step2:  <img src="images/hanford_variables.png">
3. Calculate the basic descriptive statistics on the data
Step3:  4. Calculate the coefficient of correlation (r) and generate the scatter plot. Does there seem to be a correlation worthy of investigation?
Step4:  5. Create a linear regression model based on the available data to predict the mortality rate given a level of exposure
Step5:  6. Plot the linear regression line on the scatter plot of values. Calculate the r^2 (coefficient of determination)
Step6:  7. Predict the mortality rate (Cancer per 100,000 man years) given an index of exposure = 10
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt # package for doing plotting (necessary for adding the line)
import statsmodels.formula.api as smf # package we'll be using for linear regression
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import numpy as np
plt.style.use('ggplot')
import dateutil.parser
import math
import random
import matplotlib.ticker as plticker
matplotlib.rcParams['ps.fonttype'] = 42
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.describe()
df.median()
rang= df['Mortality'].max() - df['Mortality'].min()
rang
iqr_m = df['Mortality'].quantile(q=0.75)- df['Mortality'].quantile(q=0.25)
iqr_m
iqr_e = df['Exposure'].quantile(q=0.75)- df['Exposure'].quantile(q=0.25)
iqr_e
UAL_m= (iqr_m*1.5) + df['Mortality'].quantile(q=0.75)
UAL_m
UAL_e= (iqr_m*1.5) + df['Exposure'].quantile(q=0.75)
UAL_e
LAL_m= df['Mortality'].quantile(q=0.25) - (iqr_e*1.5)  
LAL_m
LAL_e= df['Exposure'].quantile(q=0.25) - (iqr_e*1.5)  
LAL_e
len(df[df['Mortality']> UAL_m]) 
len(df[df['Exposure']> UAL_e]) 
len(df[df['Mortality']< LAL_m]) 
len(df[df['Mortality'] > UAL_m])
Explanation: <img src="images/hanford_variables.png">
3. Calculate the basic descriptive statistics on the data
End of explanation
df.corr()
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()
lm.params
def exposure_predict(exposure):
    df = pd.read_csv("data/hanford.csv")
    lm = smf.ols(formula="Mortality~Exposure",data=df).fit()
    mortality = exposure * lm.params.Exposure + lm.params.Intercept
    return mortality
Explanation: 5. Create a linear regression model based on the available data to predict the mortality rate given a level of exposure
End of explanation
intercept, slope = lm.params
df.plot(kind="scatter",x="Exposure",y="Mortality")
plt.plot(df["Exposure"],slope*df["Exposure"]+intercept,"-",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
exposure_predict(10)
Explanation: 7. Predict the mortality rate (Cancer per 100,000 man years) given an index of exposure = 10
End of explanation
<END_TASK>