It may be of interest to use the embedding that is calculated on a training sample set to predict scores on a test set (or, equivalently, on new data).
After loading the nipalsMCIA library, we randomly split
the NCI60 cancer cell line data into training and test sets.
# devel version
# install.packages("devtools")
devtools::install_github("Muunraker/nipalsMCIA", ref = "devel",
force = TRUE, build_vignettes = TRUE) # devel versiondata(NCI60)
set.seed(8)
num_samples <- dim(data_blocks[[1]])[1]
num_train <- round(num_samples * 0.7, 0)
train_samples <- sample.int(num_samples, num_train)
data_blocks_train <- data_blocks
data_blocks_test <- data_blocks
for (i in seq_along(data_blocks)) {
data_blocks_train[[i]] <- data_blocks_train[[i]][train_samples, ]
data_blocks_test[[i]] <- data_blocks_test[[i]][-train_samples, ]
}
# Split corresponding metadata
metadata_train <- data.frame(metadata_NCI60[train_samples, ],
row.names = rownames(data_blocks_train$mrna))
colnames(metadata_train) <- c("cancerType")
metadata_test <- data.frame(metadata_NCI60[-train_samples, ],
row.names = rownames(data_blocks_test$mrna))
colnames(metadata_test) <- c("cancerType")
# Create train and test mae objects
data_blocks_train_mae <- simple_mae(data_blocks_train, row_format = "sample",
colData = metadata_train)
data_blocks_test_mae <- simple_mae(data_blocks_test, row_format = "sample",
colData = metadata_test)The get_metadata_colors() function returns an assignment
of a color for the metadata columns. The nmb_get_gs()
function returns the global scores from the input
NipalsResult object.
meta_colors <- get_metadata_colors(mcia_results = MCIA_train, color_col = 1,
color_pal_params = list(option = "E"))
global_scores <- nmb_get_gs(MCIA_train)
MCIA_out <- data.frame(global_scores[, 1:2])
MCIA_out$cancerType <- nmb_get_metadata(MCIA_train)$cancerType
colnames(MCIA_out) <- c("Factor.1", "Factor.2", "cancerType")
# plot the results
ggplot(data = MCIA_out, aes(x = Factor.1, y = Factor.2, color = cancerType)) +
geom_point(size = 3) +
labs(title = "MCIA for NCI60 training data") +
scale_color_manual(values = meta_colors) +
theme_bw()We use the function to generate new factor scores on the test data
set using the MCIA_train model. The new dataset in the form of an MAE
object is input using the parameter test_data.
We once again plot the top two factor scores for both the training and test datasets
MCIA_out_test <- data.frame(MCIA_test_scores[, 1:2])
MCIA_out_test$cancerType <-
MultiAssayExperiment::colData(data_blocks_test_mae)$cancerType
colnames(MCIA_out_test) <- c("Factor.1", "Factor.2", "cancerType")
MCIA_out_test$set <- "test"
MCIA_out$set <- "train"
MCIA_out_full <- rbind(MCIA_out, MCIA_out_test)
rownames(MCIA_out_full) <- NULL
# plot the results
ggplot(data = MCIA_out_full,
aes(x = Factor.1, y = Factor.2, color = cancerType, shape = set)) +
geom_point(size = 3) +
labs(title = "MCIA for NCI60 training and test data") +
scale_color_manual(values = meta_colors) +
theme_bw()## R version 4.6.0 (2026-04-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.4 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 grid stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] MultiAssayExperiment_1.38.0 SummarizedExperiment_1.42.0
## [3] Biobase_2.72.0 GenomicRanges_1.64.0
## [5] Seqinfo_1.2.0 IRanges_2.46.0
## [7] S4Vectors_0.50.0 BiocGenerics_0.58.0
## [9] generics_0.1.4 MatrixGenerics_1.24.0
## [11] matrixStats_1.5.0 stringr_1.6.0
## [13] nipalsMCIA_1.10.0 ggpubr_0.6.3
## [15] ggplot2_4.0.3 fgsea_1.38.0
## [17] dplyr_1.2.1 ComplexHeatmap_2.28.0
## [19] BiocStyle_2.40.0
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.2.1 viridisLite_0.4.3 farver_2.1.2
## [4] S7_0.2.2 fastmap_1.2.0 pracma_2.4.6
## [7] digest_0.6.39 lifecycle_1.0.5 cluster_2.1.8.2
## [10] magrittr_2.0.5 compiler_4.6.0 rlang_1.2.0
## [13] sass_0.4.10 tools_4.6.0 yaml_2.3.12
## [16] data.table_1.18.2.1 knitr_1.51 ggsignif_0.6.4
## [19] labeling_0.4.3 S4Arrays_1.12.0 DelayedArray_0.38.1
## [22] RColorBrewer_1.1-3 abind_1.4-8 BiocParallel_1.46.0
## [25] withr_3.0.2 purrr_1.2.2 sys_3.4.3
## [28] colorspace_2.1-2 scales_1.4.0 iterators_1.0.14
## [31] cli_3.6.6 rmarkdown_2.31 crayon_1.5.3
## [34] otel_0.2.0 RSpectra_0.16-2 rjson_0.2.23
## [37] BiocBaseUtils_1.14.0 cachem_1.1.0 parallel_4.6.0
## [40] XVector_0.52.0 BiocManager_1.30.27 vctrs_0.7.3
## [43] Matrix_1.7-5 jsonlite_2.0.0 carData_3.0-6
## [46] car_3.1-5 GetoptLong_1.1.1 rstatix_0.7.3
## [49] Formula_1.2-5 clue_0.3-68 maketools_1.3.2
## [52] foreach_1.5.2 tidyr_1.3.2 jquerylib_0.1.4
## [55] glue_1.8.1 codetools_0.2-20 cowplot_1.2.0
## [58] stringi_1.8.7 shape_1.4.6.1 gtable_0.3.6
## [61] tibble_3.3.1 pillar_1.11.1 htmltools_0.5.9
## [64] circlize_0.4.18 R6_2.6.1 doParallel_1.0.17
## [67] evaluate_1.0.5 lattice_0.22-9 png_0.1-9
## [70] backports_1.5.1 broom_1.0.12 bslib_0.10.0
## [73] Rcpp_1.1.1-1.1 fastmatch_1.1-8 SparseArray_1.12.0
## [76] xfun_0.57 buildtools_1.0.0 pkgconfig_2.0.3
## [79] GlobalOptions_0.1.4