R/correlations.R

Defines functions cor.rec cor.orc

Documented in cor.orc cor.rec

#' Observed Residual Correlations
#'
#' Compute observed residual correlation (ORC) matrix among observed residuals
#' for variables supplied data.
#'
#' `r lifecycle::badge('experimental')`
#'
#' @param data A data.frame or tibble
#' @param na.rm logical (defaults to TRUE)
#' @returns A numeric matrix of correlations among variable residuals.
#' @examples
#'   # Use the SCWB data example
#'   data(SCWB)
#'   cor.orc(SCWB[, 1:20])
#' @import dplyr tidyselect
#' @export
cor.orc <- function(data, na.rm = TRUE) {
  temp.data <- data
  temp.data <- append_observed_residuals(temp.data, na.rm = na.rm)

  # compute correlations of residuals
  Qij <- temp.data %>%
    select(contains("resid_")) %>%
    cor(use = "pairwise.complete.obs")
  rownames(Qij) <- colnames(Qij) <- colnames(data)

  Qij
}



#' Relative Excess Correlations
#'
#' Compute relative excess correlation (REC) matrix among variables in supplied
#' data.
#'
#' `r lifecycle::badge('experimental')`
#'
#' @param data A data.frame or tibble
#' @param na.rm logical (defaults to TRUE)
#' @returns A numeric matrix of correlations among variable residuals.
#' @examples
#'   # Use the SCWB data example
#'   data(SCWB)
#'   cor.rec(SCWB[, 1:20])
#' @export
cor.rec <- function(data, na.rm = TRUE) {
  temp.data <- data
  temp.data <- append_observed_residuals(temp.data, na.rm = na.rm)

  # observed correlations
  Cij <- temp.data %>%
    select(!contains("resid_")) %>%
    cor(use = "pairwise.complete.obs")
  rownames(Cij) <- colnames(Cij) <- colnames(data)

  # compute relative excess correlation
  diag(Cij) <- NA
  rho.i <- rowMeans(Cij, na.rm = TRUE)
  rho.. <- mean(Cij, na.rm = TRUE)
  Qij.star <- matrix(nrow = ncol(Cij), ncol = ncol(Cij))
  for (i in 1:ncol(Cij)) {
    for (j in 1:ncol(Cij)) {
      if (i != j) {
        Qij.star[i, j] <- (Cij[i, j] - rho..) - ((rho.i[i] - rho..) + (rho.i[j] - rho..))
      }
    }
  }
  rownames(Qij.star) <- colnames(Qij.star) <- colnames(data)

  Qij.star
}

Try the recmetrics package in your browser

Any scripts or data that you put into this service are public.

recmetrics documentation built on May 29, 2024, 6:02 a.m.