# Predicting New MCIA scores

Max Mattessich, Joaquin Reyna, Edel Aron and Anna Konstorum

#### Compiled: February 20, 2025

# Predicting MCIA global (factor) scores for new test samples

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.

## Installation

    # devel version

    # install.packages("devtools")
    devtools::install_github("Muunraker/nipalsMCIA", ref = "devel",
                             force = TRUE, build_vignettes = TRUE) # devel version

    # release version
    if (!require("BiocManager", quietly = TRUE))
      install.packages("BiocManager")

    BiocManager::install("nipalsMCIA")

    library(ggplot2)
    library(MultiAssayExperiment)
    library(nipalsMCIA)

## Split the data

    data(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)

## Run nipalsMCIA on training data

    MCIA_train <- nipals_multiblock(data_blocks = data_blocks_train_mae,
                                    col_preproc_method = "colprofile", num_PCs = 10,
                                    plots = "none", tol = 1e-9)

## Visualize model on training data using metadata on cancer type

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()

## Generate factor scores for test data using the MCIA_train model

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`.

    MCIA_test_scores <- predict_gs(mcia_results = MCIA_train,
                                   test_data = data_blocks_test_mae)

## Visualize new scores with old

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()

# Session Info

Session Info

    sessionInfo()

    ## R version 4.4.2 (2024-10-31 ucrt)
    ## Platform: x86_64-w64-mingw32/x64
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    grid      stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] MultiAssayExperiment_1.32.0 SummarizedExperiment_1.36.0
    ##  [3] Biobase_2.66.0              GenomicRanges_1.58.0
    ##  [5] GenomeInfoDb_1.42.3         IRanges_2.40.1
    ##  [7] S4Vectors_0.44.0            BiocGenerics_0.52.0
    ##  [9] MatrixGenerics_1.18.1       matrixStats_1.5.0
    ## [11] stringr_1.5.1               nipalsMCIA_1.4.4
    ## [13] ggpubr_0.6.0                ggplot2_3.5.1
    ## [15] fgsea_1.32.2                dplyr_1.1.4
    ## [17] ComplexHeatmap_2.22.0       BiocStyle_2.34.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] rlang_1.1.5             magrittr_2.0.3          clue_0.3-66
    ##  [4] GetoptLong_1.0.5        compiler_4.4.2          png_0.1-8
    ##  [7] vctrs_0.6.5             pkgconfig_2.0.3         shape_1.4.6.1
    ## [10] crayon_1.5.3            fastmap_1.2.0           backports_1.5.0
    ## [13] magick_2.8.5            XVector_0.46.0          labeling_0.4.3
    ## [16] rmarkdown_2.29          pracma_2.4.4            UCSC.utils_1.2.0
    ## [19] tinytex_0.55            purrr_1.0.4             xfun_0.51
    ## [22] zlibbioc_1.52.0         cachem_1.1.0            jsonlite_1.9.0
    ## [25] DelayedArray_0.32.0     BiocParallel_1.40.0     broom_1.0.7
    ## [28] parallel_4.4.2          cluster_2.1.8           R6_2.6.1
    ## [31] bslib_0.9.0             stringi_1.8.4           RColorBrewer_1.1-3
    ## [34] car_3.1-3               jquerylib_0.1.4         Rcpp_1.0.14
    ## [37] bookdown_0.42           iterators_1.0.14        knitr_1.49
    ## [40] snow_0.4-4              BiocBaseUtils_1.8.0     Matrix_1.7-2
    ## [43] tidyselect_1.2.1        abind_1.4-8             yaml_2.3.10
    ## [46] doParallel_1.0.17       codetools_0.2-20        lattice_0.22-6
    ## [49] tibble_3.2.1            withr_3.0.2             evaluate_1.0.3
    ## [52] circlize_0.4.16         pillar_1.10.1           BiocManager_1.30.25
    ## [55] carData_3.0-5           foreach_1.5.2           generics_0.1.3
    ## [58] munsell_0.5.1           scales_1.3.0            glue_1.8.0
    ## [61] tools_4.4.2             data.table_1.16.4       RSpectra_0.16-2
    ## [64] ggsignif_0.6.4          fastmatch_1.1-6         cowplot_1.1.3
    ## [67] Cairo_1.6-2             tidyr_1.3.1             colorspace_2.1-1
    ## [70] GenomeInfoDbData_1.2.13 Formula_1.2-5           cli_3.6.4
    ## [73] S4Arrays_1.6.0          viridisLite_0.4.2       gtable_0.3.6
    ## [76] rstatix_0.7.2           sass_0.4.9              digest_0.6.37
    ## [79] SparseArray_1.6.2       farver_2.1.2            rjson_0.2.23
    ## [82] htmltools_0.5.8.1       lifecycle_1.0.4         httr_1.4.7
    ## [85] GlobalOptions_0.1.2