2014-09-02 87 views
7

我正在開發一個需要運行ctree然後以交互模式繪製它的項目 - 就像'D3.js'樹佈局一樣,我的主要障礙是將ctree輸出轉換爲json格式,稍後由javascript使用。將ctree輸出轉換爲JSON格式(用於D3樹佈局)

以下是我所需要的(與例如從虹膜數據):

> 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圖:

enter image description here enter image description here

我已經嘗試在ctree對象上使用RJSONIO,這並沒有多大幫助。

有沒有人將ctree對象/輸出轉換爲JSON以使用D3.js樹佈局?如果沒有,沒有人有任何想法的算法,可以將一個輸出轉換爲另一個?

在此先感謝您的幫助!

回答

7

訣竅是提取irisct對象的有用位,並且只將它們轉換爲JSON。事情是這樣的:

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(
     nodeID  = nodeID, 
     variableName = psplit$variableName, 
     splitPoint = psplit$splitpoint, 
     pValue  = 1 - round(criterion$maxcriterion, 3), 
     statistic = round(max(criterion$statistic), 3), 
     left   = get_ctree_parts(x$left), 
     right  = get_ctree_parts(x$right) 
    ) 
) 
} 

get_ctree_parts.TerminalNode <- function(x, ...) 
{ 
    with(
    x, 
    list(
     nodeID  = nodeID, 
     weights = sum(weights), 
     prediction = prediction 
    ) 
) 
} 

useful_bits_of_irisct <- get_ctree_parts(irisct) 
toJSON(useful_bits_of_irisct) 

我想這個答案了通過明智使用unclass功能。例如:

unclass(irisct) 
unclass(attr(irisct, "tree")) 
unclass(attr(irisct, "tree")$psplit) 

包中的打印方法,party:::print.SplittingNodeparty:::print.TerminalNode也非常有用的。 (輸入party:::print.並自動完成以查看可用內容。)