## ----include = FALSE----------------------------------------------------------
Sys.setenv(OMP_THREAD_LIMIT = "2")  # stay within two cores
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)

## ----setup--------------------------------------------------------------------
library(awe)

## ----regression-example-------------------------------------------------------
fit <- awe(mtcars, target = "mpg", k = 3, seed = 1)

## ----regression-methods-------------------------------------------------------
summary(fit)
predict(fit, newdata = mtcars[1:5, ])

## ----regression-plot, fig.alt = "Permutation importance bar chart for the mpg model"----
plot(fit)                              # permutation importance

## ----regression-pdp, fig.alt = "Partial dependence curve for weight"----------
plot(fit, type = "pdp", var = "wt")    # partial dependence

## ----classification-example---------------------------------------------------
fit_iris <- awe(iris, target = "Species", k = 3, seed = 1)

## ----classification-summary---------------------------------------------------
fit_iris$model$leaderboard
fit_iris$evaluation

## ----step-by-step-------------------------------------------------------------
ex  <- awe_explore(mtcars, target = "mpg")
sp  <- awe_preprocess(mtcars, target = "mpg", stratify = TRUE, seed = 1)
mo  <- awe_model(sp$train, target = "mpg", k = 3, seed = 1)
wr  <- awe_wrap(mo)
xp  <- awe_explain(wr, newdata = sp$train)
ev  <- awe_evaluate(wr, newdata = sp$test)

mo$leaderboard
xp$importance
ev$metrics

## ----stratified-split---------------------------------------------------------
sp <- awe_preprocess(iris, target = "Species", test_prop = 0.3, stratify = TRUE, seed = 1)
prop.table(table(sp$train$Species))
prop.table(table(sp$test$Species))

## ----story-report, eval = FALSE-----------------------------------------------
#  awe_story(fit)
#  awe_report(fit, file = tempfile("awe_report"), format = "html")

## ----feature-selection--------------------------------------------------------
fs <- awe_select_features(mtcars, target = "mpg", top_n = 5)
fs
plot_feature_selection(fs)

## ----model-bank---------------------------------------------------------------
mo <- awe_model(mtcars, target = "mpg", k = 3, seed = 1)
mo$leaderboard

## ----leaderboard-plot, fig.alt = "Bar chart comparing cross-validated model scores"----
plot_leaderboard(mo)

## ----tree-plot, fig.alt = "Decision tree for the mpg model"-------------------
plot_tree(mo, which = "decision_tree")

## ----varimp-plot, fig.alt = "Variable importance bar chart"-------------------
wr <- awe_wrap(mo)
vi <- varImp(wr)
vi
plot(vi)

## ----preprocessing-plots, fig.alt = "Correlation heatmap and missingness plot for mtcars"----
plot_correlation(mtcars)
plot_missingness(mtcars)

## ----registry-----------------------------------------------------------------
awe_registry("binary")

## ----ensemble-----------------------------------------------------------------
ens <- awe_ensemble(mo, top_n = 3)
ens
predict(ens, newdata = mtcars[1:5, ])

## ----tune---------------------------------------------------------------------
tn <- awe_tune(mtcars, target = "mpg", model = "decision_tree", k = 3, seed = 1)
tn$best_params

## ----compare, fig.alt = "Grouped bar chart comparing two model runs"----------
mo_fs <- awe_model(awe_select_features(mtcars, target = "mpg", top_n = 5)$data,
                    target = "mpg", k = 3, seed = 1)
cmp <- awe_compare(all_predictors = mo, top5 = mo_fs)
cmp
plot(cmp)

## ----roc-confusion, fig.alt = "ROC curve and confusion matrix heatmap"--------
iris2 <- iris[iris$Species != "setosa", ]
iris2$Species <- droplevels(iris2$Species)
mo2 <- awe_model(iris2, target = "Species", k = 3, seed = 1)
wr2 <- awe_wrap(mo2)
plot_roc(wr2)

ev2 <- awe_evaluate(wr2, newdata = iris2)
plot_confusion(ev2)

## ----residuals, fig.alt = "Residuals vs fitted and normal Q-Q plots"----------
plot_residuals(wr)

## ----theme--------------------------------------------------------------------
awe_palette(4)

