Cubist is a rule based model that builds regression solutions based on building rules. In this article, we will learn how to use cubist model in r.

For this tutorial, we will use the Boston data set which includes
housing data with features of the houses and their prices. We would like
to predict the `medv`

column or the medium value.

```
library(MASS)
data(Boston)
str(Boston)
```

```
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
```

To create a basic Cubist model in R, we can use the `cubist`

function
from the `cubist`

function. We pass the formula of the model `medv ~.`

which means to model medium value by all other predictors. We also pass
our data `Boston`

.

`library(Cubist)`

```
## Warning: package 'Cubist' was built under R version 4.0.5
## Loading required package: lattice
```

```
features = subset(Boston, select=-c(medv))
target = subset(Boston, select=medv)[,1]
model = cubist(features, target)
model
```

```
##
## Call:
## cubist.default(x = features, y = target)
##
## Number of samples: 506
## Number of predictors: 13
##
## Number of committees: 1
## Number of rules: 4
```

We will now see how to model a ridge regression using the `Caret`

package. We will use this library as it provides us with many features
for real life modeling. To do this, we use the `train`

method. We pass
the same parameters as above, but in addition we pass the
`method = 'rf'`

model to tell Caret to use a lasso model.

`library(caret)`

`## Loading required package: ggplot2`

```
set.seed(1)
model <- train(
medv ~ .,
data = Boston,
method = 'cubist'
)
model
```

```
## Cubist
##
## 506 samples
## 13 predictor
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 506, 506, 506, 506, 506, 506, ...
## Resampling results across tuning parameters:
##
## committees neighbors RMSE Rsquared MAE
## 1 0 4.906555 0.7379561 2.936537
## 1 5 4.829737 0.7488338 2.837241
## 1 9 4.831213 0.7480432 2.842246
## 10 0 3.529736 0.8513274 2.272161
## 10 5 3.465366 0.8577794 2.207494
## 10 9 3.466467 0.8573427 2.206431
## 20 0 3.425982 0.8594506 2.212515
## 20 5 3.355960 0.8658640 2.143559
## 20 9 3.360982 0.8651655 2.144974
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were committees = 20 and neighbors = 5.
```

Here we can see that caret automatically trained over multiple hyper parameters. We can easily plot those to visualize.

`plot(model)`

One feature that we use from Caret is preprocessing. Often in real life
data science we want to run some pre processing before modeling. We will
center and scale our data by passing the following to the train method:
`preProcess = c("center", "scale")`

.

```
set.seed(1)
model2 <- train(
medv ~ .,
data = Boston,
method = 'cubist',
preProcess = c("center", "scale")
)
model2
```

```
## Cubist
##
## 506 samples
## 13 predictor
##
## Pre-processing: centered (13), scaled (13)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 506, 506, 506, 506, 506, 506, ...
## Resampling results across tuning parameters:
##
## committees neighbors RMSE Rsquared MAE
## 1 0 4.904962 0.7390994 2.929891
## 1 5 4.831118 0.7495491 2.833273
## 1 9 4.831585 0.7488174 2.837360
## 10 0 3.543803 0.8504440 2.291523
## 10 5 3.476877 0.8570426 2.217470
## 10 9 3.480213 0.8565179 2.215752
## 20 0 3.457679 0.8569993 2.226730
## 20 5 3.398680 0.8626777 2.152162
## 20 9 3.398507 0.8624353 2.150988
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were committees = 20 and neighbors = 9.
```

Often when we are modeling, we want to split our data into a train and
test set. This way, we can check for overfitting. We can use the
`createDataPartition`

method to do this. In this example, we use the
target `medv`

to split into an 80/20 split, `p = .80`

.

This function will return indexes that contains 80% of the data that we should use for training. We then use the indexes to get our training data from the data set.

```
set.seed(1)
inTraining <- createDataPartition(Boston$medv, p = .80, list = FALSE)
training <- Boston[inTraining,]
testing <- Boston[-inTraining,]
```

We can then fit our model again using only the training data.

```
set.seed(1)
model3 <- train(
medv ~ .,
data = training,
method = 'cubist',
preProcess = c("center", "scale")
)
model3
```

```
## Cubist
##
## 407 samples
## 13 predictor
##
## Pre-processing: centered (13), scaled (13)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 407, 407, 407, 407, 407, 407, ...
## Resampling results across tuning parameters:
##
## committees neighbors RMSE Rsquared MAE
## 1 0 5.105071 0.7204939 3.120618
## 1 5 5.040083 0.7305274 3.033722
## 1 9 5.041142 0.7292094 3.037932
## 10 0 3.861458 0.8192079 2.449168
## 10 5 3.813028 0.8256909 2.389976
## 10 9 3.819712 0.8245150 2.389547
## 20 0 3.698578 0.8331434 2.364653
## 20 5 3.644622 0.8388160 2.292814
## 20 9 3.649504 0.8380138 2.296908
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were committees = 20 and neighbors = 5.
```

Now, we want to check our data on the test set. We can use the `subset`

method to get the features and test target. We then use the `predict`

method passing in our model from above and the test features.

Finally, we calculate the RMSE and r2 to compare to the model above.

```
test.features = subset(testing, select=-c(medv))
test.target = subset(testing, select=medv)[,1]
predictions = predict(model3, newdata = test.features)
# RMSE
sqrt(mean((test.target - predictions)^2))
```

`## [1] 2.611903`

```
# R2
cor(test.target, predictions) ^ 2
```

`## [1] 0.9223673`

In practice, we don’t normal build our data in on training set. It is
common to use a data partitioning strategy like k-fold cross-validation
that resamples and splits our data many times. We then train the model
on these samples and pick the best model. Caret makes this easy with the
`trainControl`

method.

We will use 10-fold cross-validation in this tutorial. To do this we
need to pass three parameters `method = "cv"`

, `number = 10`

(for
10-fold). We store this result in a variable.

```
set.seed(1)
ctrl <- trainControl(
method = "cv",
number = 10,
)
```

Now, we can retrain our model and pass the `ctrl`

response to the
`trControl`

parameter. Notice the our call has added
`trControl = set.seed`

.

```
# set.seed(1)
model4 <- train(
medv ~ .,
data = training,
method = 'cubist',
preProcess = c("center", "scale"),
trControl = ctrl
)
model4
```

```
## Cubist
##
## 407 samples
## 13 predictor
##
## Pre-processing: centered (13), scaled (13)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 367, 366, 367, 366, 365, 367, ...
## Resampling results across tuning parameters:
##
## committees neighbors RMSE Rsquared MAE
## 1 0 3.905715 0.8186095 2.626573
## 1 5 3.566908 0.8497957 2.326520
## 1 9 3.662474 0.8417196 2.402689
## 10 0 3.368629 0.8620058 2.342268
## 10 5 3.082114 0.8832821 2.087365
## 10 9 3.146574 0.8783804 2.126777
## 20 0 3.388005 0.8614741 2.345478
## 20 5 3.068712 0.8848840 2.074521
## 20 9 3.142148 0.8793731 2.124737
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were committees = 20 and neighbors = 5.
```

`plot(model4)`

This results seemed to have improved our accuracy for our training data. Let’s check this on the test data to see the results.

```
test.features = subset(testing, select=-c(medv))
test.target = subset(testing, select=medv)[,1]
predictions = predict(model4, newdata = test.features)
# RMSE
sqrt(mean((test.target - predictions)^2))
```

`## [1] 2.611903`

```
# R2
cor(test.target, predictions) ^ 2
```

`## [1] 0.9223673`

To tune a cubist model, we can give the model different values of
`committees`

and `neighbors`

. Caret will retrain the model using
different tuning values and select the best version.

```
set.seed(1)
tuneGrid <- expand.grid(
committees = c(5, 10),
neighbors = c(5, 9)
)
model5 <- train(
medv ~ .,
data = training,
method = 'cubist',
preProcess = c("center", "scale"),
trControl = ctrl,
tuneGrid = tuneGrid
)
model5
```

```
## Cubist
##
## 407 samples
## 13 predictor
##
## Pre-processing: centered (13), scaled (13)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 367, 366, 367, 366, 365, 367, ...
## Resampling results across tuning parameters:
##
## committees neighbors RMSE Rsquared MAE
## 5 5 3.099071 0.8852719 2.125691
## 5 9 3.149099 0.8809142 2.163837
## 10 5 3.082114 0.8832821 2.087365
## 10 9 3.146574 0.8783804 2.126777
##
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were committees = 10 and neighbors = 5.
```

Finally, we can again plot the model to see how it performs over different tuning parameters.

`plot(model5)`