From 2b659779085e00723d9d4dbb407fd51c58f64a60 Mon Sep 17 00:00:00 2001 From: cjvanlissa Date: Tue, 24 Sep 2024 14:52:41 +0200 Subject: [PATCH 1/2] Add merge S3 method for varimp, to facilitate piecewise analyses --- R/mergeVarimp.R | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 R/mergeVarimp.R diff --git a/R/mergeVarimp.R b/R/mergeVarimp.R new file mode 100644 index 0000000..a77894d --- /dev/null +++ b/R/mergeVarimp.R @@ -0,0 +1,31 @@ +#' @exportS3Method merge semforest.varimp +merge.semforest.varimp <- function(x, y, ...){ + return(merge_internal_varimp(list(x, y))) +} + +merge_internal_varimp <- function (varimp_list) +{ + numtrees <- sapply(varimp_list, function(x){length(x$ll.baselines)}) + numfeatures <- sapply(varimp_list, function(x){dim(x$importance)[2]}) + if(length(unique(numfeatures)) > 1) stop("Not all semforest.varimp objects have the same number of variables.") + varnames <- sapply(varimp_list, `[[`, "var.names") + if(any(apply(varnames, 1, function(x){length(unique(x))}) > 1)) stop("Not all var.names are the same.") + out <- list( + ll.baselines = vector("numeric", sum(numtrees)), + importance = matrix(nrow = sum(numtrees), ncol = numfeatures[1]), + elapsed = varimp_list[[1]]$elapsed, + var.names = varimp_list[[1]]$var.names + ) + colnames(out$importance) <- colnames(varimp_list[[1]]$importance) + for (i in 2:length(varimp_list)) { + out$elapsed <- out$elapsed + varimp_list[[i]]$elapsed + } + index_trees <- c(0, numtrees, 0) + for (i in 1:length(varimp_list)) { + indcs <- (sum(index_trees[1:i])+1):sum(index_trees[1:i+1]) + out$ll.baselines[indcs] <- varimp_list[[i]]$ll.baselines + out$importance[indcs, 1:numfeatures[1]] <- varimp_list[[i]]$importance + } + class(out) <- class(varimp_list[[1]]) + return(out) +} \ No newline at end of file From 1493ecb2c3b9681d233b130e69189deb5b3fd817 Mon Sep 17 00:00:00 2001 From: Andreas Brandmaier Date: Mon, 30 Sep 2024 08:49:35 +0200 Subject: [PATCH 2/2] Update mergeVarimp.R - allow public method "merge.semforst.varimp" to take any number of arguments - renamed internal function --- R/mergeVarimp.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/mergeVarimp.R b/R/mergeVarimp.R index a77894d..c292c6e 100644 --- a/R/mergeVarimp.R +++ b/R/mergeVarimp.R @@ -1,9 +1,10 @@ #' @exportS3Method merge semforest.varimp -merge.semforest.varimp <- function(x, y, ...){ - return(merge_internal_varimp(list(x, y))) +merge.semforest.varimp <- function(...){ + if (length(list(...))<2) stop("Need at least two arguments to merge!") + return(.merge_varimp( list(...) )) } -merge_internal_varimp <- function (varimp_list) +.merge_varimp <- function (varimp_list) { numtrees <- sapply(varimp_list, function(x){length(x$ll.baselines)}) numfeatures <- sapply(varimp_list, function(x){dim(x$importance)[2]}) @@ -28,4 +29,4 @@ merge_internal_varimp <- function (varimp_list) } class(out) <- class(varimp_list[[1]]) return(out) -} \ No newline at end of file +}