Switch to unified view

a b/partyMod/tests/TreeGrow-regtest.Rout.save
1
2
R Under development (unstable) (2014-06-29 r66051) -- "Unsuffered Consequences"
3
Copyright (C) 2014 The R Foundation for Statistical Computing
4
Platform: x86_64-unknown-linux-gnu (64-bit)
5
6
R is free software and comes with ABSOLUTELY NO WARRANTY.
7
You are welcome to redistribute it under certain conditions.
8
Type 'license()' or 'licence()' for distribution details.
9
10
R is a collaborative project with many contributors.
11
Type 'contributors()' for more information and
12
'citation()' on how to cite R or R packages in publications.
13
14
Type 'demo()' for some demos, 'help()' for on-line help, or
15
'help.start()' for an HTML browser interface to help.
16
Type 'q()' to quit R.
17
18
> 
19
> set.seed(290875)
20
> library("party")
21
Loading required package: grid
22
Loading required package: zoo
23
24
Attaching package: 'zoo'
25
26
The following objects are masked from 'package:base':
27
28
    as.Date, as.Date.numeric
29
30
Loading required package: sandwich
31
Loading required package: strucchange
32
Loading required package: modeltools
33
Loading required package: stats4
34
> if (!require("TH.data"))
35
+     stop("cannot load package TH.data")
36
Loading required package: TH.data
37
> if (!require("coin"))
38
+     stop("cannot load package coin")
39
Loading required package: coin
40
Loading required package: survival
41
Loading required package: splines
42
> 
43
> ### get rid of the NAMESPACE
44
> attach(asNamespace("party"))
45
The following objects are masked from package:party:
46
47
    cforest, cforest_classical, cforest_control, cforest_unbiased,
48
    conditionalTree, ctree, ctree_control, ctree_memory, edge_simple,
49
    mob, mob_control, node_barplot, node_bivplot, node_boxplot,
50
    node_density, node_hist, node_inner, node_scatterplot, node_surv,
51
    node_terminal, proximity, ptrafo, reweight, sctest.mob, varimp,
52
    varimpAUC
53
54
> 
55
> gtctrl <- new("GlobalTestControl")
56
> tlev <- levels(gtctrl@testtype)
57
> 
58
> data(GlaucomaM, package = "TH.data")
59
> gtree <- ctree(Class ~ ., data = GlaucomaM)
60
> tree <- gtree@tree
61
> stopifnot(isequal(tree[[5]][[3]], 0.059))
62
> predict(gtree)
63
  [1] normal   normal   normal   normal   normal   normal   normal   normal  
64
  [9] normal   normal   normal   glaucoma normal   normal   normal   normal  
65
 [17] normal   normal   normal   normal   normal   normal   normal   normal  
66
 [25] normal   normal   normal   normal   normal   normal   normal   normal  
67
 [33] normal   normal   glaucoma normal   normal   normal   normal   normal  
68
 [41] normal   normal   glaucoma normal   normal   normal   normal   normal  
69
 [49] normal   normal   normal   normal   normal   normal   normal   normal  
70
 [57] normal   normal   normal   normal   normal   normal   normal   normal  
71
 [65] normal   normal   normal   normal   normal   glaucoma normal   normal  
72
 [73] normal   normal   normal   normal   normal   normal   normal   normal  
73
 [81] glaucoma normal   normal   normal   normal   normal   normal   normal  
74
 [89] normal   normal   normal   normal   normal   normal   normal   normal  
75
 [97] normal   normal   glaucoma glaucoma glaucoma glaucoma normal   normal  
76
[105] normal   normal   normal   glaucoma glaucoma normal   glaucoma glaucoma
77
[113] glaucoma glaucoma glaucoma glaucoma glaucoma normal   normal   glaucoma
78
[121] glaucoma glaucoma glaucoma glaucoma glaucoma glaucoma normal   glaucoma
79
[129] normal   glaucoma normal   glaucoma glaucoma glaucoma glaucoma glaucoma
80
[137] glaucoma glaucoma glaucoma glaucoma glaucoma glaucoma glaucoma glaucoma
81
[145] glaucoma glaucoma normal   glaucoma glaucoma glaucoma glaucoma normal  
82
[153] glaucoma glaucoma glaucoma glaucoma normal   glaucoma glaucoma glaucoma
83
[161] glaucoma glaucoma normal   normal   glaucoma glaucoma normal   glaucoma
84
[169] glaucoma glaucoma glaucoma glaucoma normal   glaucoma glaucoma glaucoma
85
[177] normal   glaucoma normal   glaucoma glaucoma glaucoma normal   glaucoma
86
[185] glaucoma glaucoma normal   glaucoma glaucoma normal   glaucoma normal  
87
[193] glaucoma glaucoma glaucoma glaucoma
88
Levels: glaucoma normal
89
> 
90
> # print(tree)
91
> 
92
> stump <- ctree(Class ~ ., data = GlaucomaM, 
93
+                control = ctree_control(stump = TRUE))
94
> print(stump)
95
96
     Conditional inference tree with 2 terminal nodes
97
98
Response:  Class 
99
Inputs:  ag, at, as, an, ai, eag, eat, eas, ean, eai, abrg, abrt, abrs, abrn, abri, hic, mhcg, mhct, mhcs, mhcn, mhci, phcg, phct, phcs, phcn, phci, hvc, vbsg, vbst, vbss, vbsn, vbsi, vasg, vast, vass, vasn, vasi, vbrg, vbrt, vbrs, vbrn, vbri, varg, vart, vars, varn, vari, mdg, mdt, mds, mdn, mdi, tmg, tmt, tms, tmn, tmi, mr, rnf, mdic, emd, mv 
100
Number of observations:  196 
101
102
1) vari <= 0.059; criterion = 1, statistic = 71.475
103
  2)*  weights = 87 
104
1) vari > 0.059
105
  3)*  weights = 109 
106
> 
107
> data(treepipit, package = "coin")
108
> 
109
> tr <- ctree(counts ~ ., data = treepipit)
110
> tr
111
112
     Conditional inference tree with 2 terminal nodes
113
114
Response:  counts 
115
Inputs:  age, coverstorey, coverregen, meanregen, coniferous, deadtree, cbpiles, ivytree, fdist 
116
Number of observations:  86 
117
118
1) coverstorey <= 40; criterion = 0.998, statistic = 13.678
119
  2)*  weights = 24 
120
1) coverstorey > 40
121
  3)*  weights = 62 
122
> plot(tr)
123
> 
124
> 
125
> data(GlaucomaM, package = "TH.data")
126
> 
127
> tr <- ctree(Class ~ ., data = GlaucomaM)
128
> tr
129
130
     Conditional inference tree with 4 terminal nodes
131
132
Response:  Class 
133
Inputs:  ag, at, as, an, ai, eag, eat, eas, ean, eai, abrg, abrt, abrs, abrn, abri, hic, mhcg, mhct, mhcs, mhcn, mhci, phcg, phct, phcs, phcn, phci, hvc, vbsg, vbst, vbss, vbsn, vbsi, vasg, vast, vass, vasn, vasi, vbrg, vbrt, vbrs, vbrn, vbri, varg, vart, vars, varn, vari, mdg, mdt, mds, mdn, mdi, tmg, tmt, tms, tmn, tmi, mr, rnf, mdic, emd, mv 
134
Number of observations:  196 
135
136
1) vari <= 0.059; criterion = 1, statistic = 71.475
137
  2) vasg <= 0.066; criterion = 1, statistic = 29.265
138
    3)*  weights = 79 
139
  2) vasg > 0.066
140
    4)*  weights = 8 
141
1) vari > 0.059
142
  5) tms <= -0.066; criterion = 0.951, statistic = 11.221
143
    6)*  weights = 65 
144
  5) tms > -0.066
145
    7)*  weights = 44 
146
> plot(tr)
147
> 
148
> data(GBSG2, package = "TH.data")  
149
> 
150
> GBSG2tree <- ctree(Surv(time, cens) ~ ., data = GBSG2)
151
> GBSG2tree
152
153
     Conditional inference tree with 4 terminal nodes
154
155
Response:  Surv(time, cens) 
156
Inputs:  horTh, age, menostat, tsize, tgrade, pnodes, progrec, estrec 
157
Number of observations:  686 
158
159
1) pnodes <= 3; criterion = 1, statistic = 56.156
160
  2) horTh == {no}; criterion = 0.965, statistic = 8.113
161
    3)*  weights = 248 
162
  2) horTh == {yes}
163
    4)*  weights = 128 
164
1) pnodes > 3
165
  5) progrec <= 20; criterion = 0.999, statistic = 14.941
166
    6)*  weights = 144 
167
  5) progrec > 20
168
    7)*  weights = 166 
169
> plot(GBSG2tree)
170
> plot(GBSG2tree, terminal_panel = node_surv(GBSG2tree))
171
> survfit(Surv(time, cens) ~ as.factor(GBSG2tree@where), data = GBSG2)
172
Call: survfit(formula = Surv(time, cens) ~ as.factor(GBSG2tree@where), 
173
    data = GBSG2)
174
175
                             records n.max n.start events median 0.95LCL
176
as.factor(GBSG2tree@where)=3     248   248     248     88   2093    1814
177
as.factor(GBSG2tree@where)=4     128   128     128     31     NA    2372
178
as.factor(GBSG2tree@where)=6     144   144     144    103    624     525
179
as.factor(GBSG2tree@where)=7     166   166     166     77   1701    1174
180
                             0.95UCL
181
as.factor(GBSG2tree@where)=3      NA
182
as.factor(GBSG2tree@where)=4      NA
183
as.factor(GBSG2tree@where)=6     797
184
as.factor(GBSG2tree@where)=7    2018
185
> names(GBSG2)
186
 [1] "horTh"    "age"      "menostat" "tsize"    "tgrade"   "pnodes"  
187
 [7] "progrec"  "estrec"   "time"     "cens"    
188
> 
189
> tr <- ctree(Surv(time, cens) ~ ., data = GBSG2, 
190
+             control = ctree_control(teststat = "max", 
191
+                                     testtype = "Univariate"))
192
There were 18 warnings (use warnings() to see them)
193
> tr
194
195
     Conditional inference tree with 10 terminal nodes
196
197
Response:  Surv(time, cens) 
198
Inputs:  horTh, age, menostat, tsize, tgrade, pnodes, progrec, estrec 
199
Number of observations:  686 
200
201
1) pnodes <= 3; criterion = 1, statistic = 7.494
202
  2) horTh == {no}; criterion = 0.996, statistic = 2.848
203
    3) menostat == {Post}; criterion = 0.978, statistic = 2.286
204
      4)*  weights = 112 
205
    3) menostat == {Pre}
206
      5) age <= 37; criterion = 1, statistic = 3.858
207
        6)*  weights = 21 
208
      5) age > 37
209
        7)*  weights = 115 
210
  2) horTh == {yes}
211
    8) progrec <= 74; criterion = 0.975, statistic = 2.241
212
      9)*  weights = 73 
213
    8) progrec > 74
214
      10)*  weights = 55 
215
1) pnodes > 3
216
  11) progrec <= 20; criterion = 1, statistic = 3.865
217
    12) pnodes <= 9; criterion = 0.991, statistic = 2.612
218
      13)*  weights = 87 
219
    12) pnodes > 9
220
      14)*  weights = 57 
221
  11) progrec > 20
222
    15) horTh == {no}; criterion = 0.976, statistic = 2.251
223
      16)*  weights = 101 
224
    15) horTh == {yes}
225
      17) menostat == {Post}; criterion = 0.965, statistic = 2.105
226
        18)*  weights = 45 
227
      17) menostat == {Pre}
228
        19)*  weights = 20 
229
> plot(tr)
230
> 
231
> data("mammoexp", package = "TH.data")
232
> attr(mammoexp$ME, "scores") <- 1:3   
233
> attr(mammoexp$SYMPT, "scores") <- 1:4
234
> attr(mammoexp$DECT, "scores") <- 1:3 
235
> names(mammoexp)[names(mammoexp) == "SYMPT"] <- "symptoms"
236
> names(mammoexp)[names(mammoexp) == "PB"] <- "benefit"
237
> 
238
> names(mammoexp)
239
[1] "ME"       "symptoms" "benefit"  "HIST"     "BSE"      "DECT"    
240
> tr <- ctree(ME ~ ., data = mammoexp)
241
> tr
242
243
     Conditional inference tree with 3 terminal nodes
244
245
Response:  ME 
246
Inputs:  symptoms, benefit, HIST, BSE, DECT 
247
Number of observations:  412 
248
249
1) symptoms <= Agree; criterion = 1, statistic = 29.933
250
  2)*  weights = 113 
251
1) symptoms > Agree
252
  3) benefit <= 8; criterion = 0.988, statistic = 9.17
253
    4)*  weights = 208 
254
  3) benefit > 8
255
    5)*  weights = 91 
256
> plot(tr)
257
> 
258
> treeresponse(tr, newdata = mammoexp[1:5,])
259
[[1]]
260
[1] 0.3990385 0.3798077 0.2211538
261
262
[[2]]
263
[1] 0.84070796 0.05309735 0.10619469
264
265
[[3]]
266
[1] 0.3990385 0.3798077 0.2211538
267
268
[[4]]
269
[1] 0.6153846 0.2087912 0.1758242
270
271
[[5]]
272
[1] 0.3990385 0.3798077 0.2211538
273
274
> 
275
> ### check different user interfaces
276
> data("iris")
277
> x <- as.matrix(iris[,colnames(iris) != "Species"])
278
> y <- iris[,"Species"]
279
> newx <- x
280
> 
281
> ls <- LearningSample(x, y)
282
> p1 <- unlist(treeresponse(ctree(Species ~ ., data = iris), newdata = as.data.frame(newx)))
283
> p2 <- unlist(treeresponse(ctreefit(ls, control = ctree_control()), newdata = as.matrix(newx)))
284
> stopifnot(identical(max(abs(p1 - p2)), 0))
285
> 
286
> set.seed(29)
287
> p1 <- unlist(treeresponse(cforestfit(ls, control = cforest_unbiased(mtry = 1)), newdata = as.matrix(newx)))
288
> set.seed(29)
289
> p2 <- unlist(treeresponse(cforest(Species ~ ., data = iris, control = cforest_unbiased(mtry = 1)), 
290
+              newdata = as.data.frame(newx)))
291
> stopifnot(identical(max(abs(p1 - p2)), 0))
292
> 
293
> proc.time()
294
   user  system elapsed 
295
  2.492   0.112   2.604