## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4, message = FALSE ) ## ----setup-------------------------------------------------------------------- library(CCI) ## ----------------------------------------------------------------------------- make_data <- function(n, effect) { Z1 <- rnorm(n) Z2 <- rnorm(n) X <- sin(Z1) + Z2 + rnorm(n, sd = 0.5) Y <- Z1 * Z2 + effect * X + rnorm(n, sd = 0.5) data.frame(Z1, Z2, X, Y) } set.seed(12) weak <- make_data(400, effect = 0.5) # H0 false, weak effect set.seed(13) null <- make_data(400, effect = 0) # H0 true ## ----------------------------------------------------------------------------- res_weak <- CCI.test(Y ~ X | Z1 + Z2, data = weak, seed = 1, progress = FALSE) summary(res_weak) plot(res_weak) ## ----------------------------------------------------------------------------- res_param <- CCI.test(Y ~ X | Z1 + Z2, data = weak, parametric = TRUE, seed = 1, progress = FALSE) c(empirical = res_weak$p.value, parametric = res_param$p.value) ## ----------------------------------------------------------------------------- res_null <- CCI.test(Y ~ X | Z1 + Z2, data = null, nperm = 100, seed = 1, progress = FALSE) QQplot(res_null, nperm = 50, progress = FALSE) QQplot(res_weak, nperm = 50, progress = FALSE) ## ----------------------------------------------------------------------------- res_tuned <- CCI.test(Y ~ X | Z1 + Z2, data = weak, tune = TRUE, samples = 5, folds = 3, seed = 1, progress = FALSE) res_tuned$p.value ## ----------------------------------------------------------------------------- set.seed(1) tuned <- CCI.pretuner(Y ~ X | Z1 + Z2, data = weak, method = "xgboost", nrounds = c(100, 200), eta = c(0.05, 0.1, 0.3), max_depth = 2:4, samples = 8, folds = 3, progress = FALSE) head(tuned$tuning_result, 3) best <- get_tuned_params(tuned$best_param) str(best) ## ----------------------------------------------------------------------------- res_xgb <- do.call(CCI.test, c(list(formula = Y ~ X | Z1 + Z2, data = weak, method = "xgboost", seed = 1, progress = FALSE), best)) res_xgb$p.value ## ----------------------------------------------------------------------------- deparse(CCI.direction(Y ~ X | Z1 + Z2, data = weak, method = "xgboost", nrounds = 100)) ## ----------------------------------------------------------------------------- res_dir <- do.call(CCI.test, c(list(formula = Y ~ X | Z1 + Z2, data = weak, method = "xgboost", choose_direction = TRUE, seed = 1, progress = FALSE), best)) res_dir$p.value