## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5 ) ## ----setup-------------------------------------------------------------------- library(dormancy) ## ----basic-example------------------------------------------------------------ set.seed(42) n <- 500 # Create variables x <- rnorm(n) condition <- sample(c(0, 1), n, replace = TRUE) # Relationship between x and y only exists when condition == 1 y <- ifelse(condition == 1, 0.8 * x + rnorm(n, 0, 0.3), # Strong relationship rnorm(n)) # No relationship data <- data.frame(x = x, y = y, condition = factor(condition)) # Overall correlation is weak cor(data$x, data$y) ## ----detect------------------------------------------------------------------- result <- dormancy_detect(data, method = "conditional", verbose = TRUE) print(result) ## ----conditional-------------------------------------------------------------- result_cond <- dormancy_detect(data, method = "conditional") ## ----threshold---------------------------------------------------------------- set.seed(123) n <- 400 x <- runif(n, -2, 2) # Relationship only emerges for extreme values of x y <- ifelse(abs(x) > 1, 0.9 * sign(x) + rnorm(n, 0, 0.2), rnorm(n, 0, 0.5)) data_thresh <- data.frame(x = x, y = y) result_thresh <- dormancy_detect(data_thresh, method = "threshold") print(result_thresh) ## ----phase-------------------------------------------------------------------- set.seed(456) n <- 300 t <- seq(0, 4*pi, length.out = n) x <- sin(t) + rnorm(n, 0, 0.2) y <- cos(t) + rnorm(n, 0, 0.2) # Add relationship only in certain phases phase <- atan2(y, x) y <- ifelse(phase > 0 & phase < pi/2, 0.7 * x + rnorm(n, 0, 0.2), y) data_phase <- data.frame(x = x, y = y) result_phase <- dormancy_detect(data_phase, method = "phase") ## ----cascade------------------------------------------------------------------ set.seed(789) n <- 300 x <- rnorm(n) y <- rnorm(n) z <- rnorm(n) # Relationship x-y emerges when z is extreme y <- ifelse(abs(z) > 1.5, 0.8 * x + rnorm(n, 0, 0.2), y) data_cascade <- data.frame(x = x, y = y, z = z) result_cascade <- dormancy_detect(data_cascade, method = "cascade") ## ----trigger------------------------------------------------------------------ set.seed(42) n <- 500 x <- rnorm(n) z <- sample(c(0, 1), n, replace = TRUE) y <- ifelse(z == 1, 0.8 * x + rnorm(n, 0, 0.3), rnorm(n)) data <- data.frame(x = x, y = y, z = factor(z)) result <- dormancy_detect(data, method = "conditional") if (nrow(result$patterns) > 0) { triggers <- dormancy_trigger(result, sensitivity = 0.5, n_bootstrap = 50) print(triggers) } ## ----depth-------------------------------------------------------------------- if (nrow(result$patterns) > 0) { depths <- dormancy_depth(result, method = "combined") print(depths) } ## ----risk--------------------------------------------------------------------- if (nrow(result$patterns) > 0) { risk <- dormancy_risk(result, time_horizon = 1, risk_tolerance = 0.3) print(risk) } ## ----awaken------------------------------------------------------------------- if (nrow(result$patterns) > 0) { awakening <- awaken(result, pattern_id = 1, intensity = 1, n_sim = 100) print(awakening) } ## ----hibernate---------------------------------------------------------------- set.seed(42) n <- 400 time <- 1:n x <- rnorm(n) # Relationship that fades over time effect_strength <- exp(-time / 150) y <- effect_strength * 0.8 * x + (1 - effect_strength) * rnorm(n) data_time <- data.frame(time = time, x = x, y = y) hib <- hibernate(data_time, time_var = "time", window_size = 0.15) print(hib) ## ----scout-------------------------------------------------------------------- set.seed(42) n <- 300 x <- rnorm(n) y <- rnorm(n) # Create a region with hidden pattern mask <- x > 1 & y > 1 y[mask] <- 0.9 * x[mask] + rnorm(sum(mask), 0, 0.1) data_scout <- data.frame(x = x, y = y) scout <- dormancy_scout(data_scout, grid_resolution = 15, scout_method = "density") print(scout) ## ----plot, eval=FALSE--------------------------------------------------------- # # Overview plot # dormancy:::plot.dormancy(result, type = "overview") # # # Pattern network # dormancy:::plot.dormancy(result, type = "patterns") # # # Risk-focused # dormancy:::plot.dormancy(result, type = "risk") # # # Scout map # dormancy:::plot.dormancy_map(scout) ## ----example_visual----------------------------------------------------------- dormancy:::plot.dormancy_map(scout)