# Using Partial Correlations and Correlations to calculate each other's values

EDIT: I have a correlation matrix with some known values and some unknown values, and i have a partial correlation matrix with the exact opposite known and unknown values. For example, my correlation matrix is: $$\begin{bmatrix}1&.1&.5&.5\\.1&1&.4&.4\\.5&.4&1&?\\.5&.4&?&1\end{bmatrix}$$ and my partial correlation matrix is: $$\begin{bmatrix}1&?&?&?\\?&1&?&?\\?&?&1&0\\?&?&0&1\end{bmatrix}$$ How can I solve for the question mark values in the correlation matrix above? In general, the method will need to work for larger matrices with question marks in the lower right quadrant of the correlation matrix.

thank you!

Original unedited question below, which while off topic, gives some motivational background for why I want to solve the above problem.

Everywhere I look, I can find calculators that measure partial correlations for 3 variables; a good example is this one: https://datatab.net/tutorial/partial-correlation . However, I can't seem to find any such calculator in R. Plus I need something that can handle more variables.

Currently the best approach I can find in R is to simulate a dataset with the known partial correlation coefficients and then I measure the covariance of the final data to get my answer for the needed correlation.

#generate x1 and x2 by drawing each from a standard normal distribution so they have the correlation specified by r, 0.1 in the case below.

x1x2 <- rnorm_multi(n=1000000, vars=2,mu=0,sd=1,r=0.1)


#generate y1 and y2, such that each of them has correlations with x1 and x2 as specified by r, which in this case means cor(x1, y1)=0.5 and cor(x2, y1)=0.4

y1<-rnorm_pre(x1x2, r = c(0.5, 0.4))
y2<-rnorm_pre(x1x2, r = c(0.5, 0.4))


#finally, measure the covariance of y1 and y2 to get the correlation I'm interested in.

cov(y1,y2)
[1] 0.372786


This works, I've used my correlation matrix for x1, x2, and y to determine the correlation between two different ys correlated with the same x value $$\begin{bmatrix}1&.1&.5\\.1&1&.4\\.5&.4&1\end{bmatrix}$$ Is there an R package to do these calculations? If not, are there any other approaches you can recommend?

-thank you

Quick search on CRAN shows ppcor package. It mentions derivation of a general matrix formula of the semi-partial correlation for fast computation. Also upon reading the documentation, corpcor is mentioned as well to compute partial correlation from a given correlation matrix.Further reading. Below are examples directly from ppcor and corpcor package

# load corpcor library
library("corpcor")

# covariance matrix
m.cov = rbind(
c(3,1,1,0),
c(1,3,0,1),
c(1,0,2,0),
c(0,1,0,2)
)
#       [,1] [,2] [,3] [,4]
# [1,]    3    1    1    0
# [2,]    1    3    0    1
# [3,]    1    0    2    0
# [4,]    0    1    0    2

# corresponding correlation matrix
m.cor.1 = cov2cor(m.cov)

#       [,1]      [,2]      [,3]      [,4]
# [1,] 1.0000000 0.3333333 0.4082483 0.0000000
# [2,] 0.3333333 1.0000000 0.0000000 0.4082483
# [3,] 0.4082483 0.0000000 1.0000000 0.0000000
# [4,] 0.0000000 0.4082483 0.0000000 1.0000000

# compute partial correlations (from covariance matrix)
m.pcor.1 = cor2pcor(m.cov)
#         [,1]       [,2]        [,3]        [,4]
# [1,]  1.0000000  0.4000000  0.43852901 -0.17541160
# [2,]  0.4000000  1.0000000 -0.17541160  0.43852901
# [3,]  0.4385290 -0.1754116  1.00000000  0.07692308
# [4,] -0.1754116  0.4385290  0.07692308  1.00000000

# compute partial correlations (from correlation matrix)
cor2pcor(m.cor.1)
#         [,1]       [,2]        [,3]        [,4]
# [1,]  1.0000000  0.4000000  0.43852901 -0.17541160
# [2,]  0.4000000  1.0000000 -0.17541160  0.43852901
# [3,]  0.4385290 -0.1754116  1.00000000  0.07692308
# [4,] -0.1754116  0.4385290  0.07692308  1.00000000

###############################################################

library(ppcor)
# data
y.data <- data.frame(
hl=c(7,15,19,15,21,22,57,15,20,18),
disp=c(0.000,0.964,0.000,0.000,0.921,0.000,0.000,1.006,0.000,1.011),
deg=c(9,2,3,4,1,3,1,3,6,1),
BC=c(1.78e-02,1.05e-06,1.37e-05,7.18e-03,0.00e+00,0.00e+00,0.00e+00
,4.48e-03,2.10e-06,0.00e+00)
)
# partial correlation
pcor(y.data)

# $$estimate # hl disp deg BC # hl 1.0000000 -0.6720863 -0.6161163 0.1148459 # disp -0.6720863 1.0000000 -0.7215522 0.2855420 # deg -0.6161163 -0.7215522 1.0000000 0.6940953 # BC 0.1148459 0.2855420 0.6940953 1.0000000 # #$$p.value
# hl       disp        deg         BC
# hl   0.00000000 0.06789202 0.10383620 0.78654997
# disp 0.06789202 0.00000000 0.04332869 0.49299871
# deg  0.10383620 0.04332869 0.00000000 0.05615021
# BC   0.78654997 0.49299871 0.05615021 0.00000000
#
# $$statistic # hl disp deg BC # hl 0.0000000 -2.2232666 -1.916030 0.2831875 # disp -2.2232666 0.0000000 -2.552768 0.7298173 # deg -1.9160295 -2.5527682 0.000000 2.3617433 # BC 0.2831875 0.7298173 2.361743 0.0000000 # #$$n
# [1] 10
#
# $$gp # [1] 2 # #$$method
# [1] "pearson"

# partial correlation between "hl" and "disp" given "deg" and "BC"
pcor.test(y.data$$hl,y.data$$disp,y.data[,c("deg","BC")])

# estimate    p.value statistic  n gp  Method
# 1 -0.6720863 0.06789202 -2.223267 10  2 pearson

# semi-partial (part) correlation
spcor(y.data)
#
# $$estimate # hl disp deg BC # hl 1.0000000 -0.5791734 -0.4991364 0.07377194 # disp -0.5505041 1.0000000 -0.6320921 0.18071040 # deg -0.3180603 -0.4237587 1.0000000 0.39204867 # BC 0.0669124 0.1724434 0.5580398 1.00000000 # #$$p.value
# hl      disp       deg        BC
# hl   0.0000000 0.1324601 0.2079431 0.8621787
# disp 0.1573861 0.0000000 0.0926718 0.6684724
# deg  0.4426360 0.2954469 0.0000000 0.3367589
# BC   0.8749132 0.6830213 0.1506047 0.0000000
#
# $$statistic # hl disp deg BC # hl 0.0000000 -1.7402746 -1.410960 0.1811974 # disp -1.6152392 0.0000000 -1.998086 0.4500579 # deg -0.8217590 -1.1459717 0.000000 1.0438882 # BC 0.1642694 0.4288224 1.647252 0.0000000 # #$$n
# [1] 10
#
# $$gp # [1] 2 # #$$method
# [1] "pearson"

• thank you! i never knew about the corpcor package, and it has amazing features. The problem for me, is it doesn't seem to be able to do calculations to solve for the missing matrix values. It expects either a full correlation matrix to convert to partial correlations, or it expects a full partial correlation matrix to create the correlation matrix. BUT, I have a mix of unknown values in each matrix. My correlation matrix is: 1, .1, .5, .5, .1, 1, .4, .4, .5, .4, 1, ??, .5, .4, ??, 1 and my pcor matrix is the opposite: 1 ?? ?? ?? ?? 1 ?? ?? ?? ?? 1 0 ?? ?? 0 1 Commented Oct 8, 2023 at 13:30