2014-09-02 7 views
7

मैं एक परियोजना पर काम कर रहा हूं जिसके लिए ctree चलाने की आवश्यकता है और फिर इसे इंटरैक्टिव मोड में प्लॉट करें - जैसे 'D3.js' पेड़ लेआउट, मेरी मुख्य बाधा है ctree आउटपुट को json स्वरूप में परिवर्तित करने के लिए, बाद में जावास्क्रिप्ट द्वारा उपयोग करने के लिए।जेएसओएन प्रारूप (डी 3 पेड़ लेआउट के लिए) में ctree आउटपुट को कनवर्ट करना

के बाद किया जाता है क्या मैं (आईरिस डेटा से उदाहरण के साथ) की जरूरत है:

> 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 

अब मैं कुछ कलन विधि का उपयोग निम्नलिखित JSON प्रारूप में ctee उत्पादन परिवर्तित करना चाहते हैं (मैं इसे मैन्युअल रूप से किया था), हालांकि,

0123:

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

यहाँ और D3.js भूखंडों दोनों आर के दो तस्वीरें हैं: यह शायद सबसे अच्छा तरीका यह कन्वर्ट करने के लिए नहीं है enter image description here

मैं पहले से ctree वस्तु, कि ज्यादा मदद नहीं की पर RJSONIO उपयोग करने की कोशिश।

क्या किसी ने कभी भी डी 3.जेएस पेड़ लेआउट के उपयोग के लिए JSON में ctree ऑब्जेक्ट/आउटपुट को परिवर्तित किया है? यदि नहीं, तो क्या किसी को एल्गोरिदम का कोई विचार है जो एक आउटपुट को दूसरे में परिवर्तित कर सकता है?

किसी भी मदद के लिए अग्रिम धन्यवाद!

उत्तर

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.SplittingNode और party:::print.TerminalNode भी बहुत उपयोगी थे। (party:::print. टाइप करें और यह देखने के लिए स्वतः पूर्ण है कि क्या उपलब्ध है।)

संबंधित मुद्दे