R/gbm-accessors.r

Defines functions distribution_name.GBMDist distribution_name.GBMFit distribution_name.default distribution_name iteration_error trees

Documented in distribution_name iteration_error trees

## Accessor methods for gbm fit objects

##' Extract trees from GBMFit objects
##'
##' @param obj a GBMFit object
##' @return a vector containing the trees generated in the fit
##' @export
trees <- function(obj) {
    check_if_gbm_fit(obj)
    obj$trees
}

##' Extract errors from GBMFit objects
##'
##' @param obj a GBMFit object
##' @param which which error measure to extract
##' @return a vector giving the error by tree
##' @export
iteration_error <- function(obj, which=c('train', 'valid', 'cv')) {
    check_if_gbm_fit(obj)
    switch(match.arg(which),
           train=obj$train.error,
           valid=obj$valid.error,
           cv=obj$cv_error,
           stop("Unknown error measure"))
}

##' What is the distribution name used here?
##'
##' @param obj the object for which the distribution name is needed
##' @param ... other parameters
##' @return a string identifying the distribution
##' @export
distribution_name <- function(obj, ...) {
    UseMethod("distribution_name")
}

##' @export
distribution_name.default <- function(obj, ...) {
    stop("I don't know how to handle this")
}

##' @export
distribution_name.GBMFit <- function(obj, ...) {
    distribution_name(obj$distribution)
}

##' @export
distribution_name.GBMDist <- function(obj, ...) {
    obj$name
}
gbm-developers/gbm3 documentation built on April 28, 2024, 10:04 p.m.