-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path04d_CaseStudy2_LR_model_selection_backward.R
336 lines (259 loc) · 11.3 KB
/
04d_CaseStudy2_LR_model_selection_backward.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
############################################################################
### Al.I. Cuza University of Iași ###
### Faculty of Economics and Business Administration ###
### Department of Accounting, Information Systems and Statistics ###
############################################################################
###
############################################################################
### Data Processing/Analysis/Science with R ###
############################################################################
###
############################################################################
### 4d. Regression Models Selection ###
############################################################################
#### last update: 18.11.2024
### For a comprehensive discussion about Regression, see
### https://github.com/marinfotache/Data-Processing-Analysis-Science-with-R/tree/master/11%20Scoring%20(Regression)%20and%20Classification
library(tidyverse)
library(janitor)
options(scipen=999)
library (broom)
#install.packages("ggrepel")
library(ggrepel)
#install.packages('corrplot')
library(corrplot)
#install.packages('car')
library(car)
#install.packages('corrgram')
library(corrgram)
library(DataExplorer)
setwd('/Users/marinfotache/Google Drive/R(Mac)-1 googledrive/DataSets')
############################################################################
### Case study: `States` (USA)
# Example taken from Kabacoff's R in Action (Manning), 2013, 2015
# state.x77 dataset in the base package
# Explore the relationship between a state’s murder rate and other characteristics of
# the state, including population, illiteracy rate, average income,
# and frost levels (mean number of days below freezing).
############################################################################
# state.x77 dataset is contained in a matrix, so one must convert it:
test <- state.x77
glimpse(test)
row.names(test)
# older version (base R)
states1 <- as.data.frame(state.x77)
states1$State <- row.names(states1)
names(states1) <- str_replace_all(names(states1), ' |\\.', '_')
head(states1)
# newer version
states <- state.x77 %>%
as_tibble() %>%
mutate (state = row.names(state.x77)) %>%
janitor::clean_names()
glimpse (states)
# Get a comprehensive report about variables distribution and correlation
# for details, see section
# https://github.com/marinfotache/Data-Processing-Analysis-Science-with-R/tree/master/09%20Exploratory%20Data%20Analysis
config <- configure_report(
add_introduce = TRUE,
add_plot_intro = TRUE,
add_plot_missing = TRUE,
add_plot_str = TRUE,
add_plot_histogram = TRUE,
add_plot_density = TRUE,
add_plot_qq = TRUE,
add_plot_prcomp = FALSE,
add_plot_boxplot = TRUE,
add_plot_scatterplot = TRUE
)
DataExplorer::create_report(states, config = config)
# examine bivariate relationships
cor(states %>% select (-state))
corrplot::corrplot(cor(states %>% select (-state),
method = "spearman"), method = "number", type = "upper")
################################################################
## `All-in Model` - the model containing all variables ##
## (except `State`) ##
################################################################
states_lm1 <- lm(murder ~ ., data = states %>% select (-state))
summary(states_lm1)
states_lm1$coefficients
####################################################
## with the `broom` package, we can collect simply
## and rigurously the main model parameters
model_overall <- broom::glance(states_lm1)
model_overall
model_predictors <- tidy(states_lm1)
model_predictors
model_details <- augment(states_lm1)
model_details
# Residuals Sum of Squares
rss <- sum((states$murder - predict(states_lm1))^2)
rss
states_lm1$residuals
sum(states_lm1$residuals^2)
# Mean Squared Error
mse <- mean((states$murder - predict(states_lm1))^2)
mse
# Density plot of the residuals
title_ <- paste0('Residuals Distribution for ALL-IN Model')
ggplot(data = model_details, aes(x = .resid)) +
geom_density(colour="black") +
xlab("Residuals") + ylab("Density") +
ggtitle(title_) +
theme_bw() +
scale_fill_grey() +
theme(strip.text.x = element_text(size = 9)) +
scale_y_continuous() +
scale_x_continuous(breaks=seq(-4, 4, .5)) +
theme(axis.text.x = element_text(angle=45, vjust=.5, hjust=.5))
# Box plot of the residuals
ggplot(data = model_details, aes(x = 1, y = .resid)) +
geom_boxplot() +
ggtitle(title_) +
theme_bw() +
scale_fill_grey() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks = seq(-3, 3, .5)) +
xlab("Residuals")
# for getting the model formula, instead of ...
states_lm1$call
# ... we'll gather information from...
states_lm1$terms[[1]]
states_lm1$terms[[2]]
states_lm1$terms[[3]]
# this is the vector with all possible (simple) predictors
predictors <- setdiff(names(states), c('state', 'murder'))
####################################################################
#### Task: ####
#### Find the best model (what `best` is ?) that explains ####
#### and/or predict the outcome (Murder rate) ####
####################################################################
####################################################################
### Model selection ###
####################################################################
### a. Backward elimination ###
### b. Forward selection ###
### c. Bidirectional elimination ###
### d. Score comparison (for all possible models) ###
####################################################################
####################################################################
### a. Backward elimination ###
####################################################################
## a.1. Choose a significance level (usually 0.05 or 0.10)
## a.2. Start with `all-in` model
## a.3. Remove least significant predictor (the predictor
## with the highest p-value)
## a.4. Build the model (without the predictor found in a.3.)
## a.5. Check if there is at least one predictor with
## the p-value above the significance level.
## If so, go to a.3
## In not, STOP.
current_predictors <- predictors
the_backward_models <- tibble()
removed_predictors <- tibble()
models_residuals <- tibble()
current_step <- 0
## a.1. Choose a significance level (usually 0.05 or 0.10)
significance_level <- 0.05
# loop variable
stop <- FALSE
while (!stop) {
current_step <- current_step + 1
# fit the model
formula_ <- paste('murder ~',
paste(current_predictors, collapse = ' + '))
current_model <- lm(formula = formula_,
data = states %>% select (-state))
# summary(current_model)
# add the overall information about the data into the dataframe
# `the_backward_models`
the_backward_models <- bind_rows(the_backward_models,
glance(current_model) %>%
mutate (predictors = paste(current_predictors,
collapse = ' + '),
step = current_step))
# find the least significant predictor
predictor_details <- tidy(current_model)
the_least_significant_predictor <- predictor_details %>%
filter (term != '(Intercept)') %>%
mutate (max_p_value = max(p.value), step = current_step) %>%
filter (p.value == max_p_value) %>%
head(1)
# the the p-value of the least significant predictor is smaller
# than the significance level, then STOP
if (the_least_significant_predictor[1,]$p.value < significance_level )
break
# here, we still have predictors with p-value above
# the significance level
# we'll remove it from the model ...
current_predictors <- setdiff(current_predictors,
the_least_significant_predictor[1,]$term )
# ... and store it in the `removed_predictors` data frame
removed_predictors <- bind_rows(removed_predictors,
the_least_significant_predictor )
# store the model residuals
models_residuals <- bind_rows(models_residuals,
augment(states_lm1) %>%
mutate(step = current_step, n_of_predictors =
length(current_predictors)) )
}
# The resulted model is:
final_model_backard <- current_model
summary(final_model_backard)
# Compute the residuals sum of squares (RSS)
# for each step of the backward elimination
models_residuals <- models_residuals %>%
mutate (rss = .resid ^ 2)
# Density plot of the Residuals (for checking the normality)
title_ <- paste0('Residuals - Backward Elimination')
ggplot(data = models_residuals,
aes(x = .resid, color = factor(n_of_predictors) )) +
geom_density(alpha = 0.2) +
xlab("Residuals") + ylab("Density") +
ggtitle(title_) +
theme_bw() +
scale_fill_grey() +
scale_y_continuous() +
scale_x_continuous(breaks=seq(-4, 4, .5)) +
theme(axis.text.x = element_text(angle=45, vjust=.5, hjust=.5)) +
theme(legend.position = "bottom", legend.direction = "horizontal")
# Box plot of the residuals sum of squares for models on each step of
# the backaward elimination
title_ <- paste0('Residuals Sum of Squares - Backward Elimination')
ggplot(data = models_residuals,
aes(x = factor(n_of_predictors), y = rss)) +
geom_boxplot() +
ggtitle(title_) +
xlab("Number of predictors in the model") +
theme_bw() +
scale_fill_grey() +
scale_y_continuous()
# plot the BIC for all the models
title_ <- 'BIC (Bayesian Information Criterion) Evolution for Each Step\n of Backward Selection'
ggplot(the_backward_models, aes(x = step, y = BIC)) +
geom_point(col = 'red') +
geom_text_repel(aes(label = predictors), size = 3.5) +
ggtitle(title_) +
theme_bw() +
scale_fill_grey() +
scale_y_continuous(breaks=seq(180, 280, 1))
title_ <- 'BIC, AIC and Adjusted R^2 for Each Step\n of Backward Selection'
ggplot(the_backward_models, aes(x = step, y = BIC)) +
geom_point(col = 'red', size = 2) +
geom_point(aes(x = step, y = AIC), col = 'darkgreen', size = 2) +
geom_point(aes(x = step, y = adj.r.squared), col = 'magenta', size = 2) +
geom_text(aes(x = step, y = step * 10 + 300, label = str_wrap(predictors, width = 20)),
size = 3.5, hjust = 0.5) +
geom_text_repel(aes(x = step, BIC, label = paste0('BIC=', round(BIC, 3)) ),
size = 3.5, col = 'red') +
geom_text_repel(aes(x = step, AIC, label = paste0('AIC=', round(BIC, 3)) ),
size = 3.5, col = 'darkgreen') +
geom_text_repel(aes(x = step, adj.r.squared, label = paste0('adj.r.squared=',
round(adj.r.squared, 3)) ),
size = 3.5, col = 'magenta') +
ggtitle(title_) +
theme_bw() +
scale_fill_grey() +
scale_y_continuous(limits = c(0, 350), breaks=seq(200, 250, 5)) +
scale_x_continuous(limits = c(0, 5), breaks=seq(0, 5, 1))