# Calculate Newey-West standard errors without an lm object in R

I asked this question yesterday on StackOverflow, and got an answer, but we agreed that it seems a bit hackish and there may be a better way to look at it.

The question: I would like calculate the Newey-West (HAC) standard errors for a vector (in this case a vector of stock returns). The function NeweyWest() in the sandwich package does this, but takes an lm object as an input. The solution Joris Meys offered is to project the vector onto 1, which turns my vector into residuals to feed into NeweyWest(). That is:

as.numeric(NeweyWest(lm(rnorm(100) ~ 1)))


for the variance of the mean.

Should I be doing it like this? Or is there a way to more directly do what I want? Thanks!

• Question is not clear. What do you mean by "standard error for a vector"? Typically we want the standard error of a parameter estimate. What parameter are you estimating? The code you provided produces the Newey West estimate of the squared standard error of the mean. Is that what you want? – Cyrus S Dec 13 '10 at 0:23
• @Cyrus -- By "vector" I mean not an lm object. I frequently have a vector (let's say a series of stock returns) that I don't want to involve in any regressions (because I don't care about it's projection, other than on 1), but for which I still want the HAC standard error. In this case the parameter estimate is the stock return. The answer above does that, but requires calculating the lm object, which I really don't need. So I'm wondering if there's a routine in R that does this without creating an lm object. – Richard Herron Dec 13 '10 at 11:07
• Sorry, still not clear: "In this case the parameter estimate is the stock return." By that, do you mean the "average of the stock returns in the series"? If yes, then what you've got is perfectly fine. – Cyrus S Dec 13 '10 at 12:59
• @Cyrus -- I know that what I have works, but I was hoping that there is a way to calculate the SEs without passing through the lm object for the case of a single vector. I guess not. Thanks for helping me clarify my question! – Richard Herron Dec 13 '10 at 15:54

Suppose we have a regression

\begin{align*} y=X\beta+u \end{align*}

Then OLS estimate $\hat{\beta}$ is \begin{align*} \widehat{\beta}-\beta=(X'X)^{-1}X'u \end{align*} and assuming that $\hat{\beta}$ is unbiased estimate we have \begin{align*} Var(\widehat{\beta})=E\left[(X'X)^{-1}X'uu'X(X'X)^{-1}\right] \end{align*}

The usual OLS assumptions are that $E(u|X)=0$ and $E(uu'|X)=\sigma^2I_n$ which gives us \begin{align*} Var(\widehat{\beta})=\sigma^2E(X'X)^{-1} \end{align*} This covariance matrix is usually reported in statistical packages.

If $u_i$ are heteroscedastic and (or) autocorellated, then $E(uu'|X)\neq\sigma^2I_n$ and the usual output gives misleading results. To get the correct results HAC standard errors are calculated. All the methods for HAC errors calculate \begin{align*} diag(E(X'X)^{-1}X'uu'X(X'X)^{-1}). \end{align*} They differ on their assumptions what $E(uu'|X)$ looks like.

So it is natural then that function NeweyWest requests linear model. Newey-West method calculates the correct standard errors of linear model estimator. So your solution is perfectly correct if you assume that your stock returns follow the model \begin{align} r_t=\mu+u_t \end{align} and you want to estimate $Var(\mu)$ guarding against irregularities in $u_t$.

If on the other hand you want to estimate "correct" $Var(r_t)$ (whatever that means), you should check out volatility models, such as GARCH and its variants. They assume that \begin{align*} r_t=\sigma_t\varepsilon_t \end{align*} where $\varepsilon_t$ are iid normal. The goal is then to correctly estimate $\sigma_t$. Then $Var(r_t)=Var(\sigma_t)$ and you have "correct" estimate of your variance, guarding against usual idiosyncrasies of stock returns such as volatility clustering, skewness and etc.

• Thanks! There may not be a more efficient way for me to code this than forming the lm object. – Richard Herron Dec 13 '10 at 11:11
• I guess the lm object is the way to go! Thanks for a great summary... sometimes in the application I get too far from the theory. – Richard Herron Dec 17 '10 at 13:33