Skip to content

Commit f75079f

Browse files
authored
Minor docstring cleanup (#166)
1 parent 95688bf commit f75079f

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

r-package/policytree/R/policy_tree.R

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,55 @@
4141
#'
4242
#' @examples
4343
#' \donttest{
44-
#' # Fit a depth two tree on doubly robust treatment effect estimates from a causal forest.
44+
#' # Construct doubly robust scores using a causal forest.
4545
#' n <- 10000
4646
#' p <- 10
47-
#' # Discretizing continuous covariates decreases runtime.
47+
#' # Discretizing continuous covariates decreases runtime for policy learning.
4848
#' X <- round(matrix(rnorm(n * p), n, p), 2)
4949
#' colnames(X) <- make.names(1:p)
5050
#' W <- rbinom(n, 1, 1 / (1 + exp(X[, 3])))
5151
#' tau <- 1 / (1 + exp((X[, 1] + X[, 2]) / 2)) - 0.5
5252
#' Y <- X[, 3] + W * tau + rnorm(n)
5353
#' c.forest <- grf::causal_forest(X, Y, W)
54+
#'
55+
#' # Retrieve doubly robust scores.
5456
#' dr.scores <- double_robust_scores(c.forest)
5557
#'
56-
#' tree <- policy_tree(X, dr.scores, 2)
58+
#' # Learn a depth-2 tree on a training set.
59+
#' train <- sample(1:n, n / 2)
60+
#' tree <- policy_tree(X[train, ], dr.scores[train, ], 2)
5761
#' tree
5862
#'
59-
#' # Predict treatment assignment.
60-
#' predicted <- predict(tree, X)
63+
#' # Evaluate the tree on a test set.
64+
#' test <- -train
6165
#'
62-
#' plot(X[, 1], X[, 2], col = predicted)
63-
#' legend("topright", c("control", "treat"), col = c(1, 2), pch = 19)
64-
#' abline(0, -1, lty = 2)
66+
#' # One way to assess the policy is to see whether the leaf node (group) the test set samples
67+
#' # are predicted to belong to have mean outcomes in accordance with the prescribed policy.
6568
#'
66-
#' # Predict the leaf assigned to each sample.
67-
#' node.id <- predict(tree, X, type = "node.id")
68-
#' # Can be reshaped to a list of samples per leaf node with `split`.
69-
#' samples.per.leaf <- split(1:n, node.id)
69+
#' # Get the leaf node assigned to each test sample.
70+
#' node.id <- predict(tree, X[test, ], type = "node.id")
7071
#'
71-
#' # The value of all arms (along with SEs) by each leaf node.
72-
#' values <- aggregate(dr.scores, by = list(leaf.node = node.id),
73-
#' FUN = function(x) c(mean = mean(x), se = sd(x) / sqrt(length(x))))
74-
#' print(values, digits = 2)
72+
#' # Doubly robust estimates of E[Y(control)] and E[Y(treated)] by leaf node.
73+
#' values <- aggregate(dr.scores[test, ], by = list(leaf.node = node.id),
74+
#' FUN = function(dr) c(mean = mean(dr), se = sd(dr) / sqrt(length(dr))))
75+
#' print(values, digits = 1)
7576
#'
76-
#' # Take cost of treatment into account by offsetting the objective
77+
#' # Take cost of treatment into account by, for example, offsetting the objective
7778
#' # with an estimate of the average treatment effect.
78-
#' # See section 5.1 in Athey and Wager (2021) for more details, including
79-
#' # suggestions on using cross-validation to assess the accuracy of the learned policy.
8079
#' ate <- grf::average_treatment_effect(c.forest)
8180
#' cost.offset <- ate[["estimate"]]
8281
#' dr.scores[, "treated"] <- dr.scores[, "treated"] - cost.offset
8382
#' tree.cost <- policy_tree(X, dr.scores, 2)
8483
#'
85-
#' # If there are too many covariates to make tree search computationally feasible,
86-
#' # one can consider for example only the top 5 features according to GRF's variable importance.
84+
#' # Predict treatment assignment for each sample.
85+
#' predicted <- predict(tree, X)
86+
#'
87+
#' # If there are too many covariates to make tree search computationally feasible, then one
88+
#' # approach is to consider for example only the top features according to GRF's variable importance.
8789
#' var.imp <- grf::variable_importance(c.forest)
8890
#' top.5 <- order(var.imp, decreasing = TRUE)[1:5]
8991
#' tree.top5 <- policy_tree(X[, top.5], dr.scores, 2, split.step = 50)
92+
#'
9093
#' }
9194
#' @seealso \code{\link{hybrid_policy_tree}} for building deeper trees.
9295
#' @export

r-package/policytree/man/policy_tree.Rd

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

0 commit comments

Comments
 (0)