<jupyter_start><jupyter_text>Housing Price Code to accompany Lecture on  Regression  <NAME> ()    credit: <NAME>. ()<jupyter_code>import numpy as np
import pandas as pd
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt
from sklearn.datasets import load_boston # boston housing data
from statsmodels.stats.outliers_influence import summary_table
import statsmodels.formula.api as smf
from mpl_toolkits.mplot3d import Axes3D

import warnings
warnings.filterwarnings('ignore')

%matplotlib inline
plt.style.use('ggplot')<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>boston = load_boston()
predictors = boston.data
response = boston.target #MEDV (median home values)
boston_data = np.column_stack([predictors,response])
col_names = np.append(boston.feature_names, 'MEDV')
boston_df = pd.DataFrame(boston_data, columns = col_names)
boston_df.head()<jupyter_output><empty_output><jupyter_text>Simple Linear RegressionRegress LSTAT (% of households with low economic status) on MEDV (median home values)<jupyter_code># using scikit-learn
x = boston_df.LSTAT.values
y = boston_df.MEDV.values
ols = LinearRegression(fit_intercept=True)
ols.fit(x[:,np.newaxis], y)
print("slope:    ", ols.coef_[0])
print("intercept:", ols.intercept_)
# using statsmodels
X = sm.add_constant(x)
ols = sm.OLS(y, X).fit()
print(ols.summary())
result, data, colnames = summary_table(ols, alpha=0.05)
print('column_names: ', colnames)
ci_low, ci_high = data[:,4:6].T

plt.figure(figsize=(10,8))
plt.scatter(x, y, facecolor='None', edgecolor='b', label='')
plt.xlabel('LSTAT')
plt.ylabel('MEDV')
plt.plot(x, ols.fittedvalues, color='r')
plt.plot(x, ci_low, '--', color='.75')
plt.plot(x, ci_high, '--', color='.75')<jupyter_output>column_names:  ['Obs', 'Dep Var\nPopulation', 'Predicted\nValue', 'Std Error\nMean Predict', 'Mean ci\n95% low', 'Mean ci\n95% upp', 'Predict ci\n95% low', 'Predict ci\n95% upp', 'Residual', 'Std Error\nResidual', 'Student\nResidual', "Cook's\nD"]<jupyter_text>Multiple Linear RegressionRegress LSTAT and AGE on MEDV<jupyter_code>X = sm.add_constant(boston_df[['LSTAT','AGE']])
ols = sm.OLS(y, X).fit()
print(ols.summary())
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(111, projection='3d',azim=-60, elev=5)
ax.scatter(X.loc[:,'LSTAT'], X.loc[:,'AGE'], y, facecolors='b', edgecolor='k', depthshade=True);
ax.set_xlabel('LSTAT');
ax.set_ylabel('AGE')
ax.set_zlabel('MEDV')
xx1, xx2 = np.meshgrid(np.linspace(X.LSTAT.min(), X.LSTAT.max(), 100), 
                       np.linspace(X.AGE.min(), X.AGE.max(), 100))
Z = ols.params[0] + ols.params[1] * xx1 + ols.params[2] * xx2
surf = ax.plot_surface(xx1, xx2, Z, cmap=plt.cm.RdBu_r, alpha=0.75, linewidth=0)<jupyter_output><empty_output><jupyter_text>Interaction Terms<jupyter_code>ols = smf.ols(formula='MEDV ~ LSTAT*AGE', data=boston_df).fit()
print(ols.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:                   MEDV   R-squared:                       0.556
Model:                            OLS   Adj. R-squared:                  0.553
Method:                 Least Squares   F-statistic:                     209.3
Date:                Wed, 06 Feb 2019   Prob (F-statistic):           4.86e-88
Time:                        23:58:27   Log-Likelihood:                -1635.0
No. Observations:                 506   AIC:                             3278.
Df Residuals:                     502   BIC:                             3295.
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|[...]<jupyter_text>Polynomial Regression<jupyter_code>quadratic_fit = smf.ols('MEDV ~ LSTAT + I(LSTAT**2)', data=boston_df).fit()
print(quadratic_fit.summary())
plt.figure(figsize=(10,8))
plt.scatter(x, y, facecolor='None', edgecolor='b', label='')
plt.xlabel('LSTAT')
plt.ylabel('MEDV')
plt.plot(x, quadratic_fit.fittedvalues, color='g', marker='o', linestyle='none')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17741559953434227, 'fraction_numerical': 0.024679860302677534, 'mean_word_length': 3.498429319371728, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '428416', 'license': 'MIT', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1469, 'n_words': 288, 'path': 'codes/Regression/Python/housing.ipynb', 'repo_name': 'xiaoluli97/data-analysis', 'size': '4304'}