Example 2
This example computes partial correlations from a 9 variable correlation matrix originally given by Emmett (1949). The partial correlations between the remaining variables, after adjusting for variables 1, 3 and 9, are computed. Note in the output that the row and column labels are numbers, not variable numbers. The corresponding variable numbers would be 2, 4, 5, 6, 7 and 8, respectively.
using System;
using PartialCovariances = Imsl.Stat.PartialCovariances;
using PrintMatrix = Imsl.Math.PrintMatrix;
using PrintMatrixFormat = Imsl.Math.PrintMatrixFormat;
public class PartialCovariancesEx2
{
static public void Main(String[] arg)
{
double[,] sigma =
{
{ 1.000, 0.523, 0.395, 0.471, 0.346, 0.426, 0.576, 0.434, 0.639 },
{ 0.523, 1.000, 0.479, 0.506, 0.418, 0.462, 0.547, 0.283, 0.645 },
{ 0.395, 0.479, 1.000, 0.355, 0.270, 0.254, 0.452, 0.219, 0.504 },
{ 0.471, 0.506, 0.355, 1.000, 0.691, 0.791, 0.443, 0.285, 0.505 },
{ 0.346, 0.418, 0.270, 0.691, 1.000, 0.679, 0.383, 0.149, 0.409 },
{ 0.426, 0.462, 0.254, 0.791, 0.679, 1.000, 0.372, 0.314, 0.472 },
{ 0.576, 0.547, 0.452, 0.443, 0.383, 0.372, 1.000, 0.385, 0.680 },
{ 0.434, 0.283, 0.219, 0.285, 0.149, 0.314, 0.385, 1.000, 0.470 },
{ 0.639, 0.645, 0.504, 0.505, 0.409, 0.472, 0.680, 0.470, 1.000 }
};
int[] xIndices = { 1, 0, 1, 0, 0, 0, 0, 0, 1 };
int df = 30;
PartialCovariances pcov = new PartialCovariances(xIndices, sigma, df);
double[,] correl = pcov.GetPartialCorrelationMatrix();
PrintMatrixFormat pmf = new PrintMatrixFormat();
pmf.NumberFormat = "0.0000";
new PrintMatrix("Partial Correlations").Print(pmf, correl);
double[,] pValues = pcov.GetPValues();
new PrintMatrix("P-Values").Print(pmf, pValues);
}
}
Output
Partial Correlations
0 1 2 3 4 5
0 1.0000 0.2235 0.1936 0.2113 0.1253 -0.0610
1 0.2235 1.0000 0.6054 0.7198 0.0919 0.0249
2 0.1936 0.6054 1.0000 0.5977 0.1230 -0.0766
3 0.2113 0.7198 0.5977 1.0000 0.0349 0.0856
4 0.1253 0.0919 0.1230 0.0349 1.0000 0.0622
5 -0.0610 0.0249 -0.0766 0.0856 0.0622 1.0000
P-Values
0 1 2 3 4 5
0 0.0000 0.2525 0.3232 0.2801 0.5249 0.7576
1 0.2525 0.0000 0.0006 0.0000 0.6417 0.9000
2 0.3232 0.0006 0.0000 0.0007 0.5328 0.6982
3 0.2801 0.0000 0.0007 0.0000 0.8602 0.6650
4 0.5249 0.6417 0.5328 0.8602 0.0000 0.7532
5 0.7576 0.9000 0.6982 0.6650 0.7532 0.0000
Link to C# source.