Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (95 sloc) 6.32 KB
---
title: "Getting Started with autoCaret"
author: "Greg Ceccarelli"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{autoCaret}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
This vignette is designed to introduce you to the `autoCaret` R package. This package is built on top of both the [caret](https://cran.r-project.org/web/packages/caret/) and [caretEnsemble](https://cran.r-project.org/web/packages/caretEnsemble/) R packages for machine learning and can take as input an R dataframe suitable for binary classification. Currently, [binary classification](https://en.wikipedia.org/wiki/Binary_classification), is the primary purpose of `autoCaret`.
The main function, `automodel`, will do the following:
1. Validate the input data frame - binarizing the target variable if possible.
2. Split the dataframe into both a training and test set
3. Preprocess the input data - center, scale and remove variables with zero variance
3. Check for class imbalance and attempt to downsample, as default, to help combat poor predictive accuracy
4. Build a suite of models determined by the `methodlist` parameter. If NULL, defaults to "glm", "rpart", rf" &"xgbLinear""
5. Blend the models into an ensemble model using the `caretEnsemble` package
6. Use the ensemble to make predictions on the held out test set
7. Return, to the end user, an `autoCaret` model object with a variety of useful pieces of metadata about the modeling process
The `autoCaret` model object is fully accessible and can be summarized using the the `summary()` generic function.
Additionally, predictions can be made using the `predict()` generic function passing in as parameters an `autoCaret` object and new data.
---
## Installation
At the time of this writing, this package is not hosted on CRAN, but can be obtained from GitHub. To do so, first make sure you have [devtools](https://cran.r-project.org/web/packages/devtools/index.html) installed.
``` {r, eval=FALSE}
install.packages("devtools")
```
Now we can install from GitHub using the following line:
``` {r, eval=FALSE}
devtools::install_github("gregce/autoCaret")
```
Once the `autoCaret` package is installed, you may access its functionality as you would any other package by calling:
``` {r, eval=FALSE}
library("autoCaret")
```
If all went well, check out the vignette("autoCaret") which will pull up this vignette!
## Basic Usage
We begin by loading the `mlbench` pakacage and some example data, Sonar, which is commonly used to demostrate machine learning functionality. In this example, we will attempt to distinguish Mines (M) from Rocks (R) using binary classification with an initial dataset of where N=208 and P=60.
As a general rule, when using `autoCaret::autoModel` defaults, datasets less than 100mb should yield optimal performance. and in order to avoid extremely long run times and/or high memory requirements.
```{r, eval=FALSE, message=FALSE}
library(mlbench)
library(autoCaret)
# Load the data into Memory from the mlbench package
data("Sonar")
# Take a brief peak at the Sonar dataframe
dplyr::glimpse(Sonar)
```
Having both the data loaded and having inspected it, we can now make use of the `autoCaret::autoModel()` function As stated above, we intend to try and distinguish Rocks (R) from Mines (R), so we will attempt to predict the `Class` variable in the Sonar dataframe.
Using it's defaults, `autoModel` has 2 arguments we need to specify: `df` and `y`.
`df` is the Dataframe that we'd like to use build a binary classification model, while `y` is our classification target or response variable. We can use a non-exported package function, `autoCaret:::checkBinaryTrait` to determine if our `y` variable is indeed binary. The `autoModel` functionality will perform this for us as well.
```{r, eval=FALSE, cache=TRUE, warning=FALSE, message=FALSE}
# Manually check that our intended y paramter is indeed binary
autoCaret:::checkBinaryTrait(Sonar$Class)
# Generate an autoCaret object using the autoModel function
mod <- autoCaret::autoModel(df = Sonar, y = Class, progressBar = FALSE)
```
In the example above, the returned object, `mod`, is an `autoCaret` object containing 16 objects. To confirm, we can run the below two commmands:
```{r eval=FALSE, warning=FALSE, message=FALSE}
# Check class of autoCaret object
class(mod)
# High level
nrow(summary.default(mod))
```
Running the summary function on our model output displays a wealth of information about the contents of the object as well as the procedural steps taken during modeling. In our example, we observe:
- that our initial dataset of 208 observation was split into a training and test set containing 167 and 41 observations
- Modeling took .64 minutes and entailed resampling our dataset 10 times
- We used the four default models to create an ensemble.
- Using the ensemble model that was generated to predict on the test set yield predictions with 92% accuracy.
```{r eval=FALSE, warning=FALSE, message=FALSE}
# Use the summary generic to store a summary of autoCaret object
overview <- summary(mod)
```
We can also access each of object variables included in the above displayed summary output via the object itself.
```{r eval=FALSE, warning=FALSE, message=FALSE}
# Print the overview to the console
overview
```
## Predicting new data
So now that we have a sense of how successful our auto modeling approach was, we'd likely want to use the model, `mod`, we built previously to make predictions on new data we receive.
Because this is an illustrative example, we'lll take a shortcut by just resampling the same data that we used to train on. The main point here is that you can simply pass your `autoCaret` model object, `mod`, into the `predict()` function along with new observations to generate predictions.
```{r eval=FALSE, warning=FALSE, message=FALSE}
new <- Sonar[sample(1:nrow(Sonar), 50, replace=TRUE),]
#Make predicitons
preds <- predict(mod, new)
#Print Predictions
preds
```
How well did we do? Well a confusion matrix from the `caret` package can tell us!
- We only mispredicted one example, for overall accuracy of .98
- **Note:** we wouldn't expect this level of accuracy using real data given we resampled from our original training set.
```{r eval=FALSE, warning=FALSE, message=FALSE}
## How well did we do?
caret::confusionMatrix(data = preds, reference = new$Class)
```