
Compare correlations between two independent groups
Source:R/PlotCorrelationComparisons.R
PlotCorrelationComparisons.RdComputes correlations or partial correlations separately within two
independent groups, compares corresponding correlations, and visualizes
the between-group difference as a heatmap. Tile color represents
DeltaR = r_comparison - r_reference, significance stars represent the
statistical test comparing the two correlations, and striped tiles indicate
correlations with opposite signs between groups.
Usage
PlotCorrelationComparisons(
data,
predictor_vars = NULL,
outcome_vars = NULL,
group_var,
comparison_group = NULL,
reference_group = NULL,
covariates = NULL,
method = "pearson",
Relabel = TRUE,
TreatOrdinalAs = "Categorical",
min_n = 4,
eps = 1e-12,
fdr_scope = c("matrix", "per_outcome", "per_predictor"),
reversal_style = c("outline", "stripe", "none"),
interactive = c("none", "plotly", "girafe", "both"),
low_color = "#B2182B",
mid_color = "white",
high_color = "#2166AC",
color_limits = c(-2, 2),
cluster_rows = FALSE,
cluster_columns = FALSE,
triangle = c("full", "upper", "lower"),
IncludeTrendSymbol = FALSE,
Pthresh = 0.05,
PVar = "PValue"
)Arguments
- data
A data frame.
- predictor_vars
Character vector of predictor variables. If
NULL, variable selection is inherited fromPlotCorrelationsHeatmap().- outcome_vars
Optional character vector of outcome variables. If
NULL, the same variables are used on both axes.- group_var
Character string naming the grouping variable.
- comparison_group
Optional level of
group_varused as the comparison group. Positive DeltaR values indicate a more positive correlation in this group relative to the reference group.- reference_group
Optional level of
group_varused as the reference group.- covariates
Optional character vector of covariates used to calculate partial correlations within each group.
- method
Correlation method. Either
"pearson"or"spearman".- Relabel
Logical indicating whether variable labels should be used when available.
- TreatOrdinalAs
Passed to
PlotCorrelationsHeatmap().- min_n
Minimum number of complete observations required for an individual correlation.
- eps
Variance tolerance passed to
PlotCorrelationsHeatmap().- fdr_scope
Scope for FDR correction of correlation-comparison tests. One of
"matrix","per_outcome", or"per_predictor".- reversal_style
How correlations with opposite signs should be shown. One of
"outline"(default),"stripe", or"none". Stripes are a static-only display option and requireggpattern.- interactive
Optional interactive output. One of
"none"(default),"plotly","girafe", or"both". Static ggplots are always retained inUnadjusted$plotandFDRCorrected$plot; requested widgets are added underInteractive.- low_color
Color representing negative DeltaR values.
- mid_color
Color representing DeltaR = 0.
- high_color
Color representing positive DeltaR values.
- color_limits
Limits for the DeltaR color scale. The theoretical range is -2 to 2.
- cluster_rows, cluster_columns
Logical; cluster heatmap rows and columns.
- triangle
Display "full" (default), "upper", or "lower" half of a symmetric comparison matrix. This affects plots only; returned matrices and Results remain complete.
- IncludeTrendSymbol
Logical; add a plus sign for
0.05 < p <= 0.10.- Pthresh
Significance threshold, default
0.05. An explicitly supplied value takes precedence over a legacy significance threshold argument.- PVar
P-value column used for significance decisions. Defaults to raw
"PValue"; select"PValueFDR"to use Benjamini-Hochberg adjusted values. Explicit legacy p-column names remain accepted. Existing, explicitly named FDR plots and adjusted-value columns are retained.Explicit
Pthreshtakes precedence overAlpha,FDRAlpha,label_q, oradjusted_significance_threshold. Pairwisestar_p = "adjusted"retains its legacy adjustment method unlessPVaris supplied;PVar = "PValueFDR"always selects BH.star_p = "none"suppresses legacy pairwise stars. Stars retain conventional p-value tiers within the requested cutoff.
Value
A list containing:
- Correlations
The original
PlotCorrelationsHeatmap()objects for the comparison and reference groups.- Unadjusted
Matrices and heatmap using raw comparison p-values.
- FDRCorrected
Matrices and heatmap using FDR-adjusted comparison p-values.
- Results
A tibble with one row per correlation pair.
- DirectionReversal
Logical matrix indicating opposite correlation signs between groups.
- Metadata
Comparison settings, group information, and the inferential approximation used.
- Interactive
Optional Plotly and/or ggiraph widgets.
Details
Correlations are calculated using PlotCorrelationsHeatmap() so variable
handling, covariate adjustment, ordinal handling, labels, and missing-data
behavior remain consistent with the SciDataReportR correlation workflow.
Pearson correlations without covariates use the usual independent-samples
Fisher-z comparison. Spearman and residualized partial-correlation
comparisons use an approximate Fisher-z calculation; inspect
Results$InferenceStatus or Metadata$InferenceStatus before interpreting
those p-values.
When comparison_group and reference_group are both omitted for a
two-level factor, the first factor level is used as the reference group and
the second factor level as the comparison group. For non-factor grouping
variables, observed order is used. When more than two groups are present,
both groups must be specified explicitly.
Categorical input safeguards apply to group_var.
Numeric columns are converted locally to factors, with a warning, only when
all observed values are finite whole-number codes and there are at most 10
distinct non-missing values. Other numeric inputs error: explicitly convert
the column to a factor first if categorical treatment is intended (for
example, df_All$cohort <- factor(df_All$cohort)). Few unique values alone
do not establish categorical meaning. Factors, character columns, and logical
columns are accepted; more than 10 observed categories warn and continue.
Entirely missing columns produce unavailable statistical requests; unsupported types error. Caller data are unchanged,
and existing restrictions on the required number of groups still apply.
Statistical results and provenance
See inference-results for the shared Results and Metadata contract.
Examples
data(SampleData)
# If Sex is a factor with levels c("Male", "Female"),
# Male is automatically the reference and Female the comparison.
#
# res <- PlotCorrelationComparisons(
# data = SampleData,
# predictor_vars = c("age", "AXL", "Ferritin", "IL_6"),
# outcome_vars = c("Cortisol", "Insulin"),
# group_var = "Sex",
# covariates = "education",
# method = "spearman",
# triangle = "upper"
# )
#
# res$FDRCorrected$plot