BMultivariate · multivariate.gcca
Generalized canonical correlation analysis (GCCA, MAXVAR)
What do three or more sets of variables measured on the same rows have in common?
Generalized CCA extends canonical correlation analysis to two or more sets ("views") of variables. It finds shared scores for the rows that every set predicts as well as possible (the MAXVAR criterion), reports how strongly each set matches each shared dimension, which variables carry it, and — with three or more sets — whether the shared scores hold when any one set is left out. A ridge steadies sets with many or collinear variables. With two sets and no ridge it reproduces CCA.
A real result
Example “Three views”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
Generalized CCA (MAXVAR), first eigenvalue = 2.43 of 3
- Mean set correlation (dim. 1)
- 0.8999
- Weakest set (dim. 1)
- 0.8733
Plots
View data
| Dimension | Value | Set | Correlation |
|---|---|---|---|
| Dimension 1 | 0.92 | Set 1 | 0.9203 |
| Dimension 2 | 0.88 | Set 1 | 0.8767 |
| Dimension 1 | 0.91 | Set 2 | 0.9061 |
| Dimension 2 | 0.77 | Set 2 | 0.7668 |
| Dimension 1 | 0.87 | Set 3 | 0.8733 |
| Dimension 2 | 0.89 | Set 3 | 0.8868 |
View data
| Shared dimension 1 | Shared dimension 2 |
|---|---|
| −0.8662 | 0.5033 |
| 1.037 | 0.9927 |
| 0.7787 | 0.3418 |
| 0.0710 | −0.1070 |
| −0.2455 | −1.331 |
| −0.5462 | −0.1637 |
| 1.343 | 0.0275 |
| 0.6741 | −2.689 |
| 0.3498 | −0.9516 |
| 1.362 | −0.8360 |
| −0.4604 | 0.8087 |
| −1.056 | −1.351 |
| −0.1895 | 1.828 |
| 0.7390 | 0.5620 |
| −1.785 | 0.1106 |
| 0.2522 | 1.231 |
| 0.2042 | 0.1727 |
| 2.029 | 0.5799 |
| 0.7797 | 0.5404 |
| 0.7448 | −0.3454 |
| 0.4782 | 1.817 |
| 0.0821 | −0.1918 |
| 0.2032 | −2.765 |
| 0.6022 | 1.296 |
| −1.083 | −1.339 |
| 0.8620 | −0.6666 |
| 1.654 | −0.4891 |
| 0.8789 | −0.1138 |
| 1.558 | −0.1575 |
| −0.5571 | −0.9393 |
| −0.7213 | −0.0910 |
| −1.806 | 1.276 |
| 1.232 | 0.5551 |
| −0.4271 | 0.5967 |
| 0.5641 | −0.9546 |
| −2.179 | 0.1793 |
| −1.035 | −0.2902 |
| −2.523 | −0.1192 |
| −0.2719 | 1.970 |
| 0.2229 | −0.3587 |
| 1.779 | 0.5179 |
| 0.5536 | 0.1476 |
| 0.2503 | 0.2582 |
| 0.5204 | −1.391 |
| −0.2905 | 0.4761 |
| −1.972 | −1.756 |
| −0.1687 | −0.6290 |
| −1.430 | 0.8774 |
| 1.413 | −0.0755 |
| −0.1792 | −0.8273 |
| 0.5390 | 0.1927 |
| −0.0386 | −2.187 |
| −0.7827 | 0.8405 |
| −0.1266 | −0.5320 |
| 0.5925 | 0.1294 |
| −0.6335 | 0.2093 |
| 1.818 | −0.2780 |
| −0.7667 | 0.8926 |
| −0.4076 | −1.769 |
| 0.8147 | 0.8312 |
First 60 of 90 rows.
Assumptions
- Linear relations — Each set enters through a weighted sum of its variables; curved relations between sets are missed.
- Rows per variable — 90 rows for 9 variables (10.0 per variable). Enough for stable weights.
Shared dimensions (MAXVAR eigenvalues)
| Dimension | Eigenvalue (out of 3) | Mean r² with the sets |
|---|---|---|
| 1 | 2.431 | 0.8102 |
| 2 | 2.143 | 0.7143 |
How well each set matches the shared scores (correlation)
| Set | Variables | Dimension 1 | Dimension 2 |
|---|---|---|---|
| Set 1 | a1, a2, a3 | 0.9203 | 0.8767 |
| Set 2 | b1, b2 | 0.9061 | 0.7668 |
| Set 3 | c1, c2, c3, c4 | 0.8733 | 0.8868 |
Structure correlations with the shared scores
| Variable | Set | Dimension 1 | Dimension 2 |
|---|---|---|---|
| a1 | Set 1 | 0.6952 | −0.5237 |
| a2 | Set 1 | 0.7979 | 0.0866 |
| a3 | Set 1 | 0.4949 | 0.7093 |
| b1 | Set 2 | 0.8363 | −0.3055 |
| b2 | Set 2 | 0.6700 | 0.5079 |
| c1 | Set 3 | 0.6097 | −0.5935 |
| c2 | Set 3 | 0.5053 | 0.6999 |
| c3 | Set 3 | 0.4050 | 0.0908 |
| c4 | Set 3 | −0.0839 | −0.0761 |
Leave one set out: do the shared scores hold?
| Set left out | |r| of dimension 1 | Subspace similarity |
|---|---|---|
| Set 1 | 0.9720 | 0.9201 |
| Set 2 | 0.5046 | 0.9167 |
| Set 3 | 0.9674 | 0.9074 |
- Criterion: MAXVAR (Kettenring 1971) — the shared scores maximize the sum of squared correlations with each set's best weighted sum; variables standardized; signs make each dimension's largest structure correlation positive. Subspace similarity = mean squared cosine between the shared-score subspaces with and without the set (1 = identical).
Generalized CCA (MAXVAR), first eigenvalue = 2.43 of 3
- Mean set correlation (dim. 1)
- 0.8999
- Weakest set (dim. 1)
- 0.8733
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Linear relations — Each set enters through a weighted sum of its variables; curved relations between sets are missed.
- Rows per variable — 90 rows for 9 variables (10.0 per variable). Enough for stable weights.
Shared dimensions (MAXVAR eigenvalues)
| Dimension | Eigenvalue (out of 3) | Mean r² with the sets |
|---|---|---|
| 1 | 2.431 | 0.8102 |
| 2 | 2.143 | 0.7143 |
How well each set matches the shared scores (correlation)
| Set | Variables | Dimension 1 | Dimension 2 |
|---|---|---|---|
| Set 1 | a1, a2, a3 | 0.9203 | 0.8767 |
| Set 2 | b1, b2 | 0.9061 | 0.7668 |
| Set 3 | c1, c2, c3, c4 | 0.8733 | 0.8868 |
Structure correlations with the shared scores
| Variable | Set | Dimension 1 | Dimension 2 |
|---|---|---|---|
| a1 | Set 1 | 0.6952 | −0.5237 |
| a2 | Set 1 | 0.7979 | 0.0866 |
| a3 | Set 1 | 0.4949 | 0.7093 |
| b1 | Set 2 | 0.8363 | −0.3055 |
| b2 | Set 2 | 0.6700 | 0.5079 |
| c1 | Set 3 | 0.6097 | −0.5935 |
| c2 | Set 3 | 0.5053 | 0.6999 |
| c3 | Set 3 | 0.4050 | 0.0908 |
| c4 | Set 3 | −0.0839 | −0.0761 |
Leave one set out: do the shared scores hold?
| Set left out | |r| of dimension 1 | Subspace similarity |
|---|---|---|
| Set 1 | 0.9720 | 0.9201 |
| Set 2 | 0.5046 | 0.9167 |
| Set 3 | 0.9674 | 0.9074 |
- Criterion: MAXVAR (Kettenring 1971) — the shared scores maximize the sum of squared correlations with each set's best weighted sum; variables standardized; signs make each dimension's largest structure correlation positive. Subspace similarity = mean squared cosine between the shared-score subspaces with and without the set (1 = identical).
Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.
Worked examples in the app
- Three viewsrep2_views · view_1: a1, a2, a3 · view_2: b1, b2 · view_3: c1, c2, c3, c4
- Three views ridgerep2_views · view_1: a1, a2, a3 · view_2: b1, b2 · view_3: c1, c2, c3, c4
- Two views is ccarep2_lifecycle · view_1: pop15, pop75 · view_2: sr, dpi, ddpi
- Degenerate constantrep2_views · view_1: a1, a2, a3 · view_2: b1, b2 · view_3: c1, c2, c3, c4
- Degenerate n3rep2_views · view_1: a1, a2, a3 · view_2: b1, b2 · view_3: c1, c2, c3, c4
- Degenerate shared variablerep2_views · view_1: a1, a2 · view_2: a2, b1
In the guides
Where it sits in the decision guides
- Multiview and multiway structure (CCA, generalized CCA, tensors) · DS & ML Decision Guide · Discover — find structure without labels
What you need
- Set 1numeric · 1–50 columns
- Variables of the first set (rows with a missing value are left out).
- Set 2numeric · 1–50 columns
- Variables of the second set.
- Set 3numeric · 0–50 columns
- Variables of a third set (optional, but GCCA is about three or more).
- Set 4numeric · 0–50 columns
- Variables of a fourth set (optional).
- Set 5numeric · 0–50 columns
- Variables of a fifth set (optional).
- Set 6numeric · 0–50 columns
- Variables of a sixth set (optional).
Choices you make
model
- Ridgedefault 0
Added to each set's correlation matrix; steadies sets with many or collinear variables (0 = classical MAXVAR).
- Dimensionsdefault 2
Shared dimensions to report.
statistics
- Leave one set outdefault on
Refit without each set and compare the shared scores (three or more sets).
Checks and remedies
CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.
Linear relationsNot checked from data: state why it holds
Each set enters through a weighted sum of its variables; curved relations are missed.
Rows per variable
Below about 10 rows per variable the weights are unstable; add a ridge.
What it reports
- n
- CCA for three or more sets: shared row scores that every set predicts as well as possible (MAXVAR criterion).
- m
- CCA for three or more sets: shared row scores that every set predicts as well as possible (MAXVAR criterion).
- k
- CCA for three or more sets: shared row scores that every set predicts as well as possible (MAXVAR criterion).
- ridge
- Amount added to each set's correlation matrix in ridge CCA; steadies weights with many or collinear variables (0 = classical).
- mean_view_r1
- Correlation between one set's best weighted sum and a GCCA shared score; near 1 means that set carries the dimension.
- min_view_r1
- Correlation between one set's best weighted sum and a GCCA shared score; near 1 means that set carries the dimension.
- missing_share
- Values that were not recorded; how they went missing decides which fixes are safe.
Per group, level or term: eigen[<k>], view_r1[<set>], loo_r1[<set>]
Python and R
Python and R agree to 10 decimals (relative tolerance 1e-9). Python uses pandas, numpy, codesplash; R uses stats, nanoparquet, codesplashr.
Default traps between the two languages
- Both engines run the codesplash twins (SVD of the stacked whitened sets, the same sign rule), so every number matches. RGCCA implements related but different criteria (it maximizes a sum of covariance-type functions), so it is not an oracle; the two-set identity eigenvalue = 1 + canonical r is checked instead.
The exact script CodeSplash ran for example “Three views” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Generalized canonical correlation analysis (GCCA, MAXVAR)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method multivariate.gcca 1.0.0 · spec c2971b0f8274c4c1
# Dataset: rep2_views (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.errors import check_groups, check_not_constant
from codesplash.stats import multiview as mvw
# %% Data
df = pd.read_parquet("tests/parity/fixtures/rep2_views.parquet", columns=["a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4"])
n_input = len(df)
df = df.dropna(subset=["a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4"]) # infinite values stop the run (registry/errors.yaml)
# %% Analysis
# Generalized canonical correlation analysis, MAXVAR criterion (Kettenring 1971) with a ridge: shared
# scores G that every set predicts as well as possible (G = leading eigenvectors of the sum of the sets'
# ridge projection matrices). With two sets and no ridge it is CCA (eigenvalue = 1 + canonical r).
VIEW_ROLES = [["a1", "a2", "a3"], ["b1", "b2"], ["c1", "c2", "c3", "c4"]]
views_vars = [v for v in VIEW_ROLES if v]
ridge = float(0)
n_comp = int(2)
loo = True
m = len(views_vars)
seen = {}
for i, vs in enumerate(views_vars):
for v in vs:
if v in seen:
cs.cs_error("CS_E_INVALID_OPTION", option="The sets", allowed="separate variables (each in one set only)", value=v)
seen[v] = i
n = len(df)
cs.check_rows(n, max(len(vs) for vs in views_vars) + 2 if ridge == 0 else 5, "Generalized CCA")
for vs in views_vars:
for v in vs:
check_not_constant(df[v], v)
views = [df[vs].to_numpy(dtype=float) for vs in views_vars]
fit = mvw.gcca(views, ridge, n_comp)
k = fit["k"]
lovo = mvw.gcca_leave_one_out(views, ridge, k) if loo and m >= 3 else None
set_names = [f"Set {i + 1}" for i in range(m)]
# %% Assumptions
p_total = sum(len(vs) for vs in views_vars)
ratio = n / p_total
assumptions = [
{"key": "linear", "label": "Linear relations", "status": "info",
"detail": "Each set enters through a weighted sum of its variables; curved relations between sets are missed."},
{"key": "rows_per_variable", "label": "Rows per variable", "status": "ok" if ratio >= 10 else "warn",
"detail": f"{n} rows for {p_total} variables ({ratio:.1f} per variable). " + ("Enough for stable weights." if ratio >= 10
else "Below about 10 rows per variable the weights are unstable; a ridge helps.")},
]
# %% Result
values = {"n": n, "m": m, "k": k, "ridge": ridge,
"mean_view_r1": float(np.mean(fit["view_r"][:, 0])), "min_view_r1": float(np.min(fit["view_r"][:, 0]))}
for j in range(k):
values[f"eigen[{j + 1}]"] = float(fit["eigen"][j])
for i in range(m):
values[f"view_r1[{i + 1}]"] = float(fit["view_r"][i, 0])
if lovo:
values[f"loo_r1[{i + 1}]"] = lovo[i]["r1"]
values["missing_share"] = n_dropped / n_input if n_input else 0.0
tables = [{"key": "eigen", "title": "Shared dimensions (MAXVAR eigenvalues)",
"columns": [{"key": "dimension", "label": "Dimension", "type": "integer"},
{"key": "eigen", "label": f"Eigenvalue (out of {m})", "type": "number"},
{"key": "mean_r2", "label": "Mean r² with the sets", "type": "number"}],
"rows": [{"dimension": j + 1, "eigen": float(fit["eigen"][j]), "mean_r2": float(np.mean(fit["view_r"][:, j] ** 2))} for j in range(k)]},
{"key": "views", "title": "How well each set matches the shared scores (correlation)",
"columns": [{"key": "set", "label": "Set", "type": "string"}, {"key": "variables", "label": "Variables", "type": "string"}]
+ [{"key": f"d{j + 1}", "label": f"Dimension {j + 1}", "type": "number"} for j in range(k)],
"rows": [{"set": set_names[i], "variables": ", ".join(views_vars[i]), **{f"d{j + 1}": float(fit["view_r"][i, j]) for j in range(k)}} for i in range(m)]},
{"key": "structure", "title": "Structure correlations with the shared scores",
"columns": [{"key": "variable", "label": "Variable", "type": "string"}, {"key": "set", "label": "Set", "type": "string"}]
+ [{"key": f"d{j + 1}", "label": f"Dimension {j + 1}", "type": "number"} for j in range(k)],
"rows": [{"variable": v, "set": set_names[i], **{f"d{j + 1}": float(fit["struct"][i][c, j]) for j in range(k)}}
for i, vs in enumerate(views_vars) for c, v in enumerate(vs)]}]
if lovo:
tables.append({"key": "leave_one_out", "title": "Leave one set out: do the shared scores hold?",
"columns": [{"key": "set", "label": "Set left out", "type": "string"},
{"key": "r1", "label": "|r| of dimension 1", "type": "number"},
{"key": "subspace", "label": "Subspace similarity", "type": "number"}],
"rows": [{"set": set_names[d["view"]], "r1": d["r1"], "subspace": d["subspace"]} for d in lovo]})
hr, hc, hv = [], [], []
for i in range(m):
for j in range(k):
hr.append(set_names[i]); hc.append(f"Dimension {j + 1}"); hv.append(float(fit["view_r"][i, j]))
charts = [cs.chart("views", "matrix-heatmap", "Each set's correlation with the shared scores", {
"row": cs.col("Set", hr, "categorical"), "col": cs.col("Dimension", hc, "categorical"),
"value": cs.col("Correlation", np.array(hv)), "label": cs.col("Value", [f"{x:.2f}" for x in hv], "categorical")})]
if k >= 2:
charts.append(cs.chart("scores", "scatter", "Shared scores", {
"x": cs.col("Shared dimension 1", fit["scores"][:, 0]), "y": cs.col("Shared dimension 2", fit["scores"][:, 1])}))
weakest = int(np.argmin(fit["view_r"][:, 0]))
summary = (f"Generalized CCA (MAXVAR{', ridge ' + format(ridge, 'g') if ridge > 0 else ''}) of {m} sets on {n} rows: the first shared dimension "
f"has eigenvalue {fit['eigen'][0]:.3f} of {m}, and the sets correlate with it between r = {fit['view_r'][:, 0].min():.2f} and "
f"{fit['view_r'][:, 0].max():.2f} ({set_names[weakest]} least)."
+ (f" Leaving one set out, dimension 1 keeps |r| ≥ {min(d['r1'] for d in lovo):.2f}." if lovo else ""))
cs.publish({
"method": "multivariate.gcca",
"methodVersion": "1.0.0",
"title": "Generalized canonical correlation analysis",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n,
"summary": summary,
"apa": f"Generalized CCA (MAXVAR), first eigenvalue = {fit['eigen'][0]:.2f} of {m}",
"statistics": [{"key": "mean_view_r1", "label": "Mean set correlation (dim. 1)", "value": values["mean_view_r1"]},
{"key": "min_view_r1", "label": "Weakest set (dim. 1)", "value": values["min_view_r1"]}],
"tables": tables,
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Criterion: MAXVAR (Kettenring 1971) — the shared scores maximize the sum of squared correlations with each set's best weighted sum; "
+ (f"ridge {ridge:g} added to each set's correlation matrix; " if ridge > 0 else "")
+ "variables standardized; signs make each dimension's largest structure correlation positive."
+ (" Subspace similarity = mean squared cosine between the shared-score subspaces with and without the set (1 = identical)." if lovo else "")],
"values": values,
"charts": charts,
})
The exact script CodeSplash ran for example “Three views” · R version 4.5.1 (2025-06-13)
# Generalized canonical correlation analysis (GCCA, MAXVAR)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method multivariate.gcca 1.0.0 · spec c2971b0f8274c4c1
# Dataset: rep2_views (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/rep2_views.parquet", col_select = c("a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("a1", "a2", "a3", "b1", "b2", "c1", "c2", "c3", "c4")) # infinite values stop the run (registry/errors.yaml)
# %% Analysis
# Generalized canonical correlation analysis, MAXVAR criterion (Kettenring 1971) with a ridge: shared
# scores G that every set predicts as well as possible (G = leading eigenvectors of the sum of the sets'
# ridge projection matrices). With two sets and no ridge it is CCA (eigenvalue = 1 + canonical r).
VIEW_ROLES <- list(c("a1", "a2", "a3"), c("b1", "b2"), c("c1", "c2", "c3", "c4"))
views_vars <- Filter(length, VIEW_ROLES)
ridge <- as.numeric(0)
n_comp <- as.integer(2)
loo <- TRUE
m <- length(views_vars)
seen <- character(0)
for (vs in views_vars) {
for (v in vs) {
if (v %in% seen) cs_error("CS_E_INVALID_OPTION", option = "The sets", allowed = "separate variables (each in one set only)", value = v)
seen <- c(seen, v)
}
}
n <- nrow(df)
cs_check_rows(n, if (ridge == 0) max(lengths(views_vars)) + 2 else 5, "Generalized CCA")
for (vs in views_vars) for (v in vs) cs_check_not_constant(df[[v]], v)
views <- lapply(views_vars, function(vs) { M <- as.matrix(df[vs]); storage.mode(M) <- "double"; M })
fit <- cs_mview_gcca(views, ridge, n_comp)
k <- fit$k
lovo <- if (loo && m >= 3) cs_mview_gcca_leave_one_out(views, ridge, k) else NULL
set_names <- sprintf("Set %d", seq_len(m))
# %% Assumptions
p_total <- sum(lengths(views_vars))
ratio <- n / p_total
assumptions <- list(
list(key = "linear", label = "Linear relations", status = "info",
detail = "Each set enters through a weighted sum of its variables; curved relations between sets are missed."),
list(key = "rows_per_variable", label = "Rows per variable", status = if (ratio >= 10) "ok" else "warn",
detail = paste0(sprintf("%d rows for %d variables (%.1f per variable). ", n, p_total, ratio),
if (ratio >= 10) "Enough for stable weights." else "Below about 10 rows per variable the weights are unstable; a ridge helps."))
)
# %% Result
values <- list(n = n, m = m, k = k, ridge = ridge, mean_view_r1 = mean(fit$view_r[, 1]), min_view_r1 = min(fit$view_r[, 1]))
for (j in seq_len(k)) values[[sprintf("eigen[%d]", j)]] <- fit$eigen[j]
for (i in seq_len(m)) {
values[[sprintf("view_r1[%d]", i)]] <- fit$view_r[i, 1]
if (!is.null(lovo)) values[[sprintf("loo_r1[%d]", i)]] <- lovo[[i]]$r1
}
values$missing_share <- if (n_input > 0) n_dropped / n_input else 0
dimcols <- lapply(seq_len(k), function(j) list(key = sprintf("d%d", j), label = sprintf("Dimension %d", j), type = "number"))
tables <- list(
list(key = "eigen", title = "Shared dimensions (MAXVAR eigenvalues)",
columns = list(list(key = "dimension", label = "Dimension", type = "integer"),
list(key = "eigen", label = sprintf("Eigenvalue (out of %d)", m), type = "number"),
list(key = "mean_r2", label = "Mean r² with the sets", type = "number")),
rows = lapply(seq_len(k), function(j) list(dimension = j, eigen = fit$eigen[j], mean_r2 = mean(fit$view_r[, j]^2)))),
list(key = "views", title = "How well each set matches the shared scores (correlation)",
columns = c(list(list(key = "set", label = "Set", type = "string"), list(key = "variables", label = "Variables", type = "string")), dimcols),
rows = lapply(seq_len(m), function(i) {
row <- list(set = set_names[i], variables = paste(views_vars[[i]], collapse = ", "))
for (j in seq_len(k)) row[[sprintf("d%d", j)]] <- fit$view_r[i, j]
row
})),
list(key = "structure", title = "Structure correlations with the shared scores",
columns = c(list(list(key = "variable", label = "Variable", type = "string"), list(key = "set", label = "Set", type = "string")), dimcols),
rows = unlist(lapply(seq_len(m), function(i) lapply(seq_along(views_vars[[i]]), function(c) {
row <- list(variable = views_vars[[i]][c], set = set_names[i])
for (j in seq_len(k)) row[[sprintf("d%d", j)]] <- fit$struct[[i]][c, j]
row
})), recursive = FALSE))
)
if (!is.null(lovo)) {
tables[[length(tables) + 1]] <- list(key = "leave_one_out", title = "Leave one set out: do the shared scores hold?",
columns = list(list(key = "set", label = "Set left out", type = "string"), list(key = "r1", label = "|r| of dimension 1", type = "number"),
list(key = "subspace", label = "Subspace similarity", type = "number")),
rows = lapply(lovo, function(d) list(set = set_names[d$view + 1L], r1 = d$r1, subspace = d$subspace)))
}
hr <- character(0); hc <- character(0); hv <- numeric(0)
for (i in seq_len(m)) for (j in seq_len(k)) { hr <- c(hr, set_names[i]); hc <- c(hc, sprintf("Dimension %d", j)); hv <- c(hv, fit$view_r[i, j]) }
charts <- list(cs_chart("views", "matrix-heatmap", "Each set's correlation with the shared scores", list(
row = cs_col("Set", hr, "categorical"), col = cs_col("Dimension", hc, "categorical"),
value = cs_col("Correlation", hv), label = cs_col("Value", sprintf("%.2f", hv), "categorical"))))
if (k >= 2) {
charts[[2]] <- cs_chart("scores", "scatter", "Shared scores", list(
x = cs_col("Shared dimension 1", fit$scores[, 1]), y = cs_col("Shared dimension 2", fit$scores[, 2])))
}
weakest <- which.min(fit$view_r[, 1])
summary <- paste0(sprintf("Generalized CCA (MAXVAR%s) of %d sets on %d rows: the first shared dimension has eigenvalue %.3f of %d, and the sets correlate with it between r = %.2f and %.2f (%s least).",
if (ridge > 0) paste0(", ridge ", format(ridge)) else "", m, n, fit$eigen[1], m, min(fit$view_r[, 1]), max(fit$view_r[, 1]), set_names[weakest]),
if (!is.null(lovo)) sprintf(" Leaving one set out, dimension 1 keeps |r| ≥ %.2f.", min(vapply(lovo, function(d) d$r1, 0))) else "")
cs_publish(list(
method = "multivariate.gcca",
methodVersion = "1.0.0",
title = "Generalized canonical correlation analysis",
engine = cs_engine_info("stats", "nanoparquet"),
n = n,
summary = summary,
apa = sprintf("Generalized CCA (MAXVAR), first eigenvalue = %.2f of %d", fit$eigen[1], m),
statistics = list(list(key = "mean_view_r1", label = "Mean set correlation (dim. 1)", value = values$mean_view_r1),
list(key = "min_view_r1", label = "Weakest set (dim. 1)", value = values$min_view_r1)),
tables = tables,
assumptions = assumptions,
warnings = warnings,
notes = list(paste0("Criterion: MAXVAR (Kettenring 1971) — the shared scores maximize the sum of squared correlations with each set's best weighted sum; ",
if (ridge > 0) sprintf("ridge %s added to each set's correlation matrix; ", format(ridge)) else "",
"variables standardized; signs make each dimension's largest structure correlation positive.",
if (!is.null(lovo)) " Subspace similarity = mean squared cosine between the shared-score subspaces with and without the set (1 = identical)." else "")),
values = values,
charts = charts
))
Sources
- Kettenring, J. R. (1971). Canonical analysis of several sets of variables. Biometrika, 58, 433–451.
- Carroll, J. D. (1968). Generalization of canonical correlation analysis to three or more sets of variables. Proc. 76th Annual Convention of the APA, 227–228.
- Tenenhaus, A., & Tenenhaus, M. (2011). Regularized generalized canonical correlation analysis. Psychometrika, 76, 257–284.
From the CodeSplash method registry · multivariate.gcca v1.0.0.