Skip to content

Instantly share code, notes, and snippets.

@lf-araujo
Last active September 28, 2022 18:37
Show Gist options
  • Save lf-araujo/93c3b4959b7a51f2540d617cdc09e682 to your computer and use it in GitHub Desktop.
Save lf-araujo/93c3b4959b7a51f2540d617cdc09e682 to your computer and use it in GitHub Desktop.
Wrap no more

Wrap no more

The ggplot2 plus syntax is a much better fit for what I was attempting. For model building, summing models makes more sense than piping them. And also, the code needed to implement this is much shorter. So for something like:

mb <- mxModel("bivariate Heterogeneity Path Specification", 
    type = "RAM",
    manifestVars = c('X','Y')) +
    mxPath( from=c('X','Y'), arrows=2, free=T, values=1, lbound=.01 ) +
    mxPath( from="X", to="Y", arrows=2, free=T, values=.2, lbound=.01) +
    mxPath( from="one", to=c("X", 'Y'), arrows=1, free=T, values=c(0.1,-0.1),
    ubound=c(NA,0), lbound=c(0,NA))

mb <- mxGenerateData(mb, nrows = 1000, returnModel = T)
out <- mxRun(mb)
summary(out)

Or want to separate the data object from the model building part?

model <- twinData |>
  dplyr::select(ht1, bmi1, zygosity) |>
  mxData(type = "raw") |>
  mxModel(name ="test", manifestVars = c("ht1", "bmi1"), type = "RAM") +
    mxPath( from="one", to=c("ht1","bmi1"), arrows=1,  free=TRUE,  values=c(1,1) ) +
    mxPath( from="ht1", to="bmi1", arrows=1,  free=TRUE, values=1) +
    mxPath( from=c("ht1","bmi1"), arrows=2,  free=TRUE, values = c(1,1) ) 


run <- mxRun(model)
summary(run)

You just need to add a new function into the MxModel method:

`+.MxModel`  <- function (e1, e2) {
  mxModel(e1, e2)
}

Three lines!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment