~elachuni/chaid/partykit

« back to all changes in this revision

Viewing changes to partykit/pkg/tests/regtest-party.R

  • Committer: Anthony Lenton
  • Date: 2010-12-07 18:42:51 UTC
  • Revision ID: anthony.lenton@canonical.com-20101207184251-ei0pvabzhjly2ez6
Initial import from svn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
library("partykit")
 
3
library("rpart")
 
4
fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
 
5
 
 
6
pfit <- as.party(fit)
 
7
 
 
8
all(predict(pfit, newdata = kyphosis, type = "node") == fit$where)
 
9
 
 
10
library("RWeka")
 
11
library("rJava")
 
12
 
 
13
itree <- J48(Species ~ ., data = iris)
 
14
pitree <- as.party(itree)
 
15
all(predict(pitree) == predict(pitree, newdata = iris[, 3:4]))
 
16
 
 
17
all.equal(predict(itree, type = "prob", newdata = iris), predict(pitree, type = "prob", newdata = iris))
 
18
all.equal(predict(itree,  newdata = iris), predict(pitree, newdata = iris))
 
19
 
 
20
data("GlaucomaM", package = "ipred")
 
21
 
 
22
w <- runif(nrow(GlaucomaM))
 
23
fit <- rpart(Class ~ ., data = GlaucomaM, weights = w)
 
24
pfit <- as.party(fit)
 
25
all(predict(pfit, type = "node") == fit$where)
 
26
tmp <- GlaucomaM[sample(1:nrow(GlaucomaM), 100),]
 
27
all.equal(predict(fit, type = "prob", newdata = tmp), predict(pfit, type = "prob", newdata = tmp))
 
28
all.equal(predict(fit, type = "class", newdata = tmp), predict(pfit, newdata = tmp))                             
 
29
 
 
30
itree <- J48(Class ~ ., data = GlaucomaM)
 
31
pitree <- as.party(itree)
 
32
all.equal(predict(itree, newdata = tmp, type = "prob"), predict(pitree, newdata = tmp, type = "prob"))
 
33
 
 
34
 
 
35
data("airquality")
 
36
aq <- subset(airquality, !is.na(Ozone))
 
37
 
 
38
w <- runif(nrow(aq), max = 3)
 
39
aqr <- rpart(Ozone ~ ., data = aq, weights = w)
 
40
aqp <- as.party(aqr)
 
41
 
 
42
tmp <- subset(airquality, is.na(Ozone))
 
43
all.equal(predict(aqr, newdata = tmp), predict(aqp, newdata = tmp))
 
44
 
 
45
data("GBSG2", package = "ipred")
 
46
library("survival")
 
47
fit <- rpart(Surv(time, cens) ~ ., data = GBSG2)
 
48
pfit <- as.party(fit)
 
49
pfit$fitted
 
50
predict(pfit, newdata = GBSG2[1:100,], type = "prob")
 
51
predict(pfit, newdata = GBSG2[1:100,], type = "response")
 
52
 
 
53
dgp <- function(n)
 
54
    data.frame(y = gl(4, n), x1 = rnorm(4 * n), x2 = rnorm(4 * n))
 
55
 
 
56
learn <- dgp(100)
 
57
fit <- as.party(rpart(y ~ ., data = learn))
 
58
test <- dgp(100000)
 
59
system.time(id <- fitted_node(node_party(fit), test))
 
60
system.time(yhat <- predict_party(fit, id = id, newdata = test))
 
61
 
 
62
### multiple responses
 
63
f <- fitted(pfit)
 
64
f[["(response)"]] <- data.frame(srv = f[["(response)"]], hansi = runif(nrow(f)))
 
65
mp <- party(node_party(pfit), fitted = f, data = pfit$data)
 
66
class(mp) <- c("constparty", "party")
 
67
 
 
68
predict(mp, newdata = GBSG2[1:10,])
 
69
 
 
70
### predictions in info slots
 
71
tmp <- data.frame(x = rnorm(100))
 
72
pfit <- party(node = partynode(1L, split = partysplit(1L, breaks = 0), 
 
73
              kids = list(partynode(2L, info = -0.5), partynode(3L, info = 0.5))), data = tmp)
 
74
pfit
 
75
p <- predict(pfit, newdata = tmp)
 
76
p
 
77
table(p, sign(tmp$x))