tidySEM icon indicating copy to clipboard operation
tidySEM copied to clipboard

How to fix exogenous variables in OpenMx?

Open cjvanlissa opened this issue 2 years ago • 6 comments

The top example does not work, the bottom one does - but it is inconvenient to specify the variance of exogenous variables by hand:

mod <- as_ram("y1 ~ a*Species
              y1~~b*y1", fixed.x = TRUE)
res <- run_mx(mod, data = df)
mod <- as_ram("y1 ~ a*Species
              y1~~b*y1
              Species ~~ .25*Species", fixed.x = TRUE)
res <- run_mx(mod, data = df)

cjvanlissa avatar Dec 01 '22 13:12 cjvanlissa

@maugavilla this is a real headscratcher for me... do you have ideas?

cjvanlissa avatar Dec 01 '22 13:12 cjvanlissa

I think a possible solution is to fix the $values slot of the S model matrix for the exogeneours variables to the observed variance in the data. Because right now their variance is not included at all when fixed.x=T, but we need to include it, but as a fixed value.

Not sure where to add it into the as.ram() parser, but something like this

` dat <- iris colnames(dat) <- c("SepalLength","SepalWidth", "PetalLength","PetalWidth","Species")

mod <- as_ram("SepalLength ~ aPetalLength SepalLength ~~bSepalLength", fixed.x = T) mod

mod$S$values["PetalLength","PetalLength"] <- var(dat$PetalLength)

res <- run_mx(mod, data = dat) summary(res)`

Hope this makes sense

maugavilla avatar Dec 07 '22 12:12 maugavilla

I've implemented a fix that does this, but only for the variance of observed variables. Not sure if that is correct, what do you think?

cjvanlissa avatar Dec 08 '22 10:12 cjvanlissa

I think this makes sense, and matches how lavaan handles is in several parts. For example, I get the same estimates, number of parameters, df. But the logLik is different. Which makes me think that lavaan ignores the X side of the equations for the logLik estimation. While OpenMx still includes them, leading to the discrepancy in fit but on model parameters

` library(tidySEM)

dat <- iris colnames(dat) <- c("SepalLength","SepalWidth", "PetalLength","PetalWidth","Species")

mod <- as_ram("SepalLength ~ aPetalLength SepalLength ~~bSepalLength", fixed.x = T) mod mod$S #mod$S$values["PetalLength","PetalLength"] <- var(dat$PetalLength)

res <- run_mx(mod, data = dat) summary(res)

library(lavaan)

ss <- sem("SepalLength ~ aPetalLength SepalLength ~~bSepalLength", data=dat, meanstructure=T, fixed.x=T) summary(ss, fit.measures=T) parameterestimates(ss, ci=T)

logLik(res) logLik(ss) `

maugavilla avatar Dec 08 '22 11:12 maugavilla

Any thoughts about what the consequences of this are for different types of models, and whether we should do anything about this?

cjvanlissa avatar Dec 08 '22 14:12 cjvanlissa

We would need to adjust the fit measures in OpenMx to ignore the x side. Which I dont think we can, or would be easy. I would suggest to add a warning, when fixed.x =T, saying tha fit indices might be unreliable in OpenMx

maugavilla avatar Dec 08 '22 15:12 maugavilla