
Plot ANOVA Relationships Matrix
Source:R/PlotAnovaRelationshipsMatrix.R
PlotAnovaRelationshipsMatrix.RdThis function plots the relationship between continuous and categorical variables
using ANOVA or Kruskal-Wallis tests. It generates a "heatmap" with points
colored and shaped based on statistical significance and effect size. When
generalized eta-squared is unavailable, raw p-values are colored with
scale_color_pvalue(); the FDR-corrected plot uses adjusted p-values.
Usage
PlotAnovaRelationshipsMatrix(
data,
CatVars,
ContVars,
covariates = NULL,
Relabel = TRUE,
Parametric = TRUE,
Ordinal = FALSE,
min_n = 4,
eps = 1e-08,
Data = lifecycle::deprecated(),
fdr_scope = c("matrix", "per_outcome", "per_predictor"),
Covariates = lifecycle::deprecated()
)Arguments
- data
The data frame containing the variables of interest.
- CatVars
Character vector of categorical variable names.
- ContVars
Character vector of continuous variable names.
- covariates
Optional character vector of covariate names for ANCOVA analysis.
- Relabel
Logical indicating whether to relabel variables with their labels (default is TRUE).
- Parametric
Logical indicating whether to use parametric (ANOVA) or non-parametric (Kruskal-Wallis) tests (default is TRUE).
- Ordinal
Logical, indicating whether ordinal variables should be considered.
- min_n
Minimum number of complete observations required for a tested relationship.
- eps
Small positive value used to avoid zero-size plotting artifacts.
- Data
Deprecated (since 19.15.0). Use
datainstead.- fdr_scope
Either
"matrix"(default) or"per_outcome", passed toApplyFDRCorrection()."matrix"corrects across all p-values at once (historical behavior)."per_outcome"corrects separately within each outcome: outcomes are the continuous variables (ContVars).- Covariates
Deprecated (since 19.15.0). Use
covariatesinstead.
Value
A list containing three ggplot objects: p (scatter plot without multiple comparison correction), p_FDR (scatter plot with FDR correction), and pvaltable (data frame of p-values and significance).
Parametric and non-parametric tests
ANOVA assumes roughly normal residuals and similar variance across groups. Biomarker concentrations are often right-skewed, and group sizes are often uneven, both of which make the F-test unreliable.
Parametric = FALSE switches to Kruskal-Wallis, which compares ranks rather
than means and assumes neither normality nor equal variance. The matrix is
built and read exactly the same way.
Comparing the two p-value tables shows which conclusions depended on the choice of test. Relationships that hold under both are the ones to trust. Where they disagree, the direction is informative: significant only under ANOVA suggests the result is being carried by skew or a few extreme values, while significant only under Kruskal-Wallis suggests a real shift in the bulk of the distribution that outliers were masking from the mean-based test.
Kruskal-Wallis is a rank test and has no ANCOVA form, so covariates
requires Parametric = TRUE.
Examples
data(SampleData)
data(SampleVariableTypes)
# Attach labels and factor levels for readable axes
Labelled <- RevalueData(SampleData, SampleVariableTypes)$RevaluedData
result <- PlotAnovaRelationshipsMatrix(
Labelled,
CatVars = c("Diagnosis", "sex", "Genotype"),
ContVars = c("age", "ACE_CD143_Angiotensin_Converti",
"ACTH_Adrenocorticotropic_Hormon", "AXL", "Adiponectin",
"Alpha_1_Antichymotrypsin", "Alpha_1_Antitrypsin",
"Alpha_1_Microglobulin", "Alpha_2_Macroglobulin",
"Apolipoprotein_A1")
)
# Raw p-value associations
result$Unadjusted$plot
# FDR-adjusted associations
result$FDRCorrected$plot
#> Warning: Using size for a discrete variable is not advised.
# The same matrix using Kruskal-Wallis instead of ANOVA
result_NonParametric <- PlotAnovaRelationshipsMatrix(
Labelled,
CatVars = c("Diagnosis", "sex", "Genotype"),
ContVars = c("age", "ACE_CD143_Angiotensin_Converti",
"ACTH_Adrenocorticotropic_Hormon", "AXL", "Adiponectin",
"Alpha_1_Antichymotrypsin", "Alpha_1_Antitrypsin",
"Alpha_1_Microglobulin", "Alpha_2_Macroglobulin",
"Apolipoprotein_A1"),
Parametric = FALSE
)
result_NonParametric$Unadjusted$plot
result_NonParametric$FDRCorrected$plot
#> Warning: Using size for a discrete variable is not advised.
# Where the two tests disagree
cols_Key <- c("CategoricalVariable", "ContinuousVariable", "p")
df_Compare <- merge(
result$Unadjusted$PvalTable[, cols_Key],
result_NonParametric$Unadjusted$PvalTable[, cols_Key],
by = c("CategoricalVariable", "ContinuousVariable"),
suffixes = c("_ANOVA", "_KruskalWallis")
)
df_Compare$AgreesAt05 <-
(df_Compare$p_ANOVA < 0.05) == (df_Compare$p_KruskalWallis < 0.05)
htmltools::browsable(htmltools::HTML(as.character(
FreezeTableHeader(
dplyr::mutate(
df_Compare,
dplyr::across(dplyr::where(is.numeric), \(x) signif(x, 3))
),
height = "320px", full_width = TRUE
)
)))
CategoricalVariable
ContinuousVariable
p_ANOVA
p_KruskalWallis
AgreesAt05
Diagnosis
ACE_CD143_Angiotensin_Converti
8.87e-01
7.45e-01
TRUE
Diagnosis
ACTH_Adrenocorticotropic_Hormon
3.04e-01
1.85e-01
TRUE
Diagnosis
AXL
2.62e-01
4.80e-01
TRUE
Diagnosis
Adiponectin
4.70e-02
3.03e-02
TRUE
Diagnosis
Alpha_1_Antichymotrypsin
8.00e-03
7.97e-03
TRUE
Diagnosis
Alpha_1_Antitrypsin
1.50e-04
1.13e-04
TRUE
Diagnosis
Alpha_1_Microglobulin
4.00e-03
1.95e-03
TRUE
Diagnosis
Alpha_2_Macroglobulin
2.70e-02
3.43e-02
TRUE
Diagnosis
Apolipoprotein_A1
6.87e-01
7.96e-01
TRUE
Diagnosis
age
5.54e-01
7.06e-01
TRUE
Genotype
ACE_CD143_Angiotensin_Converti
4.19e-01
4.14e-01
TRUE
Genotype
ACTH_Adrenocorticotropic_Hormon
6.09e-01
5.13e-01
TRUE
Genotype
AXL
7.27e-01
6.40e-01
TRUE
Genotype
Adiponectin
6.46e-01
6.29e-01
TRUE
Genotype
Alpha_1_Antichymotrypsin
9.47e-01
9.57e-01
TRUE
Genotype
Alpha_1_Antitrypsin
9.72e-01
9.80e-01
TRUE
Genotype
Alpha_1_Microglobulin
4.03e-01
4.39e-01
TRUE
Genotype
Alpha_2_Macroglobulin
1.40e-01
1.48e-01
TRUE
Genotype
Apolipoprotein_A1
3.05e-01
1.98e-01
TRUE
Genotype
age
3.05e-01
5.18e-01
TRUE
sex
ACE_CD143_Angiotensin_Converti
2.90e-01
3.97e-01
TRUE
sex
ACTH_Adrenocorticotropic_Hormon
1.34e-01
1.82e-01
TRUE
sex
AXL
6.52e-01
3.67e-01
TRUE
sex
Adiponectin
4.36e-01
4.15e-01
TRUE
sex
Alpha_1_Antichymotrypsin
1.20e-06
1.60e-06
TRUE
sex
Alpha_1_Antitrypsin
3.45e-05
6.39e-05
TRUE
sex
Alpha_1_Microglobulin
0.00e+00
0.00e+00
TRUE
sex
Alpha_2_Macroglobulin
6.10e-02
2.11e-02
FALSE
sex
Apolipoprotein_A1
1.44e-04
2.31e-04
TRUE
sex
age
7.40e-02
7.30e-02
TRUE
# Covariate adjustment (parametric only)
PlotAnovaRelationshipsMatrix(
Labelled,
CatVars = c("Diagnosis", "sex"),
ContVars = c("AXL", "Adiponectin", "Alpha_1_Antitrypsin"),
covariates = "age"
)$FDRCorrected$plot
#> Warning: Using size for a discrete variable is not advised.