R Under development (unstable) (2024-08-16 r87026 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > suppressWarnings(RNGversion("3.5.2")) > > ## load package and fix seed > library("partykit") Loading required package: grid Loading required package: libcoin Loading required package: mvtnorm > set.seed(1) > > ## rpart: kyphosis data > library("rpart") > data("kyphosis", package = "rpart") > fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) > pfit <- as.party(fit) > all(predict(pfit, newdata = kyphosis, type = "node") == fit$where) [1] TRUE > > ## J48: iris data > if (require("RWeka")) { + data("iris", package = "datasets") + itree <- J48(Species ~ ., data = iris) + pitree <- as.party(itree) + stopifnot(all(predict(pitree) == predict(pitree, newdata = iris[, 3:4]))) + + print(all.equal(predict(itree, type = "prob", newdata = iris), + predict(pitree, type = "prob", newdata = iris))) + print(all.equal(predict(itree, newdata = iris), + predict(pitree, newdata = iris))) + + ## rpart/J48: GlaucomaM data + data("GlaucomaM", package = "TH.data") + w <- runif(nrow(GlaucomaM)) + fit <- rpart(Class ~ ., data = GlaucomaM, weights = w) + pfit <- as.party(fit) + print(all(predict(pfit, type = "node") == fit$where)) + tmp <- GlaucomaM[sample(1:nrow(GlaucomaM), 100),] + print(all.equal(predict(fit, type = "prob", newdata = tmp), predict(pfit, type = "prob", newdata = tmp))) + print(all.equal(predict(fit, type = "class", newdata = tmp), predict(pfit, newdata = tmp))) + itree <- J48(Class ~ ., data = GlaucomaM) + pitree <- as.party(itree) + print(all.equal(predict(itree, newdata = tmp, type = "prob"), predict(pitree, newdata = tmp, type = "prob"))) + } Loading required package: RWeka [1] TRUE [1] "names for current but not for target" [1] TRUE [1] TRUE [1] TRUE [1] TRUE > > ## rpart: airquality data > data("airquality") > aq <- subset(airquality, !is.na(Ozone)) > w <- runif(nrow(aq), max = 3) > aqr <- rpart(Ozone ~ ., data = aq, weights = w) > aqp <- as.party(aqr) > tmp <- subset(airquality, is.na(Ozone)) > all.equal(predict(aqr, newdata = tmp), predict(aqp, newdata = tmp)) [1] TRUE > > ## rpart: GBSG2 data > data("GBSG2", package = "TH.data") > library("survival") > fit <- rpart(Surv(time, cens) ~ ., data = GBSG2) > pfit <- as.party(fit) > pfit$fitted (fitted) (response) 1 6 1814 2 8 2018 3 8 712 4 8 1807 5 6 772 6 11 448 7 3 2172+ 8 5 2161+ 9 11 471 10 10 2014+ 11 10 577 12 10 184 13 5 1840+ 14 6 1842+ 15 5 1821+ 16 5 1371 17 8 707 18 5 1743+ 19 5 1781+ 20 8 865 21 6 1684 22 5 1701+ 23 6 1701+ 24 6 1693+ 25 11 379 26 5 1105 27 6 548 28 8 1296 29 6 1483+ 30 6 1570+ 31 6 1469+ 32 6 1472+ 33 5 1342+ 34 6 1349+ 35 8 1162 36 6 1342+ 37 11 797 38 8 1232+ 39 6 1230+ 40 8 1205+ 41 8 1090+ 42 8 1095+ 43 6 449 44 6 972+ 45 6 825+ 46 6 2438+ 47 6 2233+ 48 11 286 49 8 1861+ 50 8 1080 51 6 1521 52 6 1693+ 53 6 1528 54 10 169 55 8 272 56 6 731 57 8 2059+ 58 8 1853+ 59 10 1854+ 60 8 1645+ 61 11 544 62 8 1666+ 63 6 353 64 8 1791+ 65 6 1685+ 66 3 191 67 10 370 68 8 173 69 11 242 70 6 420 71 8 438 72 6 1624+ 73 10 1036 74 11 359 75 8 171 76 11 959 77 6 1351+ 78 10 486 79 11 525 80 5 762 81 10 175 82 3 1195+ 83 10 338 84 8 1125+ 85 8 916+ 86 6 972+ 87 3 867+ 88 10 249 89 11 281 90 6 758+ 91 11 377 92 6 1976+ 93 5 2539+ 94 6 2467+ 95 5 876 96 5 2132+ 97 11 426 98 5 554 99 8 1246 100 5 1926+ 101 11 1207 102 8 1852+ 103 8 1174 104 8 1250+ 105 6 530 106 10 1502+ 107 5 1364+ 108 8 1170 109 6 1729+ 110 5 1642+ 111 5 1218 112 3 1358+ 113 11 360 114 5 550 115 8 857+ 116 8 768+ 117 6 858+ 118 6 770+ 119 10 679 120 6 1164 121 6 350 122 6 578 123 6 1460 124 6 1434+ 125 6 1763 126 6 889 127 11 357 128 8 547 129 5 1722+ 130 6 2372+ 131 6 2030 132 5 1002 133 3 1280 134 8 338 135 6 533 136 6 168+ 137 5 1169+ 138 5 1675 139 6 1862+ 140 5 629+ 141 10 1167+ 142 6 495 143 5 967+ 144 5 1720+ 145 6 598 146 11 392 147 3 1502+ 148 6 229+ 149 8 310+ 150 5 1296+ 151 10 488+ 152 3 942+ 153 8 570+ 154 10 1177+ 155 8 1113+ 156 8 288 157 6 723+ 158 11 403 159 10 1225 160 8 338 161 8 1337 162 6 1420 163 8 2048+ 164 8 600 165 5 1765+ 166 8 491 167 10 305 168 6 1582+ 169 8 1771+ 170 8 960 171 10 571 172 8 675+ 173 5 285 174 8 1472+ 175 5 1279 176 6 148+ 177 6 1863+ 178 5 1933+ 179 10 358 180 5 734+ 181 6 2372 182 6 2563+ 183 5 2372+ 184 6 1989 185 8 2015 186 6 1956+ 187 8 945 188 6 2153+ 189 6 838 190 8 113 191 6 1833+ 192 8 1722+ 193 8 241 194 5 1352 195 8 1702+ 196 8 1222+ 197 6 1089+ 198 3 1243+ 199 8 579 200 8 1043 201 6 2234+ 202 5 2297+ 203 6 2014+ 204 6 518 205 6 940+ 206 6 766+ 207 11 251 208 6 1959+ 209 3 1897+ 210 11 160 211 5 970+ 212 5 892+ 213 5 753+ 214 8 348 215 10 275 216 5 1329 217 6 1193 218 10 698 219 10 436 220 6 552 221 6 564 222 6 2239+ 223 5 2237+ 224 6 529 225 3 1820+ 226 6 1756+ 227 11 515 228 11 272 229 6 891 230 6 1356+ 231 10 1352+ 232 6 1077+ 233 6 675 234 10 855+ 235 6 740+ 236 6 2551+ 237 8 754 238 8 819 239 10 1280 240 5 2388+ 241 5 2296+ 242 6 1884+ 243 6 1059 244 8 859+ 245 6 1109+ 246 6 1192 247 8 1806 248 11 500 249 10 1589 250 10 1463 251 6 1826+ 252 8 1231+ 253 5 1117+ 254 8 836 255 6 1222+ 256 8 663+ 257 10 722 258 5 322+ 259 6 1150 260 10 446 261 10 1855+ 262 10 238 263 6 1838+ 264 11 1826+ 265 6 1093 266 3 2051+ 267 10 370 268 5 861 269 10 1587 270 6 552 271 3 2353+ 272 8 2471+ 273 8 893 274 5 2093 275 10 2612+ 276 8 956 277 10 1637+ 278 5 2456+ 279 8 2227+ 280 6 1601 281 6 1841+ 282 8 2177+ 283 6 2052+ 284 11 973+ 285 3 2156+ 286 8 1499+ 287 3 2030+ 288 8 573 289 5 1666+ 290 8 1979+ 291 8 1786+ 292 8 1847+ 293 5 2009+ 294 5 1926+ 295 3 1490+ 296 11 233 297 8 1240+ 298 3 1751+ 299 8 1878+ 300 5 1171+ 301 10 1751+ 302 6 1756+ 303 8 714 304 8 1505+ 305 8 776 306 6 1443+ 307 3 1317+ 308 6 870+ 309 11 859 310 6 223 311 6 1212+ 312 5 1119+ 313 8 740+ 314 8 1062+ 315 5 8+ 316 6 936+ 317 8 740+ 318 10 632 319 5 1760+ 320 6 1013+ 321 10 779+ 322 10 375 323 8 1323+ 324 5 1233+ 325 5 986+ 326 10 650+ 327 10 628+ 328 6 1866+ 329 8 491 330 6 1918 331 10 72 332 10 1140 333 8 799 334 6 1105 335 5 548 336 11 227 337 6 1838+ 338 3 1833+ 339 6 550 340 6 426 341 8 1834+ 342 6 1604+ 343 10 772+ 344 8 1146 345 6 371 346 8 883 347 6 1735+ 348 8 554 349 11 790 350 5 1340+ 351 11 490 352 3 1557+ 353 6 594 354 10 828+ 355 8 594 356 8 841+ 357 5 695+ 358 5 2556+ 359 6 1753 360 10 417 361 6 956 362 8 1846+ 363 8 1703+ 364 6 1720+ 365 6 1355+ 366 6 1603+ 367 8 476 368 8 1350+ 369 5 1341+ 370 3 2449+ 371 8 2286 372 6 456 373 6 536 374 5 612 375 6 2034 376 6 1990 377 10 2456 378 3 2205+ 379 6 544 380 10 336 381 6 2057+ 382 8 575 383 5 2011+ 384 8 537 385 3 2217+ 386 8 1814 387 6 890 388 5 1114+ 389 10 974+ 390 6 296+ 391 8 2320+ 392 8 795 393 8 867 394 5 1703+ 395 6 670 396 8 981 397 5 1094+ 398 5 755 399 10 1388 400 6 1387 401 10 535 402 6 1653+ 403 6 1904+ 404 8 1868+ 405 3 1767+ 406 6 855 407 6 1157 408 8 1869+ 409 8 1152+ 410 6 1401+ 411 6 918+ 412 10 745 413 8 1283+ 414 6 1481 415 8 1807+ 416 6 542 417 10 1441+ 418 8 1277+ 419 8 1486+ 420 5 273+ 421 10 177 422 6 545 423 6 1185+ 424 11 631+ 425 6 995+ 426 8 1088+ 427 6 877+ 428 8 798+ 429 8 2380+ 430 5 1679 431 10 498 432 8 2138+ 433 8 2175+ 434 5 2271+ 435 6 17+ 436 6 964 437 10 540 438 11 747 439 11 650 440 11 410 441 11 624 442 10 1560+ 443 11 455 444 5 1629+ 445 8 1730+ 446 6 1483+ 447 6 687 448 6 308 449 10 563 450 5 46+ 451 6 2144+ 452 10 344 453 6 945+ 454 6 1905+ 455 11 855 456 5 2370+ 457 6 853+ 458 8 692+ 459 8 475 460 5 2195+ 461 8 758+ 462 8 648 463 3 761+ 464 8 596+ 465 11 195 466 10 473 467 5 747+ 468 5 2659+ 469 11 1977 470 6 2401+ 471 5 1499+ 472 11 1856+ 473 11 595 474 5 2148+ 475 6 2126+ 476 8 1975 477 6 1641 478 8 1717+ 479 5 1858+ 480 5 2049+ 481 8 1502 482 3 1922+ 483 5 1818+ 484 11 1100+ 485 6 1499+ 486 6 359 487 8 1645+ 488 5 1356+ 489 6 1632+ 490 6 967+ 491 6 1091+ 492 6 918 493 6 557 494 5 1219 495 6 2170+ 496 6 729 497 10 1449 498 5 991 499 6 481 500 6 1655+ 501 6 857 502 6 369 503 5 1627+ 504 5 1578+ 505 8 732 506 8 460 507 6 1208+ 508 8 730 509 8 722+ 510 6 717+ 511 8 651+ 512 5 637+ 513 6 615+ 514 10 42+ 515 11 307 516 8 983 517 11 120 518 6 1525 519 8 1680+ 520 11 1730 521 8 805 522 8 2388+ 523 6 559 524 10 1977+ 525 6 476 526 5 1514+ 527 5 1617+ 528 6 1094 529 5 784 530 10 181 531 10 415 532 8 1120 533 10 316 534 8 637 535 6 247 536 8 888+ 537 10 622 538 6 806+ 539 6 1163+ 540 5 1721+ 541 5 741+ 542 6 372 543 6 1331+ 544 8 394 545 3 652+ 546 10 657+ 547 6 567+ 548 10 429+ 549 5 566+ 550 11 15+ 551 8 98 552 5 368+ 553 5 432+ 554 5 319+ 555 10 65+ 556 8 16+ 557 10 29+ 558 8 18+ 559 8 17+ 560 10 308 561 6 1965+ 562 11 548 563 10 293 564 8 2017+ 565 10 1093+ 566 3 792+ 567 6 586 568 6 1434+ 569 8 67+ 570 8 623+ 571 6 2128+ 572 6 1965+ 573 6 2161+ 574 10 1183 575 6 1108 576 5 2065+ 577 6 1598+ 578 6 491 579 10 1366 580 6 424+ 581 11 859 582 11 180 583 5 1625+ 584 3 1938+ 585 8 1343 586 6 646 587 6 2192+ 588 6 502 589 10 1675+ 590 11 1363 591 11 420 592 8 982 593 6 1459+ 594 6 1192+ 595 6 1264+ 596 8 1095+ 597 8 1078+ 598 3 737+ 599 8 461+ 600 11 465 601 11 842 602 6 918+ 603 8 374 604 6 1089+ 605 5 1527+ 606 8 285 607 6 1306 608 10 797 609 5 1441+ 610 6 343 611 8 936+ 612 5 195+ 613 6 503 614 11 827 615 5 1427+ 616 6 854+ 617 8 177 618 10 281 619 5 205 620 8 751+ 621 8 629 622 6 526+ 623 6 463+ 624 5 529+ 625 8 623+ 626 11 546+ 627 5 213+ 628 6 276+ 629 8 2010+ 630 8 2009+ 631 3 1984+ 632 10 1981+ 633 10 624 634 10 742 635 3 1818+ 636 8 1493 637 5 1432+ 638 5 801 639 6 1182+ 640 6 71+ 641 10 114+ 642 6 63+ 643 6 1722+ 644 5 1692+ 645 6 177+ 646 5 57+ 647 5 1152+ 648 6 733+ 649 6 1459 650 5 2237+ 651 6 933+ 652 5 2056+ 653 10 1729+ 654 10 2024+ 655 5 2039 656 6 2027+ 657 6 2007+ 658 6 1253 659 6 1789+ 660 8 1707+ 661 6 1714+ 662 5 1717+ 663 6 329 664 5 1624+ 665 6 1600+ 666 5 385 667 3 1475+ 668 5 1435+ 669 11 541+ 670 5 1329+ 671 8 1357+ 672 6 1343+ 673 5 748 674 5 1090 675 6 1219+ 676 5 553+ 677 8 662 678 5 969+ 679 8 974+ 680 8 866 681 10 504 682 6 721+ 683 11 186+ 684 8 769 685 6 727 686 8 1701 > predict(pfit, newdata = GBSG2[1:100,], type = "prob") $`1` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`2` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`3` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`4` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`5` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`6` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`7` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 33 2 NA NA NA $`8` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`9` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`10` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`11` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`12` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`13` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`14` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`15` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`16` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`17` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`18` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`19` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`20` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`21` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`22` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`23` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`24` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`25` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`26` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`27` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`28` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`29` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`30` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`31` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`32` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`33` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`34` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`35` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`36` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`37` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`38` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`39` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`40` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`41` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`42` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`43` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`44` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`45` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`46` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`47` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`48` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`49` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`50` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`51` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`52` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`53` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`54` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`55` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`56` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`57` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`58` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`59` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`60` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`61` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`62` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`63` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`64` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`65` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`66` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 33 2 NA NA NA $`67` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`68` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`69` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`70` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`71` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`72` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`73` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`74` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`75` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`76` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`77` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`78` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`79` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`80` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`81` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`82` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 33 2 NA NA NA $`83` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`84` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`85` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`86` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`87` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 33 2 NA NA NA $`88` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 87 55 742 577 1366 $`89` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`90` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`91` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`92` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`93` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`94` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 221 89 1989 1641 NA $`95` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`96` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`97` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 57 48 500 426 747 $`98` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA $`99` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 166 77 1701 1174 2018 $`100` Call: survfit(formula = y ~ 1, weights = w, subset = w > 0) n events median 0.95LCL 0.95UCL [1,] 122 28 NA NA NA > predict(pfit, newdata = GBSG2[1:100,], type = "response") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1989 1701 1701 1701 1989 500 Inf Inf 500 742 742 742 Inf 1989 Inf Inf 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 1701 Inf Inf 1701 1989 Inf 1989 1989 500 Inf 1989 1701 1989 1989 1989 1989 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 Inf 1989 1701 1989 500 1701 1989 1701 1701 1701 1989 1989 1989 1989 1989 500 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 1701 1701 1989 1989 1989 742 1701 1989 1701 1701 742 1701 500 1701 1989 1701 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 1989 Inf 742 1701 500 1989 1701 1989 742 500 1701 500 1989 742 500 Inf 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 742 Inf 742 1701 1701 1989 Inf 742 500 1989 500 1989 Inf 1989 Inf Inf 97 98 99 100 500 Inf 1701 Inf > > ### multiple responses > f <- fitted(pfit) > f[["(response)"]] <- data.frame(srv = f[["(response)"]], hansi = runif(nrow(f))) > mp <- party(node_party(pfit), fitted = f, data = pfit$data) > class(mp) <- c("constparty", "party") > predict(mp, newdata = GBSG2[1:10,]) srv hansi 1 1989 0.5017739 2 1701 0.4933225 3 1701 0.4933225 4 1701 0.4933225 5 1989 0.5017739 6 500 0.4823559 7 Inf 0.4662471 8 Inf 0.4839262 9 500 0.4823559 10 742 0.5108953 > > ### pruning > ## create party > data("WeatherPlay", package = "partykit") > py <- party( + partynode(1L, + split = partysplit(1L, index = 1:3), + kids = list( + partynode(2L, + split = partysplit(3L, breaks = 75), + kids = list( + partynode(3L, info = "yes"), + partynode(4L, info = "no"))), + partynode(5L, + split = partysplit(3L, breaks = 20), + kids = list( + partynode(6L, info = "no"), + partynode(7L, info = "yes"))), + partynode(8L, + split = partysplit(4L, index = 1:2), + kids = list( + partynode(9L, info = "yes"), + partynode(10L, info = "no"))))), + WeatherPlay) > names(py) <- LETTERS[nodeids(py)] > > ## print > print(py) [A] root | [B] outlook in sunny | | [C] humidity <= 75: yes | | [D] humidity > 75: no | [E] outlook in overcast | | [F] humidity <= 20: no | | [G] humidity > 20: yes | [H] outlook in rainy | | [I] windy in false: yes | | [J] windy in true: no > (py5 <- nodeprune(py, 5)) [A] root | [B] outlook in sunny | | [C] humidity <= 75: yes | | [D] humidity > 75: no | [E] outlook in overcast: * | [H] outlook in rainy | | [I] windy in false: yes | | [J] windy in true: no > nodeids(py5) [1] 1 2 3 4 5 6 7 8 > (pyH <- nodeprune(py5, "H")) [A] root | [B] outlook in sunny | | [C] humidity <= 75: yes | | [D] humidity > 75: no | [E] outlook in overcast: * | [H] outlook in rainy: * > nodeids(pyH) [1] 1 2 3 4 5 6 > > ct <- ctree(Species ~ ., data = iris) > nt <- node_party(ctree(Species ~ ., data = iris)) > (ctp <- nodeprune(ct, 4)) ### party method Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [1] root | [2] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%) | [3] Petal.Length > 1.9 | | [4] Petal.Width <= 1.7: versicolor (n = 54, err = 9.3%) | | [5] Petal.Width > 1.7: virginica (n = 46, err = 2.2%) Number of inner nodes: 2 Number of terminal nodes: 3 > (ntp <- nodeprune(nt, 4)) ### partynode method [1] root | [2] V4 <= 1.9 * | [3] V4 > 1.9 | | [4] V5 <= 1.7 * | | [5] V5 > 1.7 * > > ### check if both methods do the same > p1 <- predict(party(ntp, data = model.frame(ct)), type = "node") > p2 <- predict(ctp, type = "node") > stopifnot(max(abs(p1 - p2)) == 0) > > names(ct) <- LETTERS[nodeids(ct)] > (ctp <- nodeprune(ct, "D")) Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [A] root | [B] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%) | [C] Petal.Length > 1.9 | | [D] Petal.Width <= 1.7: versicolor (n = 54, err = 9.3%) | | [G] Petal.Width > 1.7: virginica (n = 46, err = 2.2%) Number of inner nodes: 2 Number of terminal nodes: 3 > > table(predict(ct, type = "node"), + predict(ctp, type = "node")) 2 4 5 2 50 0 0 5 0 46 0 6 0 8 0 7 0 0 46 > > (ct <- nodeprune(ct, names(ct)[names(ct) != "A"])) Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [A] root | [B] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%) | [C] Petal.Length > 1.9: versicolor (n = 100, err = 50.0%) Number of inner nodes: 1 Number of terminal nodes: 2 > table(predict(ct, type = "node")) 2 3 50 100 > > nodeprune(ct, "B") Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [A] root | [B] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%) | [C] Petal.Length > 1.9: versicolor (n = 100, err = 50.0%) Number of inner nodes: 1 Number of terminal nodes: 2 > nodeprune(ct, "C") Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [A] root | [B] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%) | [C] Petal.Length > 1.9: versicolor (n = 100, err = 50.0%) Number of inner nodes: 1 Number of terminal nodes: 2 > nodeprune(ct, "A") Model formula: Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width Fitted party: [A] root: setosa (n = 150, err = 66.7%) Number of inner nodes: 0 Number of terminal nodes: 1 > > options(digits = 3) > ### check different predict flavours for numeric responses > x <- runif(100) > dd <- data.frame(y = rnorm(length(x), mean = 2 * (x < .5)), x = x) > ct <- ctree(y ~ x, data = dd) > nd <- data.frame(x = (1:9) / 10) > predict(ct, newdata = nd, type = "node") 1 2 3 4 5 6 7 8 9 2 2 2 2 2 4 4 4 5 > predict(ct, newdata = nd, type = "response") 1 2 3 4 5 6 7 8 9 2.112 2.112 2.112 2.112 2.112 0.378 0.378 0.378 -0.511 > predict(ct, newdata = nd, type = "prob") $`1` Empirical CDF Call: ecdf(y) x[1:58] = -0.3, -0.1, 0.1, ..., 4, 4 $`2` Empirical CDF Call: ecdf(y) x[1:58] = -0.3, -0.1, 0.1, ..., 4, 4 $`3` Empirical CDF Call: ecdf(y) x[1:58] = -0.3, -0.1, 0.1, ..., 4, 4 $`4` Empirical CDF Call: ecdf(y) x[1:58] = -0.3, -0.1, 0.1, ..., 4, 4 $`5` Empirical CDF Call: ecdf(y) x[1:58] = -0.3, -0.1, 0.1, ..., 4, 4 $`6` Empirical CDF Call: ecdf(y) x[1:26] = -1, -0.7, -0.7, ..., 2, 2 $`7` Empirical CDF Call: ecdf(y) x[1:26] = -1, -0.7, -0.7, ..., 2, 2 $`8` Empirical CDF Call: ecdf(y) x[1:26] = -1, -0.7, -0.7, ..., 2, 2 $`9` Empirical CDF Call: ecdf(y) x[1:16] = -2, -2, -2, ..., 0.7, 1 > predict(ct, newdata = nd, type = "quantile") 10% 50% 90% 1 1.064 2.126 3.254 2 1.064 2.126 3.254 3 1.064 2.126 3.254 4 1.064 2.126 3.254 5 1.064 2.126 3.254 6 -0.534 0.396 1.361 7 -0.534 0.396 1.361 8 -0.534 0.396 1.361 9 -1.659 -0.383 0.485 > predict(ct, newdata = nd, type = "quantile", at = NULL) $`1` function (p, ...) quantile(y, probs = p, ...) $`2` function (p, ...) quantile(y, probs = p, ...) $`3` function (p, ...) quantile(y, probs = p, ...) $`4` function (p, ...) quantile(y, probs = p, ...) $`5` function (p, ...) quantile(y, probs = p, ...) $`6` function (p, ...) quantile(y, probs = p, ...) $`7` function (p, ...) quantile(y, probs = p, ...) $`8` function (p, ...) quantile(y, probs = p, ...) $`9` function (p, ...) quantile(y, probs = p, ...) > predict(ct, newdata = nd, type = "density") $`1` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`2` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`3` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`4` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`5` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`6` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`7` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`8` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) $`9` function (v) .approxfun(x, y, v, method, yleft, yright, f, na.rm) > predict(ct, newdata = nd, type = "density", at = (1:9) / 10) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 1 0.0501 0.0493 0.0508 0.0565 0.0678 0.0854 0.109 0.138 0.169 2 0.0501 0.0493 0.0508 0.0565 0.0678 0.0854 0.109 0.138 0.169 3 0.0501 0.0493 0.0508 0.0565 0.0678 0.0854 0.109 0.138 0.169 4 0.0501 0.0493 0.0508 0.0565 0.0678 0.0854 0.109 0.138 0.169 5 0.0501 0.0493 0.0508 0.0565 0.0678 0.0854 0.109 0.138 0.169 6 0.5393 0.5764 0.5965 0.5940 0.5639 0.5057 0.429 0.350 0.290 7 0.5393 0.5764 0.5965 0.5940 0.5639 0.5057 0.429 0.350 0.290 8 0.5393 0.5764 0.5965 0.5940 0.5639 0.5057 0.429 0.350 0.290 9 0.3705 0.3422 0.3106 0.2778 0.2458 0.2160 0.189 0.166 0.145 > > proc.time() user system elapsed 2.70 0.79 2.51