From 00500dc70dacd39281e3ffbc9402bfc6b319cec5 Mon Sep 17 00:00:00 2001 From: talegari Date: Thu, 26 Oct 2023 23:41:18 +0530 Subject: [PATCH 1/4] added depvar to result --- R/ranger.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/ranger.R b/R/ranger.R index 54a18342..a5a7aba9 100644 --- a/R/ranger.R +++ b/R/ranger.R @@ -146,6 +146,7 @@ ##' \item{\code{importance.mode}}{Importance mode used.} ##' \item{\code{num.samples}}{Number of samples.} ##' \item{\code{inbag.counts}}{Number of times the observations are in-bag in the trees.} +##' \item{\code{dependent.variable.name}}{Name of the dependent variable. This is NULL when x/y interface is used.} ##' @examples ##' ## Classification forest with default settings ##' ranger(Species ~ ., data = iris) @@ -276,6 +277,7 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL, stop("Error: Invalid formula.") } data.selected <- parse.formula(formula, data, env = parent.frame()) + dependent.variable.name = names(data.selected)[1] y <- data.selected[, 1] x <- data.selected[, -1, drop = FALSE] } @@ -974,6 +976,10 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL, } } + ## slot: dependent.variable.name + ## will be NULL only when x/y interface is used + result$dependent.variable.name = dependent.variable.name + class(result) <- "ranger" ## Prepare quantile prediction From fe888b8ebdc14a6ea1542438e84e633d88c07388 Mon Sep 17 00:00:00 2001 From: Marvin Wright Date: Tue, 7 Nov 2023 22:39:01 +0100 Subject: [PATCH 2/4] also save name of status variable name for survival --- R/ranger.R | 11 ++++++++--- man/ranger.Rd | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/R/ranger.R b/R/ranger.R index 12b4db5f..9766a356 100644 --- a/R/ranger.R +++ b/R/ranger.R @@ -148,6 +148,7 @@ ##' \item{\code{num.samples}}{Number of samples.} ##' \item{\code{inbag.counts}}{Number of times the observations are in-bag in the trees.} ##' \item{\code{dependent.variable.name}}{Name of the dependent variable. This is NULL when x/y interface is used.} +##' \item{\code{status.variable.name}}{Name of the status variable (survival only). This is NULL when x/y interface is used.} ##' @examples ##' ## Classification forest with default settings ##' ranger(Species ~ ., data = iris) @@ -278,7 +279,10 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL, stop("Error: Invalid formula.") } data.selected <- parse.formula(formula, data, env = parent.frame()) - dependent.variable.name = names(data.selected)[1] + dependent.variable.name <- all.vars(formula)[1] + if (is.Surv(data.selected[, 1])) { + status.variable.name <- all.vars(formula)[2] + } y <- data.selected[, 1] x <- data.selected[, -1, drop = FALSE] } @@ -1004,9 +1008,10 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL, } } - ## slot: dependent.variable.name + ## Dependent (and status) variable name ## will be NULL only when x/y interface is used - result$dependent.variable.name = dependent.variable.name + result$dependent.variable.name <- dependent.variable.name + result$status.variable.name <- status.variable.name class(result) <- "ranger" diff --git a/man/ranger.Rd b/man/ranger.Rd index ff202a07..361200ae 100644 --- a/man/ranger.Rd +++ b/man/ranger.Rd @@ -152,6 +152,8 @@ Object of class \code{ranger} with elements \item{\code{importance.mode}}{Importance mode used.} \item{\code{num.samples}}{Number of samples.} \item{\code{inbag.counts}}{Number of times the observations are in-bag in the trees.} + \item{\code{dependent.variable.name}}{Name of the dependent variable. This is NULL when x/y interface is used.} + \item{\code{status.variable.name}}{Name of the status variable (survival only). This is NULL when x/y interface is used.} } \description{ Ranger is a fast implementation of random forests (Breiman 2001) or recursive partitioning, particularly suited for high dimensional data. From 1bacb3f2594529a322c8e1d658ebc39478f5cec7 Mon Sep 17 00:00:00 2001 From: Marvin Wright Date: Tue, 7 Nov 2023 22:50:41 +0100 Subject: [PATCH 3/4] is.Surv is in survival package --- R/ranger.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ranger.R b/R/ranger.R index 9766a356..ee24e8bc 100644 --- a/R/ranger.R +++ b/R/ranger.R @@ -280,7 +280,7 @@ ranger <- function(formula = NULL, data = NULL, num.trees = 500, mtry = NULL, } data.selected <- parse.formula(formula, data, env = parent.frame()) dependent.variable.name <- all.vars(formula)[1] - if (is.Surv(data.selected[, 1])) { + if (survival::is.Surv(data.selected[, 1])) { status.variable.name <- all.vars(formula)[2] } y <- data.selected[, 1] From 0bf78762907e187b9c94eedd90de3200fc18b4e3 Mon Sep 17 00:00:00 2001 From: Marvin Wright Date: Tue, 7 Nov 2023 22:50:50 +0100 Subject: [PATCH 4/4] update tests --- tests/testthat/test_classification.R | 4 ++-- tests/testthat/test_print.R | 2 +- tests/testthat/test_regression.R | 4 ++-- tests/testthat/test_survival.R | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test_classification.R b/tests/testthat/test_classification.R index 9f219cf0..0d015c9d 100644 --- a/tests/testthat/test_classification.R +++ b/tests/testthat/test_classification.R @@ -10,9 +10,9 @@ rg.class <- ranger(Species ~ ., data = iris) rg.mat <- ranger(dependent.variable.name = "Species", data = dat, classification = TRUE) ## Basic tests (for all random forests equal) -test_that("classification result is of class ranger with 14 elements", { +test_that("classification result is of class ranger with 15 elements", { expect_is(rg.class, "ranger") - expect_equal(length(rg.class), 14) + expect_equal(length(rg.class), 15) }) test_that("classification prediction returns factor", { diff --git a/tests/testthat/test_print.R b/tests/testthat/test_print.R index fd9f3512..3ca91b4a 100644 --- a/tests/testthat/test_print.R +++ b/tests/testthat/test_print.R @@ -16,7 +16,7 @@ expect_that(print(rf$forest), prints_text("Ranger forest object")) expect_that(print(predict(rf, iris)), prints_text("Ranger prediction")) ## Test str ranger function -expect_that(str(rf), prints_text("List of 14")) +expect_that(str(rf), prints_text("List of 15")) ## Test str forest function expect_that(str(rf$forest), prints_text("List of 9")) diff --git a/tests/testthat/test_regression.R b/tests/testthat/test_regression.R index 8bb3d11a..dd3bdd4e 100644 --- a/tests/testthat/test_regression.R +++ b/tests/testthat/test_regression.R @@ -7,9 +7,9 @@ context("ranger_reg") rg.reg <- ranger(Sepal.Length ~ ., data = iris) ## Basic tests (for all random forests equal) -test_that("regression result is of class ranger with 14 elements", { +test_that("regression result is of class ranger with 15 elements", { expect_is(rg.reg, "ranger") - expect_equal(length(rg.reg), 14) + expect_equal(length(rg.reg), 15) }) test_that("regression prediction returns numeric vector", { diff --git a/tests/testthat/test_survival.R b/tests/testthat/test_survival.R index 6676d6e0..6226eb6f 100644 --- a/tests/testthat/test_survival.R +++ b/tests/testthat/test_survival.R @@ -8,9 +8,9 @@ context("ranger_surv") rg.surv <- ranger(Surv(time, status) ~ ., data = veteran, num.trees = 10) ## Basic tests (for all random forests equal) -test_that("survival result is of class ranger with 15 elements", { +test_that("survival result is of class ranger with 17 elements", { expect_is(rg.surv, "ranger") - expect_equal(length(rg.surv), 15) + expect_equal(length(rg.surv), 17) }) test_that("results have right number of trees", {