I need to estimate a linear regression with the OLS method. Since I assume the error terms to be correlated, I would like to account for heteroskedasticity and autocorrelation in the error terms. I already wrote some code, but I wonder whether it is correct.

So far, my code is:

myregression <- lm(x ~ y + z)
coeftest(myregression, vcov=NeweyWest(myregression))

Any help is highly appreciated!

  • 1
    $\begingroup$ Presumably you are using the sandwich and lmtest packages. You can type ?coeftest and ?NeweyWest for examples (at the end of the help page). I personally prefer dynlm objects for timeseries type data, in case that's where your autocorrelation comes from... $\endgroup$ Jan 4, 2017 at 16:47
  • $\begingroup$ Thank you very much P.Windridge! I am using these packages. And I allready wrote the instructions. However, since it is very important that I do not make any mistake, I decided to ask for help here. As far as I understand you would rather use myregression <- dynlm(formula = x ~ y + z) coeftest(myregression, vcov = NeweyWest(myregression)) However, to clarify: The result I receive is a linear model with heteroskedasticity and autocorrelation robust errors according to NeweyWest? $\endgroup$
    – user144267
    Jan 4, 2017 at 16:52
  • 2
    $\begingroup$ If your question is only about R code, it is off topic here. If you have a statistical question about autocorrelation or sandwich estimators, please edit to clarify. $\endgroup$ Jan 4, 2017 at 17:41
  • $\begingroup$ Be aware that the NeweyWest function performs a procedure called prewhitening before actually computing the HAC standard errors. This is done in order to "increase the performance" of the HAC algorithm and might be a good idea in your case, but you should be aware that this is done by default (you can turn it off though). See here for a related question and here for the documentation of the NeweyWest function. $\endgroup$
    – Candamir
    Nov 8, 2017 at 21:18

1 Answer 1


This question is about code but seeing as I've been looking at HAC estimates recently in R I will "answer".

  1. I have not checked the R implementation of Newey-West is exactly as in their original paper. However, your code does indeed calculate R's NeweyWest HAC estimate using the default bandwidth selection/lag method. (You can view this parameter with the "verbose=T" option.)

  2. If you know the form of the correlations in your data then you can take less of a "sledgehammer" approach than Newey-West. E.g. Prais-Winsten or Cochrane-Orcutt.

  3. Be aware that serial correlation is being examined here and so the order that your observations are sorted in does matter.

If you are interested you might consider a toy example where you generate correlated residuals on purpose to see how the Newey-West std error estimates/p-values differ. In the example below the correlated residuals make it "look like" the response can be fitted against a straight line. The lag parameter is automatically (and correctly) chosen = 1 (seen with verbose=T option). If you play with the process generating the residuals you can see how it changes.

correlated_residuals<-arima.sim(list(ar = .9), n)
summary(fit) # standard estimates
  • $\begingroup$ Why do you say Newey-West is like a sledgehammer? $\endgroup$
    – badmax
    Jan 22, 2019 at 5:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.