[R] ctree() crossvalidation

Franziska Rupprecht franziska.rupprecht at googlemail.com
Sat Mar 17 20:35:47 CET 2012


Dear all,

I use the function ctree() from the party library to calculate
classification tree models.
I want to validate models by 10-fold cross validation and estimate
mean and standard deviation of correct classification rates (CCR) from
the10 resulting confusion matrices.
So far I use the “write.table” command to export the 10 confusion matrices.
However I would rather estimate mean and standard deviations of CCR’s
for the three categories of the response variable directly in R from
the 10 confusion matrices.

for(i in 1:10){
# Randomly select 90% of observations to create training matrix,
# using remaining 10% for testing; AKA k-fold, where k = 10.
select2 <- sample(1:nrow(variablen), 0.9*nrow(variablen))
#fix(test2)
train2 <- variablen[select2,]
test2 <- variablen[-select2,]
# Fit model on training data
ctree.model <- ctree(TRANSITION ~ ., data = train2, controls =
ctree_control(mincriterion=0.99, teststat="quad",
testtype="Bonferroni", minsplit = 100, minbucket= 30, stump = FALSE,
nresample = 9999, maxsurrogate = 0, mtry = 0, savesplitstats = TRUE,
maxdepth = 4), xtrafo = ptrafo, ytrafo = ptrafo, scores = NULL)
# Create fitted values based on this model, but using test data
PredCtree <- cbind(predict(ctree.model, newdata=test2, type="class"))
# Create confusion matrix
crosstabEval <- table(test2[,1], PredCtree)
ConMa<-prop.table(crosstabEval, 1)
write.table(ConMa, "E:/ConMa10.txt", append = FALSE, quote = TRUE, sep = " ")
}

Thanks for your for advice!

Franziska



More information about the R-help mailing list