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

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

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

1 Resposta

0 votos
respondida Fev 19, 2016 por danielcajueiro (5,186 pontos)  

Example 7.1:

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

# Ex. 7.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']

    # Model 1
    y,X = ps.dmatrices('wage ~ female + educ + exper + tenure',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('wage ~ female',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Example 7.6

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

# Ex. 7.6

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']

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

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

    # 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() # Fit model
    print results.summary()

Example 7.8:

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

# Ex. 7.1

if __name__ == '__main__':

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/LAWSCH85.raw',delim_whitespace=True,header=None,na_values=".")
    df.columns=['rank','salary','cost','LSAT','GPA','libvol','faculty','age','clsize','north','south','east','west','lsalary','studfac','top10','r11_25','r26_40','r41_60','llibvol','lcost']



    df['r61_100'] = 0
    df.loc[(df['rank'] > 60) & (df['rank'] <= 100), 'r61_100'] = 1

    ps.NAAction(on_NA='drop', NA_types=['None', 'NaN'])
    y,X = ps.dmatrices('lsalary ~ top10 + r11_25 + r26_40 + r41_60 + r61_100  + LSAT + GPA + libvol + cost',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()
...