## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5, out.width = "100%" ) cores <- 2 ## ----eval = FALSE------------------------------------------------------------- # cores <- parallel::detectCores(logical = FALSE) ## ----setup, message = FALSE--------------------------------------------------- library(staRsom) data(eem_list) data(pf_models) ## ----eval = FALSE------------------------------------------------------------- # # download the drEEM data set # eem_list <- eem_load_dreem() # # remove blanks and samples that were excluded in the drEEM tutorial # eem_list <- eem_exclude(eem_list, exclude = list(sample = eem_names(eem_list)[grepl("bl|0A", eem_names(eem_list))])) ## ----------------------------------------------------------------------------- eem_list ## ----------------------------------------------------------------------------- eem_list <- eem_list |> # same wavelength range as the PARAFAC model pf4 eem_range(ex = c(250, 455), em = c(290, 578)) |> # remove Raman and Rayleigh scattering of 1st and 2nd order eem_rem_scat(remove_scatter = c(TRUE, TRUE, TRUE, TRUE), remove_scatter_width = c(15, 15, 18, 19)) |> # interpolate the removed areas eem_interp(type = 1, extend = FALSE, cores = cores) ggeem(eem_list) ## ----------------------------------------------------------------------------- # train the map, the seed makes the result reproducible som <- eemsom(eem_list, seed = 42, verbose = TRUE) som ## ----fig.height = 3----------------------------------------------------------- eemsom_plot_training(som) ## ----------------------------------------------------------------------------- eemsom_quality(som) ## ----eval = FALSE------------------------------------------------------------- # # parallel batch training with 4 cores and fewer training rounds # som_pbatch <- eemsom(eem_list, mode = "pbatch", cores = 4, rlen = 100, seed = 42) # # lower resolution: remove every second excitation and emission wavelength # eem_list_reduced <- eem_exclude(eem_list, list(ex = eem_list[[1]]$ex[c(FALSE, TRUE)], em = eem_list[[1]]$em[c(FALSE, TRUE)])) ## ----------------------------------------------------------------------------- eemsom_samples(som) ## ----fig.show = "hold", out.width = "48%", fig.width = 4, fig.height = 5------ eemsom_plot_map(som, type = "counts", unit_labels = TRUE) eemsom_plot_map(som, type = "umatrix") ## ----fig.height = 6----------------------------------------------------------- # one value per sample, in the order of the samples in eem_list site <- substr(eem_names(eem_list), 1, 3) eemsom_plot_map(som, type = "samples", colour = site, labels = TRUE) ## ----fig.height = 8----------------------------------------------------------- ggeem(som) ## ----fig.height = 6----------------------------------------------------------- # peaks B, T, A and C, the closest available wavelengths are used eemsom_plot_cplane(som, ex = c(275, 275, 260, 350), em = c(310, 340, 450, 450)) ## ----fig.height = 6----------------------------------------------------------- eemsom_plot_cplane(som, ex = c(275, 275, 260, 350), em = c(310, 340, 450, 450), scale = "each") ## ----------------------------------------------------------------------------- eemsom_kselect(som, k = 2:8) ## ----fig.show = "hold", out.width = "48%", fig.width = 4, fig.height = 5------ # three clusters, complete linkage by default som <- eemsom_cluster(som, k = 3) eemsom_plot_map(som, type = "cluster") eemsom_plot_silhouette(som) ## ----------------------------------------------------------------------------- # number of samples per cluster table(cluster = eemsom_samples(som)$cluster) ## ----------------------------------------------------------------------------- regions <- eemsom_regions(som, seed = 42) regions # criteria for the numbers of regions regions$kselect ggeem(regions) ## ----------------------------------------------------------------------------- fri <- eemsom_fri(eem_list, regions) fri ## ----------------------------------------------------------------------------- # 10 maps from bootstrap samples, calculated in parallel stab <- eemsom_stability(eem_list, som, nrep = 10, seed = 42, cores = cores) stab eemsom_plot_stability(stab) ## ----------------------------------------------------------------------------- # the two samples excluded by staRdom, prepared like the example data data(eem_list_outliers) eem_list_outliers <- eem_list_outliers |> eem_rem_scat(remove_scatter = c(TRUE, TRUE, TRUE, TRUE), remove_scatter_width = c(15, 15, 18, 19)) |> eem_interp(type = 1, extend = FALSE, cores = cores) # example data and the two outliers together eem_all <- eem_bind(eem_list, eem_list_outliers) som_all <- eemsom(eem_all, seed = 42) smp <- eemsom_samples(som_all) ## ----------------------------------------------------------------------------- # one map without each sample loo <- eemsom_stability(eem_all, som_all, type = "loo", nrep = 1, seed = 42, cores = cores) loo ## ----------------------------------------------------------------------------- outliers <- data.frame(smp[, c("sample", "hits", "umatrix", "distance")], loo = loo$loo$distance, leverage = unname(eempf_leverage(pf2[[4]])$A[smp$sample])) # samples with the highest leave-one-out distances first outliers[order(-outliers$loo), ] ## ----fig.height = 6----------------------------------------------------------- eemsom_plot_map(som_all, type = "samples", colour = loo$loo$distance, labels = TRUE) ## ----------------------------------------------------------------------------- # one eemlist per cluster eem_groups <- eemsom_split(eem_list, som, min_samples = 5) lengths(eem_groups) ## ----eval = FALSE------------------------------------------------------------- # # one set of PARAFAC models per group # pf_groups <- lapply(eem_groups, eem_parafac, comps = 3:6, normalise = TRUE, nstart = 25, cores = cores) ## ----------------------------------------------------------------------------- pred <- eemsom_predict(som, eem_list_outliers) pred ## ----------------------------------------------------------------------------- # map of the relative contributions of the PARAFAC components pfsom <- eempf_som(pf4[[1]], seed = 42) pfsom eemsom_quality(pfsom) ## ----fig.height = 6----------------------------------------------------------- eemsom_plot_cplane(pfsom, scale = "each") ## ----fig.show = "hold", out.width = "48%", fig.width = 4, fig.height = 6------ eemsom_kselect(pfsom, k = 2:8) pfsom <- eemsom_cluster(pfsom, k = 4) eemsom_plot_map(pfsom, type = "cluster") eemsom_plot_map(pfsom, type = "counts") ## ----fig.height = 4----------------------------------------------------------- # prototypes of one unit per cluster units <- which(!duplicated(pfsom$clusters)) ggeem(pfsom, units = units) ## ----------------------------------------------------------------------------- eemsom_predict(pfsom, eem_list_outliers)