| 1 | #' @include b_Algorithm.R |
| 2 | |
| 3 | #' @title Regression Tree |
| 4 | #' |
| 5 | #' @description Regression Tree using the \code{tree} package. |
| 6 | #' Inherits \code{\link{Algorithm}} |
| 7 | #' |
| 8 | #' @field nleaf Number of leaf nodes after pruning. Default: Inf (no pruning) |
| 9 | #' |
| 10 | RegressionTree = setRefClass( |
| 11 | Class = "RegressionTree", |
| 12 | |
| 13 | fields = c( |
| 14 | nleaf = "numeric" |
| 15 | ), |
| 16 | |
| 17 | contains = "Algorithm", |
| 18 | |
| 19 | methods = list( |
| 20 | initialize = function(...) |
| 21 | { |
| 22 | callSuper(...) |
| 23 | if (length(nleaf) == 0 || nleaf < 1) |
| 24 | nleaf <<- Inf |
| 25 | }, |
| 26 | predict_noNA = function(XY, x) |
| 27 | { |
| 28 | require(tree, quietly=TRUE) |
| 29 | rt = tree(Measure ~ ., data=XY) |
| 30 | treeSize = sum( rt$frame[["var"]] == "<leaf>" ) |
| 31 | if (treeSize > nleaf) |
| 32 | rt = prune.tree(rt, best = nleaf) |
| 33 | return (stats::predict(rt, as.data.frame(x))) |
| 34 | } |
| 35 | ) |
| 36 | ) |