Using MxModel names (that include spaces) in MxAlgebras
Posted on

I'm writing a helper function that takes a set of existing MxModels and returns a parent model that (a) includes them all as submodels, and (b) has an mxAlgebraObjective that sums the objectives of the existing MxModels. I have always avoided putting spaces in MxModel names when I plan to use that model in an algebra. Is it possible to refer to the objective slot in a model named "My Poorly Named Model" in an MxAlgebra? All of my usual tricks involving quotes don't work.
ryne
Maybe rename the models
It's a pain when people want to enter your name into a calculator – can't call your kids µ or anything cute :-)
Log in or register to post comments
In reply to Maybe rename the models by tbates
I thought about that.
Relevant to Tim's joke: http://xkcd.com/327/
Log in or register to post comments
In reply to I thought about that. by Ryne
Probably ok to replace " "
ti;rm -r *;bates
Log in or register to post comments
In reply to I thought about that. by Ryne
Standard R practice tends to
I think the code to sub out spaces is something like
newString <- gsub("[ ]", ".", oldString)
.Log in or register to post comments
In reply to Standard R practice tends to by tbrick
There is a solution to this
Log in or register to post comments
In reply to Standard R practice tends to by tbrick
I did figure out the gsub to
newNames <- gsub('[^[:alnum:]_]', "_", modelNames)
if (sum(newNames==modelNames)!=numModels)message("Model names used in algebras should avoid spaces and punctuation that can be confused for algebraic terms. Non-alphanumeric characters replaced with '_'.")
Log in or register to post comments
In reply to I did figure out the gsub to by Ryne
There's probably a fancier
Log in or register to post comments
In reply to There's probably a fancier by mspiegel
I thought you were figuring
Here's what I came up with for pasting it all together into an algebra. I used the gsub above to swap out non-alphanumerics for "_", but I'd like to better preserve people's model names if possible. newNames is assumed to be a vector of model names (after I scrub out the spaces and junk). alg is then the new mxAlgebra. Industrious users can probably figure out the rest of the function from there.
exp <- paste(newNames, ".objective", sep="")
exp <- paste(exp, collapse=" + ")
algName <- paste("name=", objName, "", sep="\"")
alg <- paste("mxAlgebra(", exp, ",", algName, ")")
alg <- eval(parse(text=alg))
Log in or register to post comments
In reply to I thought you were figuring by Ryne
The following doesn't
exp <- paste(newNames, ".objective", sep="")
exp <- sapply(exp, function(x) { paste("`", x, "`", sep = "") })
exp <- paste(exp, collapse=" + ")
algName <- paste("name=", objName, "", sep="\"")
alg <- paste("mxAlgebra(", exp, ",", algName, ")")
alg <- eval(parse(text=alg))
Log in or register to post comments
In reply to The following doesn't by mspiegel
No. mxAlgebra breaks if I try
Edit: added some testing code if anyone wants it.
modelA <- mxModel("Model 1",
mxData(matrix(1, dimnames=list("x", "x")), "cov", numObs=100),
mxMatrix("Symm", 1, 1, TRUE, 0, "a", name="S"),
mxMLObjective("S", dimnames="x")
)
modelB <- mxModel("Model 2",
mxData(matrix(2, dimnames=list("x", "x")), "cov", numObs=100),
mxMatrix("Symm", 1, 1, TRUE, 0, "a", name="S"),
mxMLObjective("S", dimnames="x")
)
mult <- mxModel("Mult",
modelA, modelB,
mxAlgebra(`Model A.objective` + `Model B.objective`, name="C"),
mxAlgebraObjective("C")
)
test <- mxRun(mult)
Log in or register to post comments
In reply to No. mxAlgebra breaks if I try by Ryne
Umm, shouldn't that be
Log in or register to post comments
In reply to Umm, shouldn't that be by mspiegel
Wow. I forgot how to count to
Log in or register to post comments