R/PairsByInvariance.R

Defines functions PairsByInvariance

Documented in PairsByInvariance

#' Sort pairs of group by the extent of their measurement invariance
#'
#' \code{PairsByInvariance} sorts pairs of group by the extent of their 
#' measurement invariance and returns a data frame.
#' 
#' Pairs of groups are organized into a data frame where pairs with lower change
#' in CFIs (i.e., greater invariance) higher in the data frame. 
#'
#' @param matrix.list A list of invariance matrices from 
#' \code{\link{InvarianceMatrices}}
#' @return This function returns list of data frames with each row in a data
#' frame representing a pair of countries and also contaning the change in CFI.  
#'
#' @export
PairsByInvariance <- function(matrix.list) {
  # creating list to fill in and output
  out.list <- list()
  # for each invariance matrix, list pairs by invariance
  for(matrix.number in 1:length(matrix.list)){
    # subset matrix.list to a matrix and convert to a 1 column data frame
    temp.matrix <- matrix.list[[matrix.number]]
    temp.data.frame <- as.data.frame(as.vector(temp.matrix))
    colnames(temp.data.frame) <- "delta.cfi"
    # add columns to data frame specifying groups for each delta cfi
    temp.data.frame$Group1 <- colnames(temp.matrix)
    temp.data.frame$Group2 <- rep(colnames(temp.matrix), 
                                   each = length(colnames(temp.matrix)))
    #clean up and order the data frame
    temp.data.frame <- temp.data.frame[!is.na(temp.data.frame$delta.cfi), ]
    temp.data.frame <- temp.data.frame[order(temp.data.frame$delta.cfi), ]
    temp.data.frame <- temp.data.frame[
      seq(1, nrow(temp.data.frame), 2), ]
    # fill in output list
    out.list[[matrix.number]] <- temp.data.frame
    # create names for output list
    names(out.list)[matrix.number] <- names(matrix.list)[matrix.number]
  }
  return(out.list)
}
sethmargolis/PairMeasInv documentation built on May 23, 2019, 1:48 p.m.