The correlation between x and y after linearly removing one or more
covariates z from both – the standard tool for asking whether an
association survives adjustment for a confound. With method = "spearman"
the variables are rank-transformed first (a rank / robust partial
correlation). The coefficient reproduces base R's residual method and the
ppcor package to machine precision.
Usage
partialCorrelation(x, y = NULL, z = NULL, method = c("pearson", "spearman"))Arguments
- x
Either the first numeric variable (with
yandzgiven), or a matrix/data frame whose column 1 isx, column 2 isyand the remaining columns are the covariates. Incomplete rows are dropped.- y
The second numeric variable when
xis a single vector.- z
The covariate(s) to control for: a numeric vector or matrix.
- method
"pearson" (default) or "spearman" (rank-based).
Value
A list with statistic (the partial correlation), p_value
(two-sided t-test), df (n - 2 - k), n and k (number of covariates).