--- title: "Introduction to awe: Analyze, Wrap, Explain" author: "Olushina Olawale Awe" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Introduction to awe: Analyze, Wrap, Explain} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, 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 ) ``` ## Why awe? Most R packages stop once a model is fitted. Very few carry a user through the *entire* analytical workflow: ``` Import -> Explore -> Model -> Wrap -> Explain -> Evaluate -> Communicate ``` **awe** (Analyze, Wrap, Explain) is a single, coherent interface over that whole pipeline. It depends only on base R plus `rpart`, so it always runs; packages like `ggplot2`, `randomForest`, `xgboost`, `nnet`, and `DALEX` are used automatically when installed, for nicer plots, more model candidates, and SHAP-style explanations. ```{r setup} library(awe) ``` ## One call, start to finish ```{r regression-example} fit <- awe(mtcars, target = "mpg", k = 3, seed = 1) ``` The call above walks through six stages and prints a running log: exploratory summary, preprocessing (missing-value imputation and a stratified train/test split), automatic model selection with cross-validation, wrapping the winning model in a common interface, explainability, and evaluation on the held-out test set. The returned object supports familiar generics: ```{r regression-methods} summary(fit) predict(fit, newdata = mtcars[1:5, ]) ``` ```{r regression-plot, fig.alt = "Permutation importance bar chart for the mpg model"} plot(fit) # permutation importance ``` ```{r regression-pdp, fig.alt = "Partial dependence curve for weight"} plot(fit, type = "pdp", var = "wt") # partial dependence ``` ## Classification works the same way The target type -- continuous, binary, or multiclass -- is detected automatically, so no extra arguments are needed for a classification problem: ```{r classification-example} fit_iris <- awe(iris, target = "Species", k = 3, seed = 1) ``` ```{r classification-summary} fit_iris$model$leaderboard fit_iris$evaluation ``` ## Running the pipeline step by step Every stage of `awe()` is also available on its own, for users who want to customise or swap out a step (for example, plugging in their own preprocessing before modelling): ```{r 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 train/test splitting `awe_preprocess()` stratifies the split by default: for a categorical target it samples `test_prop` from each class so proportions match between train and test; for a continuous target it stratifies by quartile bins of the target so both sets span a similar range. ```{r 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)) ``` Set `stratify = FALSE` for a plain random split instead. ## Narrative summaries and reports `awe_story()` drafts a plain-language executive summary, key findings, recommendations, and limitations from the pieces above; `awe_report()` writes this out as a self-contained Markdown file (and to HTML when `rmarkdown` and Pandoc are available): ```{r story-report, eval = FALSE} awe_story(fit) awe_report(fit, file = tempfile("awe_report"), format = "html") ``` ## Automatic feature selection `awe_select_features()` screens predictors before modelling: a near-zero-variance filter, a pairwise correlation filter (numeric predictors), and a univariate association score against the target (correlation, ANOVA F, or Cramer's V depending on variable types). Pass `select_features = TRUE` (and optionally `top_n_features`) to `awe()` to run it as part of the pipeline, or call it directly: ```{r feature-selection} fs <- awe_select_features(mtcars, target = "mpg", top_n = 5) fs plot_feature_selection(fs) ``` ## A larger model bank, selected like caret::train() `awe_model()` tries every candidate learner appropriate to the target type and keeps the best by cross-validated score -- similar to `caret::train()`, but the whole pre/post pipeline is included. Base learners (`lm`/`glm`, `rpart`, and a dependency-free k-NN) always run; `randomForest`, `ranger`, `nnet`, `e1071` (SVM, naive Bayes), `kernlab` (SVM), `glmnet` (ridge/lasso/elastic net/regularized logistic), `gbm`, `class` (k-NN classification), `MASS` (LDA/QDA/robust regression), `earth` (MARS), `pls` (partial least squares), and `xgboost` are added automatically when installed: ```{r model-bank} mo <- awe_model(mtcars, target = "mpg", k = 3, seed = 1) mo$leaderboard ``` Restrict the candidate set with `models = c(...)` if you only want to compare specific engines. ## Plotting models and trees ```{r leaderboard-plot, fig.alt = "Bar chart comparing cross-validated model scores"} plot_leaderboard(mo) ``` ```{r tree-plot, fig.alt = "Decision tree for the mpg model"} plot_tree(mo, which = "decision_tree") ``` ```{r varimp-plot, fig.alt = "Variable importance bar chart"} wr <- awe_wrap(mo) vi <- varImp(wr) vi plot(vi) ``` `plot(fit, type = "leaderboard")` and `plot(fit, type = "tree")` provide the same views directly from a full `awe()` result. ## Preprocessing diagnostics ```{r preprocessing-plots, fig.alt = "Correlation heatmap and missingness plot for mtcars"} plot_correlation(mtcars) plot_missingness(mtcars) ``` ## Listing candidates without fitting anything `awe_registry()` shows every candidate `awe_model()` would try for a given target type, and whether it returns class probabilities (used by `plot_roc()`): ```{r registry} awe_registry("binary") ``` ## Ensembling the top candidates `awe_ensemble()` combines the best few cross-validated candidates into a single performance-weighted ensemble -- a weighted average for regression, a weighted vote for classification -- and returns an ordinary `AWEWrapper`, so it works with `predict()`, `awe_evaluate()`, `awe_explain()`, and `varImp()` exactly like any single fitted model: ```{r ensemble} ens <- awe_ensemble(mo, top_n = 3) ens predict(ens, newdata = mtcars[1:5, ]) ``` ## Hyperparameter tuning `awe_tune()` runs a small, dependency-free grid search with cross-validation for a handful of model families (`decision_tree`, `random_forest`, `glmnet`, `svm_radial`), returning the full grid of scores, the best parameters, and a ready-to-use `AWEWrapper` fit with them: ```{r tune} tn <- awe_tune(mtcars, target = "mpg", model = "decision_tree", k = 3, seed = 1) tn$best_params ``` ## Comparing runs `awe_compare()` stacks the leaderboards of several `awe_model()`/`awe()` runs -- for example, before and after feature selection -- into one table, with a grouped-bar `plot()` method: ```{r 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) ``` ## Classification diagnostics: ROC and confusion matrix For binary targets, `plot_roc()` draws an ROC curve with a rank-based AUC; for any classification target, `plot_confusion()` draws a confusion-matrix heatmap from an `AWEEvaluation`: ```{r 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) ``` ## Regression diagnostics `plot_residuals()` gives the familiar residuals-vs-fitted and normal Q-Q pair for continuous targets: ```{r residuals, fig.alt = "Residuals vs fitted and normal Q-Q plots"} plot_residuals(wr) ``` ## A consistent visual identity Every plotting function in the package shares the same palette and `ggplot2` theme, both exported for reuse in your own plots: ```{r theme} awe_palette(4) ``` ```r ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point(colour = awe_palette(1)) + theme_awe() ``` ## Design philosophy The novelty in **awe** is not new machine-learning algorithms -- it is a single, dependable workflow: automatic statistical reasoning based on variable types, a uniform model-wrapping interface, explainability by default, and communication-first outputs, all reproducible from one call.