reposting, I must have accidentally deleted the original post:
I am writing a teaching and demonstration script that will:
- Create a random covariance matrix of a specified size, and calculate the corresponding correlation matrix (C)
- Create random multivariate normal data with a specified number of rows and mean vector, based on the covariance matrix
- output the simulated data to a data table and run the multivariate platform analysis on that data
what I also want to include in the output of the script, in the report window, is a display of the starting correlation matrix (C) that I had created, and the corresponding color map, similar to what is shown in the multivariate platform for the estimated correlation matrix. I cannot figure out how to create the axis boxes for the correlation (C) matrix, nor how to create the color map on correlations of the C matrix.
Here is my script:
Names Default To Here( 1 );
//simulate multivariate normal data with random correlation
//number of rows of data
n = 10;
// number of variables
k = 20;
//create list of variable names
varlist = {};
For( ii = 1, ii <= k, ii++,
Insert Into( varlist, "X" || Char( ii ) )
);
//create mean vector
meanvec = J( k, 1, 0 );
//create random M matrix
M = Diag( J( k, 1, 10 + Random Normal() ) );
//make empty E matrix
E = [];
//fill the E matrix
For( ii = 1, ii <= k, ii++,
// create random eigenvector
vec = J( k, 1, Random Uniform() - (0.5) );
// normalize it
vec = vec / Sqrt( vec` * vec );
//append to E matrix
E = E || vec;
);
// create covariance matrix from eigenvector (E) matrix and eigenvalue (M) matrix
covariance = E * M * E`;
// create correlation matrix by scaling covariance by the diagonal elements
correlation = J( k, k, . );
For( ii = 1, ii <= k, ii++,
For( jj = 1, jj <= k, jj++,
correlation[ii, jj] = covariance[ii, jj] / (Sqrt( covariance[ii, ii] * covariance[jj, jj] ))
)
);
// simulate multivariate normal data
data = J( n, k, Random Normal() );
data = data * Sqrt( Diag( M ) ) * E`;
// make into data table
_dt_out = As Table( data, <<column names( varlist ) );
_dt_out << Set Name( "Simulated Multivariate Normal Data" );
// make an expression to create the run the multivariate platform on the simulated data table
createMultivariateExpr = Expr(
xvarstring = "";
For( ii = 1, ii <= k - 1, ii++,
xvarstring = xvarstring || ":" || varlist[ii] || ", "
);
xvarstring = xvarstring || ":" || varlist[ii];
multivarstring = "
Multivariate(
Y(" || xvarstring ||
" ),
Estimation Method( \!"Row-wise\!" ),
Matrix Format( \!"Square\!" ),
Scatterplot Matrix(
Density Ellipses( 1 ),
Shaded Ellipses( 0 ),
Ellipse Color( 3 )
),
Color Map on Correlations( 1 ),
SendToReport( Dispatch( {}, \!"Scatterplot Matrix\!", OutlineBox, {Close( 1 )} ) )
)
";
Eval( Parse( multivarstring ) );
);
New Window( "correlations",
V List Box(
Outline Box( "Random Starting Correlation Structure",
V List Box(
Outline Box( "Correlation Matrix (C)",
Text Box( "how to add axis boxes?" ),
_mb = Matrix Box( correlation )
//how do I get axis boxes alongside and above the matrix box?
),
Outline Box( "Color Map on Correlation Matrix (C)",
// how to do this???
Text Box( "How do I do this?" )
)
)
),
Outline Box( "Multivariate Analysis on Simulated Data", createMultivariateExpr )
)
);
_mb << Set Conditional Format( "Correlation" );
Here is an example output: