Modeling in R
In this example, we will use the rpart
package, which is used to build a decision tree. The tree with the minimum prediction error is selected. After that, the tree is applied to make predictions for unlabeled data with the predict function.
One way to call rpart
is to give it a list of variables and see what happens. Although we have discussed missing values, rpart
has built-in code for dealing with missing values. So let's dive in, and look at the code.
Firstly, we need to call the libraries that we need:
library(rpart) library(rpart.plot) library(caret) library(e1071) library(arules)
Next, let's load in the data, which will be in the AdultUCI
variable:
data("AdultUCI"); AdultUCI
## 75% of the sample size sample_size <- floor(0.80 * nrow(AdultUCI)) ## set the seed to make your partition reproductible set.seed(123) ## Set a variable to have the sample size training.indicator <- sample(seq_len(nrow(AdultUCI)), size = sample_size) # Set up the training and test sets...