Skip to content

Commit 315fc59

Browse files
committed
Updating R docs to include more examples
1 parent 573265d commit 315fc59

18 files changed

+287
-15
lines changed

R/data.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ RandomEffectsDataset <- R6::R6Class(
228228
#'
229229
#' @return `ForestDataset` object
230230
#' @export
231+
#'
232+
#' @examples
233+
#' covariate_matrix <- matrix(runif(10*100), ncol = 10)
234+
#' basis_matrix <- matrix(rnorm(3*100), ncol = 3)
235+
#' weight_vector <- rnorm(100)
236+
#' forest_dataset <- createForestDataset(covariate_matrix)
237+
#' forest_dataset <- createForestDataset(covariate_matrix, basis_matrix)
238+
#' forest_dataset <- createForestDataset(covariate_matrix, basis_matrix, weight_vector)
231239
createForestDataset <- function(covariates, basis=NULL, variance_weights=NULL){
232240
return(invisible((
233241
ForestDataset$new(covariates, basis, variance_weights)
@@ -240,6 +248,11 @@ createForestDataset <- function(covariates, basis=NULL, variance_weights=NULL){
240248
#'
241249
#' @return `Outcome` object
242250
#' @export
251+
#'
252+
#' @examples
253+
#' X <- matrix(runif(10*100), ncol = 10)
254+
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
255+
#' outcome <- createOutcome(y)
243256
createOutcome <- function(outcome){
244257
return(invisible((
245258
Outcome$new(outcome)
@@ -254,6 +267,13 @@ createOutcome <- function(outcome){
254267
#'
255268
#' @return `RandomEffectsDataset` object
256269
#' @export
270+
#'
271+
#' @examples
272+
#' rfx_group_ids <- sample(1:2, size = 100, replace = TRUE)
273+
#' rfx_basis <- matrix(rnorm(3*100), ncol = 3)
274+
#' weight_vector <- rnorm(100)
275+
#' rfx_dataset <- createRandomEffectsDataset(rfx_group_ids, rfx_basis)
276+
#' rfx_dataset <- createRandomEffectsDataset(rfx_group_ids, rfx_basis, weight_vector)
257277
createRandomEffectsDataset <- function(group_labels, basis, variance_weights=NULL){
258278
return(invisible((
259279
RandomEffectsDataset$new(group_labels, basis, variance_weights)

R/forest.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,13 @@ Forest <- R6::R6Class(
758758
#'
759759
#' @return `ForestSamples` object
760760
#' @export
761+
#'
762+
#' @examples
763+
#' num_trees <- 100
764+
#' leaf_dimension <- 2
765+
#' is_leaf_constant <- FALSE
766+
#' is_exponentiated <- FALSE
767+
#' forest_samples <- createForestSamples(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
761768
createForestSamples <- function(num_trees, leaf_dimension=1, is_leaf_constant=F, is_exponentiated=F) {
762769
return(invisible((
763770
ForestSamples$new(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
@@ -773,6 +780,13 @@ createForestSamples <- function(num_trees, leaf_dimension=1, is_leaf_constant=F,
773780
#'
774781
#' @return `Forest` object
775782
#' @export
783+
#'
784+
#' @examples
785+
#' num_trees <- 100
786+
#' leaf_dimension <- 2
787+
#' is_leaf_constant <- FALSE
788+
#' is_exponentiated <- FALSE
789+
#' forest <- createForest(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
776790
createForest <- function(num_trees, leaf_dimension=1, is_leaf_constant=F, is_exponentiated=F) {
777791
return(invisible((
778792
Forest$new(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
@@ -786,6 +800,20 @@ createForest <- function(num_trees, leaf_dimension=1, is_leaf_constant=F, is_exp
786800
#' @param forest_samples (Optional) Container of forest samples from which to re-initialize active forest. If not provided, active forest will be reset to an ensemble of single-node (i.e. root) trees.
787801
#' @param forest_num (Optional) Index of forest samples from which to initialize active forest. If not provided, active forest will be reset to an ensemble of single-node (i.e. root) trees.
788802
#' @export
803+
#'
804+
#' @examples
805+
#' num_trees <- 100
806+
#' leaf_dimension <- 1
807+
#' is_leaf_constant <- TRUE
808+
#' is_exponentiated <- FALSE
809+
#' active_forest <- createForest(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
810+
#' forest_samples <- createForestSamples(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
811+
#' forest_samples$add_forest_with_constant_leaves(0.0)
812+
#' forest_samples$add_numeric_split_tree(0, 0, 0, 0, 0.5, -1.0, 1.0)
813+
#' forest_samples$add_numeric_split_tree(0, 1, 0, 1, 0.75, 3.4, 0.75)
814+
#' active_forest$set_root_leaves(0.1)
815+
#' resetActiveForest(active_forest, forest_samples, 0)
816+
#' resetActiveForest(active_forest)
789817
resetActiveForest <- function(active_forest, forest_samples=NULL, forest_num=NULL) {
790818
if (is.null(forest_samples)) {
791819
root_reset_active_forest_cpp(active_forest$forest_ptr)
@@ -805,6 +833,42 @@ resetActiveForest <- function(active_forest, forest_samples=NULL, forest_num=NUL
805833
#' @param residual Residual which will also be updated
806834
#' @param is_mean_model Whether the model being updated is a conditional mean model
807835
#' @export
836+
#'
837+
#' @examples
838+
#' n <- 100
839+
#' p <- 10
840+
#' num_trees <- 100
841+
#' leaf_dimension <- 1
842+
#' is_leaf_constant <- TRUE
843+
#' is_exponentiated <- FALSE
844+
#' alpha <- 0.95
845+
#' beta <- 2.0
846+
#' min_samples_leaf <- 2
847+
#' max_depth <- 10
848+
#' feature_types <- as.integer(rep(0, p))
849+
#' leaf_model <- 0
850+
#' sigma2 <- 1.0
851+
#' leaf_scale <- as.matrix(1.0)
852+
#' variable_weights <- rep(1/p, p)
853+
#' a_forest <- 1
854+
#' b_forest <- 1
855+
#' cutpoint_grid_size <- 100
856+
#' X <- matrix(runif(n*p), ncol = p)
857+
#' forest_dataset <- createForestDataset(X)
858+
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(n)
859+
#' outcome <- createOutcome(y)
860+
#' rng <- createCppRNG(1234)
861+
#' forest_model <- createForestModel(forest_dataset, feature_types, num_trees, n, alpha, beta, min_samples_leaf, max_depth)
862+
#' active_forest <- createForest(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
863+
#' forest_samples <- createForestSamples(num_trees, leaf_dimension, is_leaf_constant, is_exponentiated)
864+
#' forest_model$sample_one_iteration(
865+
#' forest_dataset, outcome, forest_samples, active_forest,
866+
#' rng, feature_types, leaf_model, leaf_scale, variable_weights,
867+
#' a_forest, b_forest, sigma2, cutpoint_grid_size, keep_forest = TRUE,
868+
#' gfr = FALSE, pre_initialized = TRUE
869+
#' )
870+
#' resetActiveForest(active_forest, forest_samples, 0)
871+
#' resetForestModel(forest_model, active_forest, forest_dataset, outcome, TRUE)
808872
resetForestModel <- function(forest_model, forest, dataset, residual, is_mean_model) {
809873
reset_forest_model_cpp(forest_model$tracker_ptr, forest$forest_ptr, dataset$data_ptr, residual$data_ptr, is_mean_model)
810874
}

R/generics.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,15 @@
44
#' @param ... Other parameters to be used in random effects extraction
55
#' @return List of random effect samples
66
#' @export
7+
#'
8+
#' @examples
9+
#' n <- 100
10+
#' p <- 10
11+
#' X <- matrix(runif(n*p), ncol = p)
12+
#' rfx_group_ids <- sample(1:2, size = n, replace = TRUE)
13+
#' rfx_basis <- rep(1.0, n)
14+
#' y <- (-5 + 10*(X[,1] > 0.5)) + (-2*(rfx_group_ids==1)+2*(rfx_group_ids==2)) + rnorm(n)
15+
#' bart_model <- bart(X_train=X, y_train=y, rfx_group_ids_train=rfx_group_ids,
16+
#' rfx_basis_train = rfx_basis, num_gfr=0, num_mcmc=10)
17+
#' rfx_samples <- getRandomEffectSamples(bart_model)
718
getRandomEffectSamples <- function(object, ...) UseMethod("getRandomEffectSamples")

R/kernel.R

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@
3434
#'
3535
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute leaf indices. If not provided,
3636
#' this function will return leaf indices for every sample of a forest.
37-
#' This function uses 1-indexing, so the first forest sample corresponds to `forest_num = 1`, and so on.
37+
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
3838
#' @return List of vectors. Each vector is of size `num_obs * num_trees`, where `num_obs = nrow(covariates)`
3939
#' and `num_trees` is the number of trees in the relevant forest of `model_object`.
4040
#' @export
41+
#'
42+
#' @examples
43+
#' X <- matrix(runif(10*100), ncol = 10)
44+
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
45+
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
46+
#' computeForestLeafIndices(bart_model, X, "mean")
47+
#' computeForestLeafIndices(bart_model, X, "mean", 0)
48+
#' computeForestLeafIndices(bart_model, X, "mean", c(1,3,9))
4149
computeForestLeafIndices <- function(model_object, covariates, forest_type=NULL, forest_inds=NULL) {
4250
# Extract relevant forest container
4351
object_name <- class(model_object)[1]
@@ -84,9 +92,9 @@ computeForestLeafIndices <- function(model_object, covariates, forest_type=NULL,
8492
if (is.null(forest_inds)) {
8593
forest_inds <- as.integer(1:num_forests - 1)
8694
} else {
87-
stopifnot(all(forest_inds <= num_forests))
88-
stopifnot(all(forest_inds >= 1))
89-
forest_inds <- as.integer(forest_inds - 1)
95+
stopifnot(all(forest_inds <= num_forests-1))
96+
stopifnot(all(forest_inds >= 0))
97+
forest_inds <- as.integer(forest_inds)
9098
}
9199

92100
# Compute leaf indices
@@ -122,9 +130,17 @@ computeForestLeafIndices <- function(model_object, covariates, forest_type=NULL,
122130
#'
123131
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute leaf indices. If not provided,
124132
#' this function will return leaf indices for every sample of a forest.
125-
#' This function uses 1-indexing, so the first forest sample corresponds to `forest_num = 1`, and so on.
133+
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
126134
#' @return Vector of size `length(forest_inds)` with the leaf scale parameter for each requested forest.
127135
#' @export
136+
#'
137+
#' @examples
138+
#' X <- matrix(runif(10*100), ncol = 10)
139+
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
140+
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
141+
#' computeForestLeafVariances(bart_model, "mean")
142+
#' computeForestLeafVariances(bart_model, "mean", 0)
143+
#' computeForestLeafVariances(bart_model, "mean", c(1,3,5))
128144
computeForestLeafVariances <- function(model_object, forest_type, forest_inds=NULL) {
129145
# Extract relevant forest container
130146
stopifnot(class(model_object) %in% c("bartmodel", "bcfmodel"))
@@ -170,9 +186,9 @@ computeForestLeafVariances <- function(model_object, forest_type, forest_inds=NU
170186
if (is.null(forest_inds)) {
171187
forest_inds <- as.integer(1:num_forests)
172188
} else {
173-
stopifnot(all(forest_inds <= num_forests))
174-
stopifnot(all(forest_inds >= 1))
175-
forest_inds <- as.integer(forest_inds)
189+
stopifnot(all(forest_inds <= num_forests-1))
190+
stopifnot(all(forest_inds >= 0))
191+
forest_inds <- as.integer(forest_inds + 1)
176192
}
177193

178194
# Gather leaf scale parameters
@@ -205,9 +221,17 @@ computeForestLeafVariances <- function(model_object, forest_type, forest_inds=NU
205221
#'
206222
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute max leaf indices. If not provided,
207223
#' this function will return max leaf indices for every sample of a forest.
208-
#' This function uses 1-indexing, so the first forest sample corresponds to `forest_num = 1`, and so on.
224+
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
209225
#' @return Vector containing the largest possible leaf index computable by `computeForestLeafIndices` for the forests in a designated forest sample container.
210226
#' @export
227+
#'
228+
#' @examples
229+
#' X <- matrix(runif(10*100), ncol = 10)
230+
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
231+
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
232+
#' computeForestMaxLeafIndex(bart_model, X, "mean")
233+
#' computeForestMaxLeafIndex(bart_model, X, "mean", 0)
234+
#' computeForestMaxLeafIndex(bart_model, X, "mean", c(1,3,9))
211235
computeForestMaxLeafIndex <- function(model_object, covariates, forest_type=NULL, forest_inds=NULL) {
212236
# Extract relevant forest container
213237
object_name <- class(model_object)[1]
@@ -247,9 +271,9 @@ computeForestMaxLeafIndex <- function(model_object, covariates, forest_type=NULL
247271
if (is.null(forest_inds)) {
248272
forest_inds <- as.integer(1:num_forests - 1)
249273
} else {
250-
stopifnot(all(forest_inds <= num_forests))
251-
stopifnot(all(forest_inds >= 1))
252-
forest_inds <- as.integer(forest_inds - 1)
274+
stopifnot(all(forest_inds <= num_forests-1))
275+
stopifnot(all(forest_inds >= 0))
276+
forest_inds <- as.integer(forest_inds)
253277
}
254278

255279
# Compute leaf indices

R/model.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ ForestModel <- R6::R6Class(
170170
#'
171171
#' @return `CppRng` object
172172
#' @export
173+
#'
174+
#' @examples
175+
#' rng <- createCppRNG(1234)
176+
#' rng <- createCppRNG()
173177
createCppRNG <- function(random_seed = -1){
174178
return(invisible((
175179
CppRNG$new(random_seed)
@@ -189,6 +193,19 @@ createCppRNG <- function(random_seed = -1){
189193
#'
190194
#' @return `ForestModel` object
191195
#' @export
196+
#'
197+
#' @examples
198+
#' num_trees <- 100
199+
#' n <- 100
200+
#' p <- 10
201+
#' alpha <- 0.95
202+
#' beta <- 2.0
203+
#' min_samples_leaf <- 2
204+
#' max_depth <- 10
205+
#' feature_types <- as.integer(rep(0, p))
206+
#' X <- matrix(runif(n*p), ncol = p)
207+
#' forest_dataset <- createForestDataset(X)
208+
#' forest_model <- createForestModel(forest_dataset, feature_types, num_trees, n, alpha, beta, min_samples_leaf, max_depth)
192209
createForestModel <- function(forest_dataset, feature_types, num_trees, n, alpha, beta, min_samples_leaf, max_depth) {
193210
return(invisible((
194211
ForestModel$new(forest_dataset, feature_types, num_trees, n, alpha, beta, min_samples_leaf, max_depth)

man/computeForestLeafIndices.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/computeForestLeafVariances.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/computeForestMaxLeafIndex.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createCppRNG.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createForest.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createForestDataset.Rd

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createForestModel.Rd

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/createForestSamples.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)