My question is how do we check the constant variance assumption in a regression model?
1 Answer
Another name for contant variance assumption is homoskedasticity, and its opposite is heteroskedasticity. To find heteroskedasticity, you can both visually look at the relationship between the fitted values (predicted y
) and the residuals and perform the Breusch-Pagan test. In R:
# generate a heteroskedastic independent variable x
set.seed(0)
n <- 1000
x <- rgamma(n, shape=6, scale=1/2)
e <- rnorm(length(x), sd=abs(sin(x)))
# which determines the dependent variable y
y <- x + e
# look at the relationship
plot(y ~ x)
By looking at the scatterplot it already becomes apparent that y
doesn't have a constant variance.
You can then plot the linear model itself to get a plot of the relationship between the fitted values (predicted y) and the residuals:
# fit a linear model
lm1 <- lm(y ~ x)
# look at the residuals
plot(lm1)
# press enter once
Again, it becomes pretty clear that the variance of the residuals isn't constant.
To get a quantitative measure of heteroskedasticity, perform the Breusch-Pagan test (requires the lmtest
-Package):
# load library
library(lmtest)
# perform test
bptest(lm1)
This gives:
studentized Breusch-Pagan test
data: lm1
BP = 12.2634, df = 1, p-value = 0.0004619
A high BP-Value with a significant p-value means that the Null-Hypothesis of fit and residuals being uncorrelated can be rejected, thus you have heteroskedasticity in your data (in this case).