RatioRMS <- function(var_exp1, var_exp2, var_obs, posRMS = 1, pval = TRUE, 
                     parallel = FALSE, ncores = NULL) {
  #
  #  Enlarge var_exps & var_obs to 10 dim + move posRMS to 1st pos 
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  #
  #  RMS ratio and its pvalue computation
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  dim_names <- names(dim(var_exp1))
   var_exp1 <- aperm(var_exp1, c(posRMS, c(1:length(dim(var_exp1)))[-c(posRMS)]))
   var_exp2 <- aperm(var_exp2, c(posRMS, c(1:length(dim(var_exp2)))[-c(posRMS)]))
   var_obs <- aperm(var_obs, c(posRMS, c(1 :length(dim(var_obs)))[-c(posRMS)]))
  margins <- list(c(2 : length(dim(var_exp1))), c(2 : length(dim(var_exp2))), 
                  c(2 : length(dim(var_obs))))   
  RRMS <- Apply(list(var_exp1, var_exp2, var_obs), margins = margins, AtomicFun = ".RatioRMS", 
                 pval = pval, parallel = parallel, ncores = ncores)
  
  #  
  #  Output
  # ~~~~~~~~
  #
  names(dim(RRMS))[2 : length(dim(RRMS))] <- dim_names[-posRMS]
  RRMS
}

.RatioRMS <- function(exp, exp_ref, obs, pval = TRUE) {
  #
  #  RMS ratio and its pvalue computation
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  if (is.matrix(exp)) {
    dif1 <- rowMeans(exp, na.rm = TRUE) - obs 
  } else {
    dif1 <- exp - obs
  }
  if (is.matrix(exp_ref)) {
    dif2 <- rowMeans(exp_ref, na.rm = TRUE) - obs 
  } else {
    dif2 <- exp_ref - obs
  }
  rms1 <- mean(dif1 ** 2, na.rm = TRUE) ** 0.5
  rms2 <- mean(dif2 ** 2, na.rm = TRUE) ** 0.5
  rms2[which(abs(rms2) <= (max(abs(rms2), na.rm = TRUE) / 1000))] <- max(
                           abs(rms2), na.rm = TRUE) / 1000
  enlratiorms <- (rms1 / rms2)
  if (pval) {
    eno1 <- Eno(dif1, 1)
    eno2 <- Eno(dif2, 1)
    F <- (eno1 * (rms1) ** 2 / (eno1 - 1)) / (eno2 * (rms2) ** 2 / (eno2 - 1))
    F[which(F < 1)] <- 1 / F[which(F < 1)]
    if (!is.na(eno1) && !is.na(eno2) && eno1 > 2 && eno2 > 2) {
      p_val <- (1 - pf(F, eno1 - 1, eno2 - 1)) * 2
    } 
  } else {
    p_val = c()
  }
  
  # Output
  c(enlratiorms, p_val)
}
