Attachment | Size |
---|---|
OpenMx Single Level Model ESA.R | 2.18 KB |
Snapshot Data.PNG | 30.53 KB |
Hello all,
I posted this question in the Mixed Effects and Nested Models discussion board, but haven't heard back in a few weeks and my analysis is a bit time-dependent.
I know similar questions have been posted here but I'm still unable to figure out the code for my model, as I am super new to OpenMx and structural equation modeling in general. I have data from a cluster-randomized RCT, collected at three time-points on children nested within schools. I would like to look at the effect of the intervention over time for the children, but taking the clustering from the schools into account. I'm trying to get my structural equation model to match this mixed model (with the data in long format) from the nlme package:
MixedModel <- lme(ESA ~ Time + trtGrp + Time*trtGrp,
random = list(School = ~1, idChild = ~1),
weights =varIdent(School),
corr = corCompSymm(form= ~Time),
data=DataSet,
method = "REML")
where ESA is the outcome (emotional self-awareness), Time is as a factor variable (so gives the slopes from Time 1 [ref] to Time 2, and Time 1 to Time 3), trtGrp is the intervention group, and the variance/covariance matrix for the three time-points is compound symmetry. The intercept is random ONLY for Schools. The intercept is fixed for Children, and the slope is fixed for both Children and School.
For structural equation modeling, I've been using a latent growth curve, with the data in wide format. Since the measures are not linear from T1 to T3, I've created two slopes (t1 to t2, and t1 to t3). The bit I'm struggling with is adding a random intercept for schools in OpenMx. I've attached my script to this post, as well as a screenshot of part of my data.
With this model, I can match everything to my mixed model except for the school intercept variance (since I haven't added it in here). I have no idea how to add another level, or how to add it so that the only thing it does is give a random intercept for School. Someone did respond to my original post and pointed me to this example: https://github.com/OpenMx/OpenMx/blob/master/inst/models/passing/xxm-2.R
However, I'm still unable to figure out how to make this work for my own data. Again, all I really need is for the intercept to be random at the School level, everything else is a fixed effect. If anyone is able to help, I would greatly appreciate it!
Thanks so much!
Can you provide a small subset of the data so that this script is a complete executable example? Providing some data as an image is not helpful.
Sorry for the delay, I thought I responded last week but must not have submitted. I clearly misunderstood what you meant by subset, sorry about that! I attached my data in csv format, is that okay? Please let me know if you're unable to use this.
I believe my data needs to be in wide format since it's repeated measures (and I want the covariance matrix to be compound symmetry) and I want two separate slopes from T1 to T2 and T1 to T3. Thanks so much for your help!
Maybe this is the model you want?
These two are three-level models:
xxm-3.R
xxm-4.R
If you're just having trouble extending to a 3-level model as an SEM, then these may help. The data are all structured as "tall", not "wide".
Dear OpenMx Community,
I am a first-time poster to the forum. I am advanced in R, moderately so in SEM but a newbie for OpenMx. Thanks to the admin for creating an account for me so I can learn from and contribute to the OpenMx community.
This request pertains to helping with the syntax for a 3-level model: people over time but in teams. So, I post under this topic.
I attach "TheIdea.jpg" which is a theorized plot of my idea. Specifically, I seek to test if the gender of team members leads to an assessment that is predictably lower or higher than an anticipated curvilinear (inverted-U) assessment over time. I also attach SEMModel-Sought.jpg. This is what I think the SEM model should look like, the model that I have tried to create in OpenMx. I include this picture, as my error may be conceptual.
I have sought guidance from the examples from this topic as well as the online MPlus user guide. I have also chatted with avid uses of Mplus... but to no avail.
In attempting to generate OpenMX R Code, I was able (I think) to confirm the Level 1 and Level 2 models that I sought to produce. These are MxModelLv1.jpg and MxModelLv2.jpg respectively. A tangent: Is there a better way to diagram a 3 level (or more) model for OpenMx?
I paste the RCode I have been creating below and I attach an excerpt of the data for your reference (DataIndividualForOpenMx.csv).
Any guidance would be appreciated.
Happy New Year!
Do you have a specific question, or do you just want us to painstaking inspect your 300-line model script?
To be clear, we are happy to help. I just do not understand what you are asking for.
AdminHunter,
Thank you for your reply. I apologize for my delayed post. I just became aware of your reply yesterday. I will now make a point of manually checking back at a much quicker rate. Anyway...
It seems that your 2 biggest concerns were: (1) focus of inquiry and (2) length of code excerpt. Let me try to address each in turn...
1)Focus of inquiry: Whenever I "link" between levels OpenMx replies: "Error: Foreign key XXX' not found in XXX.dat"
However, when I check the Data of Level 2 (~Team~) and Level 1 (~Individual~) the variable I designate as the "Foreign key" is present. I think there is a syntax issue that I cannot find. If willing, please check the code starting with "Lv2Data<-mxData(" and "Lv1Data<-mxData(". Am I designating the MxData assignment correctly? There may may also be a issue with the code under "####Connect Lv1Data to Lv2Data". Is there something else you have in mind that might cause an error related to "linking" between levels?
2) length of code excerpt: To address this concern, I removed the slope and square terms. It reduces the code somewhat. Stepping away from the lose in conceptual sense, I hope this reduced code and accompanying graphs help diagnose what I think is a syntax problem. I feel limited in my ability to reduce further, as I want to provide a "reproducible error". If you think there is a way to further reduce the code length... just let me know and I am willing to do so... and willing to do so promptly.
I often have problems with "starting values". There are rules of thumb etc that I try to follow. I understand that mxAutoStart() can help with this issue. Can you confirm I am using it correctly?
PS: I live and work in a developing nation. Though MPlus etc may have more simple syntax, PhD students and other researchers here are eager to learn OpenMx because it helps them cross a paywall that they cannot otherwise cross. Think of me as a bridge... So... we all thank you for working through this with me.
The reduced code is below:
Sorry... for your convenience, I re-post the data.
Ahh. I understand the question and know the solution now. Thank you for clarifying!
mxAutoStart()
does not work for multilevel models (any model that uses foreign keys). The immediate solution for you is to just not use that function. The model ran fine for me without it.The immediate solution for the OpenMx development team is to catch this situation and give an informative and useful error message. Longer term we might extend
mxAutoStart()
to multilevel models, but have no current plans to do so.Sorry for the uninformative error!
Thank you for the prompt response. I will try without mxAutoStart(). I am glad to know you could run it without mxAutoStart(). Is there any guidance that you can provide about starting values for multi-level models? Will the same "rules of thumb" work for multi-level models as for "single" level models?
The same general guidance applies to multi-level models as other models. (1) Make sure the variances are positive, (2) Make sure the covariances and regression effects are not too large, (3) Make sure the means are plausibly close to the sample means.