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

+3 votos
110 visitas
perguntada Fev 19, 2016 em Economia

## 1 Resposta

0 votos
respondida Fev 19, 2016 por (5,196 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__':

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__':

# 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.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

# 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__':

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__':