# F-Tests using different variance-covariance formulas:myH0 <-c("black","white")# Ususal VCOVlinearHypothesis(reg, myH0)# Refined White VCOVlinearHypothesis(reg, myH0, vcov=hccm)# Classical White VCOVlinearHypothesis(reg, myH0, vcov=hccm(reg,type="hc0"))
Example-8-2.R
data(gpa3, package='wooldridge')# load packages (which need to be installed!)library(lmtest); library(car)# Estimate model (only for spring data)reg <-lm(cumgpa~sat+hsperc+tothrs+female+black+white, data=gpa3, subset=(spring==1))# Usual SE:coeftest(reg)# Refined White heteroscedasticity-robust SE:coeftest(reg, vcov=hccm)
Example-8-4.R
data(hprice1, package='wooldridge')# Estimate modelreg <-lm(price~lotsize+sqrft+bdrms, data=hprice1)reg# Automatic BP testlibrary(lmtest)bptest(reg)# Manual regression of squared residuals summary(lm( resid(reg)^2~ lotsize+sqrft+bdrms, data=hprice1))
Example-8-5.R
data(hprice1, package='wooldridge')# Estimate modelreg <-lm(log(price)~log(lotsize)+log(sqrft)+bdrms, data=hprice1)reg# BP testlibrary(lmtest)bptest(reg)# White testbptest(reg, ~fitted(reg) +I(fitted(reg)^2) )
Example-8-6.R
library(foreign)d401k<-read.dta("http://fmwww.bc.edu/ec-p/data/wooldridge/401ksubs.dta")# OLS (only for singles: fsize==1)lm(nettfa ~ inc +I((age-25)^2) + male + e401k, data=d401k, subset=(fsize==1))# WLSlm(nettfa ~ inc +I((age-25)^2) + male + e401k, weight=1/inc, data=d401k, subset=(fsize==1))
Example-8-7.R
data(smoke, package='wooldridge')# OLSolsreg<-lm(cigs~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn, data=smoke)olsreg# BP testlibrary(lmtest)bptest(olsreg)# FGLS: estimation of the variance functionlogu2 <-log(resid(olsreg)^2)varreg<-lm(logu2~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn, data=smoke)# FGLS: WLSw <-1/exp(fitted(varreg))lm(cigs~log(income)+log(cigpric)+educ+age+I(age^2)+restaurn, weight=w ,data=smoke)
import wooldridge as wooimport statsmodels.formula.api as smfgpa3 = woo.dataWoo('gpa3')# definition of model and hypotheses:reg = smf.ols(formula='cumgpa ~ sat + hsperc + tothrs + female + black + white', data=gpa3, subset=(gpa3['spring'] ==1))hypotheses = ['black = 0', 'white = 0']# F-Tests using different variance-covariance formulas:# ususal VCOV:results_default = reg.fit()ftest_default = results_default.f_test(hypotheses)fstat_default = ftest_default.statistic[0][0]fpval_default = ftest_default.pvalueprint(f'fstat_default: {fstat_default}\n')print(f'fpval_default: {fpval_default}\n')# refined White VCOV:results_hc3 = reg.fit(cov_type='HC3')ftest_hc3 = results_hc3.f_test(hypotheses)fstat_hc3 = ftest_hc3.statistic[0][0]fpval_hc3 = ftest_hc3.pvalueprint(f'fstat_hc3: {fstat_hc3}\n')print(f'fpval_hc3: {fpval_hc3}\n')# classical White VCOV:results_hc0 = reg.fit(cov_type='HC0')ftest_hc0 = results_hc0.f_test(hypotheses)fstat_hc0 = ftest_hc0.statistic[0][0]fpval_hc0 = ftest_hc0.pvalueprint(f'fstat_hc0: {fstat_hc0}\n')print(f'fpval_hc0: {fpval_hc0}\n')
Example-8-2.py
import wooldridge as wooimport pandas as pdimport statsmodels.formula.api as smfgpa3 = woo.dataWoo('gpa3')# define regression model:reg = smf.ols(formula='cumgpa ~ sat + hsperc + tothrs + female + black + white', data=gpa3, subset=(gpa3['spring'] ==1))# estimate default model (only for spring data):results_default = reg.fit()table_default = pd.DataFrame({'b': round(results_default.params, 5),'se': round(results_default.bse, 5),'t': round(results_default.tvalues, 5),'pval': round(results_default.pvalues, 5)})print(f'table_default: \n{table_default}\n')# estimate model with White SE (only for spring data):results_white = reg.fit(cov_type='HC0')table_white = pd.DataFrame({'b': round(results_white.params, 5),'se': round(results_white.bse, 5),'t': round(results_white.tvalues, 5),'pval': round(results_white.pvalues, 5)})print(f'table_white: \n{table_white}\n')# estimate model with refined White SE (only for spring data):results_refined = reg.fit(cov_type='HC3')table_refined = pd.DataFrame({'b': round(results_refined.params, 5),'se': round(results_refined.bse, 5),'t': round(results_refined.tvalues, 5),'pval': round(results_refined.pvalues, 5)})print(f'table_refined: \n{table_refined}\n')
Example-8-4.py
import wooldridge as wooimport pandas as pdimport statsmodels.api as smimport statsmodels.formula.api as smfimport patsy as pthprice1 = woo.dataWoo('hprice1')# estimate model:reg = smf.ols(formula='price ~ lotsize + sqrft + bdrms', data=hprice1)results = reg.fit()table_results = pd.DataFrame({'b': round(results.params, 4),'se': round(results.bse, 4),'t': round(results.tvalues, 4),'pval': round(results.pvalues, 4)})print(f'table_results: \n{table_results}\n')# automatic BP test (LM version):y, X = pt.dmatrices('price ~ lotsize + sqrft + bdrms', data=hprice1, return_type='dataframe')result_bp_lm = sm.stats.diagnostic.het_breuschpagan(results.resid, X)bp_lm_statistic = result_bp_lm[0]bp_lm_pval = result_bp_lm[1]print(f'bp_lm_statistic: {bp_lm_statistic}\n')print(f'bp_lm_pval: {bp_lm_pval}\n')# manual BP test (F version):hprice1['resid_sq'] = results.resid **2reg_resid = smf.ols(formula='resid_sq ~ lotsize + sqrft + bdrms', data=hprice1)results_resid = reg_resid.fit()bp_F_statistic = results_resid.fvaluebp_F_pval = results_resid.f_pvalueprint(f'bp_F_statistic: {bp_F_statistic}\n')print(f'bp_F_pval: {bp_F_pval}\n')
Example-8-5.py
import wooldridge as wooimport numpy as npimport pandas as pdimport statsmodels.api as smimport statsmodels.formula.api as smfimport patsy as pthprice1 = woo.dataWoo('hprice1')# estimate model:reg = smf.ols(formula='np.log(price) ~ np.log(lotsize) + np.log(sqrft) + bdrms', data=hprice1)results = reg.fit()# BP test:y, X_bp = pt.dmatrices('np.log(price) ~ np.log(lotsize) + np.log(sqrft) + bdrms', data=hprice1, return_type='dataframe')result_bp = sm.stats.diagnostic.het_breuschpagan(results.resid, X_bp)bp_statistic = result_bp[0]bp_pval = result_bp[1]print(f'bp_statistic: {bp_statistic}\n')print(f'bp_pval: {bp_pval}\n')# White test:X_wh = pd.DataFrame({'const': 1, 'fitted_reg': results.fittedvalues,'fitted_reg_sq': results.fittedvalues **2})result_white = sm.stats.diagnostic.het_breuschpagan(results.resid, X_wh)white_statistic = result_white[0]white_pval = result_white[1]print(f'white_statistic: {white_statistic}\n')print(f'white_pval: {white_pval}\n')