Partial Correlation is used to measure the relationship between two different variables by eliminating the third variable. The partial correlation matrix calculates the coefficients of partial correlation of a matrix. In the partial correlation matrix row i and column j have a partial correlation with the row i and column j of the original matrix. In this article, we will discuss calculating a partial correlation matrix in Excel VBA.
Following are the steps to calculate the partial correlation matrix with Excel VBA are:
Option Explicit
Function Partial_Cor(R)
Dim row As Integer, col As Integer
Dim ident() As Double, rowDiag() As Double, rowDiagSQRT() As Double,
Part_Corr() As Double, Negative_Cor As Variant, rowinverse As Variant
Dim i As Integer, j As Integer
row = R.row.Count
col = R.Columns.Count
rowinverse = Application.Minverse(R)
ReDim ident(1 To row, 1 To col)
ReDim rowDiag(1 To row, 1 To col)
ReDim rowDiagSQRT(1 To row, 1 To col)
ReDim Part_Corr(1 To row, 1 To col)
For i = 1 To row
For j = 1 To col
ident(i, j) = 0
rowDiag(i, j) = 0
if i = j Then
ident(i, j) = 1
rowDiag(i, j) = 1 / rowinverse(i, j)
rowDiagSQRT(i, j) = rowDiag(i, j) ^ 0.5
End if
Next j
Next i
Negative_Cor= (Application.MMult(rowDiagSQRT, Application.MMult(rowinverse, rowDiagSQRT)))
For i = 1 To row
For j = 1 To col
Part_Corr(i, j) = ident(i, j) - Neg_Cor(i, j)
Part_Corr(i, i) = -1
Next j
Next i
Partial_Cor = Part_Corr
End Function