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

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

+2 votos
265 visitas
perguntada Fev 19, 2016 em Economia por danielcajueiro (5,186 pontos)  
Compartilhe

1 Resposta

+1 voto
respondida Fev 19, 2016 por danielcajueiro (5,186 pontos)  

Example 8.1:

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

# Ex. 8.1

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/WAGE1.raw',delim_whitespace=True,header=None)
    df.columns=['wage','educ','exper','tenure','nonwhite','female','married','numdep','smsa','northcen','south','west','construc','ndurman','trcommpu','trade',\
    'services','profserv','profocc','clerocc','servocc','lwage','expersq','tenursq']



    df['marrmale']=(1-df['female']) *df['married']
    df['marrfem']=df['female'] *df['married']
    df['singfem']=df['female'] *(1-df['married'])

    # Model 1
    y,X = ps.dmatrices('np.log(wage) ~ marrmale + marrfem + singfem + educ + exper + np.power(exper,2) + tenure + np.power(tenure,2)',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()



    # Model 2
    y,X = ps.dmatrices('np.log(wage) ~ marrmale + marrfem + singfem + educ + exper + np.power(exper,2) + tenure + np.power(tenure,2)',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit(cov_type='HC0') 
    print results.summary()

Example 8.2:

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

# Ex. 8.2

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/GPA3.raw',delim_whitespace=True,header=None)
    df.columns=['term','sat','tothrs','cumgpa','season','frstsem','crsgpa','verbmath','trmgpa','hssize','hsrank','id','spring','female','black','white','ctrmgpa','ctothrs','ccrsgpa','ccrspop','cseason','hsperc','football'] 

    # if Spring   
    dfSpring=df[df['spring']==1]

    # Model 1
    y,X = ps.dmatrices('cumgpa ~ sat + hsperc + tothrs + female + black + white',data=dfSpring, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()
    hypotheses = '(black = 0),(white=0)'
    f_test = results.f_test(hypotheses)    
    print f_test 


    # Model 2
    y,X = ps.dmatrices('cumgpa ~ sat + hsperc + tothrs + female + black + white',data=dfSpring, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit(cov_type='HC0') 
    print results.summary()
    hypotheses = '(black = 0),(white=0)'
    f_test = results.f_test(hypotheses)    
    print f_test

Examples 8.4 and 8.5:

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

# Ex. 8.4 and 8.5

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Prorum/IntroductoryEconometrics/Text Data/hprice1.raw',delim_whitespace=True,header=None)
    df.columns=['price','assess','bdrms','lotsize','sqrft','colonial','lprice','lassess','llotsize','lsqrft']



    # Model 1
    y,X = ps.dmatrices('price ~ lotsize + sqrft + bdrms',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    print sm.stats.diagnostic.het_breushpagan(results.resid,X )
    print sm.stats.diagnostic.het_white(results.resid,X)

    # Model 2
    y,X = ps.dmatrices('lprice ~ llotsize + lsqrft + bdrms',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    df['lpriceFitted']=results.fittedvalues


    df['lpriceFitted2']=np.power(df['lpriceFitted'],2)

    Z = ps.dmatrix('lpriceFitted + lpriceFitted2',data=df, return_type='dataframe')
    print sm.stats.diagnostic.het_breushpagan(results.resid,X )
    print sm.stats.diagnostic.het_white(results.resid,X)
    # Special case for the white test   
    print sm.stats.diagnostic.het_breushpagan(results.resid,Z )

Example 8.6:

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

# Ex. 8.4 and 8.5

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Prorum/IntroductoryEconometrics/Text Data/401ksubs.raw',delim_whitespace=True,header=None)
    df.columns=['e401k','inc','marr','male','age','fsize','nettfa','p401k','pira','incsq','agesq']

    dfSize1=df[df['fsize']==1]



    # Model 1
    y,X = ps.dmatrices('nettfa ~ inc',data=dfSize1, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    dfSize1['age25sq']=np.power(dfSize1['age']-25,2)


    # Model 2
    y,X = ps.dmatrices('nettfa ~ inc + age25sq + male + e401k',data=dfSize1, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    # Model 3
    y,X = ps.dmatrices('nettfa ~ inc',data=dfSize1, return_type='dataframe')
    z=ps.dmatrix('inc -1',data=dfSize1, return_type='dataframe')
    model = sm.GLS(y,X,z) # Describe Model
    results = model.fit() # Fit model
    print results.summary()



    # Model 4
    y,X = ps.dmatrices('nettfa ~ inc + age25sq + male + e401k',data=dfSize1, return_type='dataframe')
    z=ps.dmatrix('inc -1',data=dfSize1, return_type='dataframe')
    model = sm.GLS(y,X,z) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Example 8.7:

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

# Ex. 8.4 and 8.5

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Prorum/IntroductoryEconometrics/Text Data/SMOKE.raw',delim_whitespace=True,header=None)
    df.columns=['educ','cigpric','white','age','income','cigs','restaurn','lincome','agesq','lcigpric']




    y,X = ps.dmatrices('cigs ~ lincome + lcigpric + educ + age + agesq + restaurn',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    df['lufitted2']=np.log(np.power(results.resid,2))
    y,X = ps.dmatrices('lufitted2 ~ lincome + lcigpric + educ + age + agesq + restaurn',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    df['gFitted']=results.fittedvalues
    df['hFitted']=np.exp(df['gFitted'])

    y,X = ps.dmatrices('cigs ~ lincome + lcigpric + educ + age + agesq + restaurn',data=df, return_type='dataframe')
    z=ps.dmatrix('hFitted -1',data=df, return_type='dataframe')
    model = sm.GLS(y,X,z) # Describe Model
    results = model.fit() # Fit model
    print results.summary()
...