-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe-linear-regression-detect-heteroscedasticity.R
123 lines (95 loc) · 4.66 KB
/
recipe-linear-regression-detect-heteroscedasticity.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
library(olsrr)
# STANDARD REGRESSION ASSUMPTIONS
# Here:
# 3. Homoscedasticity/constant variance assumption: The errors have same but unknown variance
# In other recipes:
# 1. Normality assumption: The error has normal distribution
# 2. The errors have mean zero
# 4. Independent errors: The errors are independent of each other
############################################################
# WORKFLOW
# 1. CHECK FOR HETEROSCEDASTICITY
############################################################
#####################################################################################
# 1. TESTS FOR HETEROSCEDASTICITY
# - Test 1: Bartlett Test
# - Test 2: Breusch Pagan
# - Test 3: Score Test
# - Test 4: F TEST
#####################################################################################
## CONSEQUENCES OF HETEROSCEDASTICITY
# - The OLS estimators and regression predictions based on them remain unbiased and consistent
# - The OLS estimators are no longer the best linear unbiased estimators (BLUE) because they
# are no longer efficient; so the regression predictions will be inefficient too
# - Because of the inconsistency of the covariance matrex of the estimated regression coefficient
# tests of hypotheses, (t-test, F-test) are no longer valid
# -----------------------------------------------------------------------------------
## Test 1: Bartlett Test: Tests if variance among samples is equal.
# - Sensitive to departures from normality.
# - Can be used on 2 continuous variables, 1 continuous & one grouping variable, a
# formula or a linear model
# -----------------------------------------------------------------------------------
# using grouping variable
model <- lm(mpg ~ disp + hp, data = mtcars)
resid <- residuals(model)
cyl <- as.factor(mtcars$cyl)
ols_bartlett_test(resid, group_var = cyl)
# using variables
ols_bartlett_test(hsb$rad, hsb$write)
# using a formula
mt <- mtcars
mt$cyl <- as.factor(mt$cyl)
ols_bartlett_test(mpg ~ cyl, data = mt)
# using a linear model
mtcars$cyl <- as.factor(mtcars$cyl)
model <- lm(mpg ~ cyl, data = mtcars)
ols_bartlett_test(model)
# -----------------------------------------------------------------------------------
## Test 2: Breusch Pagan test: test for herteroskedasticity (non-constant error variance).
# - ASSUMES THE ERROR TERMS ARE NORMALLY DISTRIBUTED
# - 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.
# -----------------------------------------------------------------------------------
# using fitted values of the model
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model)
# using independent variables of the model
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE)
# using independent variables of the model and performing multiple tests
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE)
# Breusch Pagan with p-value adjustment: The Bonferroni test
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'bonferroni')
# Breusch Pagan with p-value adjustment: The Sidak test
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'sidak')
# Breusch Pagan with p-value adjustment: Holm's test
model <- lm(mpg ~ disp + hp + wt + drat, data = mtcars)
ols_bp_test(model, rhs = TRUE, multiple = TRUE, p.adj = 'holm')
# -----------------------------------------------------------------------------------
# TEST 3: SCORE TEST
# - ASSUMES ERRORS ARE INDEPENDENT AND IDENTICALLY DISTRIBUTED (i.i.d)
# -----------------------------------------------------------------------------------
# using fitted values of the model
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model)
# using independent variables of the model
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model, rhs = TRUE)
# specifying variables
mmodel <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_score_test(model, vars = c('disp', 'hp'))
# -----------------------------------------------------------------------------------
## TEST 4: F TEST
# - ASSUMES THE ERRORS ARE INDEPENDENT AND IDENTICALY DISTRIBUTED
# -----------------------------------------------------------------------------------
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model)
# using independent variables of the model
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model, rhs = TRUE)
# specifying variables
model <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars)
ols_f_test(model, vars = c('disp', 'hp'))