-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharbitraryOrderCorrelation.R
36 lines (33 loc) · 1.2 KB
/
arbitraryOrderCorrelation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#' Partial correlations
#'
#' Compute arbitrary \code{k}th order partial correlations using recursion.
#' This implementation is very inefficient.
#'
# @param X A numeric matrix with observations in rows and variables
# in columns.
#' @param S A numeric correlation matrix
#' @param z A integer vector of indices of the variables to condition on. I.e.
#' length(z) is the order of the partial correlations. If \code{z} has
#' length 0 the marginal correlations are returned.
#' @return A numeric matrix of the same size as the correlation matrix
#' with the partial correlations given the variables indexed by \code{z}.
#' \code{NaN} are returned in the rows and columns of the conditioned
#' variables.
#' @seealso \code{\link{correlation}} \code{\link{correlation}}
#' @examples
#' X <- createData(6, 20)
#' Y <- createData(6, 20)
#' Z <- createData(6, 20)
#' S <- cor(X)
#' arbitraryOrderCorrelation(S, z = c(2, 3, 5))
#' @export
arbitraryOrderCorrelation <- function(S, z) {
if (length(z) == 0) {
return(S)
} else if (length(z) == 1) {
firstOrderPartialCorrelation(S, k = z)
} else {
pS <- arbitraryOrderCorrelation(S, z = z[-1])
firstOrderPartialCorrelation(pS, k = z[1])
}
}