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


In [14]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt 
import statsmodels.formula.api as smf 
import numpy as np
import scipy as sp

2. Read in the hanford.csv file


In [3]:
df = pd.read_csv("hanford.csv")

In [4]:
df


Out[4]:
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 [5]:
df.describe()


Out[5]:
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 [6]:
df.corr()


Out[6]:
Exposure Mortality
Exposure 1.000000 0.926345
Mortality 0.926345 1.000000

In [9]:
df.plot(kind="scatter",x="Exposure",y="Mortality")


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x10dc3f780>

In [18]:
r =


Out[18]:
Exposure Mortality
Exposure 1.000000 0.926345
Mortality 0.926345 1.000000

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


In [16]:
lm = smf.ols(formula="Exposure-Mortality",data=df).fit()
intercept, slop, smf.ols


---------------------------------------------------------------------------
PatsyError                                Traceback (most recent call last)
<ipython-input-16-4a5209c96e84> in <module>()
----> 1 lm = smf.ols(formula="Exposure-Mortality",data=df).fit()
      2 intercept, slop, smf.ols

/Users/skkandrach/.virtualenvs/lede/lib/python3.5/site-packages/statsmodels/base/model.py in from_formula(cls, formula, data, subset, *args, **kwargs)
    145         (endog, exog), missing_idx = handle_formula_data(data, None, formula,
    146                                                          depth=eval_env,
--> 147                                                          missing=missing)
    148         kwargs.update({'missing_idx': missing_idx,
    149                        'missing': missing})

/Users/skkandrach/.virtualenvs/lede/lib/python3.5/site-packages/statsmodels/formula/formulatools.py in handle_formula_data(Y, X, formula, depth, missing)
     63         if data_util._is_using_pandas(Y, None):
     64             result = dmatrices(formula, Y, depth, return_type='dataframe',
---> 65                                NA_action=na_action)
     66         else:
     67             result = dmatrices(formula, Y, depth, return_type='dataframe',

/Users/skkandrach/.virtualenvs/lede/lib/python3.5/site-packages/patsy/highlevel.py in dmatrices(formula_like, data, eval_env, NA_action, return_type)
    310                                       NA_action, return_type)
    311     if lhs.shape[1] == 0:
--> 312         raise PatsyError("model is missing required outcome variables")
    313     return (lhs, rhs)

PatsyError: model is missing required outcome variables

In [11]:
lm.params


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-62e627bdceb7> in <module>()
----> 1 lm.params

NameError: name 'lm' is not defined

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


In [ ]:

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


In [ ]:
def predict_mr(exposure):
    return intercept + float(expsure) * slope

In [ ]:
predict_mr(10)