R version 4.3.0 (2023-04-21) -- "Already Tomorrow" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) 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. Natural language support but running in an English locale 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 an HTML browser interface to help. Type 'q()' to quit R. > # Small example to illustrate rpart() on the car.test.frame data > > > library("rpart") > > set.seed(784) > car.test.frame[sample(nrow(car.test.frame), 4), ] Price Country Reliability Mileage Type Weight Mercury Tracer 4 8672 Mexico 4 26 Small 2285 Nissan Stanza 4 11650 Japan 5 21 Compact 2920 Buick Century 4 13150 USA 3 21 Medium 2880 Oldsmobile Cutlass Ciera 4 13150 USA 2 21 Medium 2765 Disp. HP Mercury Tracer 4 97 82 Nissan Stanza 4 146 138 Buick Century 4 151 110 Oldsmobile Cutlass Ciera 4 151 110 > > car.tree <- rpart(Mileage ~ Weight, car.test.frame, method = "anova") > > car.tree n= 60 node), split, n, deviance, yval * denotes terminal node 1) root 60 1354.58300 24.58333 2) Weight>=2567.5 45 361.20000 22.46667 4) Weight>=3087.5 22 61.31818 20.40909 * 5) Weight< 3087.5 23 117.65220 24.43478 10) Weight>=2747.5 15 60.40000 23.80000 * 11) Weight< 2747.5 8 39.87500 25.62500 * 3) Weight< 2567.5 15 186.93330 30.93333 * > printcp(car.tree) Regression tree: rpart(formula = Mileage ~ Weight, data = car.test.frame, method = "anova") Variables actually used in tree construction: [1] Weight Root node error: 1354.6/60 = 22.576 n= 60 CP nsplit rel error xerror xstd 1 0.595349 0 1.00000 1.04683 0.181646 2 0.134528 1 0.40465 0.61696 0.112918 3 0.012828 2 0.27012 0.46574 0.091113 4 0.010000 3 0.25729 0.46802 0.090865 > summary(car.tree) Call: rpart(formula = Mileage ~ Weight, data = car.test.frame, method = "anova") n= 60 CP nsplit rel error xerror xstd 1 0.59534912 0 1.0000000 1.0468270 0.18164614 2 0.13452819 1 0.4046509 0.6169648 0.11291752 3 0.01282843 2 0.2701227 0.4657376 0.09111300 4 0.01000000 3 0.2572943 0.4680214 0.09086511 Variable importance Weight 100 Node number 1: 60 observations, complexity param=0.5953491 mean=24.58333, MSE=22.57639 left son=2 (45 obs) right son=3 (15 obs) Primary splits: Weight < 2567.5 to the right, improve=0.5953491, (0 missing) Node number 2: 45 observations, complexity param=0.1345282 mean=22.46667, MSE=8.026667 left son=4 (22 obs) right son=5 (23 obs) Primary splits: Weight < 3087.5 to the right, improve=0.5045118, (0 missing) Node number 3: 15 observations mean=30.93333, MSE=12.46222 Node number 4: 22 observations mean=20.40909, MSE=2.78719 Node number 5: 23 observations, complexity param=0.01282843 mean=24.43478, MSE=5.115312 left son=10 (15 obs) right son=11 (8 obs) Primary splits: Weight < 2747.5 to the right, improve=0.1476996, (0 missing) Node number 10: 15 observations mean=23.8, MSE=4.026667 Node number 11: 8 observations mean=25.625, MSE=4.984375 > names(car.tree) [1] "frame" "where" "call" [4] "terms" "cptable" "method" [7] "parms" "control" "functions" [10] "numresp" "splits" "variable.importance" [13] "y" "ordered" > > > > if (FALSE) { + postscript("cartree.eps", + horiz = FALSE, onefile = FALSE, print.it = FALSE, + width = 5.0, height = 3.0) + } else { + pdf("cartree.pdf", + onefile = FALSE, + width = 5.0, height = 3.0) + } > > > # partition(car.tree) > > # par(mar = c(5, 4, 2, 2) + 0.8, mfrow = c(1, 1), xpd = TRUE) > par(xpd = TRUE, cex = 1.0) > plot(car.tree) > text(car.tree) > dev.off() null device 1 > > > > proc.time() user system elapsed 0.169 0.032 0.228