correlate: Correlation

Description Usage Arguments Value Author(s) Examples

Description

Computes correlation matrix for an arbitrary number of numeric variables, optionally within strata.

Usage

1
2
3
correlate(..., strata = NULL, subset = NULL, conf.level = 0.95, 
          use = "pairwise.complete.obs", method = "pearson", 
          stat = "cor", byStratum = TRUE, version = FALSE)

Arguments

...

an arbitrary number of variables for which a correlation matrix is desired. The arguments can be vectors, matrices, or lists. Individual columns of a matrix or elements of a list that are not of class numeric, factor, or Date will be omitted. Factor and Date variables are converted to integers. Character vectors will be coerced to numeric. Variables must all be of the same lengths.

strata

vector, matrix, or list of stratification variables. Descriptive statistics will be computed within strata defined by each unique combination of the stratification variables, as well as in the combined sample. If strata is supplied, all variables must be of that same length.

subset

vector indicating a subset to be used for all descriptive statistics. If subset is supplied, all variables must be of that same length.

conf.level

a numeric scalar between 0 and 1 denoting the confidence level to be used in constructing confidence intervals for the correlation.

use

character string denoting the cases to use: "everything" uses all cases (and causes NA when any needed variable is missing), "complete.obs" uses only those rows with no missing data for any variable, and "pairwise.complete.obs" computes pairwise correlations using all cases that are not missing data for the relevant variables.

method

character string denoting the correlation method to use: "pearson" denotes Pearson's correlation coefficient and "spearman" denotes Spearman's rank correlation.

stat

a vector of character strings indicating the descriptive statistic(s) to be tabulated. Possibilities include any statistic as specified by one or more of "cor", "n", "t.stat", "pval", "loCI", or "hiCI". Only enough of the string needs to be specified to disambiguate the choice. Alternatively (and more usefully), a single special format character string can be specified as described in the Details below.

byStratum

a logical scalar indicating whether statistics should be grouped by pair of variables. If TRUE, the results will be displayed in a series of tables where each table correspond to a single variable, with rows corresponding to different strata and columns reflecting all other variables. If FALSE, the results will be displayed in a series of tables where each table corresponds to a single stratum and rows and columns reflect the variables.

version

if TRUE, the version of the function will be returned. No other computations will be performed.

Value

An object of class uCorrelate is returned, which consists of a list of correlation estimates and inference for each specified stratum and for the combined dataset. Each element of the list has six arrays:

cormtx

the correlation matrix, printed.

n

matrix of sample sizes used to compute each correlation

t.stat

matrix of t-statistics, testing a correlation of 0.

pval

matrix of two-sided p-values for the t-test.

lo95%CI

lower bound of the 95% confidence interval.

hi95%CI

upper bound of the 95% confidence interval.

Author(s)

Scott S. Emerson, M.D., Ph.D., Andrew J. Spieker, Brian D. Williamson, Travis Y. Hee Wai, and Solomon Lim

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Load required libraries
library(survival)

# Reading in a dataset
mri <- read.table("http://www.emersonstatistics.com/datasets/mri.txt",header=TRUE)

# Estimated correlation matrix using all data, complete cases, or pairwise complete (the default)
with (mri, correlate(age,weight,ldl,use="everything"))
with (mri, correlate(age,weight,ldl,use="complete"))
with (mri, correlate(age,weight,ldl))

# Correlation matrices for each stratum
with (mri, correlate(age,weight,ldl,strata=male))

# Correlations grouped by variable
with (mri, correlate(age,weight,ldl,strata=male,byStratum=FALSE))

# Special formatting of inference for correlations within strata
with (mri, correlate(age,weight,ldl,strata=male,stat="@cor@ (@lo@, @hi@); P @p@; n= @n@"))

# Special formatting of inference for correlations grouped by variable
with (mri, correlate(age,weight,ldl,strata=male,stat="@cor@ (@lo@, @hi@); P @p@; n= @n@",
      byStratum=FALSE))

uwIntroStats documentation built on May 2, 2019, 4:34 a.m.