Find the data elements in a data frame that pass the rule for a node in a tree model?
I modified the code in path.rpart
to return the subset of the data that falls within a particular node rather than returning information about that node. It works by either clicking on the plot or by passing nodes just as the path.rpart
function does. Here is the code
subset.rpart <- function (tree, df, nodes) {
if (!inherits(tree, "rpart"))
stop("Not a legitimate \"rpart\" object")
stopifnot(nrow(df)==length(tree$where))
frame <- tree$frame
n <- row.names(frame)
node <- as.numeric(n)
if (missing(nodes)) {
xy <- rpart:::rpartco(tree)
i <- identify(xy, n = 1L, plot = FALSE)
if(i> 0L) {
return( df[tree$where==i, ] )
} else {
return(df[0,])
}
}
else {
if (length(nodes <- rpart:::node.match(nodes, node)) == 0L)
return(df[0,])
return ( df[tree$where %in% as.numeric(nodes), ] )
}
}
I will use it on some sample data from the package
fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
plot(fit)
text(fit)
And then to find the observations at a particular node, run
subset.rpart(fit, kyphosis)
and click on a node on the plot. After you do, all the observations at that node will be returned. You must use the same data.frame
that was used for modeling for this to work properly. Rather than clicking on a point, you can also pass in a node name that you you discover with path.rpart
# path.rpart(fit)
# node number: 10 ---> looks interesting
# root
# Start>=8.5
# Start< 14.5
# Age< 55
subset.rpart(fit, kyphosis, 10)
# Kyphosis Age Number Start
# 14 absent 1 4 12
# 20 absent 27 4 9
# 26 absent 9 5 13
# 37 absent 1 3 9
# 39 absent 20 6 9
# 42 absent 35 3 13
# 57 absent 2 3 13
# 59 absent 51 7 9
# 66 absent 17 4 10
# 69 absent 18 4 11
# 78 absent 26 7 13
# 81 absent 36 4 13
Extracting regression tree models built with R rpart
I believe what you are trying to do can be done using a little hack of the Rattle package/GUI. If you install Rattle then from the command line use the asRules()
function on your rpart fit object and you will get back a human readable rules set. I have used these rules to quickly convert to SQL or other languages in seconds. I hope this helps.
rpart node assignment
Try using the partykit package:
library(rpart)
z.auto <- rpart(Mileage ~ Weight, car.test.frame)
library(partykit)
z.auto2 <- as.party(z.auto)
predict(z.auto2, car.test.frame[1:3,], type = "node")
# Eagle Summit 4 Ford Escort 4 Ford Festiva 4
# 7 7 7
How can I get the data in one terminal node of a decision tree in RPART
Try this:
fit$where[fit$where==3] # for Node 3
# Hornet 4 Drive Hornet Sportabout Valiant Duster 360 Merc 240D Merc 280 Merc 280C
# 3 3 3 3 3 3 3
# Merc 450SE Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental Chrysler Imperial Dodge Challenger
# 3 3 3 3 3 3 3
# AMC Javelin Camaro Z28 Pontiac Firebird
3 3 3
length(fit$where[fit$where==3])
#[1] 17
mtcars[rownames(mtcars) %in% names(fit$where[fit$where==3]),]
# mpg cyl disp hp drat wt qsec vs am gear carb x
#Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 4
#Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 5
#Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 6
#Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 7
#Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8
#Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 10
#Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 11
#Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 12
#Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 13
#Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
#Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 15
#Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 16
#Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 17
#Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 22
#AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 23
#Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 24
#Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 25
compute numbers of leaf in rpart
The fit
object has all the information that you need. You can examine it using str(fit)
.
Two ways to find the number of leaves are:
sum(fit$frame$ncompete == 0)
[1] 11
AND
sum(fit$frame$var == "<leaf>")
[1] 11
Related Topics
Assigning and Removing Objects in a Loop: Eval(Parse(Paste(
How to Find Which Polygon a Point Belong to via Sf
Modify Lm or Loess Function to Use It Within Ggplot2's Geom_Smooth
Select Rows in a Dataframe in R Based on Values in One Row
How to Install the Odbc Driver for Snowflake Successfully on an M1 Apple Silicon MAC
How to Read a Subset of Large Dataset in R
How to Classify a Given Date/Time by the Season (E.G. Summer, Autumn)
Labelling the Plots with Images on Graph in Ggplot2
Adding Missing Dates to Dataframe
How to Check If Each Element in a Vector Is Integer or Not in R
Using Melt with Matrix or Data.Frame Gives Different Output
How to Reverse the Order of a Dataframe in R
Tiny Plot Output from Sankeynetwork (Networkd3) in Firefox
How to Control the Canvas Size in Ggplot
Organize Text on Geom_Point Using Geom_Text
"Error: Continuous Value Supplied to Discrete Scale" in Default Data Set Example Mtcars and Ggplot2
How to Add Main Title and Manipulating Axis Labels in Ggplot2 in Rstudio