1. Import the necessary packages to read in the data, plot, and create a linear regression model


In [26]:
import pandas as pd
import pandas as pd
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
%matplotlib inline

2. Read in the hanford.csv file


In [3]:
df = pd.read_csv('../data/hanford.csv')

County: Name of county

Exposuere: Inde of exposure

Mortality: Cancer mortality per 100000 man-years


In [5]:
df


Out[5]:
County Exposure Mortality
0 Umatilla 2.49 147.1
1 Morrow 2.57 130.1
2 Gilliam 3.41 129.9
3 Sherman 1.25 113.5
4 Wasco 1.62 137.5
5 HoodRiver 3.83 162.3
6 Portland 11.64 207.5
7 Columbia 6.41 177.9
8 Clatsop 8.34 210.3

3. Calculate the basic descriptive statistics on the data


In [6]:
df.describe()


Out[6]:
Exposure Mortality
count 9.000000 9.000000
mean 4.617778 157.344444
std 3.491192 34.791346
min 1.250000 113.500000
25% 2.490000 130.100000
50% 3.410000 147.100000
75% 6.410000 177.900000
max 11.640000 210.300000

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


In [14]:
correlation = df.corr()
print(correlation)
df.plot(kind='scatter', x='Exposure', y='Mortality')


           Exposure  Mortality
Exposure   1.000000   0.926345
Mortality  0.926345   1.000000
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fc47a145fd0>

5. Create a linear regression model based on the available data to predict the mortality rate given a level of exposure


In [21]:
lm = smf.ols(formula="Mortality~Exposure",data=df).fit()
lm.params
intercept, height = lm.params

# Function using the built math. 
def simplest_predictor(exposure, height, intercept):
    height = float(height)
    intercept = float(intercept)
    exposure = float(exposure)
    return height*exposure+intercept

In [22]:
# Input the data
exposure = input("Please enter the exposure: ")
print("The mortality rate for your exposure lies at", simplest_predictor(exposure,height,intercept), ".")


Please enter the exposure: 5
The mortality rate for your exposure lies at 160.8729121746029 .

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


In [24]:
df.plot(kind="scatter",x="Exposure",y="Mortality")
plt.plot(df["Exposure"],height*df["Exposure"]+intercept,"-",color="darkgrey") #we create the best fit line from the values in the fit model


Out[24]:
[<matplotlib.lines.Line2D at 0x7fc47a018b38>]

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


In [ ]:
def predictiong_mortality_rate(exposure):
    return intercept + float()