Update: I have included a modified version of this summaryR() command as part of my package tonymisc, which extends mtable() to report robust standard errors. The tonymisc package is available on CRAN through the install.packages() command.
If you have the right R commands at your disposal, it is simple to correct for heteroskedasticity using the robust correction that is commonly-used among economists. I recorded a video tutorial to describe the simplest (and most flexible) way I know to get R to compute robust standard errors.
The key is to use a "summary-style" command that has an option to correct for heteroskedasticity. The command I like to use is called summaryR(). Here is the script file with the summaryR() command.
I found this function on an R-help discussion board where several people were answering someone's question about extending the summary.lm() command.
I deserve none of the credit for writing this (credit goes to John Fox), but I consider it my duty to point out how nice this function is. I demonstrate how to use the function in this video
Here are is the script file I used in the video:
Here's a link to the data set.
Showing posts with label heteroskedasticity. Show all posts
Showing posts with label heteroskedasticity. Show all posts
Tuesday, April 12, 2011
Thursday, May 13, 2010
A function that reports White Standard Errors
Update: Since writing this post, I found a natural extension written by John Fox that does what the function in this post does and I have extended it in my CRAN-contributed library tonymisc. In my package, this modified and extended function is called summaryR().
One of the frustrating things about using R for econometrics is that R has some cumbersome functionality for computing the standard (in econometrics, anyway) White standard errors. As one of my favorite references on R learning points out, there are two ways to retrieve these precious standard errors.
The function is called summarize.r(), and it works just like the summary command. In fact, my function calls summary.lm, and then modifies the contents that are printed using method 1 from above.
To run my function, (1) download and install the sandwich library, (2) run the code that defines the function (see below), and (3) after fitting your regression using lm(), you can use the function by calling summarize.r(yourregression.lm).
Without further ado, here's the code you should run.
summarize.r = function(reg.lm, ...){
z = summary.lm(reg.lm)
W.VCOV = vcovHC(reg.lm,type="HC0")
sig.sq = ((summary(reg.lm)$sigma)^2)
coef = z$coefficients[,1]
White.SE <- sqrt(diag(W.VCOV)) t.robust <- coef/White.SE ## Element-by-element division df <- reg.lm$df.residual p.val.robust <- 2*(1-pt(abs(t.robust),df)) z$cov.unscaled = W.VCOV/sig.sq z$coefficients[,2] = White.SE z$coefficients[,3] = t.robust z$coefficients[,4] = p.val.robust z }
And, here's an example of putting the code to use. First, note what the summary output gives (cutting the call, residuals, and significance codes). Next, see the output from my summarize.r command.
One of the frustrating things about using R for econometrics is that R has some cumbersome functionality for computing the standard (in econometrics, anyway) White standard errors. As one of my favorite references on R learning points out, there are two ways to retrieve these precious standard errors.
- You could install and load the sandwich library. Then, apply the function vcovHC to your lmHC0. That will give you the White covariance matrix. Next, take the square root of the diagonals, and you have retrieved your standard errors.
- Install and load the car library. Then, apply the function hccm to your lm object, specifying the option hc0. Again, this only gives the covariance matrix, which you have to transform to obtain standard errors.
The function is called summarize.r(), and it works just like the summary command. In fact, my function calls summary.lm, and then modifies the contents that are printed using method 1 from above.
To run my function, (1) download and install the sandwich library, (2) run the code that defines the function (see below), and (3) after fitting your regression using lm(), you can use the function by calling summarize.r(yourregression.lm).
Without further ado, here's the code you should run.
summarize.r = function(reg.lm, ...){
z = summary.lm(reg.lm)
W.VCOV = vcovHC(reg.lm,type="HC0")
sig.sq = ((summary(reg.lm)$sigma)^2)
coef = z$coefficients[,1]
White.SE <- sqrt(diag(W.VCOV)) t.robust <- coef/White.SE ## Element-by-element division df <- reg.lm$df.residual p.val.robust <- 2*(1-pt(abs(t.robust),df)) z$cov.unscaled = W.VCOV/sig.sq z$coefficients[,2] = White.SE z$coefficients[,3] = t.robust z$coefficients[,4] = p.val.robust z }
And, here's an example of putting the code to use. First, note what the summary output gives (cutting the call, residuals, and significance codes). Next, see the output from my summarize.r command.
> summary(mathcomp.lm)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 643.59 2.06 312.388 <> summarize.r(mathcomp.lm)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 643.587 2.053 313.495 <>
Subscribe to:
Posts (Atom)