Kategorien

## Heteroskedasticity-robust standard errors in R

Writing a financial paper is often associated with an OLS regression model. One major issue can be heteroskedasticity: the variance of the error terms vary. I show you how you can detect heteroskedasticity and how to implement robust standard errors in R.

Step 1: Implement a regression model in R

`model1 <- lm(dist ~ speed, data=cars) # initial model using car data`

Step 2: Detect heteroskedasticity using the Breush Pagan Test. When the p-value is below the 10% we can reject the null hypothesis that the variance of the residuals is constant.

`require(lmtest)  # load the packagerequire(vars)`

The p-value is below the critical value of 10%.

`lmtest::bptest(model1)`
`	studentized Breusch-Pagan testdata:  model1BP = 3.2149, df = 1, p-value = 0.07297`

Step 3: Use Newey-West to correct the standard errors

`model2 <- lm(dist~speed,data=cars)model2\$coefficients <- unclass(coeftest(model2, vcov. = NeweyWest))`

Step 4: Compare the results

`summary(model1)`
`Call:lm(formula = dist ~ speed, data = cars)Residuals:    Min      1Q  Median      3Q     Max -29.069  -9.525  -2.272   9.215  43.201 Coefficients:            Estimate Std. Error t value Pr(>|t|)    (Intercept) -17.5791     6.7584  -2.601   0.0123 *  speed         3.9324     0.4155   9.464 1.49e-12 ***---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 15.38 on 48 degrees of freedomMultiple R-squared:  0.6511,	Adjusted R-squared:  0.6438 F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12`
`model2`
`Call:lm(formula = dist ~ speed, data = cars)Coefficients:             Estimate    Std. Error  t value     Pr(>|t|)  (Intercept)  -1.758e+01   7.018e+00  -2.505e+00   1.570e-02speed         3.932e+00   5.509e-01   7.138e+00   4.526e-09`

Notice that the estimated coefficients are the same but the standard errors of the two models differ! Hence, this affects the statistical significance.