Attachment | Size |
---|---|
Plan E Dissertation model general symptom building step 3 new way with latent.pdf | 230.44 KB |
Dissertation model anxiety step 4.pdf | 155.35 KB |
Hello!
I have a question about how to calculate r-squared for two different kinds of models. One is correlational and the other models change over time, with just two time points. They both include latent constructs, with anxiety and depression in both models as latent variables derived from four items each. The models are identified and have a relatively good fit CFI of .944 and .954, respectively.
1) "Plan E" Correlational Model (attached visual)
I would like to have r-squared values to represent how much this model in entirety accounts for variance in each of three outcomes (anxiety, depression, and alcohol consumption)--as well as how much of the variance in each of these three outcomes is accounted for by each of the predictors (neuroticism, DT, and AS).
Here's how I built the model:
manifests <- c("transformed_Time3_consumption_conv_5_6", "Time4depression_1c", "Time4depression_1d", "Time4depression_1e", "Time4depression_1g", "Time4anxiety_1a", "Time4anxiety_1b", "Time4anxiety_1f", "Time4anxiety_1h", "per_neurot_y1f_centered", "asi3_total_centered", "dts_total_centered") mydata10[ ,manifests] latents<- c("Time4anxiety", "Time4depression" ) myModelplanEstep3<-mxModel(model="planEstepthree", type="RAM", manifestVars=manifests, latentVars=latents, mxPath(from="Time4anxiety", to="Time4anxiety_1a", arrows=1, free=TRUE, values=0.8476268, labels=c("anxanx1a")), mxPath(from="Time4anxiety", to="Time4anxiety_1b", arrows=1, free=TRUE, values=0.8577030, labels=c("anxanx1b")), mxPath(from="Time4anxiety", to="Time4anxiety_1f", arrows=1, free=TRUE, values=0.8541549, labels=c("anxanx1f")), mxPath(from="Time4anxiety", to="Time4anxiety_1h", arrows=1, free=TRUE, values=0.8378001, labels=c("anxanx1h")), mxPath(from = "Time4anxiety_1a", to = "Time4anxiety_1a", arrows = 2, free=TRUE, values = 0.5647315, labels=c("anx1avar")), mxPath(from = "Time4anxiety_1b", to = "Time4anxiety_1b", arrows = 2, free=TRUE, values = 0.2175539, labels=c("anx1bvar")), mxPath(from = "Time4anxiety_1f", to = "Time4anxiety_1f", arrows = 2, free=TRUE, values = 0.5079372, labels=c("anx1fvar")), mxPath(from = "Time4anxiety_1h", to = "Time4anxiety_1h", arrows = 2, free=TRUE, values = 0.5221816, labels=c("anx1hvar")), mxPath(from="Time4depression", to="Time4depression_1c", arrows=1, free=TRUE, values=0.9480274, labels=c("depdep1c")), mxPath(from="Time4depression", to="Time4depression_1d", arrows=1, free=TRUE, values=0.8447853, labels=c("depdep1d")), mxPath(from="Time4depression", to="Time4depression_1e", arrows=1, free=TRUE, values=0.9699102, labels=c("depdep1e")), mxPath(from="Time4depression", to="Time4depression_1g", arrows=1, free=TRUE, values=1.0418291, labels=c("depdep1g")), mxPath(from = "Time4depression_1c", to = "Time4depression_1c", arrows = 2, free=TRUE, values = 0.4604164, labels=c("dep1cvar")), mxPath(from = "Time4depression_1d", to = "Time4depression_1d", arrows = 2, free=TRUE, values = 0.8118473, labels=c("dep1dvar")), mxPath(from = "Time4depression_1e", to = "Time4depression_1e", arrows = 2, free=TRUE, values = 0.5773899, labels=c("dep1evar")), mxPath(from = "Time4depression_1g", to = "Time4depression_1g", arrows = 2, free=TRUE, values = 0.7959805, labels=c("dep1gvar")), mxPath(from="Time4anxiety", to="Time4depression", arrows=2, free=TRUE, values=.81, labels=c("T3anxT3dep")), mxPath(from = "Time4anxiety", to = "transformed_Time3_consumption_conv_5_6", arrows=2, free=TRUE , values=-.03, labels=c("T3anxT3alc")), mxPath(from ="Time4depression", to = "transformed_Time3_consumption_conv_5_6", arrows = 2, free=TRUE, values = .05, labels=c("T3depT3alc")), mxPath(from = "Time4anxiety", to = "Time4anxiety", arrows = 2, free=FALSE, values = 1, labels=c("T3anxvar")), mxPath(from = "Time4depression", to = "Time4depression", arrows=2, free=FALSE, values = 1, labels=c("T3depvar")), mxPath(from = "transformed_Time3_consumption_conv_5_6", to = "transformed_Time3_consumption_conv_5_6", arrows = 2, free=TRUE, values = .3, labels=c("T3alcvar")), mxPath(from = "one", to = "Time4anxiety", arrows=1, free=FALSE, values = 0, labels=c("manxiety")), mxPath(from = "one", to = "Time4depression", arrows=1, free=FALSE, values = 0, labels=c("mdepression")), mxPath(from = "one", to = "transformed_Time3_consumption_conv_5_6", arrows=1, free=TRUE, values = 1.4, labels=c("malcohol")), mxPath(from = "one", to = "Time4anxiety_1a", arrows=1, free=TRUE, values = 2.22, labels=c("mT4anx1a")), mxPath(from = "one", to = "Time4anxiety_1b", arrows=1, free=TRUE, values = 1.60, labels=c("mT4anx1b")), mxPath(from = "one", to = "Time4anxiety_1f", arrows=1, free=TRUE, values = 1.81, labels=c("mT4anx1f")), mxPath(from = "one", to = "Time4anxiety_1h", arrows=1, free=TRUE, values = 1.65, labels=c("mT4anx1h")), mxPath(from = "one", to = "Time4depression_1c", arrows=1, free=TRUE, values = 2.71, labels=c("mT4dep1c")), mxPath(from = "one", to = "Time4depression_1d", arrows=1, free=TRUE, values = 3.15, labels=c("mT4dep1d")), mxPath(from = "one", to = "Time4depression_1e", arrows=1, free=TRUE, values = 2.37, labels=c("mT4dep1e")), mxPath(from = "one", to = "Time4depression_1g", arrows=1, free=TRUE, values = 2.34, labels=c("mT4dep1g")), mxPath(from ="per_neurot_y1f_centered", to = "Time4anxiety", arrows = 1, free=TRUE, values = .12, labels=c("NeurotT3anx")), mxPath(from ="per_neurot_y1f_centered", to = "Time4depression", arrows = 1, free=TRUE, values = .12, labels=c("NeurotT3dep")), mxPath(from ="per_neurot_y1f_centered", to = "transformed_Time3_consumption_conv_5_6", arrows = 1, free=TRUE, values = -.02, labels=c("NeurotT3alc")), mxPath(from = "per_neurot_y1f_centered", to = "per_neurot_y1f_centered", arrows = 2, free=TRUE, values = 8.86, labels=c("Neurotvar")), mxPath(from = "one", to = "per_neurot_y1f_centered", arrows=1, free=TRUE, values = 0.00, labels=c("mneurot")), mxPath(from ="per_neurot_y1f_centered", to = "asi3_total_centered", arrows = 2, free=TRUE, values = .5, labels=c("NeurotASI")), mxPath(from ="per_neurot_y1f_centered", to = "dts_total_centered", arrows = 2, free=TRUE, values = -.5, labels=c("NeurotDTS")), mxPath(from ="asi3_total_centered", to = "Time4anxiety", arrows = 1, free=TRUE, values = .5, labels=c("ASIT3anx")), mxPath(from ="asi3_total_centered", to = "Time4depression", arrows = 1, free=TRUE, values = .5, labels=c("ASIT3dep")), mxPath(from ="asi3_total_centered", to = "transformed_Time3_consumption_conv_5_6", arrows = 1, free=TRUE, values = .2, labels=c("ASIT3alc")), mxPath(from ="dts_total_centered", to = "Time4anxiety", arrows = 1, free=TRUE, values = -.2, labels=c("DTST3anx")), mxPath(from ="dts_total_centered", to = "Time4depression", arrows = 1, free=TRUE, values = -.5, labels=c("DTST3dep")), mxPath(from ="dts_total_centered", to = "transformed_Time3_consumption_conv_5_6", arrows = 1, free=TRUE, values = -.2, labels=c("DTST3alc")), mxPath(from ="dts_total_centered", to = "asi3_total_centered", arrows = 2, free=TRUE, values = .8, labels=c("asidts")), mxPath(from = "asi3_total_centered", to = "asi3_total_centered", arrows = 2, free=TRUE, values = 203.91, labels=c("ASIvar")), mxPath(from = "dts_total_centered", to = "dts_total_centered", arrows = 2, free=TRUE, values = 125.14, labels=c("DTSvar")), mxPath(from = "one", to = "asi3_total_centered", arrows=1, free=TRUE, values = 0, labels=c("mASI")), mxPath(from = "one", to = "dts_total_centered", arrows=1, free=TRUE, values = 0, labels=c("mDTS")), mxData(observed=mydata10, type="raw")) myModelplanEstep3Run<-mxRun(myModelplanEstep3) refMods<-mxRefModels(myModelplanEstep3Run, run = T) summary(myModelplanEstep3Run, refModels=refMods)
2) Anxiety change model (attached visual)
I would like to have r-squared values to represent how much this model in entirety accounts for variance in the "e" represented in the visual model, which represents the change in anxiety (changeanx in the code below). I would also like to have the r-squared for how much variance each of the predictors (neuroticism, AS and DT) accounts for in change in anxiety.
Here's how I built the model:
latents<- c("Time2anxiety", "Time4anxiety" , "changeanxiety", "L1", "L2") manifests <- c("Time2anxiety_1a","Time2anxiety_1b", "Time2anxiety_1f", "Time2anxiety_1h", "Time4anxiety_1a", "Time4anxiety_1b", "Time4anxiety_1f", "Time4anxiety_1h", "per_neurot_y1f_centered", "asi3_total_centered", "dts_total_centered") mydata8[ ,manifests] Finalmodelanxietystep4<-mxModel(model="finalmodelanxietystep4", type="RAM", manifestVars=manifests, latentVars=latents, mxPath(from="Time2anxiety", to="Time2anxiety_1a", arrows=1, free=TRUE, values=0.7, labels=c("anxanx1a")), mxPath(from="Time2anxiety", to="Time2anxiety_1b", arrows=1, free=TRUE, values=0.54, labels=c("anxanx1b")), mxPath(from="Time2anxiety", to="Time2anxiety_1f", arrows=1, free=TRUE, values=0.6, labels=c("anxanx1f")), mxPath(from="Time2anxiety", to="Time2anxiety_1h", arrows=1, free=TRUE, values=0.55, labels=c("anxanx1h")), mxPath(from="Time4anxiety", to="Time4anxiety_1a", arrows=1, free=TRUE, values=0.7, labels=c("anxanx1a")), mxPath(from="Time4anxiety", to="Time4anxiety_1b", arrows=1, free=TRUE, values=0.54, labels=c("anxanx1b")), mxPath(from="Time4anxiety", to="Time4anxiety_1f", arrows=1, free=TRUE, values=0.6, labels=c("anxanx1f")), mxPath(from="Time4anxiety", to="Time4anxiety_1h", arrows=1, free=TRUE, values=0.55, labels=c("anxanx1h")), mxPath(from = "L1", to = "Time2anxiety", arrows=1, free=FALSE , values=1, labels=c("L1T2anx")), mxPath(from = "L2", to = "Time4anxiety", arrows=1, free=FALSE , values=1, labels=c("L2T4anx")), mxPath(from = "L1", to = "L2", arrows=1, free=FALSE , values=1, labels=c("L1L2")), mxPath(from = "changeanxiety", to = "L2", arrows=1, free=FALSE , values=1, labels=c("changeanxT4L2")), mxPath(from ="L1", to = "changeanxiety", arrows = 2, free=TRUE, values = 0.335, labels=c("L1changeanx")), mxPath(from = "L1", to = "L1", arrows = 2, free=FALSE, values = 1, labels=c("L1var")), mxPath(from = "Time2anxiety", to = "Time2anxiety", arrows = 2, free=FALSE, values = 1, labels=c("T2anxvar")), mxPath(from = "changeanxiety", to = "changeanxiety", arrows = 2, free=TRUE, values = 0.328, labels=c("changeanxvar")), mxPath(from = "Time2anxiety_1a", to = "Time2anxiety_1a", arrows = 2, free=TRUE, values = 0.54, labels=c("anx1avar")), mxPath(from = "Time2anxiety_1b", to = "Time2anxiety_1b", arrows = 2, free=TRUE, values = 0.406, labels=c("T2anx1bvar")), mxPath(from = "Time2anxiety_1f", to = "Time2anxiety_1f", arrows = 2, free=TRUE, values = 0.5, labels=c("anx1fvar")), mxPath(from = "Time2anxiety_1h", to = "Time2anxiety_1h", arrows = 2, free=TRUE, values = 0.53, labels=c("anx1hvar")), mxPath(from = "Time4anxiety_1a", to = "Time4anxiety_1a", arrows = 2, free=TRUE, values = 0.54, labels=c("anx1avar")), mxPath(from = "Time4anxiety_1b", to = "Time4anxiety_1b", arrows = 2, free=TRUE, values = 0.258, labels=c("T4anx1bvar")), mxPath(from = "Time4anxiety_1f", to = "Time4anxiety_1f", arrows = 2, free=TRUE, values = 0.5, labels=c("anx1fvar")), mxPath(from = "Time4anxiety_1h", to = "Time4anxiety_1h", arrows = 2, free=TRUE, values = 0.53, labels=c("anx1hvar")), mxPath(from = "one", to = "changeanxiety", arrows=1, free=TRUE, values = 3.22, labels=c("mchangeanx")), mxPath(from = "one", to = "Time4anxiety", arrows=1, free=FALSE, values = 0, labels=c("mT4anx")), mxPath(from = "one", to = "Time2anxiety", arrows=1, free=FALSE, values = 0, labels=c("mT2anx")), mxPath(from = "one", to = "L1", arrows=1, free=FALSE, values = 0, labels=c("mL1")), mxPath(from = "one", to = "Time2anxiety_1a", arrows=1, free=TRUE, values = 2.29, labels=c("mT2anx1a")), mxPath(from = "one", to = "Time2anxiety_1b", arrows=1, free=TRUE, values = 1.68, labels=c("mT2anx1b")), mxPath(from = "one", to = "Time2anxiety_1f", arrows=1, free=TRUE, values = 1.85, labels=c("mT2anx1f")), mxPath(from = "one", to = "Time2anxiety_1h", arrows=1, free=TRUE, values = 1.68, labels=c("mT2anx1h")), mxPath(from = "one", to = "Time4anxiety_1a", arrows=1, free=FALSE, values = 0, labels=c("mT4anx1a")), mxPath(from = "one", to = "Time4anxiety_1b", arrows=1, free= FALSE, values = 0, labels=c("mT4anx1b")), mxPath(from = "one", to = "Time4anxiety_1f", arrows=1, free=FALSE, values = 0, labels=c("mT4anx1f")), mxPath(from = "one", to = "Time4anxiety_1h", arrows=1, free=FALSE, values = 0, labels=c("mT4anx1h")), mxPath(from = "per_neurot_y1f_centered", to = "L1", arrows=2, free=TRUE, values=1.45, labels=c("NeurotL1")), mxPath(from = "per_neurot_y1f_centered", to = "changeanxiety", arrows = 1, free=TRUE, values = -.02, labels=c("Neurotchangeanx")), mxPath(from = "per_neurot_y1f_centered", to = "per_neurot_y1f_centered", arrows = 2, free=TRUE, values = 8.39, labels=c("Neurotvar")), mxPath(from = "one", to = "per_neurot_y1f_centered", arrows = 1, free=TRUE, values = 0, labels=c("mneurot")), mxPath(from = "per_neurot_y1f_centered", to = "asi3_total_centered", arrows = 2, free=TRUE, values = .5, labels=c("Nasi")), mxPath(from = "per_neurot_y1f_centered", to = "dts_total_centered", arrows = 2, free=TRUE, values = .5, labels=c("Ndts")), mxPath(from = "L1", to = "asi3_total_centered", arrows = 2, free=TRUE, values = .5, labels=c("L1asi")), mxPath(from = "L1", to = "dts_total_centered", arrows = 2, free=TRUE, values = .5, labels=c("L1dts")), mxPath(from = "asi3_total_centered", to = "dts_total_centered", arrows = 2, free=TRUE, values = .6, labels=c("asidts")), mxPath(from = "asi3_total_centered", to = "changeanxiety", arrows = 1, free=TRUE, values = .2, labels=c("asichangeanx")), mxPath(from = "dts_total_centered", to = "changeanxiety", arrows = 1, free=TRUE, values = .2, labels=c("dtschangeanx")), mxPath(from = "asi3_total_centered", to = "asi3_total_centered", arrows = 2, free=TRUE, values = 210.0017, labels=c("asivar")), mxPath(from = "dts_total_centered", to = "dts_total_centered", arrows = 2, free=TRUE, values = 118.2442, labels=c("dtsvar")), mxPath(from = "one", to = "asi3_total_centered", arrows = 1, free=TRUE, values = 0, labels=c("masi")), mxPath(from = "one", to = "dts_total_centered", arrows = 1, free=TRUE, values = 0, labels=c("mdts")), mxData(observed=mydata8, type="raw")) Finalmodelanxietystep4Run <-mxRun(Finalmodelanxietystep4) refMods<-mxRefModels(Finalmodelanxietystep4Run, run = T) summary(Finalmodelanxietystep4Run, refModels=refMods)
Thank you so much for your help!
Internally, in OpenMx (and most SEM software) RAM models are represented via three matrices: A for one-headed arrows, S for two-headed paths, and F to filter the relevant observed variables out of the expected covariance matrix of latent and observed variables combined. It seems to me that some use of basic matrix algebra and the output matrices will answer the question. For example, with the RAM model specified on the OpenMx homepage (the top one), we get:
The A matrix's estimates are in its values slot:
and the same is true of the S matrix of covariance paths:
If we want to know the variances & covariances due to the factor G then we can isolate the G column in the S matrix by setting up a matrix to select with. It has 5 zeroes and a 1, because G is in the 6th column, and select just that column with omxSelectCols:
So far, so good. We now want the covariance matrix of G from the S matrix, for which omxSelectRowsAndCols is handy:
It's not a particularly scintillating matrix this, being 1x1 identity; if we'd had 2 factors it might be more interesting. Nevertheless, it's what we need to calculate the variances (and covariances) due to the factor. As in most simple factor models, it's just ASA' where * is matrix multiplication. In R we can finesse this with the %&% quadratic matrix operator (A%&%B calculates A%*%B%*%t(A) which is what we want).
Down the diagonal of this matrix are the contributions of factor G to the variance of each variable. To represent as $R^2$ we need to divide by the total expected covariance, which is easily grabbed from a model with mxGetExpected:
And finally we can calculate:
feeding this through diag() gives:
Note that these quantities' standard errors, likelihood-based confidence intervals or bootstrap error distributions could be examined via the mxSE(), mxCI(), mxBootstrapEval() functions, if desired. HTH!
Edit: PS In more complicated models, the A%*%B%*%t(A) formula would need to be replaced by solve(I-A)%*%B%*%t(solve(I-A)) to get the full pathway back to the latent variables. I cut a corner because the longest pathway in A was length 1 (e.g., X -> Y -> Z would have length 2). For maximum length 1, solve(I-A) and A are the same.
Sorry for the delay! Thanks for the help! Unfortunately the
A%*%B%*%t(A)
formula did not work, so I think I need to try thesolve(I-A)%*%B%*%t(solve(I-A))
formula, but I'm not sure what the I is. Could you provide the example using that method?Thanks!
My bad - I assumed that you had knowledge that you lacked. I is the identity matrix, the same size as A. So if A is 5x5 then diag(5) would give you the matrix required, or in OpenMx it could be
matrix(“Iden”,5,5, name=’I’)
At the same time “... did not work” is not the most useful report, as the problem could be anything from a user typo to the math, to bugs with OpenMx, R, the operating system or even the hardware. To help developers solve your problem it is always a good idea to include both the code and the error you are getting, so it can be easily reproduced and repaired.
I hope this helps!
Hi there,
I technically got your original code to run without error messages at least, but it did not give me the R-squared numbers I wanted. I first wanted to see how much of the variance in initial anxiety accounted for change in anxiety (latent change score). Both of these are latent factors, which maybe is the problem in using this method to get R-squared?
This is the code for building that model:
And here is what I did following your directions to try to get an R-squared for how much the initial anxiety (L1) accounted for change in anxiety.
L1 was the 12th of 13 so I used this
And it gave me this:
That seemed good, right? Because it has L1 and then all the other variables. Then I did this:
Which gave me:
This yielded:
This was odd because it only lists the eight manifest variable items that go into making the latent variables I'm interested in. I'm pretty confused now... is what I have here saying that initial anxiety is not related to these items that measure anxiety? That's definitely not the case, so something seems to have gone very wrong. Thank you so much for your help!