Install the development version from GitHub
devtools::install_github("imbs-hl/SimData")This example demonstrates how to simulate data and train random forest models.
library(SimData)
library(ranger)
set.seed(123456)
# Define block structure (2 effect blocks with high correlation, 1 null block # with independent predictors)block_info<-data.frame(
block_id= c("A", "B", "C"),
block_size= c(5, 5, 10),
rho= c(0.9, 0.9, 0),
effect_size= c(1, 1, 0)
)
# Simulate data with binary outcomeres_sim<- simulate_data_flexible(
n=600,
block_info=block_info,
type="binary"
)
# Extract datasim_data<-res_sim$data# Train / test splittrain<-sim_data[1:400, ]
test<-sim_data[401:600, ]
# Random forestrf<- ranger(
data=train,
dependent.variable.name="y",
probability=TRUE,
importance="impurity_corrected"
)
# Predictionspred<-data.frame(
truth=test$y,
pred_rf= predict(rf, test)$predictions[, 2]
)
# Visualization
boxplot(pred$pred_rf~pred$truth,
main="Random Forest Predictions",
xlab="True class",
ylab="Predicted probability")
# Variable importanceimp<- importance(rf)
print(sort(imp, decreasing=TRUE))
barplot(imp, horiz=TRUE, las=1,
main="Random Forest Variable Importance")
# Simulate data with continuous outcomeres_sim<- simulate_data_flexible(
n=600,
block_info=block_info,
type="continuous"
)
# Extract datasim_data<-res_sim$data# Train / test splittrain<-sim_data[1:400, ]
test<-sim_data[401:600, ]
# Random forestrf<- ranger(
data=train,
dependent.variable.name="y",
importance="impurity_corrected"
)
# Predictionspred<-data.frame(
truth=test$y,
pred_rf= predict(rf, test)$predictions
)
# Visualization
plot(pred$pred_rf~pred$truth,
main="Random Forest Predictions",
xlab="True values",
ylab="Predicted values")
# Variable importanceimp<- importance(rf)
print(sort(imp, decreasing=TRUE))
barplot(imp, horiz=TRUE, las=1,
main="Random Forest Variable Importance")