library(dynlm);library(lmtest);library(sandwich)data(prminwge, package='wooldridge')tsdata <-ts(prminwge, start=1950)# OLS regressionreg<-dynlm(log(prepop)~log(mincov)+log(prgnp)+log(usgnp)+trend(tsdata), data=tsdata )# results with usual SEcoeftest(reg)# results with HAC SEcoeftest(reg, vcovHAC)
Example-12-9.R
library(dynlm);library(lmtest)data(nyse, package='wooldridge')tsdata <-ts(nyse)# Linear regression of model:reg <-dynlm(return ~L(return), data=tsdata) # squared residualresidual.sq <-resid(reg)^2# Model for squared residual:ARCHreg <-dynlm(residual.sq ~L(residual.sq)) coeftest(ARCHreg)
Example-ARCH.R
library(zoo);library(quantmod);library(dynlm);library(stargazer)# Download data using the quantmod package:getSymbols("AAPL", auto.assign =TRUE)# Calculate return as the log differenceret <-diff( log(AAPL$AAPL.Adjusted) )# Subset 2008-2016 by special xts indexing:ret <- ret["2008/2016"]# AR(1) model for returnsret <-as.zoo(ret)reg <-dynlm( ret ~L(ret) ) # squared residualresidual.sq <-resid(reg)^2# Model for squared residual:ARCHreg <-dynlm(residual.sq ~L(residual.sq)) summary(ARCHreg)
import wooldridge as wooimport pandas as pdimport numpy as npimport statsmodels.api as smimport patsy as ptbarium = woo.dataWoo('barium')T =len(barium)# monthly time series starting Feb. 1978:barium.index = pd.date_range(start='1978-02', periods=T, freq='M')# perform the Cochrane-Orcutt estimation (iterative procedure):y, X = pt.dmatrices('np.log(chnimp) ~ np.log(chempi) + np.log(gas) +''np.log(rtwex) + befile6 + affile6 + afdec6', data=barium, return_type='dataframe')reg = sm.GLSAR(y, X)CORC_results = reg.iterative_fit(maxiter=100)table = pd.DataFrame({'b_CORC': CORC_results.params,'se_CORC': CORC_results.bse})print(f'reg.rho: {reg.rho}\n')print(f'table: \n{table}\n')
Example-12-9.py
import wooldridge as wooimport pandas as pdimport statsmodels.formula.api as smfnyse = woo.dataWoo('nyse')nyse['ret'] = nyse['return']nyse['ret_lag1'] = nyse['ret'].shift(1)# linear regression of model:reg = smf.ols(formula='ret ~ ret_lag1', data=nyse)results = reg.fit()# squared residuals:nyse['resid_sq'] = results.resid **2nyse['resid_sq_lag1'] = nyse['resid_sq'].shift(1)# model for squared residuals:ARCHreg = smf.ols(formula='resid_sq ~ resid_sq_lag1', data=nyse)results_ARCH = ARCHreg.fit()# print regression table:table = pd.DataFrame({'b': round(results_ARCH.params, 4),'se': round(results_ARCH.bse, 4),'t': round(results_ARCH.tvalues, 4),'pval': round(results_ARCH.pvalues, 4)})print(f'table: \n{table}\n')
Example-ARCH.py
import numpy as npimport pandas as pdimport pandas_datareader as pdrimport statsmodels.formula.api as smf# download data for 'AAPL' (= Apple) and define start and end:tickers = ['AAPL']start_date ='2007-12-31'end_date ='2016-12-31'# use pandas_datareader for the import:AAPL_data = pdr.data.DataReader(tickers, 'yahoo', start_date, end_date)# drop ticker symbol from column name:AAPL_data.columns = AAPL_data.columns.droplevel(level=1)# calculate return as the difference of logged prices:AAPL_data['ret'] = np.log(AAPL_data['Adj Close']).diff()AAPL_data['ret_lag1'] = AAPL_data['ret'].shift(1)# AR(1) model for returns:reg = smf.ols(formula='ret ~ ret_lag1', data=AAPL_data)results = reg.fit()# squared residuals:AAPL_data['resid_sq'] = results.resid **2AAPL_data['resid_sq_lag1'] = AAPL_data['resid_sq'].shift(1)# model for squared residuals:ARCHreg = smf.ols(formula='resid_sq ~ resid_sq_lag1', data=AAPL_data)results_ARCH = ARCHreg.fit()# print regression table:table = pd.DataFrame({'b': round(results_ARCH.params, 4),'se': round(results_ARCH.bse, 4),'t': round(results_ARCH.tvalues, 4),'pval': round(results_ARCH.pvalues, 4)})print(f'table: \n{table}\n')
Example-DWtest.py
import wooldridge as wooimport pandas as pdimport statsmodels.api as smimport statsmodels.formula.api as smfphillips = woo.dataWoo('phillips')T =len(phillips)# define yearly time series beginning in 1948:date_range = pd.date_range(start='1948', periods=T, freq='Y')phillips.index = date_range.year# estimation of both Phillips curve models:yt96 = (phillips['year'] <=1996)phillips['inf_diff1'] = phillips['inf'].diff()reg_s = smf.ols(formula='Q("inf") ~ unem', data=phillips, subset=yt96)reg_ea = smf.ols(formula='inf_diff1 ~ unem', data=phillips, subset=yt96)results_s = reg_s.fit()results_ea = reg_ea.fit()# DW tests:DW_s = sm.stats.stattools.durbin_watson(results_s.resid)DW_ea = sm.stats.stattools.durbin_watson(results_ea.resid)print(f'DW_s: {DW_s}\n')print(f'DW_ea: {DW_ea}\n')
calc-hac-se.jl
usingLinearAlgebra# for details, see Equations 12.41 - 12.43 in Wooldridge (2019)functioncalc_hac_se(reg, g) n =nobs(reg) X = reg.mm.m n =size(X, 1) K =size(X, 2) u =residuals(reg) ser =sqrt(sum(u .^2) / (n - K)) se_ols =coeftable(reg).cols[2] se_hac =zeros(K)for k in1:K yk = X[:, k] Xk = X[:, (1:K).!=k] bk =inv(transpose(Xk) * Xk) *transpose(Xk) * yk rk = yk .- Xk * bk ak = rk .* u vk =sum(ak .^2)for h in1:g sum_h =2* (1- h / (g +1)) *sum(ak[(h+1):n] .* ak[1:(n-h)]) vk = vk + sum_hend se_hac[k] = (se_ols[k] / ser)^2*sqrt(vk)endreturn se_hacend
Example-12-1.jl
usingWooldridgeDatasets, GLM, DataFramesinclude("calc-hac-se.jl")prminwge =DataFrame(wooldridge("prminwge"))prminwge.time = prminwge.year .-1949# OLS with regular SE:reg =lm(@formula(log(prepop) ~log(mincov) +log(prgnp) +log(usgnp) + time), prminwge)# OLS with HAC SE: hac_se =calc_hac_se(reg, 2)# print different SEs:table =DataFrame(coefficients=coeftable(reg).rownms, b=round.(coef(reg), digits=5), se_default=round.(coeftable(reg).cols[2], digits=5), hac_se=round.(hac_se, digits=5))println("table: \n$table")