Data Science Desktop Survival Guide
by Graham Williams |
|||||
Model Building |
20200607 We now build, fit, or train a model. R has most machine learning algorithms available. We will begin with a simple favourite—the decision tree algorithm— using rpart::rpart(). We record this information using the generic variables mdesc (human readable description of the model type) and mtype (type of the model).
mtype <- "rpart"
mdesc <- "decision tree"
The model will be built using tidyselect::all_of() the dplyr::select()'ed variables from the training dplyr::slice() of the dataset. The training slice is identified as the row numbers stored as tr and the column names stored as vars. This training dataset is piped on to rpart::rpart() together with a specification of the model to be built as stored in form. Using generic variables allows us to change the formula, the dataset, the observations and the variables used in building the model yet retain the same programming code. The resulting model is saved into the variable model.
|
ds %>%
select(all_of(vars)) %>% slice(tr) %>% rpart(form, .) -> model
To view the model simply reference the generic variable model on the command line. This asks R to base::print() the model.
|
model
This textual version of the model provides the basic structure of the tree. We present the details in Chapter 18. Different model builders will base::print() different information. This is our first predictive model. Be sure to spend some time to understand and reflect on the knowledge that the model is exposing.
|