matrix redimensioning

3 replies [Last post]
tbrick's picture
Offline
Joined: 07/31/2009

We've had a request for a function that changes the dimensions of a matrix.

Maybe mxRedim(mat, row, col)? Conformance would require that the total number of elements remain constant. That is, mat->rows * mat->cols == row * col.

Semantics would probably fill in by columns.

tbates's picture
Offline
Joined: 07/31/2009
Not sure what the utility of

Not sure what the utility of this is... but sounds like a function for the genEpi_ library?

genEpi_Redim(mxMatrix, newRows, newCols)

Can anyone say why it is helpful?

neale's picture
Offline
Joined: 07/31/2009
Yes, suppose you wish to fit

Yes, suppose you wish to fit a 3 factor model but currently have a matrix-style script for a 2 factor model in which the factor loadings are in the two columns of matrix L. A redim to 3 columns would add a factor (though some zeroing of a couple of elements may be necessary for model identification).

Steve's picture
Offline
Joined: 07/30/2009
Given the nature of R's

Given the nature of R's pass-by-value, a new matrix would need to be built anyway. So, it seems as if a general solution might be to implement something like (and yes, I know this is bizarre, so maybe there is a better way to implement it):

myMatrix1 <- mxMatrix("Full", nrows=2, ncols=2, values=1:4, name="A")
myMatrix2 <- mxMatrix("Full", nrows=4, ncols=4, values=1:16, name="B")
myMatrix2[2:3,2:3] <- myMatrix1

This would place the 2x2 matrix A into the submatrix [2:3,2:3] of matrix B. This makes sense from an R-ish perspective, but I'm not so sure that it could be implemented without a lot of hackery.

All of this argues for a style of model building where you create your mxMatrix pieces ahead of time. Lately I've been experimenting with this style to see how it works. It tends to look something like this in practice:

numberIndicators <- 40
numberFactors <- 3
totalVars <- numberIndicators + numberFactors
 
loadingLabels <- paste("b_F", rep(1:numberFactors, each=numberIndicators), 
                              rep(indicators, numberFactors), sep="") 
 
AMatrixFree <- matrix(FALSE, totalVars, totalVars)
AMatrixFree[1:numberIndicators,(numberIndicators+1):totalVars] <- TRUE
AMatrixVals <- matrix(0, totalVars, totalVars)
AMatrixVals[1:numberIndicators,(numberIndicators+1):totalVars] <- .2
AMatrixLabels <- matrix(NA, totalVars, totalVars)
AMatrixLabels[1:numberIndicators,(numberIndicators+1):totalVars] <- loadingLabels
 
    mxMatrix("Full", totalVars, totalVars, 
        labels=AMatrixLabels, 
        free=AMatrixFree, 
        values=AMatrixVals,
        name="A", 
        byrow=TRUE
    ),

This is an example of the specification of an A matrix that is entirely independent of how many factors and indicators in a fully cross-loaded factor model. This is currently easier in path-style, but sometimes one needs to use matrices.

The advantage is that I can change just about anything and the just pump it into a new mxMatrix of any order. The disadvantage is having to keep track of the three sets of component matrices in the mxMatrix.