This example shows how to plot heteroscedastic-and-autocorrelation consistent (HAC) corrected confidence bands using Newey-West robust standard errors.
One way to estimate the coefficients of a linear model is by OLS. However, time series models tend to have innovations that are autocorrelated and heteroscedastic (i.e., the errors are nonspherical). If a times series model has nonspherical errors, then usual formulae for standard errors of OLS coefficients are biased and inconsistent. Inference based on these inefficient standard errors tends to inflate the Type I error rate. One way to account for nonspherical errors is to use HAC standard errors. In particular, the Newey-West estimator of the OLS coefficient covariance is relatively robust against nonspherical errors.
|On this page…|
Load the data.
Load the Canadian electric power consumption data set from the World Bank located in the folder \help\toolbox\econ\examples. The data set contains four variables: year, consump, gdp, and gdp_deflator. The response, consump, is Canada's electrical energy consumption (in kWh), and the predictor, GDP, is Canada's GDP (in year 2000 USD).
Define the model.
Model the behavior of the annual difference in electrical energy consumption with respect to real GDP as a linear model:
consumpDiff = Dataset.consump-lagmatrix(Dataset.consump,1); ... % annual difference in consumption T = size(consumpDiff,1); consumpDiff = consumpDiff(2:end)/1.0e+10; % for numerical stability rGDP = Dataset.gdp./(Dataset.gdp_deflator); % deflate GDP rGDP = rGDP(2:end)/1.0e+10; % for numerical stability rGDPdes = [ones(T-1,1) rGDP]; % design matrix year = Dataset.year(2:end); Mdl = fitlm(rGDP,consumpDiff); coeff = double(Mdl.Coefficients(:,1)); EstParamCov = Mdl.CoefficientCovariance; resid = Mdl.Residuals.Raw;
EstParamCov = 0.2860 -0.2850 -0.2850 0.3013
Plot the data.
Plot the difference in energy consumption, consumpDiff versus the real GDP, to check for possible heteroscedasticity.
figure plot(rGDP,consumpDiff,'.') title('Annual Difference in Energy Consumption vs real GDP - Canada') xlabel('real GDP (year 2000 USD)') ylabel('Annual Difference in Energy Consumption (kWh)')
The figure indicates that heteroscedasticity might be present in the annual difference in energy consumption. As real GDP increases, the annual difference in energy consumption seems to be less variable.
Plot the residuals.
Plot the residuals from Mdl against the fitted values and year to assess heteroscedasticity and autocorrelation.
figure subplot(2,1,1) hold on plot(Mdl.Fitted,resid,'.') plot([min(Mdl.Fitted) max(Mdl.Fitted)],[0 0],'k-') title('Residual Plots') xlabel('Fitted Consumption') ylabel('Residuals') axis tight hold off subplot(2,2,3) autocorr(resid) subplot(2,2,4) parcorr(resid)
The residual plot reveals decreasing residual variance with increasing fitted consumption. The autocorrelation function shows that autocorrelation might be present in the first few lagged residuals.
Test for heteroscedasticity and autocorrelation.
Test for conditional heteroscedasticity using Engle's ARCH test. Test for autocorrelation using the Ljung-Box Q test. Test for overall correlation using the Durbin-Watson test.
[~,engle_pvalue] = archtest(resid); engle_pvalue [~,lbq_pvalue] = lbqtest(resid,'lags',1:3);... % significance of first three lags lbq_pvalue [dw_pvalue] = dwtest(Mdl); dw_pvalue
engle_pvalue = 0.1463 lbq_pvalue = 0.0905 0.1966 0.0522 dw_pvalue = 0.0013
The p-value of Engle's ARCH test suggests significant conditional heteroscedasticity at 15% significance level. The p-value for the Ljung-Box Q test suggests significant autocorrelation with the first and third lagged residuals at 10% significance level. The p-value for the Durbin-Watson test suggests that there is strong evidence for overall residual autocorrelation. The results of the tests suggest that the standard linear model conditions of homoscedasticity and uncorrelated errors are violated, and inferences based on the OLS coefficient covariance matrix are suspect.
One way to proceed with inference (such as constructing a confidence band) is to correct the OLS coefficient covariance matrix by estimating the Newey-West coefficient covariance.
Estimate the Newey-West coefficient covariance.
Correct the OLS coefficient covariance matrix by estimating the Newey-West coefficient covariance using hac. Compute the maximum lag to be weighted for the standard Newey-West estimate, maxLag (Newey and West, 1994). Use hac to estimate the standard Newey-West coefficient covariance.
maxLag = floor(4*(T/100)^(2/9)); [NWEstParamCov,~,NWCoeff] = hac(Mdl,'type','hac',... 'bandwidth',maxLag + 1);
Coefficient Covariance Estimates Cov | Const x1 ------|---------------- Const | 0.3720 -0.2990 x1 | -0.2990 0.2454
The Newey-West standard error for the coefficient of rGDP, labeled x1 in the table, is less than the usual OLS standard error. This suggests that, in this data set, correcting for residual heteroscedasticity and autocorrelation increases the precision in measuring the linear effect of real GDP on energy consumption.
Calculate the Working-Hotelling confidence bands.
Compute the 95% Working-Hotelling confidence band for each covariance estimate using nlpredci (Kutner et al., 2005).
modelfun = @(b,x)(b(1)*x(:,1)+b(2)*x(:,2)); % define the linear model [beta,nlresid,~,EstParamCov] = nlinfit(rGDPdes,... consump_diff,modelfun,[1,1]); % estimate the model [fity,fitcb] = nlpredci(modelfun,rGDPdes,beta,nlresid,... 'Covar',EstParamCov,'SimOpt','on'); % margin of errors conbandnl = [fity - fitcb fity + fitcb]; % confidence bands [fity,NWfitcb] = nlpredci(modelfun,rGDPdes,... beta,nlresid,'Covar',NWEstParamCov,'SimOpt','on'); % corrected margin of error NWconbandnl = [fity - NWfitcb fity + NWfitcb]; % corrected confidence bands
Plot the Working-Hotelling confidence bands.
Plot the Working-Hotelling confidence bands on the same axes twice: one plot displaying electrical energy consumption with respect to real GDP, and the other displaying the electrical energy consumption time series.
figure hold on l1 = plot(rGDP,consump_diff,'k.'); l2 = plot(rGDP,fity,'b-','LineWidth',2); l3 = plot(rGDP,conbandnl,'r-'); l4 = plot(rGDP,NWconbandnl,'g--'); title('Data with 95% Working-Hotelling Conf. Bands') xlabel('real GDP (year 2000 USD)') ylabel('Consumption (kWh)') axis([0.7 1.4 -2 2.5]) legend([l1 l2 l3(1) l4(1)],'Data','Fitted','95% Conf. Band',... 'Newey-West 95% Conf. Band','Location','SouthEast') hold off figure hold on l1 = plot(year,consump_diff); l2 = plot(year,fity,'k-','LineWidth',2); l3 = plot(year,conbandnl,'r-'); l4 = plot(year,NWconbandnl,'g--'); title('Consumption with 95% Working-Hotelling Conf. Bands') xlabel('Year') ylabel('Consumption (kWh)') legend([l1 l2 l3(1) l4(1)],'Consumption','Fitted',... '95% Conf. Band','Newey-West 95% Conf. Band',... 'Location','SouthWest') hold off
The plots show that the Newey-West estimator accounts for the heteroscedasticity in that the confidence band is wide in areas of high volatility, and thin in areas of low volatility. The OLS coefficient covariance estimator ignores this pattern of volatility.
 Kutner, M. H., C. J. Nachtsheim, J. Neter, and W. Li. Applied Linear Statistical Models. 5th Ed. New York: McGraw-Hill/Irwin, 2005.
 Newey, W. K., and K. D. West. "A Simple Positive Semidefinite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix." Econometrica. Vol. 55, 1987, pp. 703–708.
 Newey, W. K, and K. D. West. "Automatic Lag Selection in Covariance Matrix Estimation." The Review of Economic Studies. Vol. 61 No. 4, 1994, pp. 631–653.