Switch to unified view

a b/partyMod/vignettes/party.Rout.save
1
2
> options(width = 70, SweaveHooks = list(leftpar = function() par(mai = par("mai") * 
3
+     c(1, 1.1, 1, 1))))
4
5
> require("party")
6
Loading required package: party
7
Loading required package: grid
8
Loading required package: zoo
9
10
Attaching package: ‘zoo’
11
12
The following objects are masked from ‘package:base’:
13
14
    as.Date, as.Date.numeric
15
16
Loading required package: sandwich
17
Loading required package: strucchange
18
Loading required package: modeltools
19
Loading required package: stats4
20
21
> require("coin")
22
Loading required package: coin
23
Loading required package: survival
24
Loading required package: splines
25
26
> set.seed(290875)
27
28
> ls <- data.frame(y = gl(3, 50, labels = c("A", "B", 
29
+     "C")), x1 = rnorm(150) + rep(c(1, 0, 0), c(50, 50, 50)), 
30
+     x2 = runif(150))
31
32
> library("party")
33
34
> ctree(y ~ x1 + x2, data = ls)
35
36
     Conditional inference tree with 2 terminal nodes
37
38
Response:  y 
39
Inputs:  x1, x2 
40
Number of observations:  150 
41
42
1) x1 <= 0.8255248; criterion = 1, statistic = 22.991
43
  2)*  weights = 96 
44
1) x1 > 0.8255248
45
  3)*  weights = 54 
46
47
> ctree(y ~ x1 + x2, data = ls, xtrafo = function(data) trafo(data, 
48
+     numeric_trafo = rank))
49
50
     Conditional inference tree with 2 terminal nodes
51
52
Response:  y 
53
Inputs:  x1, x2 
54
Number of observations:  150 
55
56
1) x1 <= 0.8255248; criterion = 1, statistic = 22.186
57
  2)*  weights = 96 
58
1) x1 > 0.8255248
59
  3)*  weights = 54 
60
61
> ctree_control(testtype = "Bonferroni")
62
An object of class "TreeControl"
63
Slot "varctrl":
64
An object of class "VariableControl"
65
Slot "teststat":
66
[1] quad
67
Levels: max quad
68
69
Slot "pvalue":
70
[1] TRUE
71
72
Slot "tol":
73
[1] 1e-10
74
75
Slot "maxpts":
76
[1] 25000
77
78
Slot "abseps":
79
[1] 1e-04
80
81
Slot "releps":
82
[1] 0
83
84
85
Slot "splitctrl":
86
An object of class "SplitControl"
87
Slot "minprob":
88
[1] 0.01
89
90
Slot "minsplit":
91
[1] 20
92
93
Slot "minbucket":
94
[1] 7
95
96
Slot "tol":
97
[1] 1e-10
98
99
Slot "maxsurrogate":
100
[1] 0
101
102
103
Slot "gtctrl":
104
An object of class "GlobalTestControl"
105
Slot "testtype":
106
[1] Bonferroni
107
5 Levels: Bonferroni MonteCarlo Aggregated ... Teststatistic
108
109
Slot "nresample":
110
[1] 9999
111
112
Slot "randomsplits":
113
[1] FALSE
114
115
Slot "mtry":
116
[1] 0
117
118
Slot "mincriterion":
119
[1] 0.95
120
121
122
Slot "tgctrl":
123
An object of class "TreeGrowControl"
124
Slot "stump":
125
[1] FALSE
126
127
Slot "maxdepth":
128
[1] 0
129
130
Slot "savesplitstats":
131
[1] TRUE
132
133
134
135
> ctree_control(testtype = "MonteCarlo")
136
An object of class "TreeControl"
137
Slot "varctrl":
138
An object of class "VariableControl"
139
Slot "teststat":
140
[1] quad
141
Levels: max quad
142
143
Slot "pvalue":
144
[1] TRUE
145
146
Slot "tol":
147
[1] 1e-10
148
149
Slot "maxpts":
150
[1] 25000
151
152
Slot "abseps":
153
[1] 1e-04
154
155
Slot "releps":
156
[1] 0
157
158
159
Slot "splitctrl":
160
An object of class "SplitControl"
161
Slot "minprob":
162
[1] 0.01
163
164
Slot "minsplit":
165
[1] 20
166
167
Slot "minbucket":
168
[1] 7
169
170
Slot "tol":
171
[1] 1e-10
172
173
Slot "maxsurrogate":
174
[1] 0
175
176
177
Slot "gtctrl":
178
An object of class "GlobalTestControl"
179
Slot "testtype":
180
[1] MonteCarlo
181
5 Levels: Bonferroni MonteCarlo Aggregated ... Teststatistic
182
183
Slot "nresample":
184
[1] 9999
185
186
Slot "randomsplits":
187
[1] FALSE
188
189
Slot "mtry":
190
[1] 0
191
192
Slot "mincriterion":
193
[1] 0.95
194
195
196
Slot "tgctrl":
197
An object of class "TreeGrowControl"
198
Slot "stump":
199
[1] FALSE
200
201
Slot "maxdepth":
202
[1] 0
203
204
Slot "savesplitstats":
205
[1] TRUE
206
207
208
209
> ctree_control(savesplitstats = TRUE)
210
An object of class "TreeControl"
211
Slot "varctrl":
212
An object of class "VariableControl"
213
Slot "teststat":
214
[1] quad
215
Levels: max quad
216
217
Slot "pvalue":
218
[1] TRUE
219
220
Slot "tol":
221
[1] 1e-10
222
223
Slot "maxpts":
224
[1] 25000
225
226
Slot "abseps":
227
[1] 1e-04
228
229
Slot "releps":
230
[1] 0
231
232
233
Slot "splitctrl":
234
An object of class "SplitControl"
235
Slot "minprob":
236
[1] 0.01
237
238
Slot "minsplit":
239
[1] 20
240
241
Slot "minbucket":
242
[1] 7
243
244
Slot "tol":
245
[1] 1e-10
246
247
Slot "maxsurrogate":
248
[1] 0
249
250
251
Slot "gtctrl":
252
An object of class "GlobalTestControl"
253
Slot "testtype":
254
[1] Bonferroni
255
5 Levels: Bonferroni MonteCarlo Aggregated ... Teststatistic
256
257
Slot "nresample":
258
[1] 9999
259
260
Slot "randomsplits":
261
[1] FALSE
262
263
Slot "mtry":
264
[1] 0
265
266
Slot "mincriterion":
267
[1] 0.95
268
269
270
Slot "tgctrl":
271
An object of class "TreeGrowControl"
272
Slot "stump":
273
[1] FALSE
274
275
Slot "maxdepth":
276
[1] 0
277
278
Slot "savesplitstats":
279
[1] TRUE
280
281
282
283
> ctree_control(minsplit = 20)
284
An object of class "TreeControl"
285
Slot "varctrl":
286
An object of class "VariableControl"
287
Slot "teststat":
288
[1] quad
289
Levels: max quad
290
291
Slot "pvalue":
292
[1] TRUE
293
294
Slot "tol":
295
[1] 1e-10
296
297
Slot "maxpts":
298
[1] 25000
299
300
Slot "abseps":
301
[1] 1e-04
302
303
Slot "releps":
304
[1] 0
305
306
307
Slot "splitctrl":
308
An object of class "SplitControl"
309
Slot "minprob":
310
[1] 0.01
311
312
Slot "minsplit":
313
[1] 20
314
315
Slot "minbucket":
316
[1] 7
317
318
Slot "tol":
319
[1] 1e-10
320
321
Slot "maxsurrogate":
322
[1] 0
323
324
325
Slot "gtctrl":
326
An object of class "GlobalTestControl"
327
Slot "testtype":
328
[1] Bonferroni
329
5 Levels: Bonferroni MonteCarlo Aggregated ... Teststatistic
330
331
Slot "nresample":
332
[1] 9999
333
334
Slot "randomsplits":
335
[1] FALSE
336
337
Slot "mtry":
338
[1] 0
339
340
Slot "mincriterion":
341
[1] 0.95
342
343
344
Slot "tgctrl":
345
An object of class "TreeGrowControl"
346
Slot "stump":
347
[1] FALSE
348
349
Slot "maxdepth":
350
[1] 0
351
352
Slot "savesplitstats":
353
[1] TRUE
354
355
356
357
> ctree_control(maxsurrogate = 3)
358
An object of class "TreeControl"
359
Slot "varctrl":
360
An object of class "VariableControl"
361
Slot "teststat":
362
[1] quad
363
Levels: max quad
364
365
Slot "pvalue":
366
[1] TRUE
367
368
Slot "tol":
369
[1] 1e-10
370
371
Slot "maxpts":
372
[1] 25000
373
374
Slot "abseps":
375
[1] 1e-04
376
377
Slot "releps":
378
[1] 0
379
380
381
Slot "splitctrl":
382
An object of class "SplitControl"
383
Slot "minprob":
384
[1] 0.01
385
386
Slot "minsplit":
387
[1] 20
388
389
Slot "minbucket":
390
[1] 7
391
392
Slot "tol":
393
[1] 1e-10
394
395
Slot "maxsurrogate":
396
[1] 3
397
398
399
Slot "gtctrl":
400
An object of class "GlobalTestControl"
401
Slot "testtype":
402
[1] Bonferroni
403
5 Levels: Bonferroni MonteCarlo Aggregated ... Teststatistic
404
405
Slot "nresample":
406
[1] 9999
407
408
Slot "randomsplits":
409
[1] FALSE
410
411
Slot "mtry":
412
[1] 0
413
414
Slot "mincriterion":
415
[1] 0.95
416
417
418
Slot "tgctrl":
419
An object of class "TreeGrowControl"
420
Slot "stump":
421
[1] FALSE
422
423
Slot "maxdepth":
424
[1] 0
425
426
Slot "savesplitstats":
427
[1] TRUE
428
429
430
431
> ct <- ctree(y ~ x1 + x2, data = ls)
432
433
> ct
434
435
     Conditional inference tree with 2 terminal nodes
436
437
Response:  y 
438
Inputs:  x1, x2 
439
Number of observations:  150 
440
441
1) x1 <= 0.8255248; criterion = 1, statistic = 22.991
442
  2)*  weights = 96 
443
1) x1 > 0.8255248
444
  3)*  weights = 54 
445
446
> plot(ct)
447
448
> nodes(ct, 1)
449
[[1]]
450
1) x1 <= 0.8255248; criterion = 1, statistic = 22.991
451
  2)*  weights = 96 
452
1) x1 > 0.8255248
453
  3)*  weights = 54 
454
455
456
> names(nodes(ct, 1)[[1]])
457
 [1] "nodeID"     "weights"    "criterion"  "terminal"   "psplit"    
458
 [6] "ssplits"    "prediction" "left"       "right"      NA          
459
460
> Predict(ct, newdata = ls)
461
  [1] A A A A C A C A C C A A C A A A A C A C A A A C A A A C C A A C
462
 [33] A A C A A C C C A A C C C C A A A A A A C C C C A C C A C C C C
463
 [65] C C A A A A A C C A C A C C C C C C C C C C C C A C A C A C C C
464
 [97] C C C C C A C C C A C C A C C C C C C C A C C C C C C C C C C C
465
[129] C C C C C C C C C A C C C C A C C A C A C A
466
Levels: A B C
467
468
> treeresponse(ct, newdata = ls[c(1, 51, 101), ])
469
[[1]]
470
[1] 0.5740741 0.2592593 0.1666667
471
472
[[2]]
473
[1] 0.5740741 0.2592593 0.1666667
474
475
[[3]]
476
[1] 0.1979167 0.3750000 0.4270833
477
478
479
> where(ct, newdata = ls[c(1, 51, 101), ])
480
[1] 3 3 2
481
482
> data("treepipit", package = "coin")
483
484
> tptree <- ctree(counts ~ ., data = treepipit)
485
486
> plot(tptree, terminal_panel = node_hist(tptree, breaks = 0:6 - 
487
+     0.5, ymax = 65, horizontal = FALSE, freq = TRUE))
488
489
> x <- tptree@tree
490
491
> data("GlaucomaM", package = "TH.data")
492
493
> gtree <- ctree(Class ~ ., data = GlaucomaM)
494
495
> x <- gtree@tree
496
497
> plot(gtree)
498
499
> plot(gtree, inner_panel = node_barplot, edge_panel = function(...) invisible(), 
500
+     tnex = 1)
501
502
> cex <- 1.6
503
504
> inner <- nodes(gtree, c(1, 2, 5))
505
506
> layout(matrix(1:length(inner), ncol = length(inner)))
507
508
> out <- sapply(inner, function(i) {
509
+     splitstat <- i$psplit$splitstatistic
510
+     x <- GlaucomaM[[i$psplit$variableName]][splitstat > 0]
511
+     plo .... [TRUNCATED] 
512
513
> table(Predict(gtree), GlaucomaM$Class)
514
          
515
           glaucoma normal
516
  glaucoma       74      5
517
  normal         24     93
518
519
> prob <- sapply(treeresponse(gtree), function(x) x[1]) + 
520
+     runif(nrow(GlaucomaM), min = -0.01, max = 0.01)
521
522
> splitvar <- nodes(gtree, 1)[[1]]$psplit$variableName
523
524
> plot(GlaucomaM[[splitvar]], prob, pch = as.numeric(GlaucomaM$Class), 
525
+     ylab = "Conditional Class Prob.", xlab = splitvar)
526
527
> abline(v = nodes(gtree, 1)[[1]]$psplit$splitpoint, 
528
+     lty = 2)
529
530
> legend(0.15, 0.7, pch = 1:2, legend = levels(GlaucomaM$Class), 
531
+     bty = "n")
532
533
> data("GBSG2", package = "TH.data")
534
535
> stree <- ctree(Surv(time, cens) ~ ., data = GBSG2)
536
537
> plot(stree)
538
539
> treeresponse(stree, newdata = GBSG2[1:2, ])
540
[[1]]
541
Call: survfit(formula = y ~ 1, weights = weights)
542
543
records   n.max n.start  events  median 0.95LCL 0.95UCL 
544
    248     248     248      88    2093    1814      NA 
545
546
[[2]]
547
Call: survfit(formula = y ~ 1, weights = weights)
548
549
records   n.max n.start  events  median 0.95LCL 0.95UCL 
550
    166     166     166      77    1701    1174    2018 
551
552
553
> data("mammoexp", package = "TH.data")
554
555
> mtree <- ctree(ME ~ ., data = mammoexp)
556
557
> plot(mtree)
558
559
 *** Run successfully completed ***
560
> proc.time()
561
   user  system elapsed 
562
  1.404   0.056   1.457