Diff of /R/RpartEVAL.R [000000] .. [28e211]

Switch to unified view

a b/R/RpartEVAL.R
1
#' @title Evaluating the performance of the RPART Decision Tree.
2
#' @description This function evaluates the performance of the generated trees
3
#'   for error estimation by ten-fold cross validation assessment.
4
#' @export
5
#' @param data The resulted data from running the function J48DT.
6
#' @param num.folds A numeric value of the number of folds for the cross
7
#'   validation assessment. Default is 10.
8
#' @param First A string vector showing the first target cluster.  Default is
9
#'   "CL1"
10
#' @param Second A string vector showing the second target cluster.  Default is
11
#'   "CL2"
12
#' @param quiet If `TRUE`, suppresses intermediary output
13
#' @importFrom stats predict
14
#' @return Performance statistics of the model
15
RpartEVAL <- function(data, num.folds = 10, First = "CL1", Second = "CL2",
16
                      quiet = FALSE) {
17
  exp.imput.df <- as.data.frame(t(data))
18
  num.instances <- nrow(exp.imput.df)
19
  indices <- 1:num.instances
20
  classVector <- factor(colnames(data))
21
  cv.segments <- split(
22
    sample(indices), rep(1:num.folds, length = num.instances)
23
  )
24
  Rpart.performance <- cross.val(
25
    exp.imput.df, classVector, cv.segments, Rpart.performance, "rpart", quiet
26
  )
27
  if (!quiet) print(Rpart.performance)
28
  Rpart.confusion.matrix <- matrix(Rpart.performance, nrow = 2)
29
  rownames(Rpart.confusion.matrix) <- c(
30
    paste0("Predicted", First), paste0("Predicted", Second)
31
  )
32
  colnames(Rpart.confusion.matrix) <- c(First, Second)
33
  if (!quiet) print(Rpart.confusion.matrix)
34
  Rpart.sn <- round(SN(Rpart.confusion.matrix), digits = 2)
35
  Rpart.sp <- round(SP(Rpart.confusion.matrix), digits = 2)
36
  Rpart.acc <- round(ACC(Rpart.confusion.matrix), digits = 2)
37
  Rpart.mcc <- round(MCC(Rpart.confusion.matrix), digits = 2)
38
39
  if (!quiet) {
40
    message(
41
      "Rpart SN: ", Rpart.sn, "\n",
42
      "Rpart SP: ", Rpart.sp, "\n",
43
      "Rpart ACC: ", Rpart.acc, "\n",
44
      "Rpart MCC: ", Rpart.mcc, "\n"
45
    )
46
  }
47
  return(Rpart.performance)
48
}