Selecting submatrix within functions

myFun <- function() {
x <- 2
cat("Matrix in R:\n")
A1 <- matrix(1:16, ncol=4, nrow=4)
B1 <- A1[1:x,1:x, drop=FALSE]
print(B1)
myModel <- mxModel("test",
A2 <- mxMatrix("Full", nrow=4, ncol=4, values=1:16, name="A2"),
B2 <- mxAlgebra(A2[1:x, 1:x], name="B2"))
cat("Matrix in mxModel:\n")
print(mxEval(B2, mxRun(myModel, silent=TRUE)))
}
myFun()
#Matrix in R:
# [,1] [,2]
#[1,] 1 5
#[2,] 2 6
#Matrix in mxModel:
#Error in print(mxEval(B2, mxRun(myModel, silent = TRUE))) :
# error in evaluating the argument 'x' in selecting a method for function 'print': Error: Unknown reference 'x' detected in the entity 'B2' in model 'test'
x <- 3
myFun()
#Matrix in R:
# [,1] [,2]
#[1,] 1 5
#[2,] 2 6
#Matrix in mxModel:
# [,1] [,2] [,3]
#[1,] 1 5 9
#[2,] 2 6 10
#[3,] 3 7 11
sessionInfo()
#R version 2.15.2 (2012-10-26)
#Platform: i686-pc-linux-gnu (32-bit)
#
#locale:
# [1] LC_CTYPE=en_SG.UTF-8 LC_NUMERIC=C
# [3] LC_TIME=en_SG.UTF-8 LC_COLLATE=en_SG.UTF-8
# [5] LC_MONETARY=en_SG.UTF-8 LC_MESSAGES=en_SG.UTF-8
# [7] LC_PAPER=C LC_NAME=C
# [9] LC_ADDRESS=C LC_TELEPHONE=C
#[11] LC_MEASUREMENT=en_SG.UTF-8 LC_IDENTIFICATION=C
#
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#
#other attached packages:
#[1] OpenMx_1.3.1-2179
#
#loaded via a namespace (and not attached):
#[1] tools_2.15.2
eval(parse(...
I think I got around this type of problem with the following:
for (k in 1:nItems) {
elementCovString <- paste("mxAlgebra(expression=expectedCovariances[",k,",",k,"], name='itemCovariances')")
elementCov <- eval(parse(text=elementCovString))
...
YMMV of course (not sure if it will work for you), but perhaps this will help.
Log in or register to post comments
In reply to eval(parse(... by neale
Thanks Michael and Ralf. I
I will use eval(parse()) for the time being.
Log in or register to post comments
Simple solution
x <<- 2
Otherwise there are possibilities to use, as Mike pointed out, paste and eval functions. Timothy Brick wrote about this in a previous thread: http://openmx.psyc.virginia.edu/thread/1096
Log in or register to post comments
Another way to do it is: B2
Log in or register to post comments