I'm using R to make a regression tree model:
library(party)
mydata = read.csv(“data.csv”)
TreeModel = ctree(price ~., data = mydata)
I would like to extract the nodes of the tree from the root to any of the leaves. but I couldn't find any function to do that. For example if the tree is like below: I want to get the path from root to any leaves. So for the fist leaf from right< i want to get a path like (Koc, AirDecat, OTW, OTW, AirDecay) and for the left most leaf , it should be (Koc, AirDecay).Any hint is really appreciated.
As MrFlick noted, you should provide a reproducible example.
This might get you started on how to find the path of a BinaryTree object of the party package:
library(data.tree)
library(party)
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
CreateNodeFromParty <- function(splitNode) {
node <- Node$new(splitNode$nodeID,
weights = splitNode$weights,
criterion = splitNode$criterion,
psplit = splitNode$psplit)
if (!splitNode$terminal) {
node$AddChildNode( CreateNodeFromParty(splitNode$left) )
node$AddChildNode( CreateNodeFromParty(splitNode$right) )
}
return (node)
}
tree <- CreateNodeFromParty(airct@tree)
tree
This will give you the data.tree structure:
levelName
1 1
2 ¦--2
3 ¦ ¦--3
4 ¦ °--4
5 ¦ ¦--5
6 ¦ °--6
7 °--7
8 ¦--8
9 °--9
To find a specific node, do:
tree$FindNode(6)$path
Which will give you:
[1] "1" "2" "4" "6"