You are here

Selecting submatrix within functions

5 posts / 0 new
Last post
Mike Cheung's picture
Offline
Joined: 10/08/2009 - 22:37
Selecting submatrix within functions

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 x cat("Matrix in R:\n")
A1 B1 print(B1)
myModel A2 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 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

neale's picture
Offline
Joined: 07/31/2009 - 15:14
eval(parse(...

Hi Mike

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.

Mike Cheung's picture
Offline
Joined: 10/08/2009 - 22:37
Thanks Michael and Ralf. I

Thanks Michael and Ralf.

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

iloo's picture
Offline
Joined: 05/26/2010 - 09:44
Simple solution

I think the simplest solution would be to define x in the global environment, this can be done inside the function by using
x 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

mspiegel's picture
Offline
Joined: 07/31/2009 - 15:24
Another way to do it is: B2

Another way to do it is:

B2 
Log in or register to post comments