forked from grf-labs/grf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare 0.10.2 release. (grf-labs#341)
* Prepare 0.10.2 release. * Add a smoke test that runs on CRAN * Alphabetize the authors list, and add Vitor Hadad as a contributor.
- Loading branch information
1 parent
82a75e1
commit 54b5e4f
Showing
12 changed files
with
201 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^bindings$ | ||
^tests/* | ||
|
||
^tests/testthat/test_((?!cran).).* | ||
^tests/benchmarks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
Package: grf | ||
Title: Generalized Random Forests (Beta) | ||
Version: 0.10.1 | ||
Version: 0.10.2 | ||
Author: Julie Tibshirani [aut, cre], | ||
Susan Athey [aut], | ||
Stefan Wager [aut], | ||
Rina Friedberg [ctb], | ||
Vitor Hadad [ctb], | ||
Luke Miner [ctb], | ||
Stefan Wager [aut], | ||
Marvin Wright [ctb] | ||
BugReports: https://github.com/grf-labs/grf/issues | ||
Maintainer: Julie Tibshirani <[email protected]> | ||
|
@@ -26,7 +27,7 @@ Imports: | |
methods, | ||
Rcpp (>= 0.12.15), | ||
sandwich (>= 2.4-0) | ||
RoxygenNote: 6.1.0 | ||
RoxygenNote: 6.1.1 | ||
Suggests: | ||
testthat | ||
SystemRequirements: GNU make | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
rm(list = ls()) | ||
library(grf) | ||
|
||
generate_data = function(setup) { | ||
if (setup == 1) { | ||
n = 600 | ||
p = 8 | ||
X = matrix(runif(n * p), n, p) | ||
propensity = (1 + dbeta(X[,3], 2, 4)) / 4 | ||
tau = (1 + 1/(1 + exp(-20 * (X[,1] - 0.3)))) * (1 + 1/(1 + exp(-20 * (X[,2] - 0.3)))) | ||
W = rbinom(n, 1, 0.9) | ||
Y = 2 * X[,3] - 1 + (W - 0.5) * tau + rnorm(n) | ||
} else if (setup == 2) { | ||
n = 1600 | ||
p = 6 | ||
k = 3 | ||
X = matrix(rnorm(n*p), n, p) | ||
a = rowMeans(X[,1:k]) * sqrt(k) | ||
x = sign(a) * a^2 | ||
tau = 1/(1 + exp(-X[,p])) | ||
propensity = pmax(0.05, pmin(1/(1 + exp(-x)), 0.95)) | ||
mu = x - tau * (propensity - 0.5) | ||
W = rbinom(n, 1, propensity) | ||
Y = mu + W * tau + rnorm(n) | ||
} else if (setup == 3) { | ||
n = 1000 | ||
p = 10 | ||
X = matrix(runif(n * p), n, p) | ||
tau = (1 + 1/(1 + exp(-20 * (X[,1] - 0.3)))) * (1 + 1/(1 + exp(-20 * (X[,2] - 0.3)))) | ||
W = rbinom(n, 1, 0.9) | ||
Y = (W - 0.9) * tau + rnorm(n) | ||
} else if (setup == 4) { | ||
n = 1600 | ||
p = 20 | ||
X = matrix(runif(n * p), n, p) | ||
propensity = (1 + dbeta(X[,3], 2, 4)) / 4 | ||
tau = rep(0, n) | ||
W = rbinom(n, 1, 0.9) | ||
Y = 2 * X[,3] - 1 + (W - 0.5) * tau + rnorm(n) | ||
} else if (setup == 5) { | ||
n = 4000 | ||
p = 10 | ||
X = matrix(rnorm(n * p), n, p) | ||
W = rbinom(n, 1, 0.1) | ||
tau = 0.2 * (X[,3] > 0) | ||
Y = X[,1] + X[,2] + tau * W + rnorm(n) | ||
} | ||
list(X=X, W=W, Y=Y, tau=tau, n=n, p=p) | ||
} | ||
|
||
evaluate_method = function(estimate_tau, setup = 1) { | ||
data = generate_data(setup) | ||
tau.hat = estimate_tau(data$X, data$Y, data$W) | ||
plot(data$tau, tau.hat) | ||
abline(0, 1) | ||
sqrt(mean((tau.hat - data$tau)^2)) | ||
} | ||
|
||
make_causal_forest = function(stabilize.splits, min.node.size, | ||
alpha, imbalance.penalty) { | ||
function(X, Y, W) { | ||
cf = causal_forest(X, Y, W, stabilize.splits = stabilize.splits, alpha = alpha, | ||
min.node.size = min.node.size, imbalance.penalty = imbalance.penalty) | ||
cf.pred = predict(cf) | ||
print(cf) | ||
cf.pred$predictions | ||
} | ||
} | ||
|
||
res.untuned.unstab = sapply(1:5, function(setup) { | ||
evaluate_method(function(X, Y, W) { | ||
cf = causal_forest(X, Y, W, tune.parameters = FALSE, stabilize.splits = FALSE) | ||
cf.pred = predict(cf) | ||
cf.pred$predictions | ||
}, setup) | ||
}) | ||
res.untuned.unstab | ||
|
||
res.tuned.unstab = sapply(1:5, function(setup) { | ||
evaluate_method(function(X, Y, W) { | ||
cf = causal_forest(X, Y, W, tune.parameters = TRUE, stabilize.splits = FALSE) | ||
cf.pred = predict(cf) | ||
cf.pred$predictions | ||
}, setup) | ||
}) | ||
res.tuned.unstab | ||
|
||
res.untuned.stab = sapply(1:5, function(setup) { | ||
evaluate_method(function(X, Y, W) { | ||
cf = causal_forest(X, Y, W, min.node.size = 5, tune.parameters = FALSE, stabilize.splits = TRUE) | ||
cf.pred = predict(cf) | ||
cf.pred$predictions | ||
}, setup) | ||
}) | ||
res.untuned.stab | ||
|
||
res.tuned.stab = sapply(1:5, function(setup) { | ||
evaluate_method(function(X, Y, W) { | ||
cf = causal_forest(X, Y, W, tune.parameters = TRUE, stabilize.splits = TRUE) | ||
cf.pred = predict(cf) | ||
cf.pred$predictions | ||
}, setup) | ||
}) | ||
res.tuned.stab | ||
|
||
res.untuned.unstab | ||
res.tuned.unstab | ||
res.untuned.stab | ||
res.tuned.stab | ||
|
||
|
||
# res.ip = outer(c(0, 0.25, 0.5, 2, 4), 1:5, | ||
# FUN = Vectorize(function(imbalance.penalty, setup) { | ||
# evaluate_method(make_causal_forest( | ||
# stabilize.splits = TRUE, | ||
# min.node.size = 5, | ||
# alpha = 0.05, | ||
# imbalance.penalty = imbalance.penalty), | ||
# setup) | ||
# })) | ||
# | ||
# res.mns = outer(c(0, 1, 2, 4, 8, 16, 32), 1:5, | ||
# FUN = Vectorize(function(mns, setup) { | ||
# evaluate_method(make_causal_forest( | ||
# stabilize.splits = TRUE, | ||
# min.node.size = mns, | ||
# alpha = 0.05, | ||
# imbalance.penalty = 0), | ||
# setup) | ||
# })) | ||
# colnames(res.mns) = sapply(1:5, function(ii) paste("setup", ii)) | ||
# rownames(res.mns) = sapply(c(0, 1, 2, 4, 8, 16, 32), function(ii) paste("min. node size", ii)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
library(grf) | ||
set.seed(1234) | ||
|
||
test_that("regression forest split frequencies are reasonable", { | ||
n = 100 | ||
p = 6 | ||
X = matrix(rnorm(n*p), n, p) | ||
Y = 1000 * (X[,1]) + rnorm(n) | ||
rrr = regression_forest(X, Y, mtry = p) | ||
freq = split_frequencies(rrr, 4) | ||
expect_true(freq[1,1] / sum(freq[1,]) > 1/2) | ||
}) | ||
|
||
test_that("causal forests give reasonable estimates", { | ||
p = 6 | ||
n = 1000 | ||
|
||
ticks = 101 | ||
X.test = matrix(0, ticks, p) | ||
xvals = seq(-1, 1, length.out = ticks) | ||
X.test[,1] = xvals | ||
truth = 2 * (xvals > 0) | ||
|
||
X = matrix(2 * runif(n * p) - 1, n, p) | ||
W = rbinom(n, 1, 0.5) | ||
Y = (X[,1] > 0) * (2 * W - 1) + 2 * rnorm(n) | ||
|
||
forest.causal = causal_forest(X, Y, W, num.trees = 2000, | ||
ci.group.size = 4, W.hat = 0.5, | ||
compute.oob.predictions = FALSE) | ||
preds.causal.oob = predict(forest.causal, estimate.variance=TRUE) | ||
preds.causal = predict(forest.causal, X.test, estimate.variance=TRUE) | ||
|
||
expect_true(all(preds.causal$variance.estimate > 0)) | ||
expect_true(all(preds.causal.oob$variance.estimate > 0)) | ||
|
||
error = preds.causal$predictions - truth | ||
expect_true(mean(error^2) < 0.5) | ||
|
||
truth.oob = 2 * (X[,1] > 0) | ||
error.oob = preds.causal.oob$predictions - truth.oob | ||
expect_true(mean(error.oob^2) < 0.5) | ||
|
||
Z.oob = error.oob / sqrt(preds.causal.oob$variance.estimate) | ||
expect_true(mean(abs(Z.oob) > 1) < 0.5) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters