Wed, 11/14/2012 - 22:48

I am writing some functions that need to select some submatrices with variable indices. Let's consider the following sample code. The issue is that mxModel() only recognizes x in the gobal environment rather than inside a function. Have I missed something? Thanks.

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

Another way to do it is:

`B2 <- eval(substitute(mxAlgebra(A2[1:val, 1:val], name="B2"), list(val = x)))`

I think the simplest solution would be to define x in the global environment, this can be done inside the function by using

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

Hi Mike

I think I got around this type of problem with the following:

YMMV of course (not sure if it will work for you), but perhaps this will help.

Thanks Michael and Ralf.

I will use eval(parse()) for the time being.