R/sampcor.R

Defines functions sampcor

Documented in sampcor

#' Sample correlation matrix
#'
#' Returns a correlation matrix for a data sample
#'
#'
#' Accepts a data set and creates a correlation matrix using the sampcov function and the definition of the
#' correlation matrix
#' @param x data set
#' @param varnames list of names
#'
#' @return
#' @export
#'
#' @examples
#' sampcor(data, c("WEIGHT", "LENGTH", "DENSITY"))
sampcor = function(x, varnames = NULL){
  nrows = dim(x)[1]
  ncols = dim(x)[2]
  output = matrix(nrow = ncols, ncol = ncols, dimnames = list(list(),varnames))

  s = sampcov(x)

  for(i in 1:ncols){
    for(k in 1:ncols){
      output[i,k] = s[i,k] / (sqrt(s[i,i]*s[k,k]))
    }
  }
  return(output)
}
coxjohn/Lab1Intro documentation built on Jan. 26, 2020, 4:10 a.m.