Predicts the response using only the trees in the SDForest that were not trained on data of the environment of the observation. For the training data these trees are also out-of-bag. For new observations their environments have to be given. An environment that does not occur in the training data was seen by no tree, so all trees are used for it.

predictOOEnv(object, X = NULL, envs = NULL)

Arguments

object

Fitted object of class SDForest with environments.

X

Covariates of the training data or of new observations, a matrix with the columns of the training data or a data.frame as in predict.SDForest, whose covariates are matched by name. If NULL, the data saved in the object is used.

envs

Environments of the observations in X, needed for new observations. If NULL, X is taken as the training data with the environments saved in the object.

Value

A vector of out-of-environment predictions, NA for an observation whose environment was seen by every tree.

Author

Markus Ulmer

Examples

set.seed(1)
n <- 60
X <- matrix(rnorm(n * 5), nrow = n)
y <- sign(X[, 1]) * 3 + rnorm(n)
envs <- factor(rep(c("a", "b", "c"), each = n / 3))
model <- SDForest(x = X, y = y, envs = envs, nTree_leave_out = 2, 
                  Q_type = 'no_deconfounding', cp = 0.5)
#> [1] "Fitting stratified trees resulting in 6 trees."
predictOOEnv(model)
#>  [1] -3.0238079  3.4188011 -3.0238079  3.4188011  3.4188011 -3.0238079
#>  [7]  3.4188011  3.4188011  3.4188011 -3.0238079  3.4188011  3.4188011
#> [13] -3.0238079 -3.0238079  3.4188011  0.2612786  0.2612786  3.4188011
#> [19]  3.4188011  3.4188011  1.7283759  1.7283759 -1.3333015 -1.3333015
#> [25]  1.7283759 -1.3333015 -1.3333015 -1.3333015 -1.3333015  1.7283759
#> [31]  1.7283759 -1.3333015  1.7283759 -1.3333015 -1.3333015 -1.3333015
#> [37] -1.3333015 -1.3333015  1.7283759  1.7283759 -0.8146313 -0.8146313
#> [43]  2.1762501  2.1762501 -0.8146313 -0.8146313  2.1762501  2.1762501
#> [49] -0.8146313  2.1762501  2.1762501 -0.8146313  2.1762501 -0.8146313
#> [55]  2.1762501  2.1762501 -0.8146313 -0.8146313  2.1762501 -0.8146313
# new observations of environment a are predicted by the trees that left a out
X_new <- matrix(rnorm(10 * 5), nrow = 10)
predictOOEnv(model, X_new, envs = rep("a", 10))
#>  [1]  3.418801 -3.023808  3.418801  3.418801 -3.023808 -3.023808 -3.023808
#>  [8]  3.418801 -3.023808  3.418801