select_soft_power: Select the best soft-thresholding power for WGCNA

View source: R/power_selection_utils.R

select_soft_powerR Documentation

Select the best soft-thresholding power for WGCNA

Description

Analyze scale-free topology fit across candidate powers and choose the soft-thresholding power \beta.

Usage

select_soft_power(
  data_matrix,
  r2_threshold = 0.8,
  make_plots = FALSE,
  output_dir = tempdir()
)

Arguments

data_matrix

Numeric matrix/data frame (rows = samples, cols = features).

r2_threshold

Target R^2 for the scale-free model (default 0.8).

make_plots

Logical; if TRUE, write diagnostic PNGs.

output_dir

Output directory for plots when make_plots = TRUE. Defaults to tempdir() to comply with CRAN policies.

Details

The function uses a two-stage heuristic:

  1. Choose the smallest \beta whose scale-free fit R^2 exceeds r2_threshold.

  2. If no power reaches the target R^2, select the smallest power at the elbow of the curve, approximated by the discrete second derivative

    \Delta^2 f(p_i) = f(p_{i+1}) - 2 f(p_i) + f(p_{i-1}),

    and pick the p_i with the largest |\Delta^2 f(p_i)|.

Value

Integer scalar: the selected soft-thresholding power.

Examples

# --- 1. Create a small synthetic data matrix ---
set.seed(123)
example_data <- matrix(rnorm(50 * 100), nrow = 50, ncol = 100)

# --- 2. Run the function to get the selected power ---
best_power <- select_soft_power(example_data)
print(paste("Best soft power:", best_power))


TemporalForest documentation built on Dec. 23, 2025, 1:06 a.m.