Learn R Programming

quest (version 0.2.0)

cor_ml: Multilevel Correlation Matrices

Description

cor_ml decomposes correlations from multilevel data into within-group and between-group correlations. The workhorse of the function is statsBy.

Usage

cor_ml(data, vrb.nm, grp.nm, use = "pairwise.complete.obs", method = "pearson")

Value

list with two elements named "within" and "between" each containing a numeric matrix. The first "within" matrix is the within-group correlation matrix and the second "between" matrix is the between-group correlation matrix. The rownames and colnames of each numeric matrix are vrb.nm.

Arguments

data

data.frame of data.

vrb.nm

character vector of colnames from data specifying the variable columns.

grp.nm

character vector of length 1 of a colname from data specifying the grouping column.

use

character vector of length 1 specifying how to handle missing values when computing the correlations. The options are: 1. "pairwise.complete.obs" which uses pairwise deletion, 2. "complete.obs" which uses listwise deletion, and 3. "everything" which uses all cases and returns NA for any correlations from columns in data[vrb.nm] with missing values.

method

character vector of length 1 specifying which type of correlations to compute. The options are: 1. "pearson" for traditional Pearson product-moment correlations, 2. "kendall" for Kendall rank correlations, and 3. "spearman" for Spearman rank correlations.

See Also

corp_ml for multilevel correlations with significance symbols, cor_by for correlation matrices by group, cor for traditional, single-level correlation matrices, statsBy the workhorse for the cor_ml function,

Examples

Run this code

# traditional use
tmp <- c("outcome","case","session","trt_time") # roxygen2 does not like c() inside []
dat <- as.data.frame(lmeInfo::Bryant2016)[tmp]
stats_by <- psych::statsBy(dat, group = "case") # requires you to include "case" column in dat
cor_ml(data = dat, vrb.nm = c("outcome","session","trt_time"), grp.nm = "case")

# varying the \code{use} and \code{method} arguments
cor_ml(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind","Temp"), grp.nm = "Month",
   use = "pairwise", method = "pearson")
cor_ml(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind","Temp"), grp.nm = "Month",
   use = "complete", method = "kendall")
cor_ml(data = airquality, vrb.nm = c("Ozone","Solar.R","Wind","Temp"), grp.nm = "Month",
   use = "everything", method = "spearman")

Run the code above in your browser using DataLab