Exercise 3.13


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import statsmodels.formula.api as smf

3.13 a-d


In [2]:
np.random.seed(1)
X = np.random.normal(size=100)
eps = np.random.normal(scale=0.5, size=100)
Y = -1 + 0.5*X + eps
plt.plot(X, Y, 'o')


Out[2]:
[<matplotlib.lines.Line2D at 0x899be80>]

3.13 e


In [6]:
data = pd.DataFrame({'X': X, 'Y': Y})
model = smf.ols('Y ~ X', data=data)
fit = model.fit()
print(fit.summary())


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.522
Model:                            OLS   Adj. R-squared:                  0.517
Method:                 Least Squares   F-statistic:                     107.0
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           2.20e-17
Time:                        09:40:10   Log-Likelihood:                -65.124
No. Observations:                 100   AIC:                             134.2
Df Residuals:                      98   BIC:                             139.5
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     -0.9265      0.047    -19.717      0.000        -1.020    -0.833
X              0.5477      0.053     10.342      0.000         0.443     0.653
==============================================================================
Omnibus:                        0.898   Durbin-Watson:                   2.157
Prob(Omnibus):                  0.638   Jarque-Bera (JB):                0.561
Skew:                          -0.172   Prob(JB):                        0.755
Kurtosis:                       3.127   Cond. No.                         1.15
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

3.13 f


In [4]:
sns.regplot(x='X', y='Y', data=data)


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x9c125c0>

3.13 g


In [7]:
model2 = smf.ols('Y ~ X + I(X**2)', data=data)
fit2 = model2.fit()
print(fit2.summary())


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.522
Model:                            OLS   Adj. R-squared:                  0.512
Method:                 Least Squares   F-statistic:                     52.96
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           2.83e-16
Time:                        09:40:24   Log-Likelihood:                -65.107
No. Observations:                 100   AIC:                             136.2
Df Residuals:                      97   BIC:                             144.0
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     -0.9325      0.058    -16.158      0.000        -1.047    -0.818
X              0.5468      0.053     10.229      0.000         0.441     0.653
I(X ** 2)      0.0077      0.043      0.181      0.856        -0.077     0.092
==============================================================================
Omnibus:                        0.893   Durbin-Watson:                   2.152
Prob(Omnibus):                  0.640   Jarque-Bera (JB):                0.552
Skew:                          -0.170   Prob(JB):                        0.759
Kurtosis:                       3.132   Cond. No.                         2.10
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

3.13 h


In [8]:
np.random.seed(1)
X = np.random.normal(size=100)
eps = np.random.normal(scale=0.05, size=100)
Y = -1 + 0.5*X + eps
data = pd.DataFrame({'X': X, 'Y': Y})
model = smf.ols('Y ~ X', data=data)
fit = model.fit()
print(fit.summary())
sns.regplot(x='X', y='Y', data=data)


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.989
Model:                            OLS   Adj. R-squared:                  0.989
Method:                 Least Squares   F-statistic:                     9084.
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           1.97e-98
Time:                        09:41:16   Log-Likelihood:                 165.13
No. Observations:                 100   AIC:                            -326.3
Df Residuals:                      98   BIC:                            -321.1
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     -0.9926      0.005   -211.252      0.000        -1.002    -0.983
X              0.5048      0.005     95.309      0.000         0.494     0.515
==============================================================================
Omnibus:                        0.898   Durbin-Watson:                   2.157
Prob(Omnibus):                  0.638   Jarque-Bera (JB):                0.561
Skew:                          -0.172   Prob(JB):                        0.755
Kurtosis:                       3.127   Cond. No.                         1.15
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x9cd2eb8>

3.13 i


In [9]:
np.random.seed(1)
X = np.random.normal(size=100)
eps = np.random.normal(scale=5, size=100)
Y = -1 + 0.5*X + eps
data = pd.DataFrame({'X': X, 'Y': Y})
model = smf.ols('Y ~ X', data=data)
fit = model.fit()
print(fit.summary())
sns.regplot(x='X', y='Y', data=data)


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.034
Model:                            OLS   Adj. R-squared:                  0.024
Method:                 Least Squares   F-statistic:                     3.404
Date:                Sun, 09 Aug 2015   Prob (F-statistic):             0.0681
Time:                        09:41:58   Log-Likelihood:                -295.38
No. Observations:                 100   AIC:                             594.8
Df Residuals:                      98   BIC:                             600.0
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     -0.2649      0.470     -0.564      0.574        -1.197     0.668
X              0.9771      0.530      1.845      0.068        -0.074     2.028
==============================================================================
Omnibus:                        0.898   Durbin-Watson:                   2.157
Prob(Omnibus):                  0.638   Jarque-Bera (JB):                0.561
Skew:                          -0.172   Prob(JB):                        0.755
Kurtosis:                       3.127   Cond. No.                         1.15
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x9d37ef0>

In [ ]: