R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 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 a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("party-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('party') Loading required package: survival Loading required package: splines Loading required package: grid Loading required package: modeltools Loading required package: coin Loading required package: mvtnorm > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "BinaryTree-class" > > ### * BinaryTree-class > > flush(stderr()); flush(stdout()) > > ### Name: BinaryTree Class > ### Title: Class "BinaryTree" > ### Aliases: BinaryTree-class weights weights-methods > ### weights,BinaryTree-method where where-methods where,BinaryTree-method > ### response response-methods response,BinaryTree-method nodes > ### nodes-methods nodes,BinaryTree,integer-method > ### nodes,BinaryTree,numeric-method treeresponse treeresponse-methods > ### treeresponse,BinaryTree-method > ### Keywords: classes > > ### ** Examples > > data(airquality) > airq <- subset(airquality, !is.na(Ozone)) > airct <- ctree(Ozone ~ ., data = airq, + controls = ctree_control(maxsurrogate = 3)) > > ### distribution of responses in the terminal nodes > plot(airq$Ozone ~ as.factor(where(airct))) > > ### get all terminal nodes from the tree > nodes(airct, unique(where(airct))) [[1]] 5)* weights = 48 [[2]] 3)* weights = 10 [[3]] 6)* weights = 21 [[4]] 9)* weights = 10 [[5]] 8)* weights = 27 > > ### extract weights and compute predictions > pmean <- sapply(weights(airct), function(w) weighted.mean(airq$Ozone, w)) > > ### the same as > drop(Predict(airct)) [1] 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 [9] 55.60000 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 [17] 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 31.14286 [25] 55.60000 18.47917 31.14286 55.50000 55.50000 31.14286 18.47917 18.47917 [33] 18.47917 18.47917 18.47917 82.77778 55.50000 31.14286 82.77778 55.50000 [41] 82.77778 82.77778 82.77778 82.77778 18.47917 31.14286 31.14286 55.60000 [49] 31.14286 82.77778 82.77778 55.50000 55.60000 82.77778 82.77778 31.14286 [57] 55.50000 82.77778 82.77778 82.77778 31.14286 55.60000 31.14286 31.14286 [65] 82.77778 82.77778 82.77778 82.77778 55.50000 82.77778 55.50000 31.14286 [73] 31.14286 18.47917 55.60000 18.47917 31.14286 31.14286 18.47917 18.47917 [81] 31.14286 55.60000 82.77778 55.50000 82.77778 82.77778 82.77778 82.77778 [89] 82.77778 82.77778 82.77778 82.77778 55.50000 31.14286 31.14286 18.47917 [97] 18.47917 31.14286 18.47917 55.60000 18.47917 18.47917 55.60000 18.47917 [105] 18.47917 18.47917 31.14286 18.47917 18.47917 31.14286 18.47917 18.47917 [113] 55.60000 18.47917 18.47917 18.47917 > > ### or > unlist(treeresponse(airct)) [1] 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 [9] 55.60000 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 [17] 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 18.47917 31.14286 [25] 55.60000 18.47917 31.14286 55.50000 55.50000 31.14286 18.47917 18.47917 [33] 18.47917 18.47917 18.47917 82.77778 55.50000 31.14286 82.77778 55.50000 [41] 82.77778 82.77778 82.77778 82.77778 18.47917 31.14286 31.14286 55.60000 [49] 31.14286 82.77778 82.77778 55.50000 55.60000 82.77778 82.77778 31.14286 [57] 55.50000 82.77778 82.77778 82.77778 31.14286 55.60000 31.14286 31.14286 [65] 82.77778 82.77778 82.77778 82.77778 55.50000 82.77778 55.50000 31.14286 [73] 31.14286 18.47917 55.60000 18.47917 31.14286 31.14286 18.47917 18.47917 [81] 31.14286 55.60000 82.77778 55.50000 82.77778 82.77778 82.77778 82.77778 [89] 82.77778 82.77778 82.77778 82.77778 55.50000 31.14286 31.14286 18.47917 [97] 18.47917 31.14286 18.47917 55.60000 18.47917 18.47917 55.60000 18.47917 [105] 18.47917 18.47917 31.14286 18.47917 18.47917 31.14286 18.47917 18.47917 [113] 55.60000 18.47917 18.47917 18.47917 > > ### don't use the mean but the median as prediction in each terminal node > pmedian <- sapply(weights(airct), function(w) + median(airq$Ozone[rep(1:nrow(airq), w)])) > > plot(airq$Ozone, pmean, col = "red") > points(airq$Ozone, pmedian, col = "blue") > > > > cleanEx(); ..nameEx <- "ctree" > > ### * ctree > > flush(stderr()); flush(stdout()) > > ### Name: Conditional Trees > ### Title: Conditional Trees > ### Aliases: ctree conditionalTree > ### Keywords: tree > > ### ** Examples > > > ### regression > data(airquality) > airq <- subset(airquality, !is.na(Ozone)) > airct <- ctree(Ozone ~ ., data = airq, + controls = ctree_control(maxsurrogate = 3)) > airct Conditional tree with 5 terminal nodes Response: Ozone Inputs: Solar.R, Wind, Temp, Month, Day Number of observations: 116 1) Temp <= 82; criterion = 1, statistic = 56.086 2) Wind <= 6.9; criterion = 0.998, statistic = 12.969 3)* weights = 10 2) Wind > 6.9 4) Temp <= 77; criterion = 0.997, statistic = 11.599 5)* weights = 48 4) Temp > 77 6)* weights = 21 1) Temp > 82 7) Wind <= 8.6; criterion = 0.997, statistic = 11.712 8)* weights = 27 7) Wind > 8.6 9)* weights = 10 > plot(airct) > mean((airq$Ozone - predict(airct))^2) [1] 409.8803 > > ### classification > irisct <- ctree(Species ~ .,data = iris) > irisct Conditional tree with 4 terminal nodes Response: Species Inputs: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width Number of observations: 150 1) Petal.Length <= 1.9; criterion = 1, statistic = 140.264 2)* weights = 50 1) Petal.Length > 1.9 3) Petal.Width <= 1.7; criterion = 1, statistic = 67.894 4) Petal.Length <= 4.8; criterion = 0.999, statistic = 13.865 5)* weights = 46 4) Petal.Length > 4.8 6)* weights = 8 3) Petal.Width > 1.7 7)* weights = 46 > plot(irisct) > table(predict(irisct), iris$Species) setosa versicolor virginica setosa 50 0 0 versicolor 0 49 5 virginica 0 1 45 > > ### estimated class probabilities, a list > tr <- treeresponse(irisct, newdata = iris[1:10,]) > > ### ordinal regression > data(mammoexp) > mammoct <- ctree(ME ~ ., data = mammoexp) Warning: no admissible split found > plot(mammoct) > > ### estimated class probabilities > treeresponse(mammoct, newdata = mammoexp[1:10,]) [[1]] [1] 0.3990385 0.3798077 0.2211538 [[2]] [1] 0.84070796 0.05309735 0.10619469 [[3]] [1] 0.3990385 0.3798077 0.2211538 [[4]] [1] 0.6153846 0.2087912 0.1758242 [[5]] [1] 0.3990385 0.3798077 0.2211538 [[6]] [1] 0.3990385 0.3798077 0.2211538 [[7]] [1] 0.3990385 0.3798077 0.2211538 [[8]] [1] 0.3990385 0.3798077 0.2211538 [[9]] [1] 0.84070796 0.05309735 0.10619469 [[10]] [1] 0.3990385 0.3798077 0.2211538 > > ### survival analysis > if (require(ipred)) { + data(GBSG2, package = "ipred") + GBSG2ct <- ctree(Surv(time, cens) ~ .,data = GBSG2) + plot(GBSG2ct) + treeresponse(GBSG2ct, newdata = GBSG2[1:2,]) + } Loading required package: ipred Loading required package: rpart Loading required package: MASS Loading required package: mlbench Loading required package: nnet Loading required package: class [[1]] Call: survival:::survfit(formula = resp, weights = w[[i]], subset = w[[i]] > 0) n events median 0.95LCL 0.95UCL 248 88 2093 1814 Inf [[2]] Call: survival:::survfit(formula = resp, weights = w[[i]], subset = w[[i]] > 0) n events median 0.95LCL 0.95UCL 166 77 1701 1174 2018 > > > > > cleanEx(); ..nameEx <- "ctree.memory" > > ### * ctree.memory > > flush(stderr()); flush(stdout()) > > ### Name: Memory Allocation > ### Title: Memory Allocation > ### Aliases: ctree_memory > ### Keywords: misc > > ### ** Examples > > > ### setup learning sample > data(airquality) > airq <- subset(airquality, !is.na(Ozone)) > ls <- dpp(conditionalTree, Ozone ~ ., data = airq) > > ### setup memory and controls > mem <- ctree_memory(ls) > ct <- ctree_control(teststattype = "maxabs") > > ### fit 50 trees on bootstrap samples > bs <- rmultinom(50, nrow(airq), rep(1, nrow(airq))/nrow(airq)) > storage.mode(bs) <- "double" > cfit <- conditionalTree@fit > system.time(ens <- apply(bs, 2, function(w) cfit(ls, ct, weights = w, + fitmem = mem))) [1] 0.86 0.01 0.86 0.00 0.00 > > > > > cleanEx(); ..nameEx <- "mammoexp" > > ### * mammoexp > > flush(stderr()); flush(stdout()) > > ### Name: mammoexp > ### Title: Mammography Experience Study > ### Aliases: mammoexp > ### Keywords: datasets > > ### ** Examples > > > data(mammoexp) > ### fit tree with attached scores (but those are the default values anyway) > ctree(ME ~ .,data = mammoexp, + scores = list(ME = 1:3, SYMPT = 1:4, DECT = 1:3)) Warning: no admissible split found Conditional tree with 3 terminal nodes Response: ME Inputs: SYMPT, PB, HIST, BSE, DECT Number of observations: 412 1) SYMPT <= Agree; criterion = 1, statistic = 29.933 2)* weights = 113 1) SYMPT > Agree 3) PB <= 8; criterion = 0.988, statistic = 9.17 4)* weights = 208 3) PB > 8 5)* weights = 91 > > > > cleanEx(); ..nameEx <- "panelfunctions" > > ### * panelfunctions > > flush(stderr()); flush(stdout()) > > ### Name: Panel Generating Functions > ### Title: Visualization of Binary Regression Trees > ### Aliases: node_inner node_terminal edge_simple node_surv node_barplot > ### node_boxplot node_hist node_density > ### Keywords: hplot > > ### ** Examples > > > data(airquality) > airq <- subset(airquality, !is.na(Ozone)) > airct <- ctree(Ozone ~ ., data = airq) > > ### very simple; the mean is given in each terminal node > plot(airct, type = "simple") > > ### density estimates > plot(airct, terminal_panel = node_density) > > ### histograms > plot(airct, terminal_panel = node_hist) > > ### boxplots > plot(airct, terminal_panel = node_boxplot, drop_terminal = TRUE) > > > > > cleanEx(); ..nameEx <- "plot.BinaryTree" > > ### * plot.BinaryTree > > flush(stderr()); flush(stdout()) > > ### Name: Plot BinaryTree > ### Title: Visualization of Binary Regression Trees > ### Aliases: plot.BinaryTree > ### Keywords: hplot > > ### ** Examples > > > data(airquality) > airq <- subset(airquality, !is.na(Ozone)) > airct <- ctree(Ozone ~ ., data = airq) > > ### boxplots in each node > plot(airct, terminal_panel = node_boxplot, drop_terminal = TRUE) > > > > > ### *