%mathpiper,def="CorrelationMatrix"

CorrelationMatrix(dataLists) := 
[
    Local(namesList, correlationMatrix);
    
    ForEach(dataList, dataLists)
    [
        Check(IsMatrix(dataLists), "Argument", "All lists must have the same number of elements.");
    ];

    namesList := MatrixColumn(dataLists,1);
    
    namesList := "" : namesList;
    
    ForEach(dataList, dataLists)
    [
        PopFront(dataList);
    ];
    
    correlationMatrix := ZeroMatrix(Length(dataLists)+1);
    
    ForEach(rowIndex, 1 .. Length(dataLists) + 1)
    [
        ForEach(columnIndex, 1 .. Length(dataLists) + 1)
        [
            if(rowIndex >= 2 And columnIndex >= 2)
            [
                correlationMatrix[rowIndex][columnIndex] := N(CorrelationCoefficient(dataLists[rowIndex - 1],dataLists[columnIndex - 1]),2);
            ]
            else if(rowIndex = 1)
            [
                correlationMatrix[rowIndex][columnIndex] := namesList[columnIndex];
            ]
            else
            [
                correlationMatrix[rowIndex][columnIndex] := namesList[rowIndex];
            ];
        ];
    ];
    
    correlationMatrix;
];

%/mathpiper




%mathpiper_docs,name="CorrelationMatrix",categories="User Functions;Statistics & Probability",access="experimental"
*CMD CorrelationMatrix --- creates a correlation matrix
*STD
*CALL
	CorrelationMatrix(listOfLists)

*PARMS

{listOfLists} -- a list of lists which contains data to be correlated

*DESC
Creates a correlation coefficient matrix from a list of lists which contain data values.
The first element in each list is the title for the data in that list.  The
CorrelationCoefficient function is used to calculate the individual correlations.

*E.G.
/%mathpiper

dataLists :={
{"Age",25,16,8,23,31,19,15,31,21,26,24,25,36,45,16,23,31,53,11,33},
{"Level",1,2,2,3,4,4,4,5,1,1,5,5,4,4,4,1,2,2,3,2},
{"Score",78,66,78,89,87,90,98,76,56,72,84,87,69,87,88,92,97,69,79,69},
};

CorrelationMatrix(dataLists);

/%/mathpiper

    /%output,preserve="false"
      Result: {{"","Age","Level","Score"},{"Age",1,0.056,-0.15},{"Level",0.056,1,0.39},{"Score",-0.15,0.39,1}}
.   /%/output

*SEE CorrelationCoefficient
%/mathpiper_docs

    %output,preserve="false"
      
.   %/output


