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


In [20]:
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
%matplotlib inline
import statsmodels.formula.api as smf

2. Read in the hanford.csv file


In [21]:
df = pd.read_csv('hanford.csv')

3. Calculate the basic descriptive statistics on the data


In [22]:
df.describe()


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


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

r = 0.926345


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


Out[24]:
Intercept    114.715631
Exposure       9.231456
dtype: float64

In [25]:
intercept, slope = lm.params

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


Out[26]:
<matplotlib.axes._subplots.AxesSubplot at 0x10c047eb8>

Yes, there does seem to be a correlation worthy of investigation.

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


In [31]:
exposure = int(input('What is the exposure level? '))
mortality = slope * exposure + intercept
print('If the exposure is ' + str(exposure) + ' the mortality rate is probably around ' + str(round(mortality, 2)))


What is the exposure level? 4
If the exposure is 4 the mortality rate is probably around 151.64

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


In [28]:
df.plot(kind="scatter",x="Exposure",y="Mortality")
plt.plot(df["Exposure"],slope*df["Exposure"]+intercept,"-",color="darkgrey") 

plt.title('Correlation between exposure and mortality rate')
plt.xlabel('Exposure')
plt.ylabel('Mortality Rate')


Out[28]:
<matplotlib.text.Text at 0x110abc470>

In [ ]:
0.926345 ** 2

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


In [30]:
exposure = 10
mortality = slope * exposure + intercept
print('If the exposure is ' + str(exposure) + ' the mortality rate is probably around ' + str(round(mortality, 2)))


If the exposure is 10 the mortality rate is probably around 207.03