Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predict group membership in each tree of the forest #1288

Closed
FridljDa opened this issue Apr 14, 2023 · 2 comments
Closed

Predict group membership in each tree of the forest #1288

FridljDa opened this issue Apr 14, 2023 · 2 comments
Labels

Comments

@FridljDa
Copy link

Hi, I am interested in using GRF to predict the group membership for each tree. Below the code using randomForestSRC::rfsrc as reference to what I had in mind.

Example code

# simulate training data
n <- 500
p <- 3
X <- matrix(rnorm(n * p), n, p)
Y <- X[, 1] * rnorm(n)
#as data frame
df_train <- data.frame(Y,X)
#simulate small testing set 
X_test <- matrix(rnorm(5*p), 5, p)
df_test <- data.frame(X_test)
#Using randomForestSRC
src_forest <- randomForestSRC::rfsrc(Y ~ X1+X2+X3,
        data = df_train)

predict_groups <- stats::predict(src_forest, df_test, membership = TRUE)$membership 
predict_groups <- as.data.frame(predict_groups)
head(predict_groups)

Returns a data frame with 5 rows and ntrees columns. Each column corresponds to a tree. An entry corresponds to which node leaf the test set lands in.

#Using GRF
grf_forest <- grf::regression_forest(X, Y)
#r.pred <- stats::predict(grf_forest, X, membership = TRUE)

Would something as above work with GRF?

Thank you in advance!

@erikcs
Copy link
Member

erikcs commented Apr 20, 2023

Hi @FridljDa, you can do something similar with get_tree and get_leaf_node

@FridljDa
Copy link
Author

Thank you so much, this is what I was looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants