glmtree: logistic regression trees for efficient segmentation

Segmentation

Three clusters, one predictive law

Simulation

library(glmtree)
data <- generateData(n = 1000, scenario = "no tree", visualize = TRUE)

int_train <- sample.int(n = 1000, size = 0.2 * 1000)

test <- data[-int_train, ]
data <- data[int_train, ]

PCA

library(FactoMineR)
mixed <- PCA(data[, c("x1", "x2")])

data$pca1 <- predict(mixed, data)$coord[, 1]
data$pca2 <- predict(mixed, data)$coord[, 2]
test$pca1 <- predict(mixed, test)$coord[, 1]
test$pca2 <- predict(mixed, test)$coord[, 2]

data$cluster <- ifelse(data$pca1 > 1, 1, ifelse(data$pca1 > 0, 2, 3))
test$cluster <- ifelse(test$pca1 > 1, 1, ifelse(test$pca1 > 0, 2, 3))

pred <- matrix(0, nrow = 0.2 * 1000, ncol = 1)

for (j in 1:3) {
  modele <- glm(y ~ x1 + x2, data = data[data$cluster == j, ], family = binomial(link = "logit"))
  pred[test$cluster == j] <- predict(modele, test[test$cluster == j, ], type = "response")
}

normalizedGini(test$y, pred)
## [1] 0.6909617
plot(mixed, choix = "ind", label = "none")

MOB

if (require(partykit, quietly = TRUE)) {
  mob_data <- partykit::glmtree(formula = y ~ x1 + x2 | x1 + x2, data = data, family = binomial)
  plot(mob_data)
  normalizedGini(test$y, predict(mob_data, test))
}
## 
## Attaching package: 'partykit'
## The following object is masked from 'package:glmtree':
## 
##     glmtree

## [1] 0.6989529

glmtree approach

tree <- glmtree::glmtree(x = data[, c("x1", "x2")], y = data$y)
## The bic criterion for iteration 1 is 0
## The bic criterion for iteration 2 is -221.646485924906
## The bic criterion for iteration 3 is -221.646485924906
## The bic criterion for iteration 4 is -221.646485924906
## The bic criterion for iteration 5 is -221.646485924906
## The bic criterion for iteration 6 is -221.646485924906
## The bic criterion for iteration 7 is -221.646485924906
## The bic criterion for iteration 8 is -221.646485924906
## The bic criterion for iteration 9 is -221.646485924906
## The bic criterion for iteration 10 is -221.646485924906
## The bic criterion for iteration 11 is -221.646485924906
## The bic criterion for iteration 12 is -221.646485924906
## The bic criterion for iteration 13 is -221.646485924906
## The bic criterion for iteration 14 is -221.646485924906
## The bic criterion for iteration 15 is -221.646485924906
## The bic criterion for iteration 16 is -221.646485924906
## The bic criterion for iteration 17 is -221.646485924906
## The bic criterion for iteration 18 is -221.646485924906
## The bic criterion for iteration 19 is -235.079834875485
## The bic criterion for iteration 20 is -221.646485924906
## The bic criterion for iteration 21 is -221.646485924906
## The bic criterion for iteration 22 is -221.646485924906
## The bic criterion for iteration 23 is -221.646485924906
## The bic criterion for iteration 24 is -221.646485924906
## The bic criterion for iteration 25 is -221.646485924906
## The bic criterion for iteration 26 is -221.646485924906
## The bic criterion for iteration 27 is -221.646485924906
## The bic criterion for iteration 28 is -232.983958043205
## The bic criterion for iteration 29 is -221.646485924906
## The bic criterion for iteration 30 is -232.983958043205
## The bic criterion for iteration 31 is -221.646485924906
## The bic criterion for iteration 32 is -221.646485924906
## The bic criterion for iteration 33 is -221.646485924906
## The bic criterion for iteration 34 is -221.646485924906
## The bic criterion for iteration 35 is -221.646485924906
## The bic criterion for iteration 36 is -221.646485924906
## The bic criterion for iteration 37 is -221.646485924906
## The bic criterion for iteration 38 is -221.646485924906
## The bic criterion for iteration 39 is -221.646485924906
## The bic criterion for iteration 40 is -221.646485924906
## The bic criterion for iteration 41 is -235.079834875485
## The bic criterion for iteration 42 is -235.079834875485
## The bic criterion for iteration 43 is -221.646485924906
## The bic criterion for iteration 44 is -221.646485924906
## The bic criterion for iteration 45 is -221.646485924906
## The bic criterion for iteration 46 is -221.646485924906
## The bic criterion for iteration 47 is -221.646485924906
## The bic criterion for iteration 48 is -221.646485924906
## The bic criterion for iteration 49 is -221.646485924906
## The bic criterion for iteration 50 is -221.646485924906
## The bic criterion for iteration 51 is -221.646485924906
## The bic criterion for iteration 52 is -221.646485924906
## The bic criterion for iteration 53 is -221.646485924906
## The bic criterion for iteration 54 is -221.646485924906
## The bic criterion for iteration 55 is -221.646485924906
## The bic criterion for iteration 56 is -221.646485924906
## The bic criterion for iteration 57 is -221.646485924906
## The bic criterion for iteration 58 is -221.646485924906
## The bic criterion for iteration 59 is -221.646485924906
## The bic criterion for iteration 60 is -221.646485924906
## The bic criterion for iteration 61 is -221.646485924906
## The bic criterion for iteration 62 is -221.646485924906
## The bic criterion for iteration 63 is -221.646485924906
## The bic criterion for iteration 64 is -221.646485924906
## The bic criterion for iteration 65 is -221.646485924906
## The bic criterion for iteration 66 is -221.646485924906
## The bic criterion for iteration 67 is -221.646485924906
## The bic criterion for iteration 68 is -221.646485924906
## The bic criterion for iteration 69 is -221.646485924906
## The bic criterion for iteration 70 is -221.646485924906
## The bic criterion for iteration 71 is -221.646485924906
## The bic criterion for iteration 72 is -221.646485924906
## The bic criterion for iteration 73 is -221.646485924906
## The bic criterion for iteration 74 is -221.646485924906
## The bic criterion for iteration 75 is -221.646485924906
## The bic criterion for iteration 76 is -221.646485924906
## The bic criterion for iteration 77 is -221.646485924906
## The bic criterion for iteration 78 is -221.646485924906
## The bic criterion for iteration 79 is -221.646485924906
## The bic criterion for iteration 80 is -221.646485924906
## The bic criterion for iteration 81 is -221.646485924906
## The bic criterion for iteration 82 is -221.646485924906
## The bic criterion for iteration 83 is -221.646485924906
## The bic criterion for iteration 84 is -221.646485924906
## The bic criterion for iteration 85 is -221.646485924906
## The bic criterion for iteration 86 is -221.646485924906
## The bic criterion for iteration 87 is -221.646485924906
## The bic criterion for iteration 88 is -235.079834875485
## The bic criterion for iteration 89 is -221.646485924906
## The bic criterion for iteration 90 is -221.646485924906
## The bic criterion for iteration 91 is -221.646485924906
## The bic criterion for iteration 92 is -221.646485924906
## The bic criterion for iteration 93 is -221.646485924906
## The bic criterion for iteration 94 is -221.646485924906
## The bic criterion for iteration 95 is -221.646485924906
## The bic criterion for iteration 96 is -221.646485924906
## The bic criterion for iteration 97 is -221.646485924906
## The bic criterion for iteration 98 is -221.646485924906
## The bic criterion for iteration 99 is -221.646485924906
## The bic criterion for iteration 100 is -221.646485924906
## The bic criterion for iteration 101 is -221.646485924906
## The bic criterion for iteration 102 is -221.646485924906
## The bic criterion for iteration 103 is -221.646485924906
## The bic criterion for iteration 104 is -221.646485924906
## The bic criterion for iteration 105 is -221.646485924906
## The bic criterion for iteration 106 is -221.646485924906
## The bic criterion for iteration 107 is -221.646485924906
## The bic criterion for iteration 108 is -221.646485924906
## The bic criterion for iteration 109 is -221.646485924906
## The bic criterion for iteration 110 is -235.079834875485
## The bic criterion for iteration 111 is -221.646485924906
## The bic criterion for iteration 112 is -221.646485924906
## The bic criterion for iteration 113 is -221.646485924906
## The bic criterion for iteration 114 is -235.079834875485
## The bic criterion for iteration 115 is -235.079834875485
## The bic criterion for iteration 116 is -235.079834875485
## The bic criterion for iteration 117 is -235.079834875485
## The bic criterion for iteration 118 is -235.079834875485
## The bic criterion for iteration 119 is -235.079834875485
## The bic criterion for iteration 120 is -235.079834875485
## The bic criterion for iteration 121 is -235.079834875485
## The bic criterion for iteration 122 is -235.079834875485
## The bic criterion for iteration 123 is -235.079834875485
## The bic criterion for iteration 124 is -221.646485924906
## The bic criterion for iteration 125 is -221.646485924906
## The bic criterion for iteration 126 is -221.646485924906
## The bic criterion for iteration 127 is -221.646485924906
## The bic criterion for iteration 128 is -221.646485924906
## The bic criterion for iteration 129 is -221.646485924906
## The bic criterion for iteration 130 is -221.646485924906
## The bic criterion for iteration 131 is -221.646485924906
## The bic criterion for iteration 132 is -221.646485924906
## The bic criterion for iteration 133 is -221.646485924906
## The bic criterion for iteration 134 is -221.646485924906
## The bic criterion for iteration 135 is -221.646485924906
## The bic criterion for iteration 136 is -221.646485924906
## The bic criterion for iteration 137 is -221.646485924906
## The bic criterion for iteration 138 is -221.646485924906
## The bic criterion for iteration 139 is -221.646485924906
## The bic criterion for iteration 140 is -221.646485924906
## The bic criterion for iteration 141 is -221.646485924906
## The bic criterion for iteration 142 is -221.646485924906
## The bic criterion for iteration 143 is -221.646485924906
## The bic criterion for iteration 144 is -221.646485924906
## The bic criterion for iteration 145 is -221.646485924906
## The bic criterion for iteration 146 is -221.646485924906
## The bic criterion for iteration 147 is -221.646485924906
## The bic criterion for iteration 148 is -221.646485924906
## The bic criterion for iteration 149 is -221.646485924906
## The bic criterion for iteration 150 is -221.646485924906
## The bic criterion for iteration 151 is -221.646485924906
## The bic criterion for iteration 152 is -221.646485924906
## The bic criterion for iteration 153 is -221.646485924906
## The bic criterion for iteration 154 is -221.646485924906
## The bic criterion for iteration 155 is -221.646485924906
## The bic criterion for iteration 156 is -221.646485924906
## The bic criterion for iteration 157 is -221.646485924906
## The bic criterion for iteration 158 is -221.646485924906
## The bic criterion for iteration 159 is -221.646485924906
## The bic criterion for iteration 160 is -221.646485924906
## The bic criterion for iteration 161 is -221.646485924906
## The bic criterion for iteration 162 is -221.646485924906
## The bic criterion for iteration 163 is -221.646485924906
## The bic criterion for iteration 164 is -221.646485924906
## The bic criterion for iteration 165 is -221.646485924906
## The bic criterion for iteration 166 is -221.646485924906
## The bic criterion for iteration 167 is -221.646485924906
## The bic criterion for iteration 168 is -221.646485924906
## The bic criterion for iteration 169 is -221.646485924906
## The bic criterion for iteration 170 is -221.646485924906
## The bic criterion for iteration 171 is -221.646485924906
## The bic criterion for iteration 172 is -221.646485924906
## The bic criterion for iteration 173 is -221.646485924906
## The bic criterion for iteration 174 is -235.079834875485
## The bic criterion for iteration 175 is -235.079834875485
## The bic criterion for iteration 176 is -221.646485924906
## The bic criterion for iteration 177 is -221.646485924906
## The bic criterion for iteration 178 is -221.646485924906
## The bic criterion for iteration 179 is -221.646485924906
## The bic criterion for iteration 180 is -221.646485924906
## The bic criterion for iteration 181 is -221.646485924906
## The bic criterion for iteration 182 is -221.646485924906
## The bic criterion for iteration 183 is -221.646485924906
## The bic criterion for iteration 184 is -221.646485924906
## The bic criterion for iteration 185 is -221.646485924906
## The bic criterion for iteration 186 is -221.646485924906
## The bic criterion for iteration 187 is -221.646485924906
## The bic criterion for iteration 188 is -221.646485924906
## The bic criterion for iteration 189 is -221.646485924906
## The bic criterion for iteration 190 is -221.646485924906
## The bic criterion for iteration 191 is -221.646485924906
## The bic criterion for iteration 192 is -221.646485924906
## The bic criterion for iteration 193 is -221.646485924906
## The bic criterion for iteration 194 is -221.646485924906
## The bic criterion for iteration 195 is -221.646485924906
## The bic criterion for iteration 196 is -221.646485924906
## The bic criterion for iteration 197 is -221.646485924906
## The bic criterion for iteration 198 is -221.646485924906
## The bic criterion for iteration 199 is -221.646485924906
## The bic criterion for iteration 200 is -221.646485924906
plot(unlist(tree@performance$criterionEvolution), type = "l")

data$c_map <- factor(apply(predict(tree@best.tree$tree, data, type = "prob"), 1, function(p) names(which.max(p))))
test$c_map <- factor(apply(predict(tree@best.tree$tree, data, type = "prob"), 1, function(p) names(which.max(p))))

table(data$c_map)
## 
##   1 
## 200
plot(data[, 1], data[, 2], pch = 2 + data[, 3], col = as.numeric(data$c_map), xlab = "First coordinate", ylab = "Second coordinate")

plot(tree@best.tree$tree)

pred <- matrix(0, nrow = 0.2 * 1000, ncol = 1)

for (j in levels(data$c_map)) {
  modele <- glm(y ~ x1 + x2, data = data[data$c_map == j, ], family = binomial(link = "logit"))
  pred[test$c_map == j] <- predict(modele, test[test$c_map == j, ], type = "response")
}

normalizedGini(test$y, pred)
## [1] 0.6989529

One “cluster”, three predictive laws

Simulation

data <- generateData(n = 1000, scenario = "tree", visualize = TRUE)

int_train <- sample.int(n = 1000, size = 0.2 * 1000)

test <- data[-int_train, ]
data <- data[int_train, ]

PCA

mixed <- FAMD(data[, c("x1", "x2", "x3")])
## Warning: ggrepel: 127 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
## Warning: ggrepel: 14 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
dim_famd <- predict(mixed, test)$coord[, "Dim 1"] < 0

pred <- matrix(0, nrow = 0.2 * 1000, ncol = 1)

for (j in c(TRUE, FALSE)) {
  modele <- glm(y ~ x1 + x2 + x3, data = data[dim_famd == j, ], family = binomial(link = "logit"))
  pred[dim_famd == j] <- predict(modele, test[dim_famd == j, ], type = "response")
}

normalizedGini(test$y, pred)
## [1] 0.174553

MOB

if (require(partykit, quietly = TRUE)) {
  mob_data <- partykit::glmtree(formula = y ~ x1 + x2 + x3 | x1 + x2 + x3, data = data, family = binomial)
  plot(mob_data)
  normalizedGini(test$y, predict(mob_data, test))
}

## [1] 0.5238221

glmtree approach

tree <- glmtree::glmtree(x = data[, c("x1", "x2", "x3")], y = data$y)
## The bic criterion for iteration 1 is 0
## The bic criterion for iteration 2 is -318.257712801091
## The bic criterion for iteration 3 is -309.583492156519
## The bic criterion for iteration 4 is -309.583492156519
## The bic criterion for iteration 5 is -309.583492156519
## The bic criterion for iteration 6 is -309.583492156519
## The bic criterion for iteration 7 is -309.583492156519
## The bic criterion for iteration 8 is -309.583492156519
## The bic criterion for iteration 9 is -309.583492156519
## The bic criterion for iteration 10 is -310.215063588232
## The bic criterion for iteration 11 is -310.215063588232
## The bic criterion for iteration 12 is -310.215063588232
## The bic criterion for iteration 13 is -310.215063588232
## The bic criterion for iteration 14 is -310.215063588232
## The bic criterion for iteration 15 is -310.215063588232
## The bic criterion for iteration 16 is -310.215063588232
## The bic criterion for iteration 17 is -310.215063588232
## The bic criterion for iteration 18 is -310.215063588232
## The bic criterion for iteration 19 is -310.215063588232
## The bic criterion for iteration 20 is -310.215063588232
## The bic criterion for iteration 21 is -310.215063588232
## The bic criterion for iteration 22 is -310.215063588232
## The bic criterion for iteration 23 is -310.215063588232
## The bic criterion for iteration 24 is -310.215063588232
## The bic criterion for iteration 25 is -310.215063588232
## The bic criterion for iteration 26 is -310.215063588232
## The bic criterion for iteration 27 is -310.215063588232
## The bic criterion for iteration 28 is -310.215063588232
## The bic criterion for iteration 29 is -310.215063588232
## The bic criterion for iteration 30 is -310.215063588232
## The bic criterion for iteration 31 is -310.215063588232
## The bic criterion for iteration 32 is -310.215063588232
## The bic criterion for iteration 33 is -310.215063588232
## The bic criterion for iteration 34 is -310.215063588232
## The bic criterion for iteration 35 is -310.215063588232
## The bic criterion for iteration 36 is -310.215063588232
## The bic criterion for iteration 37 is -310.215063588232
## The bic criterion for iteration 38 is -310.215063588232
## The bic criterion for iteration 39 is -310.215063588232
## The bic criterion for iteration 40 is -310.215063588232
## The bic criterion for iteration 41 is -310.215063588232
## The bic criterion for iteration 42 is -310.215063588232
## The bic criterion for iteration 43 is -310.215063588232
## The bic criterion for iteration 44 is -310.215063588232
## The bic criterion for iteration 45 is -309.583492156519
## The bic criterion for iteration 46 is -310.215063588232
## The bic criterion for iteration 47 is -310.215063588232
## The bic criterion for iteration 48 is -309.583492156519
## The bic criterion for iteration 49 is -310.215063588232
## The bic criterion for iteration 50 is -309.583492156519
## The bic criterion for iteration 51 is -309.583492156519
## The bic criterion for iteration 52 is -310.215063588232
## The bic criterion for iteration 53 is -310.215063588232
## The bic criterion for iteration 54 is -309.583492156519
## The bic criterion for iteration 55 is -310.215063588232
## The bic criterion for iteration 56 is -310.215063588232
## The bic criterion for iteration 57 is -310.215063588232
## The bic criterion for iteration 58 is -310.215063588232
## The bic criterion for iteration 59 is -310.215063588232
## The bic criterion for iteration 60 is -310.215063588232
## The bic criterion for iteration 61 is -310.215063588232
## The bic criterion for iteration 62 is -310.215063588232
## The bic criterion for iteration 63 is -310.215063588232
## The bic criterion for iteration 64 is -310.215063588232
## The bic criterion for iteration 65 is -310.215063588232
## The bic criterion for iteration 66 is -310.215063588232
## The bic criterion for iteration 67 is -310.215063588232
## The bic criterion for iteration 68 is -310.215063588232
## The bic criterion for iteration 69 is -310.215063588232
## The bic criterion for iteration 70 is -310.215063588232
## The bic criterion for iteration 71 is -310.215063588232
## The bic criterion for iteration 72 is -310.215063588232
## The bic criterion for iteration 73 is -310.215063588232
## The bic criterion for iteration 74 is -310.215063588232
## The bic criterion for iteration 75 is -310.215063588232
## The bic criterion for iteration 76 is -310.215063588232
## The bic criterion for iteration 77 is -310.215063588232
## The bic criterion for iteration 78 is -310.215063588232
## The bic criterion for iteration 79 is -310.215063588232
## The bic criterion for iteration 80 is -310.215063588232
## The bic criterion for iteration 81 is -310.215063588232
## The bic criterion for iteration 82 is -310.215063588232
## The bic criterion for iteration 83 is -310.215063588232
## The bic criterion for iteration 84 is -310.215063588232
## The bic criterion for iteration 85 is -310.215063588232
## The bic criterion for iteration 86 is -310.215063588232
## The bic criterion for iteration 87 is -310.215063588232
## The bic criterion for iteration 88 is -310.215063588232
## The bic criterion for iteration 89 is -310.215063588232
## The bic criterion for iteration 90 is -310.215063588232
## The bic criterion for iteration 91 is -310.215063588232
## The bic criterion for iteration 92 is -310.215063588232
## The bic criterion for iteration 93 is -310.215063588232
## The bic criterion for iteration 94 is -310.215063588232
## The bic criterion for iteration 95 is -310.215063588232
## The bic criterion for iteration 96 is -310.215063588232
## The bic criterion for iteration 97 is -310.215063588232
## The bic criterion for iteration 98 is -310.215063588232
## The bic criterion for iteration 99 is -310.215063588232
## The bic criterion for iteration 100 is -310.215063588232
## The bic criterion for iteration 101 is -310.215063588232
## The bic criterion for iteration 102 is -310.215063588232
## The bic criterion for iteration 103 is -310.215063588232
## The bic criterion for iteration 104 is -310.215063588232
## The bic criterion for iteration 105 is -310.215063588232
## The bic criterion for iteration 106 is -310.215063588232
## The bic criterion for iteration 107 is -310.215063588232
## The bic criterion for iteration 108 is -310.215063588232
## The bic criterion for iteration 109 is -310.215063588232
## The bic criterion for iteration 110 is -310.215063588232
## The bic criterion for iteration 111 is -310.215063588232
## The bic criterion for iteration 112 is -310.215063588232
## The bic criterion for iteration 113 is -310.215063588232
## The bic criterion for iteration 114 is -310.215063588232
## The bic criterion for iteration 115 is -310.215063588232
## The bic criterion for iteration 116 is -310.215063588232
## The bic criterion for iteration 117 is -310.215063588232
## The bic criterion for iteration 118 is -310.215063588232
## The bic criterion for iteration 119 is -310.215063588232
## The bic criterion for iteration 120 is -310.215063588232
## The bic criterion for iteration 121 is -310.215063588232
## The bic criterion for iteration 122 is -310.215063588232
## The bic criterion for iteration 123 is -310.215063588232
## The bic criterion for iteration 124 is -310.215063588232
## The bic criterion for iteration 125 is -310.215063588232
## The bic criterion for iteration 126 is -310.215063588232
## The bic criterion for iteration 127 is -310.215063588232
## The bic criterion for iteration 128 is -310.215063588232
## The bic criterion for iteration 129 is -310.215063588232
## The bic criterion for iteration 130 is -310.215063588232
## The bic criterion for iteration 131 is -310.215063588232
## The bic criterion for iteration 132 is -310.215063588232
## The bic criterion for iteration 133 is -310.215063588232
## The bic criterion for iteration 134 is -310.215063588232
## The bic criterion for iteration 135 is -310.215063588232
## The bic criterion for iteration 136 is -310.215063588232
## The bic criterion for iteration 137 is -310.215063588232
## The bic criterion for iteration 138 is -310.215063588232
## The bic criterion for iteration 139 is -310.215063588232
## The bic criterion for iteration 140 is -310.215063588232
## The bic criterion for iteration 141 is -310.215063588232
## The bic criterion for iteration 142 is -310.215063588232
## The bic criterion for iteration 143 is -310.215063588232
## The bic criterion for iteration 144 is -310.215063588232
## The bic criterion for iteration 145 is -310.215063588232
## The bic criterion for iteration 146 is -310.215063588232
## The bic criterion for iteration 147 is -310.215063588232
## The bic criterion for iteration 148 is -310.215063588232
## The bic criterion for iteration 149 is -310.215063588232
## The bic criterion for iteration 150 is -310.215063588232
## The bic criterion for iteration 151 is -310.215063588232
## The bic criterion for iteration 152 is -310.215063588232
## The bic criterion for iteration 153 is -310.215063588232
## The bic criterion for iteration 154 is -310.215063588232
## The bic criterion for iteration 155 is -310.215063588232
## The bic criterion for iteration 156 is -310.215063588232
## The bic criterion for iteration 157 is -310.215063588232
## The bic criterion for iteration 158 is -310.215063588232
## The bic criterion for iteration 159 is -309.583492156519
## The bic criterion for iteration 160 is -310.215063588232
## The bic criterion for iteration 161 is -310.215063588232
## The bic criterion for iteration 162 is -310.215063588232
## The bic criterion for iteration 163 is -310.215063588232
## The bic criterion for iteration 164 is -310.215063588232
## The bic criterion for iteration 165 is -310.215063588232
## The bic criterion for iteration 166 is -310.215063588232
## The bic criterion for iteration 167 is -310.215063588232
## The bic criterion for iteration 168 is -310.215063588232
## The bic criterion for iteration 169 is -310.215063588232
## The bic criterion for iteration 170 is -310.215063588232
## The bic criterion for iteration 171 is -310.215063588232
## The bic criterion for iteration 172 is -310.215063588232
## The bic criterion for iteration 173 is -310.215063588232
## The bic criterion for iteration 174 is -310.215063588232
## The bic criterion for iteration 175 is -310.215063588232
## The bic criterion for iteration 176 is -310.215063588232
## The bic criterion for iteration 177 is -310.215063588232
## The bic criterion for iteration 178 is -310.215063588232
## The bic criterion for iteration 179 is -310.215063588232
## The bic criterion for iteration 180 is -310.215063588232
## The bic criterion for iteration 181 is -310.215063588232
## The bic criterion for iteration 182 is -310.215063588232
## The bic criterion for iteration 183 is -310.215063588232
## The bic criterion for iteration 184 is -310.215063588232
## The bic criterion for iteration 185 is -310.215063588232
## The bic criterion for iteration 186 is -310.215063588232
## The bic criterion for iteration 187 is -310.215063588232
## The bic criterion for iteration 188 is -310.215063588232
## The bic criterion for iteration 189 is -310.215063588232
## The bic criterion for iteration 190 is -310.215063588232
## The bic criterion for iteration 191 is -310.215063588232
## The bic criterion for iteration 192 is -310.215063588232
## The bic criterion for iteration 193 is -310.215063588232
## The bic criterion for iteration 194 is -310.215063588232
## The bic criterion for iteration 195 is -310.215063588232
## The bic criterion for iteration 196 is -310.215063588232
## The bic criterion for iteration 197 is -310.215063588232
## The bic criterion for iteration 198 is -310.215063588232
## The bic criterion for iteration 199 is -310.215063588232
## The bic criterion for iteration 200 is -310.215063588232
plot(unlist(tree@performance$criterionEvolution), type = "l")

data$c_map <- factor(apply(predict(tree@best.tree$tree, data, type = "prob"), 1, function(p) names(which.max(p))))
test$c_map <- factor(apply(predict(tree@best.tree$tree, test, type = "prob"), 1, function(p) names(which.max(p))))

table(data$c, data$c_map)
##    
##      8
##   1 66
##   2 64
##   3 70
plot(data[, 1], data[, 2], pch = 2 + data[, 3], col = as.numeric(data$c_map), xlab = "First coordinate", ylab = "Second coordinate")

plot(tree@best.tree$tree)

pred <- matrix(0, nrow = 0.2 * 1000, ncol = 1)

for (j in 1:nlevels(data$c_map)) {
  pred[test$c_map == levels(data$c_map)[j]] <- predict(tree@best.tree$glms[[j]], test[test$c_map == levels(data$c_map)[j], ], type = "response")
}

normalizedGini(test$y, pred)
## [1] 0.275709