forked from StochasticTree/stochtree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.R
More file actions
379 lines (366 loc) · 14.5 KB
/
Copy pathkernel.R
File metadata and controls
379 lines (366 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#' @title Query Forest Leaf Indices
#' @description Compute and return a vector representation of a forest's leaf predictions for
#' every observation in a dataset.
#'
#' The vector has a "row-major" format that can be easily re-represented as
#' as a CSR sparse matrix: elements are organized so that the first `n` elements
#' correspond to leaf predictions for all `n` observations in a dataset for the
#' first tree in an ensemble, the next `n` elements correspond to predictions for
#' the second tree and so on. The "data" for each element corresponds to a uniquely
#' mapped column index that corresponds to a single leaf of a single tree (i.e.
#' if tree 1 has 3 leaves, its column indices range from 0 to 2, and then tree 2's
#' leaf indices begin at 3, etc...).
#'
#' @param model_object Object of type `bartmodel`, `bcfmodel`, or `ForestSamples` corresponding to a BART / BCF model with at least one forest sample, or a low-level `ForestSamples` object.
#' @param covariates Covariates to use for prediction. Must have the same dimensions / column types as the data used to train a forest.
#' @param forest_type Which forest to use from `model_object`.
#' Valid inputs depend on the model type, and whether or not a given forest was sampled in that model.
#'
#' **1. BART**
#'
#' - `'mean'`: Extracts leaf indices for the mean forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' **2. BCF**
#'
#' - `'prognostic'`: Extracts leaf indices for the prognostic forest
#' - `'treatment'`: Extracts leaf indices for the treatment effect forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' **3. ForestSamples**
#'
#' - `NULL`: It is not necessary to disambiguate when this function is called directly on a `ForestSamples` object. This is the default value of this
#'
#' @param propensity (Optional) Propensities used for prediction (BCF-only).
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute leaf indices. If not provided,
#' this function will return leaf indices for every sample of a forest.
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
#' @return Vector of size `num_obs * num_trees`, where `num_obs = nrow(covariates)`
#' and `num_trees` is the number of trees in the relevant forest of `model_object`.
#' @export
#'
#' @examples
#' X <- matrix(runif(10*100), ncol = 10)
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
#' computeForestLeafIndices(bart_model, X, "mean")
#' computeForestLeafIndices(bart_model, X, "mean", 0)
#' computeForestLeafIndices(bart_model, X, "mean", c(1,3,9))
computeForestLeafIndices <- function(
model_object,
covariates,
forest_type = NULL,
propensity = NULL,
forest_inds = NULL
) {
# Extract relevant forest container
stopifnot(any(c(
inherits(model_object, "bartmodel"),
inherits(model_object, "bcfmodel"),
inherits(model_object, "ForestSamples")
)))
model_type <- ifelse(
inherits(model_object, "bartmodel"),
"bart",
ifelse(inherits(model_object, "bcfmodel"), "bcf", "forest_samples")
)
if (model_type == "bart") {
stopifnot(forest_type %in% c("mean", "variance"))
if (forest_type == "mean") {
if (!model_object$model_params$include_mean_forest) {
stop("Mean forest was not sampled in the bart model provided")
}
forest_container <- model_object$mean_forests
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bart model provided"
)
}
forest_container <- model_object$variance_forests
}
} else if (model_type == "bcf") {
stopifnot(forest_type %in% c("prognostic", "treatment", "variance"))
if (forest_type == "prognostic") {
forest_container <- model_object$forests_mu
} else if (forest_type == "treatment") {
forest_container <- model_object$forests_tau
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bcf model provided"
)
}
forest_container <- model_object$variance_forests
}
} else {
forest_container <- model_object
}
# Preprocess covariates
if ((!is.data.frame(covariates)) && (!is.matrix(covariates))) {
stop("covariates must be a matrix or dataframe")
}
if (model_type %in% c("bart", "bcf")) {
train_set_metadata <- model_object$train_set_metadata
covariates_processed <- preprocessPredictionData(
covariates,
train_set_metadata
)
} else {
if (!is.matrix(covariates)) {
stop(
"covariates must be a matrix since no covariate preprocessor is stored in a `ForestSamples` object provided as `model_object`"
)
}
covariates_processed <- covariates
}
# Handle BCF propensity covariate
if (model_type == "bcf") {
# Add propensities to covariate set if necessary
if (model_object$model_params$propensity_covariate != "none") {
if (is.null(propensity)) {
if (!model_object$model_params$internal_propensity_model) {
stop("propensity must be provided for this model")
}
# Compute propensity score using the internal bart model
propensity <- rowMeans(
predict(
model_object$bart_propensity_model,
X = covariates
)$y_hat
)
}
covariates_processed <- cbind(covariates_processed, propensity)
}
}
# Preprocess forest indices
num_forests <- forest_container$num_samples()
if (is.null(forest_inds)) {
forest_inds <- as.integer(1:num_forests - 1)
} else {
stopifnot(all(forest_inds <= num_forests - 1))
stopifnot(all(forest_inds >= 0))
forest_inds <- as.integer(forest_inds)
}
# Compute leaf indices
leaf_ind_matrix <- compute_leaf_indices_cpp(
forest_container$forest_container_ptr,
covariates_processed,
forest_inds
)
return(leaf_ind_matrix)
}
#' @title Query Forest Leaf Scale Parameters
#' @description Return each forest's leaf node scale parameters.
#'
#' If leaf scale is not sampled for the forest in question, throws an error that the
#' leaf model does not have a stochastic scale parameter.
#'
#' This function is intended for advanced use cases in which users require detailed control of sampling algorithms and data structures.
#' Minimal input validation and error checks are performed -- users are responsible for providing the correct inputs.
#' For tutorials on the "proper" usage of the stochtree's advanced workflow, we provide several vignettes at stochtree.ai
#'
#' @param model_object Object of type `bartmodel` or `bcfmodel` corresponding to a BART / BCF model with at least one forest sample
#' @param forest_type Which forest to use from `model_object`.
#' Valid inputs depend on the model type, and whether or not a given forest was sampled in that model.
#'
#' **1. BART**
#'
#' - `'mean'`: Extracts leaf indices for the mean forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' **2. BCF**
#'
#' - `'prognostic'`: Extracts leaf indices for the prognostic forest
#' - `'treatment'`: Extracts leaf indices for the treatment effect forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute leaf indices. If not provided,
#' this function will return leaf indices for every sample of a forest.
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
#' @return Vector of size `length(forest_inds)` with the leaf scale parameter for each requested forest.
#' @export
#'
#' @examples
#' X <- matrix(runif(10*100), ncol = 10)
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
#' computeForestLeafVariances(bart_model, "mean")
#' computeForestLeafVariances(bart_model, "mean", 0)
#' computeForestLeafVariances(bart_model, "mean", c(1,3,5))
computeForestLeafVariances <- function(
model_object,
forest_type,
forest_inds = NULL
) {
# Extract relevant forest container
stopifnot(any(c(
inherits(model_object, "bartmodel"),
inherits(model_object, "bcfmodel")
)))
model_type <- ifelse(inherits(model_object, "bartmodel"), "bart", "bcf")
if (model_type == "bart") {
stopifnot(forest_type %in% c("mean", "variance"))
if (forest_type == "mean") {
if (!model_object$model_params$include_mean_forest) {
stop("Mean forest was not sampled in the bart model provided")
}
if (!model_object$model_params$sample_sigma2_leaf) {
stop(
"Leaf scale parameter was not sampled for the mean forest in the bart model provided"
)
}
leaf_scale_vector <- model_object$sigma2_leaf_samples
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bart model provided"
)
}
stop(
"Leaf scale parameter was not sampled for the variance forest in the bart model provided"
)
}
} else {
stopifnot(forest_type %in% c("prognostic", "treatment", "variance"))
if (forest_type == "prognostic") {
if (!model_object$model_params$sample_sigma2_leaf_mu) {
stop(
"Leaf scale parameter was not sampled for the prognostic forest in the bcf model provided"
)
}
leaf_scale_vector <- model_object$sigma2_leaf_mu_samples
} else if (forest_type == "treatment") {
if (!model_object$model_params$sample_sigma2_leaf_tau) {
stop(
"Leaf scale parameter was not sampled for the treatment effect forest in the bcf model provided"
)
}
leaf_scale_vector <- model_object$sigma2_leaf_tau_samples
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bcf model provided"
)
}
stop(
"Leaf scale parameter was not sampled for the variance forest in the bcf model provided"
)
}
}
# Preprocess forest indices
num_forests <- model_object$model_params$num_samples
if (is.null(forest_inds)) {
forest_inds <- as.integer(1:num_forests)
} else {
stopifnot(all(forest_inds <= num_forests - 1))
stopifnot(all(forest_inds >= 0))
forest_inds <- as.integer(forest_inds + 1)
}
# Gather leaf scale parameters
leaf_scale_params <- leaf_scale_vector[forest_inds]
return(leaf_scale_params)
}
#' @title Query Forest Max Leaf Index
#' @description
#' Compute and return the largest possible leaf index computable by `computeForestLeafIndices` for the forests in a designated forest sample container.
#'
#' @param model_object Object of type `bartmodel`, `bcfmodel`, or `ForestSamples` corresponding to a BART / BCF model with at least one forest sample, or a low-level `ForestSamples` object.
#' @param forest_type Which forest to use from `model_object`.
#' Valid inputs depend on the model type, and whether or not a
#'
#' **1. BART**
#'
#' - `'mean'`: Extracts leaf indices for the mean forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' **2. BCF**
#'
#' - `'prognostic'`: Extracts leaf indices for the prognostic forest
#' - `'treatment'`: Extracts leaf indices for the treatment effect forest
#' - `'variance'`: Extracts leaf indices for the variance forest
#'
#' **3. ForestSamples**
#'
#' - `NULL`: It is not necessary to disambiguate when this function is called directly on a `ForestSamples` object. This is the default value of this
#'
#' @param forest_inds (Optional) Indices of the forest sample(s) for which to compute max leaf indices. If not provided,
#' this function will return max leaf indices for every sample of a forest.
#' This function uses 0-indexing, so the first forest sample corresponds to `forest_num = 0`, and so on.
#' @return Vector containing the largest possible leaf index computable by `computeForestLeafIndices` for the forests in a designated forest sample container.
#' @export
#'
#' @examples
#' X <- matrix(runif(10*100), ncol = 10)
#' y <- -5 + 10*(X[,1] > 0.5) + rnorm(100)
#' bart_model <- bart(X, y, num_gfr=0, num_mcmc=10)
#' computeForestMaxLeafIndex(bart_model, "mean")
#' computeForestMaxLeafIndex(bart_model, "mean", 0)
#' computeForestMaxLeafIndex(bart_model, "mean", c(1,3,9))
computeForestMaxLeafIndex <- function(
model_object,
forest_type = NULL,
forest_inds = NULL
) {
# Extract relevant forest container
stopifnot(any(c(
inherits(model_object, "bartmodel"),
inherits(model_object, "bcfmodel"),
inherits(model_object, "ForestSamples")
)))
model_type <- ifelse(
inherits(model_object, "bartmodel"),
"bart",
ifelse(inherits(model_object, "bcfmodel"), "bcf", "forest_samples")
)
if (model_type == "bart") {
stopifnot(forest_type %in% c("mean", "variance"))
if (forest_type == "mean") {
if (!model_object$model_params$include_mean_forest) {
stop("Mean forest was not sampled in the bart model provided")
}
forest_container <- model_object$mean_forests
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bart model provided"
)
}
forest_container <- model_object$variance_forests
}
} else if (model_type == "bcf") {
stopifnot(forest_type %in% c("prognostic", "treatment", "variance"))
if (forest_type == "prognostic") {
forest_container <- model_object$forests_mu
} else if (forest_type == "treatment") {
forest_container <- model_object$forests_tau
} else if (forest_type == "variance") {
if (!model_object$model_params$include_variance_forest) {
stop(
"Variance forest was not sampled in the bcf model provided"
)
}
forest_container <- model_object$variance_forests
}
} else {
forest_container <- model_object
}
# Preprocess forest indices
num_forests <- forest_container$num_samples()
if (is.null(forest_inds)) {
forest_inds <- as.integer(1:num_forests - 1)
} else {
stopifnot(all(forest_inds <= num_forests - 1))
stopifnot(all(forest_inds >= 0))
forest_inds <- as.integer(forest_inds)
}
# Compute leaf indices
output <- rep(NA, length(forest_inds))
for (i in 1:length(forest_inds)) {
output[i] <- forest_container_get_max_leaf_index_cpp(
forest_container$forest_container_ptr,
forest_inds[i]
)
}
return(output)
}