R/compute_AI.R

Defines functions compute_AI

Documented in compute_AI

#' @title The brain asymmetry index (AI)
#'
#' @description
#' AI formula = [(left-right)/(left+right)].
#' compute_AI() creates new columns that are AIs of the brain measures using the most widely-used formula.
#'
#' @param data The wide format data
#' @param ID The column of identifiers.
#' @param left_hemisphere The prefix or suffix that indicates the left hemisphere in the variable names
#' @param right_hemisphere The prefix or suffix string that indicates the right hemisphere in the variable names
#' @param hemisphere The character vector that indicates whether a hemisphere indicator in the variable names is a prefix or suffix.
#' @param separator A character vector that separates characters in the variable names.
#' @param start The column that specifies the starting point of a set of variables to calculate the AIs.
#' @param end The column that specifies the endpoint of a set of variables to calculate the AIs.
#' @return The data with AIs.
#'
#' @export
#'
#' @examples
#'
#' data(sample_data)
#'
#' compute_AI(sample_data,
#' left_hemisphere = "lh",
#' right_hemisphere = "rh",
#' separator="_",
#' ID="ID",
#' hemisphere="prefix",
#' start="lh_Thalamus",
#' end="rh_AccumbensArea")

compute_AI <- function(data = sample_data,
                       left_hemisphere = "lh",
                       right_hemisphere = "rh",
                       separator="_",
                       ID="ID",
                       hemisphere="prefix",
                       start,
                       end) {



  start <- match(start,names(data))
  end <- match(end,names(data))

  NID <- match(ID,names(data))
  data2 <- data[,c(start:end,NID)]

  namelist <- colnames(data[,c(start:end)])

  slist <- strsplit(namelist,split = separator)

  llist <- list()

  if(hemisphere=="prefix"){

    for(i in 1:length(slist)){
     llist[i] <- slist[[i]][2]
    }

  } else if(hemisphere=="suffix"){

    for(i in 1:length(slist)){
      llist[i] <- slist[[i]][1]
    }
  }
  llist <- unique(llist)

  for( i in 1:length(llist)){

    data2[[paste0("AI",separator,llist[[i]])]] <-
      (data2[[paste0(left_hemisphere,separator,llist[[i]])]] - data2[[paste0(right_hemisphere,separator,llist[[i]])]]) / (data2[[paste0(left_hemisphere,separator,llist[[i]])]] + data2[[paste0(right_hemisphere,separator,llist[[i]])]])

  }

data4 <- data2[,grep("AI", names(data2))]
data <- data.frame(cbind(data,data4))

return(data)
}

Try the Ymisc package in your browser

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

Ymisc documentation built on Oct. 18, 2023, 5:09 p.m.