R/calcPCA.R

#' Convert raw trait variables into principal components
#'
#' This function subjects the trait variables from the original dataset to the
#' Principal component analysis (PCA, \code{stats:prcomp}) and calculates
#' principal componenets scores for each sample. All variables are centered by
#' subtracting the variable mean from a particular value and scaled to the unit
#' variance by dividing the value by the standard deviation of a trait
#' (\code{stats::prcomp} parameters \code{center = T}, \code{scale = T}). Some
#' functions like, for example, \code{calcHS} require uncorrelated input
#' variables to calculate individual identity information properly.
#'
#' @param df A data frame with the first column indicating individual identity.
#' @return df A data frame with the same attributes like the \code{df}, but the
#'   original individuality traits are replaced by principal components.
#' @examples
#' summary(ANmodulation)
#' temp <- calcPIC(ANmodulation)
#' summary(temp)
#'
#' @family data transformation
#' @export


calcPCA <- function (df){
  id <- df[,1]
  pcscores <- stats::prcomp(df[,-1], center=T, scale=T, tol=0)$x
  temp <- data.frame(id,pcscores)
  return(temp)
}

Try the IDmeasurer package in your browser

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

IDmeasurer documentation built on May 9, 2019, 5:02 p.m.