# \donttest{
library(drf)
n = 500
p = 10
d = 3
# Generate training data
X = matrix(rnorm(n * p), nrow=n)
Y = matrix(rnorm(n * d), nrow=n)
Y[, 1] = Y[, 1] + X[, 1]
Y[, 2] = Y[, 2] * X[, 2]
Y[, 3] = Y[, 3] * X[, 1] + X[, 2]
# Fit DRF object
drf.forest = drf(X, Y, num.trees=100)
# Generate test data
X_test = matrix(rnorm(10 * p), nrow=10)
out = predict(drf.forest, newdata=X_test)
# Compute E[Y_1 | X] for all data in X_test directly from
# the weights representing the estimated distribution
out$weights %*% out$y[,1]
out = predict(drf.forest, newdata=X_test,
functional='mean')
# Compute E[Y_1 | X] for all data in X_test using built-in functionality
out[,1]
out = predict(drf.forest, newdata=X_test,
functional='quantile',
quantiles=c(0.25, 0.75),
transformation=function(y){y[1] * y[2] * y[3]})
# Compute 25% and 75% quantiles of Y_1*Y_2*Y_3, conditionally on X = X_test[1, ]
out[1,,]
out = predict(drf.forest, newdata=X_test,
functional='cov',
transformation=function(y){matrix(1:6, nrow=2) %*% y})
# Compute 2x2 covariance matrix for (1*Y_1 + 3*Y_2 + 5*Y_3, 2*Y_1 + 4*Y_2 + 6*Y_3),
# conditionally on X = X_test[1, ]
out[1,,]
out = predict(drf.forest, newdata=X_test,
functional='custom',
custom.functional=function(y, w){c(sum(y[, 1] * w), sum(y[, 2] * w))})
# Compute E[Y_1, Y_2 | X] for all data in X_test by providing custom functional that
# computes it from the weights
out
# }
Run the code above in your browser using DataLab