In [1]:
import pandas as pd
import numpy as np
from matplotlib.pyplot import subplots
In [2]:
data = pd.read_csv('family_heights.csv')
data.head(3)
Out[2]:
| family | father | mother | midparentHeight | children | childNum | sex | childHeight | |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 78.5 | 67.0 | 75.43 | 4 | 1 | male | 73.2 |
| 1 | 1 | 78.5 | 67.0 | 75.43 | 4 | 2 | female | 69.2 |
| 2 | 1 | 78.5 | 67.0 | 75.43 | 4 | 3 | female | 69.0 |
In [3]:
coefficients = np.polyfit(data['midparentHeight'], data['childHeight'], 1)
coefficients
Out[3]:
array([ 0.6373609 , 22.63624055])
In [6]:
data1 = data.assign(regressionEstimate = coefficients[0]*data['midparentHeight'] + coefficients[1])
data1.head(10)
Out[6]:
| family | father | mother | midparentHeight | children | childNum | sex | childHeight | regressionEstimate | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 78.5 | 67.0 | 75.43 | 4 | 1 | male | 73.2 | 70.712373 |
| 1 | 1 | 78.5 | 67.0 | 75.43 | 4 | 2 | female | 69.2 | 70.712373 |
| 2 | 1 | 78.5 | 67.0 | 75.43 | 4 | 3 | female | 69.0 | 70.712373 |
| 3 | 1 | 78.5 | 67.0 | 75.43 | 4 | 4 | female | 69.0 | 70.712373 |
| 4 | 2 | 75.5 | 66.5 | 73.66 | 4 | 1 | male | 73.5 | 69.584244 |
| 5 | 2 | 75.5 | 66.5 | 73.66 | 4 | 2 | male | 72.5 | 69.584244 |
| 6 | 2 | 75.5 | 66.5 | 73.66 | 4 | 3 | female | 65.5 | 69.584244 |
| 7 | 2 | 75.5 | 66.5 | 73.66 | 4 | 4 | female | 65.5 | 69.584244 |
| 8 | 3 | 75.0 | 64.0 | 72.06 | 2 | 1 | male | 71.0 | 68.564467 |
| 9 | 3 | 75.0 | 64.0 | 72.06 | 2 | 2 | female | 68.0 | 68.564467 |
In [7]:
data2 = data1.assign(residuals = data1['childHeight'] - data1['regressionEstimate'])
data2.head(5)
Out[7]:
| family | father | mother | midparentHeight | children | childNum | sex | childHeight | regressionEstimate | residuals | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 78.5 | 67.0 | 75.43 | 4 | 1 | male | 73.2 | 70.712373 | 2.487627 |
| 1 | 1 | 78.5 | 67.0 | 75.43 | 4 | 2 | female | 69.2 | 70.712373 | -1.512373 |
| 2 | 1 | 78.5 | 67.0 | 75.43 | 4 | 3 | female | 69.0 | 70.712373 | -1.712373 |
| 3 | 1 | 78.5 | 67.0 | 75.43 | 4 | 4 | female | 69.0 | 70.712373 | -1.712373 |
| 4 | 2 | 75.5 | 66.5 | 73.66 | 4 | 1 | male | 73.5 | 69.584244 | 3.915756 |
In [11]:
fig, ax = subplots(figsize = (5, 5))
ax.scatter(data2['midparentHeight'], data2['residuals'])
ax.axhline(y=0, color='red', linewidth = 3);
In [12]:
# We showed in class that the mean of the residuals equals 0.
# Let us check this for the heights data
print('Mean of the residuals = ', data2['residuals'].mean())
Mean of the residuals = -7.323002542212767e-14
Due to precision errors, the value does not come out to be exactly 0, but it is really close!
In [13]:
# We showed in class that the correlation between the predictor and the residuals is 0
# Let us check for the heights data
print('Correlation of midparentHeights and residuals = ', data2['midparentHeight'].corr(data2['residuals']))
Correlation of midparentHeights and residuals = -1.3549018592547941e-15
Again, due to precision errors, the value does not come out to be exactly 0, but it is really close!
In [ ]: