Created
October 11, 2019 14:30
-
-
Save topepo/008e251069e079243298e96994c9ea3f to your computer and use it in GitHub Desktop.
animating a classification boundary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidymodels) | |
library(tune) | |
library(discrim) | |
library(klaR) | |
library(gganimate) | |
options(width = 100) | |
theme_set(theme_bw()) | |
data("parabolic", package = "rsample") | |
set.seed(115) | |
data_split <- initial_split(parabolic, prop = 2 / 3) | |
data_tr <- training(data_split) | |
data_te <- testing(data_split) | |
pred_grid <- | |
expand.grid(X1 = seq(-5, 5, length = 100), | |
X2 = seq(-5, 5, length = 100)) | |
p <- | |
ggplot(data_te, aes(x = X1, y = X2)) + | |
geom_point(aes(col = class), alpha = .3) + | |
coord_equal() + | |
theme(legend.position = "top") | |
rda_mod <- | |
discrim_regularized(frac_common_cov = tune(), frac_identity = tune()) %>% | |
set_engine("klaR") | |
set.seed(20014) | |
folds <- vfold_cv(data_tr, repeats = 5) | |
roc_values <- metric_set(roc_auc) | |
set.seed(57854) | |
rda_search <- | |
tune_Bayes( | |
class ~ X1 + X2, | |
model = rda_mod, | |
rs = folds, | |
initial = 10, | |
iter = 25, | |
perf = roc_values, | |
control = Bayes_control(verbose = TRUE, no_improve = Inf) | |
) | |
models <- | |
rda_search %>% | |
collect_metrics() %>% | |
distinct(.iter, frac_common_cov, frac_identity) %>% | |
dplyr::filter(.iter > 0) %>% | |
dplyr::select(-.iter) | |
for (i in 1:nrow(models)) { | |
new_res <- | |
rda_mod %>% | |
merge(models %>% slice(i)) %>% | |
pull(x) %>% | |
pluck(1) %>% | |
fit(class ~ X1 + X2, data = data_tr) %>% | |
predict(pred_grid, type = "prob") %>% | |
dplyr::select(.pred_Class1) %>% | |
bind_cols(pred_grid) %>% | |
mutate(model = i) | |
if (i == 1) { | |
grid_res <- new_res | |
} else { | |
grid_res <- bind_rows(grid_res, new_res) | |
} | |
} | |
contour_plot <- | |
ggplot(grid_res, aes(x = X1, y = X2)) + | |
geom_point(data = data_te, aes(col = class), alpha = .3) + | |
geom_contour(aes(z = .pred_Class1), breaks = .5, col = "black") + | |
transition_states(model) + | |
labs(title = 'Test set data and contours at search iteration {closest_state}') + | |
coord_equal() + | |
theme(legend.position = "top") | |
anim <- animate(contour_plot, width = 680, height = 680, res = 72*1.2) | |
anim_save("~/tmp/discrim.gif") | |
# > sessioninfo::session_info() | |
# ─ Session info ─────────────────────────────────────────────────────────────────────────────────── | |
# setting value | |
# version R version 3.6.1 (2019-07-05) | |
# os macOS Mojave 10.14.6 | |
# system x86_64, darwin15.6.0 | |
# ui RStudio | |
# language (EN) | |
# collate en_US.UTF-8 | |
# ctype en_US.UTF-8 | |
# tz America/New_York | |
# date 2019-10-11 | |
# | |
# ─ Packages ─────────────────────────────────────────────────────────────────────────────────────── | |
# package * version date lib source | |
# assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) | |
# backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0) | |
# base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.6.0) | |
# bayesplot 1.7.0 2019-05-23 [1] CRAN (R 3.6.0) | |
# boot 1.3-22 2019-04-02 [1] CRAN (R 3.6.1) | |
# broom * 0.5.2 2019-04-07 [1] CRAN (R 3.6.0) | |
# callr 3.3.2 2019-09-22 [1] CRAN (R 3.6.0) | |
# class 7.3-15 2019-01-01 [1] CRAN (R 3.6.1) | |
# classInt 0.4-1 2019-08-06 [1] CRAN (R 3.6.0) | |
# cli 1.9.9.9000 2019-10-04 [1] Github (r-lib/cli@ad6410a) | |
# codetools 0.2-16 2018-12-24 [1] CRAN (R 3.6.1) | |
# colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0) | |
# colourpicker 1.0 2017-09-27 [1] CRAN (R 3.6.0) | |
# combinat 0.0-8 2012-10-29 [1] CRAN (R 3.6.0) | |
# crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) | |
# crosstalk 1.0.0 2016-12-21 [1] CRAN (R 3.6.0) | |
# DBI 1.0.0 2018-05-02 [1] CRAN (R 3.6.0) | |
# dials * 0.0.3 2019-10-01 [1] CRAN (R 3.6.0) | |
# DiceDesign 1.8-1 2019-07-31 [1] CRAN (R 3.6.0) | |
# digest 0.6.21 2019-09-20 [1] CRAN (R 3.6.0) | |
# discrim * 0.0.1 2019-10-10 [1] local | |
# dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.0) | |
# DT 0.9 2019-09-17 [1] CRAN (R 3.6.0) | |
# dygraphs 1.1.1.6 2018-07-11 [1] CRAN (R 3.6.0) | |
# e1071 1.7-2 2019-06-05 [1] CRAN (R 3.6.0) | |
# fansi 0.4.0 2018-10-05 [1] CRAN (R 3.6.0) | |
# farver 1.1.0 2018-11-20 [1] CRAN (R 3.6.0) | |
# foreach 1.4.7 2019-07-27 [1] CRAN (R 3.6.0) | |
# furrr 0.1.0 2018-05-16 [1] CRAN (R 3.6.0) | |
# future 1.14.0 2019-07-02 [1] CRAN (R 3.6.0) | |
# generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0) | |
# gganimate * 1.0.3 2019-04-02 [1] CRAN (R 3.6.0) | |
# ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.0) | |
# ggridges 0.5.1 2018-09-27 [1] CRAN (R 3.6.0) | |
# gifski 0.8.6 2018-09-28 [1] CRAN (R 3.6.0) | |
# globals 0.12.4 2018-10-11 [1] CRAN (R 3.6.0) | |
# glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0) | |
# gower 0.2.1 2019-05-14 [1] CRAN (R 3.6.0) | |
# GPfit 1.0-8 2019-02-08 [1] CRAN (R 3.6.0) | |
# gridExtra 2.3 2017-09-09 [1] CRAN (R 3.6.0) | |
# gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) | |
# gtools 3.8.1 2018-06-26 [1] CRAN (R 3.6.0) | |
# highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) | |
# hms 0.5.1 2019-08-23 [1] CRAN (R 3.6.0) | |
# htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0) | |
# htmlwidgets 1.3 2018-09-30 [1] CRAN (R 3.6.0) | |
# httpuv 1.5.2 2019-09-11 [1] CRAN (R 3.6.0) | |
# igraph 1.2.4.1 2019-04-22 [1] CRAN (R 3.6.0) | |
# infer * 0.5.0 2019-09-27 [1] CRAN (R 3.6.0) | |
# inline 0.3.15 2018-05-18 [1] CRAN (R 3.6.0) | |
# ipred 0.9-9 2019-04-28 [1] CRAN (R 3.6.0) | |
# iterators 1.0.12 2019-07-26 [1] CRAN (R 3.6.0) | |
# janeaustenr 0.1.5 2017-06-10 [1] CRAN (R 3.6.0) | |
# KernSmooth 2.23-15 2015-06-29 [1] CRAN (R 3.6.1) | |
# klaR * 0.6-14 2018-03-19 [1] CRAN (R 3.6.0) | |
# knitr 1.25 2019-09-18 [1] CRAN (R 3.6.0) | |
# labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0) | |
# later 1.0.0 2019-10-04 [1] CRAN (R 3.6.1) | |
# lattice 0.20-38 2018-11-04 [1] CRAN (R 3.6.1) | |
# lava 1.6.6 2019-08-01 [1] CRAN (R 3.6.0) | |
# lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) | |
# lhs 1.0.1 2019-02-03 [1] CRAN (R 3.6.0) | |
# lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.0) | |
# listenv 0.7.0 2018-01-21 [1] CRAN (R 3.6.0) | |
# lme4 1.1-21 2019-03-05 [1] CRAN (R 3.6.0) | |
# loo 2.1.0 2019-03-13 [1] CRAN (R 3.6.0) | |
# lpSolve 5.6.13.3 2019-08-19 [1] CRAN (R 3.6.0) | |
# lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0) | |
# magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) | |
# markdown 1.1 2019-08-07 [1] CRAN (R 3.6.0) | |
# MASS * 7.3-51.4 2019-03-31 [1] CRAN (R 3.6.1) | |
# Matrix 1.2-17 2019-03-22 [1] CRAN (R 3.6.1) | |
# matrixStats 0.55.0 2019-09-07 [1] CRAN (R 3.6.0) | |
# mime 0.7 2019-06-11 [1] CRAN (R 3.6.0) | |
# miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 3.6.0) | |
# minqa 1.2.4 2014-10-09 [1] CRAN (R 3.6.0) | |
# munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) | |
# nlme 3.1-140 2019-05-12 [1] CRAN (R 3.6.1) | |
# nloptr 1.2.1 2018-10-03 [1] CRAN (R 3.6.0) | |
# nnet 7.3-12 2016-02-02 [1] CRAN (R 3.6.1) | |
# parsnip * 0.0.3.9001 2019-10-11 [1] Github (tidymodels/parsnip@ae42617) | |
# pillar 1.4.2 2019-06-29 [1] CRAN (R 3.6.0) | |
# pkgbuild 1.0.5 2019-08-26 [1] CRAN (R 3.6.0) | |
# pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0) | |
# plyr 1.8.4 2016-06-08 [1] CRAN (R 3.6.0) | |
# png 0.1-7 2013-12-03 [1] CRAN (R 3.6.0) | |
# prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0) | |
# pROC 1.15.3 2019-07-21 [1] CRAN (R 3.6.0) | |
# processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0) | |
# prodlim 2018.04.18 2018-04-18 [1] CRAN (R 3.6.0) | |
# progress 1.2.2 2019-05-16 [1] CRAN (R 3.6.0) | |
# promises 1.1.0 2019-10-04 [1] CRAN (R 3.6.0) | |
# ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0) | |
# purrr * 0.3.2 2019-03-15 [1] CRAN (R 3.6.0) | |
# questionr 0.7.0 2018-11-26 [1] CRAN (R 3.6.0) | |
# R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0) | |
# Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.0) | |
# recipes * 0.1.7 2019-09-15 [1] CRAN (R 3.6.0) | |
# reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.6.0) | |
# rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.0) | |
# rpart 4.1-15 2019-04-12 [1] CRAN (R 3.6.1) | |
# rsample * 0.0.5 2019-07-12 [1] CRAN (R 3.6.0) | |
# rsconnect 0.8.15 2019-07-22 [1] CRAN (R 3.6.0) | |
# rstan 2.19.2 2019-07-09 [1] CRAN (R 3.6.0) | |
# rstanarm 2.19.2 2019-10-03 [1] CRAN (R 3.6.1) | |
# rstantools 2.0.0 2019-09-15 [1] CRAN (R 3.6.0) | |
# rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0) | |
# scales * 1.0.0 2018-08-09 [1] CRAN (R 3.6.0) | |
# sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) | |
# sf 0.8-0 2019-09-17 [1] CRAN (R 3.6.0) | |
# shiny 1.3.2 2019-04-22 [1] CRAN (R 3.6.0) | |
# shinyjs 1.0 2018-01-08 [1] CRAN (R 3.6.0) | |
# shinystan 2.5.0 2018-05-01 [1] CRAN (R 3.6.0) | |
# shinythemes 1.1.2 2018-11-06 [1] CRAN (R 3.6.0) | |
# SnowballC 0.6.0 2019-01-15 [1] CRAN (R 3.6.0) | |
# StanHeaders 2.19.0 2019-09-07 [1] CRAN (R 3.6.0) | |
# stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0) | |
# stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) | |
# survival 2.44-1.1 2019-04-01 [1] CRAN (R 3.6.1) | |
# threejs 0.3.1 2017-08-13 [1] CRAN (R 3.6.0) | |
# tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.0) | |
# tidymodels * 0.0.3 2019-10-04 [1] local | |
# tidyposterior 0.0.2 2018-11-15 [1] CRAN (R 3.6.0) | |
# tidypredict 0.4.3 2019-09-03 [1] CRAN (R 3.6.0) | |
# tidyr * 1.0.0 2019-09-11 [1] CRAN (R 3.6.0) | |
# tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0) | |
# tidytext 0.2.2 2019-07-29 [1] CRAN (R 3.6.0) | |
# timeDate 3043.102 2018-02-21 [1] CRAN (R 3.6.0) | |
# tokenizers 0.2.1 2018-03-29 [1] CRAN (R 3.6.0) | |
# transformr 0.1.1 2018-12-09 [1] CRAN (R 3.6.0) | |
# tune * 0.0.0.9002 2019-10-10 [1] local | |
# tweenr 1.0.1 2018-12-14 [1] CRAN (R 3.6.0) | |
# units 0.6-4 2019-08-22 [1] CRAN (R 3.6.0) | |
# vctrs 0.2.0 2019-07-05 [1] CRAN (R 3.6.0) | |
# withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) | |
# xfun 0.10 2019-10-01 [1] CRAN (R 3.6.0) | |
# xtable 1.8-4 2019-04-21 [1] CRAN (R 3.6.0) | |
# xts 0.11-2 2018-11-05 [1] CRAN (R 3.6.0) | |
# yardstick * 0.0.4 2019-08-26 [1] CRAN (R 3.6.0) | |
# zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0) | |
# zoo 1.8-6 2019-05-28 [1] CRAN (R 3.6.0) | |
# | |
# [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment