import wooldridge as woo
import numpy as np
import pandas as pd
import linearmodels.iv as iv
import statsmodels.formula.api as smf
card = woo.dataWoo('card')
# checking for relevance with reduced form:
reg_redf = smf.ols(
formula='educ ~ nearc4 + exper + I(exper**2) + black + smsa +'
'south + smsa66 + reg662 + reg663 + reg664 + reg665 + reg666 +'
'reg667 + reg668 + reg669', data=card)
results_redf = reg_redf.fit()
# print regression table:
table_redf = pd.DataFrame({'b': round(results_redf.params, 4),
'se': round(results_redf.bse, 4),
't': round(results_redf.tvalues, 4),
'pval': round(results_redf.pvalues, 4)})
print(f'table_redf: \n{table_redf}\n')
# OLS:
reg_ols = smf.ols(
formula='np.log(wage) ~ educ + exper + I(exper**2) + black + smsa +'
'south + smsa66 + reg662 + reg663 + reg664 + reg665 +'
'reg666 + reg667 + reg668 + reg669', data=card)
results_ols = reg_ols.fit()
# print regression table:
table_ols = pd.DataFrame({'b': round(results_ols.params, 4),
'se': round(results_ols.bse, 4),
't': round(results_ols.tvalues, 4),
'pval': round(results_ols.pvalues, 4)})
print(f'table_ols: \n{table_ols}\n')
# IV automatically:
reg_iv = iv.IV2SLS.from_formula(
formula='np.log(wage)~ 1 + exper + I(exper**2) + black + smsa + '
'south + smsa66 + reg662 + reg663 + reg664 + reg665 +'
'reg666 + reg667 + reg668 + reg669 + [educ ~ nearc4]',
data=card)
results_iv = reg_iv.fit(cov_type='unadjusted', debiased=True)
# print regression table:
table_iv = pd.DataFrame({'b': round(results_iv.params, 4),
'se': round(results_iv.std_errors, 4),
't': round(results_iv.tstats, 4),
'pval': round(results_iv.pvalues, 4)})
print(f'table_iv: \n{table_iv}\n')