if (FALSE) {
# Prepare data
data <- dat.bangertdrowns2004
data[, c(4:12)] <- apply(data[ , c(4:12)], 2, function(x){
x[is.na(x)] <- median(x, na.rm = TRUE)
x})
data$subject <- factor(data$subject)
data$yi <- as.numeric(data$yi)
# Load caret
library(caret)
set.seed(999)
# Specify the resampling method as 10-fold CV
fit_control <- trainControl(method = "cv", number = 10)
cv_mf_fit <- train(y = data$yi, x = data[,c(3:13, 16)],
method = ModelInfo_mf(), trControl = fit_control)
# Cross-validated clustered MetaForest
data("dat.bourassa1996", package = "metadat")
df <- dat.bourassa1996
df <- escalc(measure = "OR", ai = lh.le, bi = lh.re, ci = rh.le, di= rh.re,
data = df, add = 1/2, to = "all")
df$mage[is.na(df$mage)] <- median(df$mage, na.rm = TRUE)
df[c(5:8)] <- lapply(df[c(5:8)], factor)
df$yi <- as.numeric(df$yi)
# Set up 10-fold grouped CV
fit_control <- trainControl(method = "cv", index = groupKFold(df$sample,
k = 10))
# Set up a custom tuning grid for the three tuning parameters of MetaForest
rf_grid <- expand.grid(whichweights = c("random", "fixed", "unif"),
mtry = c(2, 4, 6),
min.node.size = c(2, 4, 6))
# Train the model
cv.mf.cluster <- train(y = df$yi, x = df[, c("selection", "investigator",
"hand_assess", "eye_assess",
"mage", "sex", "vi",
"sample")],
study = "sample", method = ModelInfo_mf(),
trControl = fit_control,
tuneGrid = rf_grid)
}
Run the code above in your browser using DataLab