将ctree输出转换为JSON格式(用于D3树布局)

 周周微商互联 发布于 2022-12-23 19:22

我正在开发一个需要运行a ctree然后以交互模式绘制它的项目- 比如'D3.js'树布局,我的主要障碍是将ctree输出转换为json格式,以后由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图的两张图片:

在此输入图像描述 在此输入图像描述

我已经尝试过使用RJSONIOctree对象,这没有多大帮助.

有没有人曾将ctree对象/输出转换为JSON以使用D3.js树布局?如果没有,有没有人知道一个算法可以将一个输出转换为另一个?

在此先感谢您的帮助!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有