2014-09-02 11 views
7

Sto lavorando a un progetto che richiede di eseguire uno ctree e quindi di tracciarlo in modalità interattiva, come il layout dell'albero "D3.js", il mio ostacolo principale è per convertire l'output ctree in un formato json, per un utilizzo successivo da javascript.Conversione dell'output ctree in formato JSON (per layout D3 dell'albero)

segue è quello che mi serve (con ad esempio dai dati iris):

> 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 

Ora voglio convertire il ctee uscita nel seguente formato JSON utilizzando qualche algoritmo (ho fatto manualmente), però, questo non è probabilmente il modo migliore per convertirlo:

{"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} 
    ]}] } 
     ]} 

Ecco due foto di entrambi R e D3.js piazzole:

enter image description here enter image description here

Ho già provato utilizzando RJSONIO sull'oggetto Ctree, che non ha aiutato molto.

Qualcuno ha mai convertito un oggetto/output ctree in JSON per l'utilizzo del layout dell'albero D3.js? in caso contrario, qualcuno ha qualche idea di un algoritmo in grado di convertire un output in un altro?

Grazie in anticipo per qualsiasi aiuto!

risposta

7

Il trucco è estrarre i bit utili dell'oggetto irisct e convertirli solo in JSON. Qualcosa di simile a questo:

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) 

ho pensato che questa risposta attraverso l'uso giudizioso della funzione unclass. Per esempio:

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

I metodi di stampa nel pacchetto, party:::print.SplittingNode e party:::print.TerminalNode erano anche molto utile. (Digitare party:::print. e completamento automatico per vedere quali sono disponibili.)