|
a |
|
b/partyMod/tests/TreeGrow-regtest.R |
|
|
1 |
|
|
|
2 |
set.seed(290875) |
|
|
3 |
library("party") |
|
|
4 |
if (!require("TH.data")) |
|
|
5 |
stop("cannot load package TH.data") |
|
|
6 |
if (!require("coin")) |
|
|
7 |
stop("cannot load package coin") |
|
|
8 |
|
|
|
9 |
### get rid of the NAMESPACE |
|
|
10 |
attach(asNamespace("party")) |
|
|
11 |
|
|
|
12 |
gtctrl <- new("GlobalTestControl") |
|
|
13 |
tlev <- levels(gtctrl@testtype) |
|
|
14 |
|
|
|
15 |
data(GlaucomaM, package = "TH.data") |
|
|
16 |
gtree <- ctree(Class ~ ., data = GlaucomaM) |
|
|
17 |
tree <- gtree@tree |
|
|
18 |
stopifnot(isequal(tree[[5]][[3]], 0.059)) |
|
|
19 |
predict(gtree) |
|
|
20 |
|
|
|
21 |
# print(tree) |
|
|
22 |
|
|
|
23 |
stump <- ctree(Class ~ ., data = GlaucomaM, |
|
|
24 |
control = ctree_control(stump = TRUE)) |
|
|
25 |
print(stump) |
|
|
26 |
|
|
|
27 |
data(treepipit, package = "coin") |
|
|
28 |
|
|
|
29 |
tr <- ctree(counts ~ ., data = treepipit) |
|
|
30 |
tr |
|
|
31 |
plot(tr) |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
data(GlaucomaM, package = "TH.data") |
|
|
35 |
|
|
|
36 |
tr <- ctree(Class ~ ., data = GlaucomaM) |
|
|
37 |
tr |
|
|
38 |
plot(tr) |
|
|
39 |
|
|
|
40 |
data(GBSG2, package = "TH.data") |
|
|
41 |
|
|
|
42 |
GBSG2tree <- ctree(Surv(time, cens) ~ ., data = GBSG2) |
|
|
43 |
GBSG2tree |
|
|
44 |
plot(GBSG2tree) |
|
|
45 |
plot(GBSG2tree, terminal_panel = node_surv(GBSG2tree)) |
|
|
46 |
survfit(Surv(time, cens) ~ as.factor(GBSG2tree@where), data = GBSG2) |
|
|
47 |
names(GBSG2) |
|
|
48 |
|
|
|
49 |
tr <- ctree(Surv(time, cens) ~ ., data = GBSG2, |
|
|
50 |
control = ctree_control(teststat = "max", |
|
|
51 |
testtype = "Univariate")) |
|
|
52 |
tr |
|
|
53 |
plot(tr) |
|
|
54 |
|
|
|
55 |
data("mammoexp", package = "TH.data") |
|
|
56 |
attr(mammoexp$ME, "scores") <- 1:3 |
|
|
57 |
attr(mammoexp$SYMPT, "scores") <- 1:4 |
|
|
58 |
attr(mammoexp$DECT, "scores") <- 1:3 |
|
|
59 |
names(mammoexp)[names(mammoexp) == "SYMPT"] <- "symptoms" |
|
|
60 |
names(mammoexp)[names(mammoexp) == "PB"] <- "benefit" |
|
|
61 |
|
|
|
62 |
names(mammoexp) |
|
|
63 |
tr <- ctree(ME ~ ., data = mammoexp) |
|
|
64 |
tr |
|
|
65 |
plot(tr) |
|
|
66 |
|
|
|
67 |
treeresponse(tr, newdata = mammoexp[1:5,]) |
|
|
68 |
|
|
|
69 |
### check different user interfaces |
|
|
70 |
data("iris") |
|
|
71 |
x <- as.matrix(iris[,colnames(iris) != "Species"]) |
|
|
72 |
y <- iris[,"Species"] |
|
|
73 |
newx <- x |
|
|
74 |
|
|
|
75 |
ls <- LearningSample(x, y) |
|
|
76 |
p1 <- unlist(treeresponse(ctree(Species ~ ., data = iris), newdata = as.data.frame(newx))) |
|
|
77 |
p2 <- unlist(treeresponse(ctreefit(ls, control = ctree_control()), newdata = as.matrix(newx))) |
|
|
78 |
stopifnot(identical(max(abs(p1 - p2)), 0)) |
|
|
79 |
|
|
|
80 |
set.seed(29) |
|
|
81 |
p1 <- unlist(treeresponse(cforestfit(ls, control = cforest_unbiased(mtry = 1)), newdata = as.matrix(newx))) |
|
|
82 |
set.seed(29) |
|
|
83 |
p2 <- unlist(treeresponse(cforest(Species ~ ., data = iris, control = cforest_unbiased(mtry = 1)), |
|
|
84 |
newdata = as.data.frame(newx))) |
|
|
85 |
stopifnot(identical(max(abs(p1 - p2)), 0)) |