R Under development (unstable) (2024-02-25 r85988 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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 an HTML browser interface to help. Type 'q()' to quit R. > # test.rpart.plot.R > # Check for porting problems by building a few simple models. > # For much more comprehensive tests see rpart.plot\inst\slowtests. > library(rpart.plot) Loading required package: rpart > data(trees) # anova model > Volume <- rpart(Volume~., data=trees) > print(rpart.rules(Volume, digits=5)) Volume 17.933 when Girth < 12.45 30.533 when Girth is 12.45 to 16.15 55.929 when Girth >= 16.15 > data(ptitanic) # binomial model > ptitanic1 <- ptitanic > ptitanic1$female <- ptitanic1$sex == "female" # logical variable > ptitanic1$sex <- NULL > ptitanic1$parch <- NULL > survived <- rpart(survived ~ ., data=ptitanic1, method="class") > print(rpart.rules(survived, clip.facs=TRUE)) survived 0.05 when not female & age < 9.5 & sibsp >= 3 0.14 when female & sibsp >= 3 & 3rd 0.17 when not female & age >= 9.5 0.38 when female & age >= 27.5 & sibsp < 3 & 3rd 0.39 when female & age is 16.5 to 21.5 & sibsp < 3 & 3rd 0.61 when female & age is 21.5 to 27.5 & sibsp < 3 & 3rd 0.73 when female & age < 16.5 & sibsp < 3 & 3rd 0.89 when not female & age < 9.5 & sibsp < 3 0.93 when female & 1st or 2nd > options(digits=4) # avoid numerical discrepancies across machines and R versions > options(width=1e3) > print(head(rpart.predict(survived, rules=TRUE))) died survived 1 0.0680 0.9320 because female is 1 & pclass is 1st or 2nd 2 0.1111 0.8889 because female is 0 & age < 9.5 & sibsp < 3 3 0.0680 0.9320 because female is 1 & pclass is 1st or 2nd 4 0.8291 0.1709 because female is 0 & age >= 9.5 5 0.0680 0.9320 because female is 1 & pclass is 1st or 2nd 6 0.8291 0.1709 because female is 0 & age >= 9.5 > ptitanic1 <- NULL # delete the data, will affect male and sibsp in printed rules > print(rpart.rules(survived, roundint=FALSE)) # roundint=FALSE else warning survived 0.05 when female < 0.5 & age < 9.5 & sibsp >= 2.5 0.14 when female >= 0.5 & sibsp >= 2.5 & pclass is 3rd 0.17 when female < 0.5 & age >= 9.5 0.38 when female >= 0.5 & age >= 27.5 & sibsp < 2.5 & pclass is 3rd 0.39 when female >= 0.5 & age is 16.5 to 21.5 & sibsp < 2.5 & pclass is 3rd 0.61 when female >= 0.5 & age is 21.5 to 27.5 & sibsp < 2.5 & pclass is 3rd 0.73 when female >= 0.5 & age < 16.5 & sibsp < 2.5 & pclass is 3rd 0.89 when female < 0.5 & age < 9.5 & sibsp < 2.5 0.93 when female >= 0.5 & pclass is 1st or 2nd > data(iris) # multiclass model > Species <- rpart(Species ~ ., data=iris, method="class") > print(rpart.rules(Species)) Species seto vers virg setosa [1.00 .00 .00] when Petal.Length < 2.5 versicolor [ .00 .91 .09] when Petal.Length >= 2.5 & Petal.Width < 1.8 virginica [ .00 .02 .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8 > > proc.time() user system elapsed 0.32 0.14 0.40