【发布时间】:2014-10-26 14:43:44
【问题描述】:
我正在处理一个需要运行ctree 然后以交互模式绘制它的项目——比如“D3.js”树布局,我的主要障碍是将ctree 输出转换为@ 987654325@ 格式,供javascript以后使用。
以下是我需要的(以虹膜数据为例):
> library(party)
> irisct <- ctree(Species ~ .,data = iris)
> irisct
Conditional inference tree with 4 terminal nodes
Response: Species
Inputs: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
Number of observations: 150
1) Petal.Length <= 1.9; criterion = 1, statistic = 140.264
2)* weights = 50
1) Petal.Length > 1.9
3) Petal.Width <= 1.7; criterion = 1, statistic = 67.894
4) Petal.Length <= 4.8; criterion = 0.999, statistic = 13.865
5)* weights = 46
4) Petal.Length > 4.8
6)* weights = 8
3) Petal.Width > 1.7
7)* weights = 46
现在我想使用某种算法(我手动完成)将ctee 输出转换为以下 JSON 格式,但这可能不是转换它的最佳方法:
{"name" : "Petal.Length <= 1.9 criterion = 1","value": 60, "children" : [
{"name" : "n=50" ,"value": 60},
{"name" : "Petal.Length > 1.9 criterion = 1","value": 60, "children": [
{"name" : "n=46","value": 60 },
{"name" : "Petal.Length > 4.8","value": 60, "children" :[
{"name" : "Petal.Width > 1.7" ,"value": 60},
{"name" : "46" ,"value": 60}
]}] }
]}
这是 R 和 D3.js 的两张图:
我已经尝试在 ctree 对象上使用 RJSONIO,但这并没有太大帮助。
有没有人将 ctree 对象/输出转换为 JSON 以使用 D3.js 树布局?如果没有,是否有人知道可以将一个输出转换为另一个输出的算法?
提前感谢您的帮助!
【问题讨论】:
标签: json r d3.js treeview decision-tree