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

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

+3 votos
143 visitas
perguntada Fev 19, 2016 em Economia por danielcajueiro (5,376 pontos)  
Compartilhe

1 Resposta

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

Example 6.1:

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/HPRICE2.raw',delim_whitespace=True,header=None)
    df.columns=['price','crime','nox','rooms','dist','radial','proptax','stratio','lowstat','lprice','lnox','lproptax']

    dfColumns=list(df.columns)
    for col in dfColumns:
        colZsocre='z' + col
        df[colZsocre]=(df[col] - df[col].mean())/df[col].std()

    # Multiple Regression
    y,X = ps.dmatrices('zprice ~ znox + zcrime + zrooms + zdist + zstratio -1',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Example 6.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/HPRICE2.raw',delim_whitespace=True,header=None)
    df.columns=['price','crime','nox','rooms','dist','radial','proptax','stratio','lowstat','lprice','lnox','lproptax']


    # Multiple Regression
    y,X = ps.dmatrices('np.log(price) ~ np.log(nox) + np.log(dist) + rooms + np.power(rooms,2) + stratio',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Example 6.3:

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/ATTEND.raw',delim_whitespace=True,header=None)
    df.columns=['attend','termGPA','priGPA','ACT','final','atndrte','hwrte','frosh','soph','skipped','stndfnl']   


    # Model 1
    y,X = ps.dmatrices('stndfnl ~ atndrte + priGPA + ACT + np.power(priGPA,2) + np.power(ACT,2) + priGPA*atndrte',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

    # Model 2

    df['priGPAdev']=df['priGPA'] - 2.59

    # Model 2
    y,X = ps.dmatrices('stndfnl ~ atndrte + priGPA + ACT + np.power(priGPA,2) + np.power(ACT,2) + priGPAdev*atndrte - priGPAdev',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Examples 6.4 and 6.7

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

# Ex. 6-4 and 6.7
if __name__ == '__main__':
     #dados=np.loadtxt('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/CEOSAL1.raw')   

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/CEOSAL1.raw',delim_whitespace=True,header=None)
    df.columns=['salary','pcsalary','sales','roe','pcroe','ros','indus','finance','consprod','utility','lsalary','lsales']

    # Model 1
    y,X = ps.dmatrices('salary ~ sales + roe',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('lsalary ~ lsales + roe',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()

Examples 6.5 and 6.6:

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


# Ex. 6.5 and 6.6

if __name__ == '__main__':
     #dados=np.loadtxt('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/CEOSAL1.raw')   

    df = pd.read_csv('/home/daniel/Documents/Projetos/Prorum/Python For Econometrics/DataSets/Txt/gpa2.raw',delim_whitespace=True,header=None)
    df.columns=['sat','tothrs','colgpa','athlete','verbmath','hsize','hsrank','hsperc','female','white','black','hsizesq']
    # Model 1
    y,X = ps.dmatrices('colgpa ~ sat + hsperc + hsize + np.power(hsize,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('colgpa ~ sat + hsperc + hsize + hsizesq',data=df, return_type='dataframe')
    model = sm.OLS(y,X) # Describe Model
    results = model.fit() # Fit model
    print results.summary()
comentou 2 dias atrás por Valério Londe (21 pontos)  
É a quarta edição do livro?
...