Primeira vez aqui? Seja bem vindo e cheque o FAQ!
x

Introductory Econometrics - Jeffrey M. Wooldridge: Como replicar os exemplos do Capítulo 5 usando python?

+2 votos
142 visitas

1 Resposta

0 votos
respondida Fev 18, 2016 por danielcajueiro (5,251 pontos)  

O Capítulo 5 do Wooldridge explora as propriedades assintóticas quando a amostra de interesse cresce muito. Ele discute que usando o teorema Central do Limite pode-se concluir que

\[(\hat \beta -\beta)/se(\hat \beta) \sim N(0,1)\]

ou, aproximadamente

\[(\hat \beta -\beta)/se(\hat \beta) \sim t_{n-k-1}.\]

Logo, existem poucos exemplos computacionais nesse capítulo.

Histogram of prate using the data in 401K.RAW (Figure 5.2)

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
import pandas as pd
import patsy as ps


if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/401K.raw',delim_whitespace=True,header=None)
    df.columns=['prate','mrate','totpart','totelg','age','totemp','sole','ltotemp']

    prateData=df.as_matrix(columns=['prate'])   

    fig = plt.figure()
    ax = fig.add_subplot(111)  
    ax.hist(prateData,bins=10,normed=1)
    ax.set_xlabel('Participation Rate (prate)')
    ax.set_ylabel('Proportion in Cell') 
    plt.axis([0, 100, 0, 0.07])

Econometrics Wooldridge

Exemplo 5.2:

import numpy as np
import statsmodels.api as sm
import matplotlib as plt
import pandas as pd
import patsy as ps

# Ex.  5.2

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/BWGHT.raw',delim_whitespace=True,header=None,na_values=".")
    df.columns=['faminc','cigtax','cigprice','bwght','fatheduc','motheduc','parity','male','white','cigs','lbwght','bwghtlbs','packs','lfaminc']

    dfHalf=df[:694]

    ps.NAAction(on_NA='drop', NA_types=['None', 'NaN'])
    y,X = ps.dmatrices('bwght ~ cigs + lfaminc',data=dfHalf, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    se1=results.bse['cigs']

    ps.NAAction(on_NA='drop', NA_types=['None', 'NaN'])
    y,X = ps.dmatrices('bwght ~ cigs + lfaminc',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    se2=results.bse['cigs']

    print se2/se1

Exemplo 5.3:

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
import pandas as pd
import patsy as ps

# Example 5.3

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/CRIME1.raw',delim_whitespace=True,header=None)
    df.columns=['narr86','nfarr86','nparr86','pcnv','avgsen','tottime','ptime86','qemp86','inc86','durat','black','hispan','born60','pcnvsq','pt86sq','inc86sq']



    # Model
    y,X = ps.dmatrices('narr86 ~ pcnv + avgsen +  tottime + ptime86 + qemp86',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()    

    # Model
    y,X = ps.dmatrices('narr86 ~ pcnv + ptime86 + qemp86',data=df, return_type='dataframe')
    restModel = sm.OLS(y,X) # Describe Model
    restResults = restModel.fit() # Fit model
    print restResults.summary()    


    print "(LM,pValue,difference between the degree of freedom) = ", results.compare_lm_test(restResults)

Resultados:

                           OLS Regression Results                            
==============================================================================
Dep. Variable:                 narr86   R-squared:                       0.043
Model:                            OLS   Adj. R-squared:                  0.041
Method:                 Least Squares   F-statistic:                     24.29
Date:                Thu, 18 Feb 2016   Prob (F-statistic):           5.43e-24
Time:                        10:52:32   Log-Likelihood:                -3392.7
No. Observations:                2725   AIC:                             6797.
Df Residuals:                    2719   BIC:                             6833.
Df Model:                           5                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      0.7061      0.033     21.297      0.000         0.641     0.771
pcnv          -0.1512      0.041     -3.701      0.000        -0.231    -0.071
avgsen        -0.0070      0.012     -0.568      0.570        -0.031     0.017
tottime        0.0121      0.010      1.263      0.207        -0.007     0.031
ptime86       -0.0393      0.009     -4.403      0.000        -0.057    -0.022
qemp86        -0.1031      0.010     -9.915      0.000        -0.123    -0.083
==============================================================================
Omnibus:                     2395.326   Durbin-Watson:                   1.837
Prob(Omnibus):                  0.000   Jarque-Bera (JB):           106869.684
Skew:                           4.001   Prob(JB):                         0.00
Kurtosis:                      32.618   Cond. No.                         16.3
==============================================================================

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 narr86   R-squared:                       0.041
Model:                            OLS   Adj. R-squared:                  0.040
Method:                 Least Squares   F-statistic:                     39.10
Date:                Thu, 18 Feb 2016   Prob (F-statistic):           9.91e-25
Time:                        10:52:32   Log-Likelihood:                -3394.7
No. Observations:                2725   AIC:                             6797.
Df Residuals:                    2721   BIC:                             6821.
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      0.7118      0.033     21.565      0.000         0.647     0.776
pcnv          -0.1499      0.041     -3.669      0.000        -0.230    -0.070
ptime86       -0.0344      0.009     -4.007      0.000        -0.051    -0.018
qemp86        -0.1041      0.010    -10.023      0.000        -0.124    -0.084
==============================================================================
Omnibus:                     2394.860   Durbin-Watson:                   1.836
Prob(Omnibus):                  0.000   Jarque-Bera (JB):           106169.154
Skew:                           4.002   Prob(JB):                         0.00
Kurtosis:                      32.513   Cond. No.                         8.27
==============================================================================

(LM,pValue,difference between the degree of freedom) =  (4.0707297062815408, 0.13063281201643845, 2.0)
...