Create a function to generate mxMatrix and mxAlgebra

Hi everyone,
I am writing an R function which can create and run an OpenMx model. I can do it in a simple way. Partial codes are shown below. However, I have over 20 definition variables and for each I need corresponding mxAlgebra. I may want to write a function/a loop to generate mxMatrix and mxAlgebra. I've tried a couple of methods, yet none of them worked. The mainly problem is that in the expression of mxAlgebra, I need call the name of corresponding mxMatrix. Thank you in advance!
mxMatrix("Full", 1, 1, free = T, values = 7, labels = "mu", name = "g"),
mxMatrix("Full", 1, 1, free = F, labels = "data.T1", name = "t1"),
mxMatrix("Full", 1, 1, free = F, labels = "data.T2", name = "t2"),
mxMatrix("Full", 1, 1, free = F, labels = "data.T3", name = "t3"),
mxMatrix("Full", 1, 1, free = F, labels = "data.T4", name = "t4"),
...
mxAlgebra(abs(t1 - g), name = "L20"),
mxAlgebra(abs(t2 - g), name = "L21"),
mxAlgebra(abs(t3 - g), name = "L22"),
mxAlgebra(abs(t4 - g), name = "L23"),
...
mxAlgebraFromString
Does this solve your need?
Log in or register to post comments
In reply to mxAlgebraFromString by jpritikin
thanks
Yes, it is my need. Thanks for your advice.
Log in or register to post comments
Elaboration
To elaborate on what Joshua wrote
mlist <- list()
alist <- list()
maxt <- 5
for(i in 1:maxt){
mlist[[i]] <- mxMatrix('Full', 1, 1, free=FALSE, labels=paste0('data.T', i), name=paste0('t', i))
alist[[i]] <- mxAlgebraFromString(paste0("abs(t", i, " - g)"), name=paste0("L2", i-1))
}
This creates `mlist`, a list of MxMatrix objects, and `alist`, a list of MxAlgebra objects.
> mlist
[[1]]
FullMatrix 't1'
$labels
[,1]
[1,] "data.T1"
$values
[,1]
[1,] 0
$free: No free parameters.
$lbound: No lower bounds assigned.
$ubound: No upper bounds assigned.
[[2]]
FullMatrix 't2'
$labels
[,1]
[1,] "data.T2"
$values
[,1]
[1,] 0
$free: No free parameters.
$lbound: No lower bounds assigned.
$ubound: No upper bounds assigned.
[[3]]
FullMatrix 't3'
$labels
[,1]
[1,] "data.T3"
$values
[,1]
[1,] 0
$free: No free parameters.
$lbound: No lower bounds assigned.
$ubound: No upper bounds assigned.
[[4]]
FullMatrix 't4'
$labels
[,1]
[1,] "data.T4"
$values
[,1]
[1,] 0
$free: No free parameters.
$lbound: No lower bounds assigned.
$ubound: No upper bounds assigned.
[[5]]
FullMatrix 't5'
$labels
[,1]
[1,] "data.T5"
$values
[,1]
[1,] 0
$free: No free parameters.
$lbound: No lower bounds assigned.
$ubound: No upper bounds assigned.
> alist
[[1]]
mxAlgebra 'L20'
$formula: abs(t1 - g)
$result: (not yet computed) <0 x 0 matrix>
dimnames: NULL
[[2]]
mxAlgebra 'L21'
$formula: abs(t2 - g)
$result: (not yet computed) <0 x 0 matrix>
dimnames: NULL
[[3]]
mxAlgebra 'L22'
$formula: abs(t3 - g)
$result: (not yet computed) <0 x 0 matrix>
dimnames: NULL
[[4]]
mxAlgebra 'L23'
$formula: abs(t4 - g)
$result: (not yet computed) <0 x 0 matrix>
dimnames: NULL
[[5]]
mxAlgebra 'L24'
$formula: abs(t5 - g)
$result: (not yet computed) <0 x 0 matrix>
dimnames: NULL
Log in or register to post comments
In reply to Elaboration by mhunter
Codes worked well
Thanks for your kind advice. I really appreciate it.
Log in or register to post comments