Predict the response for newdata of a TreeSurrogate object.
This function makes the TreeSurrogate object call
its internal $predict()
method.
# S3 method for class 'TreeSurrogate'
predict(object, newdata, type = "prob", ...)
The surrogate tree. A TreeSurrogate object.
A data.frame for which to predict.
Either "prob" or "class". Ignored if the surrogate tree does regression.
Further arguments for predict_party
.
A data.frame with the predicted outcome. In case of regression it is the predicted \(\hat{y}\). In case of classification it is either the class probabilities (for type "prob") or the class label (type "class")
library("randomForest")
# Fit a Random Forest on the Boston housing data set
data("Boston", package = "MASS")
rf <- randomForest(medv ~ ., data = Boston, ntree = 50)
# Create a model object
mod <- Predictor$new(rf, data = Boston[-which(names(Boston) == "medv")])
# Fit a decision tree as a surrogate for the whole random forest
dt <- TreeSurrogate$new(mod)
# Plot the resulting leaf nodes
predict(dt, newdata = Boston)
#> Warning: Dropping additional columns: medv
#> .y.hat
#> 1 25.41250
#> 2 25.41250
#> 3 37.86823
#> 4 37.86823
#> 5 37.86823
#> 6 25.41250
#> 7 20.76974
#> 8 14.79730
#> 9 14.79730
#> 10 14.79730
#> 11 14.79730
#> 12 20.76974
#> 13 14.79730
#> 14 25.41250
#> 15 20.76974
#> 16 25.41250
#> 17 25.41250
#> 18 20.76974
#> 19 20.76974
#> 20 20.76974
#> 21 14.79730
#> 22 20.76974
#> 23 14.79730
#> 24 14.79730
#> 25 14.79730
#> 26 14.79730
#> 27 20.76974
#> 28 14.79730
#> 29 20.76974
#> 30 20.76974
#> 31 14.79730
#> 32 20.76974
#> 33 14.79730
#> 34 14.79730
#> 35 14.79730
#> 36 25.41250
#> 37 20.76974
#> 38 25.41250
#> 39 20.76974
#> 40 25.41250
#> 41 37.86823
#> 42 25.41250
#> 43 25.41250
#> 44 25.41250
#> 45 25.41250
#> 46 20.76974
#> 47 20.76974
#> 48 14.79730
#> 49 14.79730
#> 50 14.79730
#> 51 20.76974
#> 52 25.41250
#> 53 25.41250
#> 54 25.41250
#> 55 20.76974
#> 56 37.86823
#> 57 25.41250
#> 58 25.41250
#> 59 25.41250
#> 60 25.41250
#> 61 20.76974
#> 62 20.76974
#> 63 25.41250
#> 64 25.41250
#> 65 37.86823
#> 66 25.41250
#> 67 20.76974
#> 68 25.41250
#> 69 20.76974
#> 70 25.41250
#> 71 25.41250
#> 72 20.76974
#> 73 25.41250
#> 74 25.41250
#> 75 25.41250
#> 76 25.41250
#> 77 20.76974
#> 78 20.76974
#> 79 20.76974
#> 80 25.41250
#> 81 25.41250
#> 82 25.41250
#> 83 25.41250
#> 84 25.41250
#> 85 25.41250
#> 86 25.41250
#> 87 20.76974
#> 88 25.41250
#> 89 37.86823
#> 90 37.86823
#> 91 25.41250
#> 92 25.41250
#> 93 25.41250
#> 94 25.41250
#> 95 20.76974
#> 96 25.41250
#> 97 20.76974
#> 98 37.86823
#> 99 37.86823
#> 100 37.86823
#> 101 25.41250
#> 102 25.41250
#> 103 20.76974
#> 104 20.76974
#> 105 20.76974
#> 106 14.79730
#> 107 14.79730
#> 108 20.76974
#> 109 20.76974
#> 110 14.79730
#> 111 20.76974
#> 112 20.76974
#> 113 14.79730
#> 114 14.79730
#> 115 20.76974
#> 116 14.79730
#> 117 20.76974
#> 118 20.76974
#> 119 14.79730
#> 120 20.76974
#> 121 20.76974
#> 122 20.76974
#> 123 14.79730
#> 124 14.79730
#> 125 14.79730
#> 126 20.76974
#> 127 14.79730
#> 128 14.79730
#> 129 14.79730
#> 130 14.79730
#> 131 20.76974
#> 132 20.76974
#> 133 20.76974
#> 134 14.79730
#> 135 14.79730
#> 136 14.79730
#> 137 14.79730
#> 138 20.76974
#> 139 14.79730
#> 140 14.79730
#> 141 14.79730
#> 142 14.79730
#> 143 14.79730
#> 144 14.79730
#> 145 14.79730
#> 146 14.79730
#> 147 14.79730
#> 148 14.79730
#> 149 14.79730
#> 150 14.79730
#> 151 20.76974
#> 152 20.76974
#> 153 20.76974
#> 154 14.79730
#> 155 14.79730
#> 156 14.79730
#> 157 14.79730
#> 158 37.86823
#> 159 25.41250
#> 160 25.41250
#> 161 25.41250
#> 162 37.86823
#> 163 37.86823
#> 164 37.86823
#> 165 20.76974
#> 166 20.76974
#> 167 37.86823
#> 168 20.76974
#> 169 20.76974
#> 170 20.76974
#> 171 20.76974
#> 172 20.76974
#> 173 20.76974
#> 174 25.41250
#> 175 25.41250
#> 176 25.41250
#> 177 20.76974
#> 178 25.41250
#> 179 25.41250
#> 180 37.86823
#> 181 37.86823
#> 182 25.41250
#> 183 37.86823
#> 184 25.41250
#> 185 20.76974
#> 186 20.76974
#> 187 37.86823
#> 188 25.41250
#> 189 25.41250
#> 190 37.86823
#> 191 37.86823
#> 192 25.41250
#> 193 37.86823
#> 194 25.41250
#> 195 25.41250
#> 196 37.86823
#> 197 37.86823
#> 198 37.86823
#> 199 37.86823
#> 200 37.86823
#> 201 37.86823
#> 202 25.41250
#> 203 37.86823
#> 204 37.86823
#> 205 37.86823
#> 206 20.76974
#> 207 20.76974
#> 208 14.79730
#> 209 20.76974
#> 210 14.79730
#> 211 14.79730
#> 212 14.79730
#> 213 14.79730
#> 214 25.41250
#> 215 14.79730
#> 216 25.41250
#> 217 20.76974
#> 218 25.41250
#> 219 14.79730
#> 220 20.76974
#> 221 37.86823
#> 222 14.79730
#> 223 20.76974
#> 224 25.41250
#> 225 37.86823
#> 226 37.86823
#> 227 37.86823
#> 228 37.86823
#> 229 37.86823
#> 230 25.41250
#> 231 20.76974
#> 232 37.86823
#> 233 37.86823
#> 234 37.86823
#> 235 25.41250
#> 236 20.76974
#> 237 25.41250
#> 238 37.86823
#> 239 25.41250
#> 240 25.41250
#> 241 20.76974
#> 242 20.76974
#> 243 20.76974
#> 244 25.41250
#> 245 20.76974
#> 246 14.79730
#> 247 25.41250
#> 248 20.76974
#> 249 25.41250
#> 250 25.41250
#> 251 25.41250
#> 252 25.41250
#> 253 37.86823
#> 254 37.86823
#> 255 25.41250
#> 256 25.41250
#> 257 37.86823
#> 258 37.86823
#> 259 37.86823
#> 260 25.41250
#> 261 37.86823
#> 262 37.86823
#> 263 37.86823
#> 264 20.76974
#> 265 37.86823
#> 266 20.76974
#> 267 20.76974
#> 268 37.86823
#> 269 37.86823
#> 270 20.76974
#> 271 20.76974
#> 272 25.41250
#> 273 25.41250
#> 274 37.86823
#> 275 25.41250
#> 276 25.41250
#> 277 37.86823
#> 278 25.41250
#> 279 25.41250
#> 280 25.41250
#> 281 37.86823
#> 282 37.86823
#> 283 37.86823
#> 284 37.86823
#> 285 37.86823
#> 286 25.41250
#> 287 20.76974
#> 288 25.41250
#> 289 25.41250
#> 290 25.41250
#> 291 25.41250
#> 292 37.86823
#> 293 25.41250
#> 294 25.41250
#> 295 20.76974
#> 296 25.41250
#> 297 25.41250
#> 298 14.79730
#> 299 25.41250
#> 300 37.86823
#> 301 25.41250
#> 302 25.41250
#> 303 25.41250
#> 304 37.86823
#> 305 37.86823
#> 306 25.41250
#> 307 37.86823
#> 308 25.41250
#> 309 25.41250
#> 310 20.76974
#> 311 20.76974
#> 312 25.41250
#> 313 20.76974
#> 314 25.41250
#> 315 25.41250
#> 316 20.76974
#> 317 14.79730
#> 318 14.79730
#> 319 20.76974
#> 320 20.76974
#> 321 25.41250
#> 322 25.41250
#> 323 25.41250
#> 324 20.76974
#> 325 25.41250
#> 326 25.41250
#> 327 25.41250
#> 328 20.76974
#> 329 20.76974
#> 330 25.41250
#> 331 25.41250
#> 332 20.76974
#> 333 25.41250
#> 334 25.41250
#> 335 25.41250
#> 336 25.41250
#> 337 20.76974
#> 338 20.76974
#> 339 25.41250
#> 340 20.76974
#> 341 25.41250
#> 342 37.86823
#> 343 25.41250
#> 344 25.41250
#> 345 25.41250
#> 346 20.76974
#> 347 20.76974
#> 348 25.41250
#> 349 25.41250
#> 350 25.41250
#> 351 25.41250
#> 352 25.41250
#> 353 25.41250
#> 354 25.41250
#> 355 25.41250
#> 356 25.41250
#> 357 14.79730
#> 358 20.76974
#> 359 20.76974
#> 360 20.76974
#> 361 25.41250
#> 362 20.76974
#> 363 20.76974
#> 364 20.76974
#> 365 37.86823
#> 366 25.41250
#> 367 20.76974
#> 368 20.76974
#> 369 25.41250
#> 370 25.41250
#> 371 37.86823
#> 372 25.41250
#> 373 25.41250
#> 374 14.79730
#> 375 14.79730
#> 376 20.76974
#> 377 14.79730
#> 378 14.79730
#> 379 14.79730
#> 380 14.79730
#> 381 14.79730
#> 382 14.79730
#> 383 14.79730
#> 384 14.79730
#> 385 14.79730
#> 386 14.79730
#> 387 14.79730
#> 388 14.79730
#> 389 14.79730
#> 390 14.79730
#> 391 14.79730
#> 392 14.79730
#> 393 14.79730
#> 394 14.79730
#> 395 14.79730
#> 396 14.79730
#> 397 14.79730
#> 398 14.79730
#> 399 14.79730
#> 400 14.79730
#> 401 14.79730
#> 402 14.79730
#> 403 14.79730
#> 404 14.79730
#> 405 14.79730
#> 406 14.79730
#> 407 14.79730
#> 408 20.76974
#> 409 14.79730
#> 410 14.79730
#> 411 20.76974
#> 412 14.79730
#> 413 14.79730
#> 414 14.79730
#> 415 14.79730
#> 416 14.79730
#> 417 14.79730
#> 418 14.79730
#> 419 14.79730
#> 420 14.79730
#> 421 14.79730
#> 422 14.79730
#> 423 20.76974
#> 424 14.79730
#> 425 14.79730
#> 426 14.79730
#> 427 14.79730
#> 428 20.76974
#> 429 14.79730
#> 430 14.79730
#> 431 14.79730
#> 432 14.79730
#> 433 20.76974
#> 434 14.79730
#> 435 14.79730
#> 436 14.79730
#> 437 14.79730
#> 438 14.79730
#> 439 14.79730
#> 440 14.79730
#> 441 14.79730
#> 442 14.79730
#> 443 14.79730
#> 444 14.79730
#> 445 14.79730
#> 446 14.79730
#> 447 14.79730
#> 448 14.79730
#> 449 14.79730
#> 450 14.79730
#> 451 14.79730
#> 452 14.79730
#> 453 14.79730
#> 454 14.79730
#> 455 14.79730
#> 456 14.79730
#> 457 14.79730
#> 458 14.79730
#> 459 14.79730
#> 460 20.76974
#> 461 14.79730
#> 462 20.76974
#> 463 20.76974
#> 464 20.76974
#> 465 20.76974
#> 466 20.76974
#> 467 14.79730
#> 468 14.79730
#> 469 14.79730
#> 470 20.76974
#> 471 14.79730
#> 472 20.76974
#> 473 20.76974
#> 474 20.76974
#> 475 14.79730
#> 476 14.79730
#> 477 14.79730
#> 478 14.79730
#> 479 14.79730
#> 480 20.76974
#> 481 20.76974
#> 482 25.41250
#> 483 37.86823
#> 484 20.76974
#> 485 20.76974
#> 486 20.76974
#> 487 20.76974
#> 488 20.76974
#> 489 14.79730
#> 490 14.79730
#> 491 14.79730
#> 492 14.79730
#> 493 20.76974
#> 494 20.76974
#> 495 20.76974
#> 496 14.79730
#> 497 14.79730
#> 498 20.76974
#> 499 20.76974
#> 500 14.79730
#> 501 20.76974
#> 502 25.41250
#> 503 25.41250
#> 504 37.86823
#> 505 25.41250
#> 506 25.41250