r - extracting predictors from ctree object -


i've checked binary tree class methods, , how extract tree structure ctree function? (which helpful understanding s4 object structure , slots), it's still unclear how final predictors of ctree object. rpart, i'd use like

 extract_preds <- function( tt ){    leaves <- tt$frame$var == '<leaf>'    as.character( unique( tt$frame$var[ leaves==f ] ) )  } 

is there similar shortcut available, or have write recursive function traverse ctree object , extract predictors? that, or regex-fest print output? thanks.

update: using baydoganm's code below. still have figure out how update res through recursions:

 library(party)   ctree_preds <- function(tr,vnames){         res <- character(0)     traverse <- function(treenode,vnames,res){     if(treenode$terminal){         return(res)     } else {         res <- c(res,vnames[treenode$psplit$variableid])         traverse(treenode$left , vnames, res )         traverse(treenode$right, vnames, res )         }     }     traverse(tr,vnames,res)     return(unique(res))  }   airq <- subset(airquality, !is.na(ozone))  airct <- ctree(ozone ~ ., data = airq,                          controls = ctree_control(maxsurrogate = 3))  plot(airct)   ctree_preds(airct@tree,names(airq)[-1]) 

below script implemented traverse tree ctree object. use same example in party package airct dataset.

require(party) data(airquality)  traverse <- function(treenode){     if(treenode$terminal){         bas=paste("current node terminal node with",treenode$nodeid,'prediction',treenode$prediction)         print(bas)         return(0)     } else {         bas=paste("current node",treenode$nodeid,"split var. id:",treenode$psplit$variablename,"split value:",treenode$psplit$splitpoint,'prediction',treenode$prediction)         print(bas) } traverse(treenode$left) traverse(treenode$right) }  airq <- subset(airquality, !is.na(ozone)) airct <- ctree(ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3)) plot(airct)  traverse(airct@tree) 

this function, traverse, traverses tree in depth-first order. can change order of traversal changing recursive part.

moreover, if want return other node characteristics, recommend checking structure of ctree object.

edit: minor code revisions.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -