-
Notifications
You must be signed in to change notification settings - Fork 2
/
readme.Rmd
138 lines (93 loc) · 6.53 KB
/
readme.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
title: "autoCaret: make machine learning easier!"
output: md_document
---
**Authors:** [Greg Ceccarelli](https://www.linkedin.com/in/gregceccarelli), [Michael Marks](https://www.linkedin.com/in/michael-marks-58948636), [Rock Baek](https://www.linkedin.com/in/rock-baek-61588623)
<br/>
**License:** [MIT](https://opensource.org/licenses/MIT)
[![Travis-CI Build Status](https://travis-ci.org/gregce/autoCaret.svg?branch=master)](https://travis-ci.org/gregce/autoCaret)
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=TRUE, 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=TRUE, 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=TRUE, 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=TRUE, 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=TRUE, 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=TRUE, 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=TRUE, warning=FALSE, message=FALSE}
## How well did we do?
caret::confusionMatrix(data = preds, reference = new$Class)
```