Heteroscedasticity

2017-06-05

Introduction

One of the assumptions made about residuals/errors in OLS regression is that the errors have the same but unknown variance. This is known as constant variance or homoscedasticity. When this assumption is violated, the problem is known as heteroscedasticity.

Consequences of Heteroscedasticity

olsrr provides the following 4 tests for detecting heteroscedasticity:

Bartlett Test

Bartlett’s test is used to test if variances across samples is equal. It is sensitive to departures from normality. The Levene test is an alternative test that is less sensitive to departures from normality.

You can perform the test using 2 continuous variables, one continuous and one grouping variable, a formula or a linear model.

Use grouping variable

model <- lm(mpg ~ disp + hp, data = mtcars)
resid <- residuals(model)
cyl <- as.factor(mtcars$cyl)
ols_bartlett_test(resid, group_var = cyl)
## 
##     Bartlett's Test of Homogenity of Variances    
## ------------------------------------------------
## Ho: Variances are equal across groups
## Ha: Variances are unequal for atleast two groups
## 
##       Test Summary       
##  ------------------------
##  DF            =    2 
##  Chi2          =    3.648 
##  Prob > Chi2   =    0.161

Using variables

ols_bartlett_test(hsb$read, hsb$write)
## 
##     Bartlett's Test of Homogenity of Variances    
## ------------------------------------------------
## Ho: Variances are equal across groups
## Ha: Variances are unequal for atleast two groups
## 
##         Data          
##  ---------------------
##  Variables: read write 
## 
##       Test Summary       
##  ------------------------
##  DF            =    1 
##  Chi2          =    1.223 
##  Prob > Chi2   =    0.269

Using formula

mt <- mtcars
mt$cyl <- as.factor(mt$cyl)
ols_bartlett_test(mpg ~ cyl, data = mt)
## 
##     Bartlett's Test of Homogenity of Variances    
## ------------------------------------------------
## Ho: Variances are equal across groups
## Ha: Variances are unequal for atleast two groups
## 
##       Test Summary       
##  ------------------------
##  DF            =    2 
##  Chi2          =    8.393 
##  Prob > Chi2   =    0.015

Using linear model

mtcars$cyl <- as.factor(mtcars$cyl)
model <- lm(mpg ~ cyl, data = mtcars)
ols_bartlett_test(model)
## 
##     Bartlett's Test of Homogenity of Variances    
## ------------------------------------------------
## Ho: Variances are equal across groups
## Ha: Variances are unequal for atleast two groups
## 
##       Test Summary       
##  ------------------------
##  DF            =    2 
##  Chi2          =    8.393 
##  Prob > Chi2   =    0.015

Breusch Pagan Test

Breusch Pagan Test was introduced by Trevor Breusch and Adrian Pagan in 1979. It is used to test for heteroskedasticity in a linear regression model and assumes that the error terms are normally distributed. It tests whether the variance of the errors from a regression is dependent on the values of the independent variables. It is a \(\chi^{2}\) test.

You can perform the test using the fitted values of the model, the predictors in the model and a subset of the independent variables. It includes options to perform multiple tests and p value adjustments. The options for p value adjustments include Bonferroni, Sidak and Holm’s method.

Use fitted values of the model

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##              Data               
##  -------------------------------
##  Response : mpg 
##  Variables: fitted values of mpg 
## 
##       Test Summary        
##  -------------------------
##  DF            =    1 
##  Chi2          =    1.4297 
##  Prob > Chi2   =    0.2318

Use independent variables of the model

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##            Data            
##  --------------------------
##  Response : mpg 
##  Variables: disp hp wt drat 
## 
##       Test Summary        
##  -------------------------
##  DF            =    4 
##  Chi2          =    1.5138 
##  Prob > Chi2   =    0.8242

Use independent variables of the model and perform multiple tests

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##            Data            
##  --------------------------
##  Response : mpg 
##  Variables: disp hp wt drat 
## 
##      Test Summary (Unadjusted p values)    
##  ----------------------------------------
##   Variable          chi2     df      p    
##  ----------------------------------------
##   disp             1.2355     1    0.2663 
##   hp               0.9210     1    0.3372 
##   wt               1.2530     1    0.2630 
##   drat             1.1668     1    0.2800 
##  ----------------------------------------
##   simultaneous     1.5138     4    0.8242 
##  ----------------------------------------

Bonferroni p value Adjustment

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'bonferroni')
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##            Data            
##  --------------------------
##  Response : mpg 
##  Variables: disp hp wt drat 
## 
##      Test Summary (Bonferroni p values)    
##  ----------------------------------------
##   Variable          chi2     df      p    
##  ----------------------------------------
##   disp             1.2355     1    1.0000 
##   hp               0.9210     1    1.0000 
##   wt               1.2530     1    1.0000 
##   drat             1.1668     1    1.0000 
##  ----------------------------------------
##   simultaneous     1.5138     4    0.8242 
##  ----------------------------------------

Sidak p value Adjustment

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'sidak')
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##            Data            
##  --------------------------
##  Response : mpg 
##  Variables: disp hp wt drat 
## 
##        Test Summary (Sidak p values)       
##  ----------------------------------------
##   Variable          chi2     df      p    
##  ----------------------------------------
##   disp             1.2355     1    0.7103 
##   hp               0.9210     1    0.8070 
##   wt               1.2530     1    0.7049 
##   drat             1.1668     1    0.7313 
##  ----------------------------------------
##   simultaneous     1.5138     4    0.8242 
##  ----------------------------------------

Holm’s p value Adjustment

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'holm')
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##            Data            
##  --------------------------
##  Response : mpg 
##  Variables: disp hp wt drat 
## 
##        Test Summary (Holm's p values)      
##  ----------------------------------------
##   Variable          chi2     df      p    
##  ----------------------------------------
##   disp             1.2355     1    0.7990 
##   hp               0.9210     1    0.3372 
##   wt               1.2530     1    1.0000 
##   drat             1.1668     1    0.5601 
##  ----------------------------------------
##   simultaneous     1.5138     4    0.8242 
##  ----------------------------------------

Score Test

Test for heteroskedasticity under the assumption that the errors are independent and identically distributed (i.i.d.). You can perform the test using the fitted values of the model, the predictors in the model and a subset of the independent variables.

Use fitted values of the model

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model)
## 
##  Score Test for Heteroskedasticity
##  ---------------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: fitted values of mpg 
## 
##       Test Summary       
##  ------------------------
##  DF            =    1 
##  Chi2          =    0.516 
##  Prob > Chi2   =    0.472

Use independent variables of the model

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model, rhs = TRUE)
## 
##  Score Test for Heteroskedasticity
##  ---------------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: disp hp wt qsec 
## 
##       Test Summary       
##  ------------------------
##  DF            =    4 
##  Chi2          =    2.039 
##  Prob > Chi2   =    0.729

Specify variables

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model, vars = c('disp', 'hp'))
## 
##  Score Test for Heteroskedasticity
##  ---------------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: disp hp 
## 
##       Test Summary       
##  ------------------------
##  DF            =    2 
##  Chi2          =    0.998 
##  Prob > Chi2   =    0.607

F Test

F Test for heteroskedasticity under the assumption that the errors are independent and identically distributed (i.i.d.). You can perform the test using the fitted values of the model, the predictors in the model and a subset of the independent variables.

Use fitted values of the model

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model)
## 
##  F Test for Heteroskedasticity
##  -----------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: fitted values of mpg 
## 
##     Test Summary      
##  ---------------------
##  Num DF     =    1 
##  Den DF     =    30 
##  F          =    0.492 
##  Prob > F   =    0.488

Use independent variables of the model

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model, rhs = TRUE)
## 
##  F Test for Heteroskedasticity
##  -----------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: disp hp wt qsec 
## 
##     Test Summary      
##  ---------------------
##  Num DF     =    4 
##  Den DF     =    27 
##  F          =    0.459 
##  Prob > F   =    0.765

Specify variables

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model, vars = c('disp', 'hp'))
## 
##  F Test for Heteroskedasticity
##  -----------------------------
##  Ho: Variance is homogenous
##  Ha: Variance is not homogenous
## 
##  Variables: disp hp 
## 
##     Test Summary      
##  ---------------------
##  Num DF     =    2 
##  Den DF     =    29 
##  F          =    0.467 
##  Prob > F   =    0.632