multi.tempted extends TEMPTED (TEMPoral TEnsor Decomposition) to
several longitudinal modalities at once, jointly decomposing M subject ×
feature × time tensors into:
- A (subjects × r) — shared subject scores; the thread tying the modalities together.
- B_m (features × r) — per-modality feature loadings.
- Zeta_m — per-modality smooth temporal curves.
- Lambda (M × r) — per-modality magnitude of each component.
Modalities may differ in their features, feature counts, and sampling times.
Citations: TEMPTED, the single-modality precursor (Shi et al.); and the underlying theory (Han et al.).
Install the development version from GitHub with either helper:
# with pak (preferred)
pak::pkg_install("loulind/multi.tempted")
# or with remotes (depreciated) remotes::install_github("loulind/multi.tempted")library(multi.tempted)
# extras used only for visualisation below
library(tidyverse)
library(corrplot)
library(plotly)
library(igraph)
library(ggraph)
library(tidygraph)
library(patchwork)
library(magick)
library(pheatmap)
library(np)The iPOP exercise study:
four omic modalities (cytokine, metabolome, lipid,
protein), each provided preprocessed on a log₁₀ scale, measured at
baseline (preworkout) and four time points post-workout. ipop also
carries subject metadata (meta_subj) and per-sample SubjectID /
timepoint (meta_visit). PCA was also performed on the lipid data to
reduce its dimensionality for the purpose of this vignette.
ipop <- multi.tempted::ipop
names(ipop)
#> [1] "meta_subj" "meta_visit" "cytokine" "metabolome" "lipid"
#> [6] "protein"
# [1] "meta_subj" "meta_visit" "cytokine" "metabolome" "lipid" "protein"# Stack the four modality tables into a named list
featuretables <- lapply(3:6, function(m) as.matrix(ipop[[m]]))
names(featuretables) <- names(ipop)[3:6]
M <- length(featuretables)
# Map visit codes to approximate minutes from baseline
timepoint <- as.vector(ipop[[2]]$timepoint)
timepoint <- case_when(
timepoint == 1 ~ 0,
timepoint == 2 ~ 12,
timepoint == 3 ~ 25,
timepoint == 4 ~ 40,
timepoint == 5 ~ 70
)
timepoints <- rep(list(timepoint), M)
subjectID <- rep(list(as.character(ipop[[2]]$SubjectID)), M)
# Grouping: a per-sample sex vector, and a subjectID--sex table
group_sex <- ipop[[1]]$Sex
group_subID <- ipop[[1]][, c("subjectID", "Sex")]multitempted_all() runs the whole pipeline; see ?multitempted_all
for every argument. The decisions that matter most:
transforms:"clr"for raw microbiome counts,"none"for already-scaled data.r: number of components.centralize: remove each modality’s mean structure first (defaultTRUE).do_ratio: log-ratio meta-features —TRUEonly for raw counts.weights: down-weight a dominant modality when scales differ (see note below).
Signs of A, B, and Zeta can flip together, exactly as in SVD; read relative patterns, not absolute sign.
Scale: the shared A pools least-squares signal, so a larger-scale modality pulls harder. These modalities share a log₁₀ scale, so equal weights are fine; otherwise standardise first or set
weights∝ 1 / modality variance.
output <- multitempted_all(
featuretables = featuretables,
timepoints = timepoints,
subjectID = subjectID,
transforms = "none", # data are already log10-transformed
do_ratio = FALSE, # not raw counts
r = 3 # number computed components (will compute more later)
)
#> Estimating component 1 of 3
#> Converged: dif = 7.06e-05 after 52 iterations
#> Estimating component 2 of 3
#> Converged: dif = 7.98e-05 after 20 iterations
#> Estimating component 3 of 3
#> Converged: dif = 9.71e-05 after 80 iterations
names(output)
#> [1] "datlists" "mean_svd" "A_hat"
#> [4] "B_hat" "Zeta_hat" "time_Zeta"
#> [7] "Lambda" "r_square" "accum_r_square"
#> [10] "metafeature_aggregate" "toppct_aggregate" "contrast"A_hat is the joint score matrix: each row places one subject in the
shared r-dimensional space. Read it like PCA scores.
plot_subject_loading(output$A_hat, group = group_subID)Zeta_m shows when each component is active within a modality: a peak
marks the time where that component’s signal is strongest.
plot_time_loading(output) +
labs(x = "Minutes from baseline", title = "Temporal loadings by modality")Feature loadings rank each modality’s features by their contribution to a component. Below: top 1% by absolute loading, negative in red, positive in blue.
plots_feat <- plot_feature_loading(output, pct = 0.05)
meta_feat <- plot_feature_loading(output, pct = 0.01) # top 1% so not as busy
# display one modality at a time:
plots_feat$cytokinemeta_feat$metabolomeplots_feat$lipidplots_feat$proteinUse the loading rankings to pick features worth inspecting directly. Here, two top cytokines (GLP-1, Insulin) smoothed over time and split by sex.
feat_mat <- ipop[["cytokine"]][, c("GLP1", "INSULIN")]
plot_feature_summary(
feature_mat = feat_mat,
time_vec = timepoints[[1]],
group_vec = group_sex,
bws = 10
) + labs(x = "Minutes from baseline")Loadings act as weights to collapse a modality into one “meta-feature”
trajectory per subject and component (handy for group comparisons).
metafeature_aggregate uses observed data; _est uses the de-noised
fit.
# returns a named list of ggplots, one per modality
traj_plots <- plot_metafeature(output$metafeature_aggregate, group = group_subID)
# display one modality:
traj_plots$cytokine + labs(x = "Minutes from baseline")Features with similar loading profiles co-respond. Spearman correlations
between loading vectors expose these modules; we refit with more
components (r = 10) for a richer profile.
output2 <- multitempted_all(
featuretables = featuretables,
timepoints = timepoints,
subjectID = subjectID,
transforms = "none", # data are already log10-transformed
do_ratio = FALSE, # not raw counts
r = 10 # computing more components to compute correlations
)
#> Estimating component 1 of 10
#> Converged: dif = 7.06e-05 after 52 iterations
#> Estimating component 2 of 10
#> Converged: dif = 7.98e-05 after 20 iterations
#> Estimating component 3 of 10
#> Converged: dif = 9.71e-05 after 80 iterations
#> Estimating component 4 of 10
#> Converged: dif = 9.14e-05 after 36 iterations
#> Estimating component 5 of 10
#> Converged: dif = 7.55e-05 after 22 iterations
#> Estimating component 6 of 10
#> Converged: dif = 8.84e-05 after 27 iterations
#> Estimating component 7 of 10
#> Converged: dif = 6.73e-05 after 18 iterations
#> Estimating component 8 of 10
#> Converged: dif = 7.42e-05 after 59 iterations
#> Estimating component 9 of 10
#> Converged: dif = 7.90e-05 after 32 iterations
#> Estimating component 10 of 10
#> Converged: dif = 7.99e-05 after 36 iterations# extract feature loadings per modality (transpose to r x p for cor())
cyto_loadings <- t(as.matrix(output2$B_hat[["cytokine"]]))
metab_loadings <- t(as.matrix(output2$B_hat[["metabolome"]]))
lipid_loadings <- t(as.matrix(output2$B_hat[["lipid"]]))
prot_loadings <- t(as.matrix(output2$B_hat[["protein"]]))
# within-modality feature correlation matrices
cyto_corr_mat <- cor(cyto_loadings, method = "spearman")
metab_corr_mat <- cor(metab_loadings, method = "spearman")
lipid_corr_mat <- cor(lipid_loadings, method = "spearman")
prot_corr_mat <- cor(prot_loadings, method = "spearman")
corrplot(cyto_corr_mat, method = "color", tl.cex = 0.001, order = "hclust",
mar = c(0, 0, 2, 0), title = "Cytokine Loadings Spearman Corr Heatmap")corrplot(metab_corr_mat, method = "color", tl.cex = 0.001, order = "hclust",
mar = c(0, 0, 2, 0), title = "Metabolite Loadings Spearman Corr Heatmap")corrplot(lipid_corr_mat, method = "color", tl.cex = 0.001, order = "hclust",
mar = c(0, 0, 2, 0), title = "Lipid Loadings Spearman Corr Heatmap")corrplot(prot_corr_mat, method = "color", tl.cex = 0.001, order = "hclust",
mar = c(0, 0, 2, 0), title = "Protein Loadings Spearman Corr Heatmap")Cross-modality correlations surface features from different modalities
driven by the same components (rectangular ordered heatmaps via
pheatmap).
cyto_v_metab <- cor(cyto_loadings, metab_loadings, method = "spearman")
cyto_v_lipid <- cor(cyto_loadings, lipid_loadings, method = "spearman")
cyto_v_prot <- cor(cyto_loadings, prot_loadings, method = "spearman")
metab_v_lipid <- cor(metab_loadings, lipid_loadings, method = "spearman")
metab_v_prot <- cor(metab_loadings, prot_loadings, method = "spearman")
lipid_v_prot <- cor(lipid_loadings, prot_loadings, method = "spearman")
pheatmap(cyto_v_metab,
main = "Cytokine (row) vs Metabolome (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)pheatmap(cyto_v_lipid,
main = "Cytokine (row) vs Lipid (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)pheatmap(cyto_v_prot,
main = "Cytokine (row) vs Protein (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)pheatmap(metab_v_lipid,
main = "Metabolome (row) vs Lipid (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)pheatmap(metab_v_prot,
main = "Metabolome (row) vs Protein (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)pheatmap(lipid_v_prot,
main = "Lipid (row) vs Protein (col) Loadings Corr Heatmap",
clustering_method = "complete",
color = colorRampPalette(c("red", "white", "blue"))(50),
breaks = seq(-1, 1, length.out = 51),
legend_breaks = c(-1, -0.5, 0, 0.5, 1),
legend_labels = c("-1", "-0.5", "0", "0.5", "1"),
border_color = NA,
fontsize_row = 0.001,
fontsize_col = 0.001)Thresholding a correlation matrix gives a co-response network: nodes are features, edges join strongly correlated pairs (blue = positive; red = negative).
# choose a modality correlation matrix and threshold,
choose_corr <- metab_corr_mat # change "metab" to one of "cyto", "lipid", or "prot"
threshold <- 0.85 # change threshold if too sparse or too dense
adjacency_matrix <- ifelse(abs(choose_corr) >= threshold, choose_corr, 0)
diag(adjacency_matrix) <- 0
g <- graph_from_adjacency_matrix(
adjacency_matrix,
mode = "undirected",
weighted = TRUE,
diag = FALSE
)
plot(
g,
vertex.size = 2,
vertex.label = NA,
edge.width = abs(E(g)$weight) * 2,
edge.color = ifelse(E(g)$weight > 0, "steelblue", "tomato"),
layout = layout_with_fr(g, weights = abs(E(g)$weight))
)Contributors only — not needed to use the package. Work against the
source tree with devtools:
# from the package root
devtools::load_all() # load the in-development source
devtools::test() # run the test suite
devtools::document() # regenerate man pages from roxygen comments
devtools::build_readme() # re-render this README.md from README.RmdA Docker image (rocker/RStudio, amd64) is provided for a reproducible environment:
docker build . --platform=linux/amd64 -t multi.tempted
docker run --platform=linux/amd64 \
-v $(pwd):/home/rstudio/work -e PASSWORD=123 -p 8787:8787 multi.tempted
# open http://localhost:8787 (user: rstudio, password: 123),
# then open multi.tempted.Rproj

















