2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#' Load Random Forest
|
|
|
|
#'
|
2019-06-06 22:53:25 +00:00
|
|
|
#' Loads a random forest that was saved using \code{\link{saveForest}}.
|
2019-05-31 22:13:24 +00:00
|
|
|
#'
|
2019-06-30 22:07:29 +00:00
|
|
|
#' @param directory The directory created that saved the previous forest.
|
2019-05-31 22:13:24 +00:00
|
|
|
#' @return A JForest object; see \code{\link{train}} for details.
|
|
|
|
#' @export
|
2019-06-30 22:07:29 +00:00
|
|
|
#' @seealso \code{\link{train}}, \code{\link{saveForest}}
|
2019-05-31 22:13:24 +00:00
|
|
|
#' @examples
|
|
|
|
#' # Regression Example
|
|
|
|
#' x1 <- rnorm(1000)
|
|
|
|
#' x2 <- rnorm(1000)
|
|
|
|
#' y <- 1 + x1 + x2 + rnorm(1000)
|
|
|
|
#'
|
|
|
|
#' data <- data.frame(x1, x2, y)
|
|
|
|
#' forest <- train(y ~ x1 + x2, data,
|
|
|
|
#' ntree=100, numberOfSplits = 5, mtry = 1, nodeSize = 5)
|
|
|
|
#'
|
2019-06-06 22:53:25 +00:00
|
|
|
#' saveForest(forest, "trees")
|
|
|
|
#' new_forest <- loadForest("trees")
|
|
|
|
loadForest <- function(directory){
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
# First load the response combiners and the split finders
|
|
|
|
nodeResponseCombiner.java <- .jcall(.class_DataUtils, makeResponse(.class_Object), "loadObject", paste0(directory, "/nodeResponseCombiner.jData"))
|
|
|
|
nodeResponseCombiner.java <- .jcast(nodeResponseCombiner.java, .class_ResponseCombiner)
|
|
|
|
|
|
|
|
splitFinder.java <- .jcall(.class_DataUtils, makeResponse(.class_Object), "loadObject", paste0(directory, "/splitFinder.jData"))
|
|
|
|
splitFinder.java <- .jcast(splitFinder.java, .class_SplitFinder)
|
|
|
|
|
|
|
|
forestResponseCombiner.java <- .jcall(.class_DataUtils, makeResponse(.class_Object), "loadObject", paste0(directory, "/forestResponseCombiner.jData"))
|
|
|
|
forestResponseCombiner.java <- .jcast(forestResponseCombiner.java, .class_ResponseCombiner)
|
|
|
|
|
|
|
|
covariateList <- .jcall(.class_DataUtils, makeResponse(.class_Object), "loadObject", paste0(directory, "/covariateList.jData"))
|
|
|
|
covariateList <- .jcast(covariateList, .class_List)
|
|
|
|
|
|
|
|
params <- readRDS(paste0(directory, "/parameters.rData"))
|
|
|
|
call <- readRDS(paste0(directory, "/call.rData"))
|
|
|
|
|
|
|
|
params$nodeResponseCombiner$javaObject <- nodeResponseCombiner.java
|
|
|
|
params$splitFinder$javaObject <- splitFinder.java
|
|
|
|
params$forestResponseCombiner$javaObject <- forestResponseCombiner.java
|
|
|
|
|
2019-06-06 22:53:25 +00:00
|
|
|
forest <- loadForestArgumentsSpecified(directory, params$nodeResponseCombiner, params$splitFinder, params$forestResponseCombiner, covariateList, call,
|
2019-06-19 20:14:11 +00:00
|
|
|
params$ntree, params$numberOfSplits, params$mtry, params$nodeSize, params$maxNodeDepth, params$splitPureNodes, params$randomSeed)
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
return(forest)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-06 22:53:25 +00:00
|
|
|
# Internal function - if you really need to use it yourself (say to load forests
|
|
|
|
# saved directly through the Java interface into R), then look at the loadForest
|
|
|
|
# function to see how this function is used. I'm also open to writing a function
|
|
|
|
# that uses the Java version's settings yaml file to recreate the forest, but
|
|
|
|
# I'd appreciate knowing that someone's going to use it first (email me; see
|
|
|
|
# README).
|
|
|
|
loadForestArgumentsSpecified <- function(treeDirectory, nodeResponseCombiner, splitFinder, forestResponseCombiner,
|
2019-06-19 20:14:11 +00:00
|
|
|
covariateList.java, call, ntree, numberOfSplits, mtry, nodeSize, maxNodeDepth = 100000, splitPureNodes=TRUE, randomSeed=NULL){
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
params <- list(
|
|
|
|
splitFinder=splitFinder,
|
|
|
|
nodeResponseCombiner=nodeResponseCombiner,
|
|
|
|
forestResponseCombiner=forestResponseCombiner,
|
|
|
|
ntree=ntree,
|
|
|
|
numberOfSplits=numberOfSplits,
|
|
|
|
mtry=mtry,
|
|
|
|
nodeSize=nodeSize,
|
|
|
|
splitPureNodes=splitPureNodes,
|
2019-06-19 20:14:11 +00:00
|
|
|
maxNodeDepth=maxNodeDepth,
|
|
|
|
randomSeed=randomSeed
|
2019-05-31 22:13:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
forest.java <- .jcall(.class_DataUtils, makeResponse(.class_Forest), "loadForest", treeDirectory, forestResponseCombiner$javaObject)
|
|
|
|
|
|
|
|
forestObject <- list(call=call, javaObject=forest.java, covariateList=covariateList.java, params=params)
|
|
|
|
class(forestObject) <- "JRandomForest"
|
|
|
|
|
|
|
|
return(forestObject)
|
|
|
|
|
|
|
|
}
|