library(olsrr) library(dplyr) library(ggplot2) library(gridExtra) library(purrr) library(tibble) library(nortest) library(goftest)

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.

- The OLS estimators and regression predictions based on them remains unbiased and consistent.
- The OLS estimators are no longer the BLUE (Best Linear Unbiased Estimators) because they are no longer efficient, so the regression predictions will be inefficient too.
- Because of the inconsistency of the covariance matrix of the estimated regression coefficients, the tests of hypotheses, (t-test, F-test) are no longer valid.

**olsrr** provides the following 4 tests for detecting heteroscedasticity:

- Bartlett Test
- Breusch Pagan Test
- Score Test
- F 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.

ols_test_bartlett(hsb, read, group_var = female)

```
ols_test_bartlett(hsb, read, write)
```

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.

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model)

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model, rhs = TRUE)

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model, rhs = TRUE, multiple = TRUE)

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model, rhs = TRUE, multiple = TRUE, p.adj = 'bonferroni')

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model, rhs = TRUE, multiple = TRUE, p.adj = 'sidak')

model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars) ols_test_breusch_pagan(model, rhs = TRUE, multiple = TRUE, p.adj = 'holm')

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.

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_score(model)

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_score(model, rhs = TRUE)

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_score(model, vars = c('disp', 'hp'))

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.

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_f(model)

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_f(model, rhs = TRUE)

model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ols_test_f(model, vars = c('disp', 'hp'))

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.