ctree function in party package - how to get the list of the splitting variable in a simple way

2k views Asked by At

I am trying to get the list of the splitting variable of a tree when using ctree.

I found a way to get a specific splitting variable of a specific node using psplit. But I would like to get the whole list of the splitting variables in the entire tree.

Is there any simple way to get it?

2

There are 2 answers

1
useR On
var.ctree<-function(fit){

  require(gdata)

  a<-capture.output(print(fit))  
  a<-a[-c(1:7)]   
  a<-trim(a)   

  v<-character()   
  for(h in 1:length(a)){
    b<-a[h]    
    b<-gsub(") ","q",b)    
    b<-gsub(")","q",b)    
    b<-gsub(" < ","q",b)    
    b<-gsub(" > ","q",b)    
    b<-gsub(" <= ","q",b)    
    b<-gsub(" <= ","q",b)    
    b<-gsub("  weights = ","q",b)    
    v[h]<-unlist(strsplit(b,"q"))[2]    
  }    
  v<-factor(v,levels=names(fit@data@get("input")))    
  v<-v[is.na(v)==FALSE]    
  tabla<-table(v)>0    
  sol<-as.numeric(table(v)>0)    
  names(sol)<-names(tabla)    
  sol    
}    
0
David Arenburg On

This will do the job

SplittedNames <- function(ctemp ) {
  intnodes <- unique(where(ctemp))
  intnodes <- sort(intnodes)
  diffnodes <- seq(1:intnodes[length(intnodes)])
  primenodes <- setdiff(diffnodes,intnodes)
  split.names <- vector()

  for (i in primenodes){
    temp <- unlist(nodes(ctemp,i)[[1]][[5]])
    split.names <- append(split.names, as.character(temp[length(temp)]))
  }

  return(paste(unique(split.names), collapse = ', '))  
}

Testing the code:

airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3))
SplittedNames(airct)
[1] "Temp, Wind"