Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umxSuperModel: handle nesting better #221

Open
lf-araujo opened this issue Sep 7, 2023 · 4 comments
Open

umxSuperModel: handle nesting better #221

lf-araujo opened this issue Sep 7, 2023 · 4 comments

Comments

@lf-araujo
Copy link
Contributor

Super models with nested submodels are handled up to two, but no more:

     data(docData)

     mzData  = subset(docData, zygosity %in% c("MZFF", "MZMM")) |>
       dplyr::mutate(ethnicity = rep(LETTERS[1:5],140))

     dzData  = subset(docData, zygosity %in% c("DZFF", "DZMM")) |>
       dplyr::mutate(ethnicity = rep(LETTERS[1:5],140))

     # ============================
     # = 2. Make a MRDoC2 model   =
     # ============================

 outa <- umxMRDoC(name = "a", pheno = c("varA1", "varA2"), prss = c("varB1", "varA3") ,
                     mzData = mzData|>dplyr::filter(ethnicity=="A"),
       dzData = dzData|>dplyr::filter(ethnicity=="A")) |>
      umxModify( regex="*", newlabels ="a_\\1" ,  autoRun = F)

 outb <- umxMRDoC(name = "b", pheno = c("varA1", "varA2"), prss = c("varB1", "varA3") ,
                     mzData = mzData|>dplyr::filter(ethnicity=="B"),
       dzData = dzData|>dplyr::filter(ethnicity=="B")) |>
      umxModify( regex="*", newlabels ="b_\\1" ,  autoRun = F)

 outc <- umxMRDoC(name = "c", pheno = c("varA1", "varA2"), prss = c("varB1", "varA3") ,
                     mzData = mzData|>dplyr::filter(ethnicity=="C"),
       dzData = dzData|>dplyr::filter(ethnicity=="C")) |>
      umxModify( regex="*", newlabels ="c_\\1" ,  autoRun = F)

multi <- umxSuperModel("multigroup", outa, outb)

# THIS WORKS!, NOW:


multi <- umxSuperModel("multigroup", outa, outb, outc)


# doesn't
#  There is already a model named 'top_2'

The issue lies at the underlying renaming functionality, that is only checking up to two submodels.

@tbates
Copy link
Owner

tbates commented Sep 7, 2023

In umxSuperModel snippet (below) , or should this be a request to enhance OpenMx::mxRename?

tmpnames = umxModelNames(newModel)
dupes = tmpnames[duplicated(tmpnames)] # "top" "MZ" "DZ"
if(length(dupes) > 0){
	suffix = 2
	subNames = names(newModel$submodels)[-1]
	for(thisSub in subNames){
		thisModel = newModel$submodels[[thisSub]]
		for(thisDupName in dupes){
			thisModel = mxRename(thisModel, paste0(thisDupName, "_", suffix), oldname = thisDupName)
		}
		newModel = mxModel(newModel, thisModel)
		suffix = suffix + 1
	}

@lf-araujo
Copy link
Contributor Author

The issue is in the excerpt above, when the innermost for loop is run, if there is more than 2 models, suffix will not have increased and mxRename will complain. There is an ugly solution:

move up suffix = suffix + 1 to the innermost loop, three lines up. Which will result in:

> umxModelNames(multi)
 [1] "a"        "b"        "c"        "top"      "topMZ"    "topDZ"    "top_2"    "topMZ_3" 
 [9] "topDZ_4"  "top_8"    "topMZ_9"  "topDZ_10"

Still thinking in something more elegant.

@tbates
Copy link
Owner

tbates commented Oct 4, 2023

I'll have a look

@lf-araujo
Copy link
Contributor Author

lf-araujo commented Oct 4, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants