# NOT RUN {
# low-dimensional example with user specified test function
n <- 200
p <- 100
library(MASS)
set.seed(3)
x <- mvrnorm(n, mu = rep(0, p), Sigma = diag(p))
colnames(x) <- paste0("Var", 1:p)
beta <- rep(0, p)
beta[c(5, 20, 46)] <- 1
y <- x %*% beta + rnorm(n)
dendr1 <- cluster_vars(x = x)
# Define own test function: partial F-test
# Many of the arguments of the function below might not be
# used but the function is required to have those arguments.
my.test <- function(x, y, clvar, colnames.cluster, arg.all,
arg.all.fix, mod.large, mod.small) {
# generate design matrices
data.large <- cbind(clvar, x)
setdiff.cluster <- setdiff(colnames(x), colnames.cluster)
data.small <- cbind(clvar, x[, setdiff.cluster])
# Replace data.small if set of indices setdiff.cluster is empty.
if (ncol(data.small) == 0) {data.small <- rep(1, length(y))}
# compare the models using a partial F test
pval <- anova(lm(y ~ data.small),
lm(y ~ data.large),
test = "F")$P[2]
return(list("pval" = pval, "mod.small" = NULL))
}
# run hierarchical testing
set.seed(76)
sign.clusters1 <- run_hierarchy(x = x, y = y, dendr = dendr1,
test.func = my.test)
## With block
# I.e. second level of the hierarchical tree is specified by
# the user. This would allow to run the code in parallel; see the 'Details'
# section.
# The column names of the data frame block are optional.
block <- data.frame("var.name" = paste0("Var", 1:p),
"block" = rep(c(1, 2), each = p/2))
dendr2 <- cluster_vars(x = x, block = block)
set.seed(76)
sign.clusters2 <- run_hierarchy(x = x, y = y, dendr = dendr2,
test.func = my.test)
# Access part of the return object or result
sign.clusters2[, "block"]
sign.clusters2[, "p.value"]
# Column names or variable names of the significant cluster in the first row.
sign.clusters2[[1, "significant.cluster"]]
# }
Run the code above in your browser using DataLab