-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
366 lines (266 loc) · 12.4 KB
/
README.Rmd
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
---
title: "BranchGLM: Efficient Variable Selection for GLMs in R"
output: github_document
---
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/BranchGLM)](https://CRAN.R-project.org/package=BranchGLM)
[![Codecov test coverage](https://codecov.io/gh/JacobSeedorff21/BranchGLM/branch/master/graph/badge.svg)](https://app.codecov.io/gh/JacobSeedorff21/BranchGLM?branch=master)
<!-- badges: end -->
# Overview
**BranchGLM** is a package for fitting GLMs and performing efficient
variable selection for GLMs.
# How to install
**BranchGLM** can be installed using the `install.packages()` function
```{r, eval = FALSE}
install.packages("BranchGLM")
```
It can also be installed via the `install_github()` function from the
**devtools** package.
```{r, eval = FALSE}
devtools::install_github("JacobSeedorff21/BranchGLM")
```
# Usage
## Fitting GLMs
### Linear regression
**BranchGLM** can fit large linear regression models very quickly,
next is a comparison of runtimes with the built-in `lm()` function.
This comparison is based upon a randomly generated linear regression model with
10000 observations and 250 covariates.
```{r linear, warning = FALSE, message = FALSE, fig.path = "README_images/"}
# Loading libraries
library(BranchGLM)
library(microbenchmark)
library(ggplot2)
# Setting seed
set.seed(99601)
# Defining function to generate dataset for linear regression
NormalSimul <- function(n, d, Bprob = .5){
x <- MASS::mvrnorm(n, mu = rep(1, d), Sigma = diag(.5, nrow = d, ncol = d) +
matrix(.5, ncol = d, nrow = d))
beta <- rnorm(d + 1, mean = 1, sd = 1)
beta[sample(2:length(beta), floor((length(beta) - 1) * Bprob))] = 0
y <- x %*% beta[-1] + beta[1] + rnorm(n, sd = 3)
df <- cbind(y, x) |>
as.data.frame()
df$y <- df$V1
df$V1 <- NULL
df
}
# Generating linear regression dataset
df <- NormalSimul(10000, 250)
# Timing linear regression methods with microbenchmark
Times <- microbenchmark("BranchGLM" = {BranchGLM(y ~ ., data = df,
family = "gaussian",
link = "identity")},
"Parallel BranchGLM" = {BranchGLM(y ~ ., data = df,
family = "gaussian",
link = "identity",
parallel = TRUE)},
"lm" = {lm(y ~ ., data = df)},
times = 100)
# Plotting results
autoplot(Times, log = FALSE)
```
### Logistic regression
**BranchGLM** can also fit large logistic regression models very quickly,
next is a comparison of runtimes with the built-in `glm()` function. This comparison
is based upon a randomly generated logistic regression model with 10000 observations
and 100 covariates.
```{r logistic, warning = FALSE, message = FALSE, fig.path = "README_images/"}
# Setting seed
set.seed(78771)
# Defining function to generate dataset for logistic regression
LogisticSimul <- function(n, d, Bprob = .5, sd = 1, rho = 0.5){
x <- MASS::mvrnorm(n, mu = rep(1, d), Sigma = diag(1 - rho, nrow = d, ncol = d) +
matrix(rho, ncol = d, nrow = d))
beta <- rnorm(d + 1, mean = 0, sd = sd)
beta[sample(2:length(beta), floor((length(beta) - 1) * Bprob))] = 0
beta[beta != 0] <- beta[beta != 0] - mean(beta[beta != 0])
p <- 1/(1 + exp(-x %*% beta[-1] - beta[1]))
y <- rbinom(n, 1, p)
df <- cbind(y, x) |>
as.data.frame()
df
}
# Generating logistic regression dataset
df <- LogisticSimul(10000, 100)
# Timing logistic regression methods with microbenchmark
Times <- microbenchmark("BFGS" = {BranchGLM(y ~ ., data = df, family = "binomial",
link = "logit", method = "BFGS")},
"L-BFGS" = {BranchGLM(y ~ ., data = df, family = "binomial",
link = "logit", method = "LBFGS")},
"Fisher" = {BranchGLM(y ~ ., data = df, family = "binomial",
link = "logit", method = "Fisher")},
"Parallel BFGS" = {BranchGLM(y ~ ., data = df, family = "binomial",
link = "logit", method = "BFGS",
parallel = TRUE)},
"Parallel L-BFGS" = {BranchGLM(y ~ ., data = df,
family = "binomial",
link = "logit", method = "LBFGS",
parallel = TRUE)},
"Parallel Fisher" = {BranchGLM(y ~ ., data = df,
family = "binomial",
link = "logit", method = "Fisher",
parallel = TRUE)},
"glm" = {glm(y ~ ., data = df, family = "binomial")},
times = 100)
# Plotting results
autoplot(Times, log = FALSE)
```
## Best subset selection
**BranchGLM** can also perform best subset selection very quickly, here is a
comparison of runtimes with the `bestglm()` function from the **bestglm** package.
This comparison is based upon a randomly generated logistic regression model with 1000
observations and 15 covariates.
```{r, warning = FALSE, message = FALSE}
# Loading bestglm
library(bestglm)
# Setting seed and creating dataset
set.seed(33391)
df <- LogisticSimul(1000, 15, .5, sd = 0.5)
# Times
## Timing switch branch and bound
BranchTime <- system.time(BranchVS <- VariableSelection(y ~ ., data = df,
family = "binomial", link = "logit",
type = "switch branch and bound", showprogress = FALSE,
parallel = FALSE, method = "Fisher",
bestmodels = 10, metric = "AIC"))
BranchTime
## Timing exhaustive search
Xy <- cbind(df[,-1], df[,1])
ExhaustiveTime <- system.time(BestVS <- bestglm(Xy, family = binomial(), IC = "AIC",
TopModels = 10))
ExhaustiveTime
```
Finding the top 10 logistic regression models according to AIC for this simulated
regression model with 15 variables with the switch branch and bound algorithm is about
`r round(ExhaustiveTime[[3]] / BranchTime[[3]], 2)` times faster than an
exhaustive search.
### Checking results
```{r}
# Results
## Checking if both methods give same results
BranchModels <- t(BranchVS$bestmodels[-1, ] == 1)
ExhaustiveModels <- as.matrix(BestVS$BestModels[, -16])
identical(BranchModels, ExhaustiveModels)
```
Hence the two methods result in the same top 10 models and the switch branch and bound
algorithm was much faster than an exhaustive search.
### Visualization
There is also a convenient way to visualize the top models with the **BranchGLM**
package.
```{r visualization1, fig.path = "README_images/"}
# Plotting models
plot(BranchVS, type = "b")
```
## Backward elimination
**BranchGLM** can also perform backward elimination very quickly with a bounding
algorithm, here is a comparison of runtimes with the `step()` function from the
**stats** package. This comparison is based upon a randomly generated logistic
regression model with 1000 observations and 50 covariates.
```{r, warning = FALSE, message = FALSE}
# Setting seed and creating dataset
set.seed(33391)
df <- LogisticSimul(1000, 50, .5, sd = 0.5)
## Times
### Timing fast backward elimination
BackwardTime <- system.time(BackwardVS <- VariableSelection(y ~ ., data = df,
family = "binomial", link = "logit",
type = "fast backward", showprogress = FALSE,
parallel = FALSE, method = "LBFGS",
metric = "AIC"))
BackwardTime
### Timing step function
fullmodel <- glm(y ~ ., data = df, family = binomial(link = "logit"))
stepTime <- system.time(BackwardStep <- step(fullmodel, direction = "backward", trace = 0))
stepTime
```
Using the fast backward elimination algorithm from the **BranchGLM** package was
about `r round(stepTime[[3]] / BackwardTime[[3]], 2)` times faster than step was
for this logistic regression model.
### Checking results
```{r}
## Checking if both methods give same results
### Getting names of variables in final model from BranchGLM
BackwardCoef <- coef(BackwardVS)
BackwardCoef <- BackwardCoef[BackwardCoef != 0, ]
BackwardCoef <- BackwardCoef[order(names(BackwardCoef))]
### Getting names of variables in final model from step
BackwardCoefGLM <- coef(BackwardStep)
BackwardCoefGLM <- BackwardCoefGLM[order(names(BackwardCoefGLM))]
identical(names(BackwardCoef), names(BackwardCoefGLM))
```
Hence the two methods result in the same best model and the fast backward
elimination algorithm is much faster than step.
### Visualization
There is also a convenient way to visualize the backward elimination path with
the **BranchGLM** package.
```{r visualization2, fig.path = "README_images/"}
## Plotting models
plot(BackwardVS, type = "b")
```
## Double backward elimination
**BranchGLM** can also perform a variant of backward elimination where up to
two variables can be removed in one step. We call this method double backward
elimination and we have also developed a faster variant that we call fast double backward
elimination. This method can result in better solutions than what is obtained from
traditional backward elimination, but is also slower. Next, we show a comparison
of runtimes and BIC values from fast backward elimination and fast double backward
elimination. This comparison is based upon a randomly generated logistic regression model
with 1000 observations and 100 covariates.
```{r, warning = FALSE, message = FALSE}
# Setting seed and creating dataset
set.seed(79897)
df <- LogisticSimul(1000, 100, .5, sd = 0.5)
## Times
### Timing fast backward
BackwardTime <- system.time(BackwardVS <- VariableSelection(y ~ ., data = df,
family = "binomial", link = "logit",
type = "fast backward", showprogress = FALSE,
parallel = FALSE, method = "LBFGS", metric = "BIC"))
BackwardTime
### Timing fast double backward
DoubleBackwardTime <- system.time(DoubleBackwardVS <- VariableSelection(y ~ ., data = df,
family = "binomial", link = "logit",
type = "fast double backward", showprogress = FALSE,
parallel = FALSE, method = "LBFGS", metric = "BIC"))
DoubleBackwardTime
```
Using the fast backward elimination algorithm from the **BranchGLM** package was
about `r round(DoubleBackwardTime[[3]] / BackwardTime[[3]], 2)` times faster than
the fast double backward elimination algorithm was
for this logistic regression model. However, the final model from double backward
elimination had a BIC of `r round(DoubleBackwardVS$bestmetrics[1], 2)` while
the final model from traditional backward elimination had a BIC of
`r round(BackwardVS$bestmetrics[1], 2)`. The difference in BIC between these
two methods is pretty small for this logistic regression model, but for some
regression models the difference can be quite large.
## Variable Importance
Calculating L0-penalization based variable importance is quite a bit slower than
just performing best subset selection. But, branch and bound algorithms are used
to make this process considerably faster than using an exhaustive search.
```{r}
# Getting variable importance values
VITime <- system.time(BranchVI <- VariableImportance(BranchVS, showprogress = FALSE))
BranchVI
# Displaying time to calculate VI values
VITime
# Plotting variable importance
barplot(BranchVI)
```
Furthermore, p-values can be found based on the variable importance values. This can
be done with the use of the parametric bootstrap.
```{r}
# Getting p-values
pvalsTime <- system.time(pvals <- VariableImportance.boot(BranchVI,
nboot = 100,
showprogress = FALSE))
pvals
```
Performing the parametric bootstrap with 100 bootstrapped replications took about
`r round(pvalsTime[[3]] / VITime[[3]], 2)` times longer than finding the
variable importance values.
```{r}
# Plotting results
boxplot(pvals, las = 1)
```