[R] multiple trees

Weiwei Shi helprhelp at yahoo.com
Thu Jan 6 01:29:09 CET 2005


Hi, there:
I made a function to do k-fold cross-validation as
below. Basically whenever I call cv(test) for example,
an error message like:
20Fold  1 
Error in model.frame(formula, rownames, variables,
varnames, extras, extranames,  : 
        variable lengths differ

please help. 

My test dataset has 142 variables, the last one is a
categorical response variable.
also, i am not sure how to save the trees into a list
or something so that I can handle, like pointer array
or something in C.

Thanks.

Weiwei Shi, Ph.D

cv<- function(all.data,n.folds=10,mcp=0.003) {

 n <- nrow(all.data)
 idx <- sample(n,n)
 all.data <- all.data[idx,]

 n.each.part <- as.integer(n/n.folds)
 r.model<- vector()
 r.model.prune<- vector()

 for(i in 1:n.folds) {
 cat('Fold ',i,'\n')
 out.fold <- ((i-1)*n.each.part+1):(i*n.each.part)
 tmp<-all.data[-(out.fold),1:141]
 r.model[i]<- rpart(all.data$V142~., data=tmp,
parms=list(split='gini'), cp=0)
 #r.model.prune[i]<-prune(r.model[i], cp=mcp)

 }
return (r.model)
}




More information about the R-help mailing list