X-Git-Url: https://git.auder.net/?p=agghoo.git;a=blobdiff_plain;f=R%2FR6_Model.R;h=96f892d4b8a3e9ec1c0e410f34b03652d907b5f9;hp=8fc232426ebaa0f0f2f1c06340a087df708e8012;hb=7b5193cdf5eb7041710c52368764feeacbb36a7c;hpb=c9c730bc6b9ca08dad41790a558776c47aed3ea1 diff --git a/R/R6_Model.R b/R/R6_Model.R index 8fc2324..96f892d 100644 --- a/R/R6_Model.R +++ b/R/R6_Model.R @@ -73,9 +73,15 @@ Model <- R6::R6Class("Model", function(dataHO, targetHO, param) { require(rpart) method <- ifelse(task == "classification", "class", "anova") + if (is.null(colnames(dataHO))) + colnames(dataHO) <- paste0("V", 1:ncol(dataHO)) df <- data.frame(cbind(dataHO, target=targetHO)) model <- rpart::rpart(target ~ ., df, method=method, control=list(cp=param)) - function(X) predict(model, X) + function(X) { + if (is.null(colnames(X))) + colnames(X) <- paste0("V", 1:ncol(X)) + predict(model, as.data.frame(X)) + } } } else if (family == "rf") { @@ -115,18 +121,17 @@ Model <- R6::R6Class("Model", require(rpart) df <- data.frame(cbind(data, target=target)) ctrl <- list( + cp = 0, minsplit = 2, minbucket = 1, - maxcompete = 0, - maxsurrogate = 0, - usesurrogate = 0, - xval = 0, - surrogatestyle = 0, - maxdepth = 30) + xval = 0) r <- rpart(target ~ ., df, method="class", control=ctrl) cps <- r$cptable[-1,1] - if (length(cps) <= 11) + if (length(cps) <= 11) { + if (length(cps == 0)) + stop("No cross-validation possible: select another model") return (cps) + } step <- (length(cps) - 1) / 10 cps[unique(round(seq(1, length(cps), step)))] }