@@ -626,6 +626,68 @@ prior = object$prior, method = c("plug-in", "predictive", "debiased", "looCV",
626626 stop(" unrecognized 'type' (must be 'class', 'membership' or 'both')" ))
627627}
628628
629+ # rpart() from {rpart}
630+ mlRpart <- function (train , ... )
631+ UseMethod(" mlRpart" )
632+
633+ mlRpart.formula <- function (formula , data , ... , subset , na.action ) {
634+ mlearning(formula , data = data , method = " mlRpart" , model.args =
635+ list (formula = formula , data = substitute(data ),
636+ subset = substitute(subset )), call = match.call(), ... ,
637+ subset = subset , na.action = substitute(na.action ))
638+ }
639+
640+ mlRpart.default <- function (train , response , ... , .args. = NULL ) {
641+ dots <- list (... )
642+ if (is.null(.args. ) || ! length(.args. )) {
643+ if (! length(response )) {# unsupervised
644+ stop(" Unsupervised classification is not possible with mlRpart(), see hclust() instead." )
645+ } else if (is.factor(response )) {
646+ type <- " classification"
647+ } else type <- " regression"
648+
649+ .args. <- list (levels = levels(response ),
650+ n = c(intial = NROW(train ), final = NROW(train )),
651+ type = type , na.action = " na.pass" ,
652+ mlearning.call = match.call(), method = " mlRpart" )
653+ }
654+
655+ # Combine train + response and use the formula interface which is the only one
656+ data <- train
657+ data $ .class <- response
658+ res <- rpart :: rpart(.class ~ . , data = data , ... )
659+ res $ predicted <- predict(res , type = " class" )
660+
661+ # Return a mlearning object
662+ structure(res , formula = .args. $ formula , train = train ,
663+ response = response , levels = .args. $ levels , n = .args. $ n , args = dots ,
664+ optim = .args. $ optim , numeric.only = FALSE , type = .args. $ type ,
665+ pred.type = c(class = " class" , membership = " prob" ),
666+ summary = NULL , na.action = .args. $ na.action ,
667+ mlearning.call = .args. $ mlearning.call , method = .args. $ method ,
668+ algorithm = " recursive partitioning tree" ,
669+ class = c(" mlRpart" , " mlearning" , " rpart" ))
670+ }
671+
672+ predict.mlRpart <- function (object , newdata ,
673+ type = c(" class" , " membership" , " both" ),
674+ method = c(" direct" , " cv" ), ... ) {
675+
676+ type <- as.character(type )[1 ]
677+
678+ # If method == "cv", delegate to cvpredict()
679+ method <- as.character(method )[1 ]
680+ if (method == " cv" ) {
681+ if (! missing(newdata ))
682+ stop(" cannot handle new data with method = 'cv'" )
683+ return (cvpredict(object = object , type = type , ... ))
684+ } else {
685+ predict.mlearning(object = object , newdata = newdata ,
686+ type = type , ... )
687+ }
688+ }
689+
690+
629691mlRforest <- function (train , ... )
630692 UseMethod(" mlRforest" )
631693
0 commit comments