Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active February 6, 2019 04:57
Show Gist options
  • Save brshallo/c05f470f675b9bf3e7ac1a4a99844a50 to your computer and use it in GitHub Desktop.
Save brshallo/c05f470f675b9bf3e7ac1a4a99844a50 to your computer and use it in GitHub Desktop.
Best subset vs stepwise, number of models created
``` r
library(tidyverse)
# as k increases from 1:p, see number of models created
p <- 10
map_dbl(1:p, ~ choose(.x, c(1:p)) %>% sum()) %>%
enframe(name = NULL) %>%
rename(best_subset = value) %>%
mutate(p = 1:p,
stepwise = 1 + p * (p + 1) / 2) %>%
ggplot(aes(x = p)) +
geom_line(aes(y = best_subset, colour = "best_subset"), size = 2) +
geom_line(aes(y = stepwise, colour = "stepwise"), size = 2) +
labs(title = "As p increases, number of models created increases", y = "Number of models created") +
theme(text = element_text(size = 20))
```
![](https://i.imgur.com/YsrzXdp.png)
<sup>Created on 2019-02-05 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment