Exercise 3.11


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.11 a


In [2]:
np.random.seed(1)
x = np.random.rand(100)
y = 2*x + np.random.rand(100)
data = pd.DataFrame({'x': x, 'y': y})
model = smf.ols('y ~ x + 0', data=data)
fit = model.fit()
print(fit.summary())


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.935
Model:                            OLS   Adj. R-squared:                  0.934
Method:                 Least Squares   F-statistic:                     1423.
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           1.50e-60
Time:                        09:46:54   Log-Likelihood:                -52.254
No. Observations:                 100   AIC:                             106.5
Df Residuals:                      99   BIC:                             109.1
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
x              2.7227      0.072     37.718      0.000         2.579     2.866
==============================================================================
Omnibus:                        6.500   Durbin-Watson:                   1.563
Prob(Omnibus):                  0.039   Jarque-Bera (JB):                2.869
Skew:                           0.057   Prob(JB):                        0.238
Kurtosis:                       2.178   Cond. No.                         1.00
==============================================================================

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

3.11 b-e


In [3]:
model2 = smf.ols('x ~ y + 0', data=data)
fit2 = model2.fit()
print(fit2.summary())


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      x   R-squared:                       0.935
Model:                            OLS   Adj. R-squared:                  0.934
Method:                 Least Squares   F-statistic:                     1423.
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           1.50e-60
Time:                        09:47:38   Log-Likelihood:                 51.273
No. Observations:                 100   AIC:                            -100.5
Df Residuals:                      99   BIC:                            -97.94
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
y              0.3434      0.009     37.718      0.000         0.325     0.361
==============================================================================
Omnibus:                        4.335   Durbin-Watson:                   1.767
Prob(Omnibus):                  0.114   Jarque-Bera (JB):                2.285
Skew:                          -0.049   Prob(JB):                        0.319
Kurtosis:                       2.266   Cond. No.                         1.00
==============================================================================

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

3.11 f


In [6]:
modelp = smf.ols('y ~ x', data=data)
fitp = modelp.fit()
print('Model 1')
print(fitp.summary())
print()
print('Model 2')
model2p = smf.ols('x ~ y', data=data)
fit2p = model2p.fit()
print(fit2p.summary())


Model 1
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.772
Model:                            OLS   Adj. R-squared:                  0.769
Method:                 Least Squares   F-statistic:                     330.9
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           3.49e-33
Time:                        09:50:31   Log-Likelihood:                -26.736
No. Observations:                 100   AIC:                             57.47
Df Residuals:                      98   BIC:                             62.68
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      0.4978      0.062      8.078      0.000         0.376     0.620
x              1.9733      0.108     18.191      0.000         1.758     2.189
==============================================================================
Omnibus:                       92.818   Durbin-Watson:                   1.766
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                8.085
Skew:                          -0.030   Prob(JB):                       0.0176
Kurtosis:                       1.608   Cond. No.                         4.26
==============================================================================

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

Model 2
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      x   R-squared:                       0.772
Model:                            OLS   Adj. R-squared:                  0.769
Method:                 Least Squares   F-statistic:                     330.9
Date:                Sun, 09 Aug 2015   Prob (F-statistic):           3.49e-33
Time:                        09:50:31   Log-Likelihood:                 54.203
No. Observations:                 100   AIC:                            -104.4
Df Residuals:                      98   BIC:                            -99.20
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept     -0.0836      0.034     -2.432      0.017        -0.152    -0.015
y              0.3910      0.021     18.191      0.000         0.348     0.434
==============================================================================
Omnibus:                       11.227   Durbin-Watson:                   1.726
Prob(Omnibus):                  0.004   Jarque-Bera (JB):                3.800
Skew:                          -0.058   Prob(JB):                        0.150
Kurtosis:                       2.052   Cond. No.                         5.19
==============================================================================

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

In [ ]: