Thanks to the the following question: Converting toJson R object into a format that fits d3.js tree layout
I have this great code that creates a Json file for d3.js (see below)
I know how to extract split points from a ctree
as following:
library(party)
irisct <- ctree(Species ~ .,data = iris)
I just dont know how to implement it in the following code:
#convert to Json that fits to d3.js layout####
get_ctree_parts <- function(x, ...)
{
UseMethod("get_ctree_parts")
}
get_ctree_parts.BinaryTree <- function(x, ...)
{
get_ctree_parts(attr(x, "tree"))
}
get_ctree_parts.SplittingNode <- function(x, ...)
{
with(
x,
list(
name = toString(nodeID),
criteria=attr(psplit$splitpoint, "levels"),
children = list(get_ctree_parts(x$left),get_ctree_parts(x$right))
)
)
}
get_ctree_parts.TerminalNode <- function(x, ...)
{
with(
x,
list(
name = paste(nodeID,
"weights",sum(weights),
"prediction",toString(paste("",toString(round(prediction,3)),"",sep=" ")),
# "criteria split",paste((attr(toString(psplit$splitpoint,levels)))),
sep = " ")
)
)
}
toJSON(get_ctree_parts(irisct))
output:
{"name":["1"],"criteria":{},"children":[{"name":["2 weights 50 prediction 1, 0, 0 "]},{"name":["3"],"criteria":{},"children":[{"name":["4"],"criteria":{},"children":[{"name":["5 weights 46 prediction 0, 0.978, 0.022 "]},{"name":["6 weights 8 prediction 0, 0.5, 0.5 "]}]},{"name":["7 weights 46 prediction 0, 0.022, 0.978 "]}]}]}
Notice that the "criteria" are left blank : {}
while i want them filled with all the levels.
Any help on that would be great !