Finding R-squared for two different models

Posted on
No user picture. rranne2@uic.edu Joined: 12/10/2018
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!

Replied on Thu, 06/20/2019 - 12:31
Picture of user. AdminNeale Joined: 03/01/2013

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:

> summary(factorModelFit <- mxRun(factorModel))
Running One Factor with 10 parameters
Summary of One Factor

free parameters:
name matrix row col Estimate Std.Error A
1 One Factor.A[1,6] A x1 G 0.39675442 0.015518623
2 One Factor.A[2,6] A x2 G 0.50315690 0.018196001
3 One Factor.A[3,6] A x3 G 0.57666351 0.020407447
4 One Factor.A[4,6] A x4 G 0.70207009 0.023963327
5 One Factor.A[5,6] A x5 G 0.79545281 0.026616042
6 One Factor.S[1,1] S x1 x1 0.04073255 0.002804281
7 One Factor.S[2,2] S x2 x2 0.03794394 0.002797378
8 One Factor.S[3,3] S x3 x3 0.04074551 0.003142855
9 One Factor.S[4,4] S x4 x4 0.03930827 0.003398652
10 One Factor.S[5,5] S x5 x5 0.03621453 0.003667530

Model Statistics:
| Parameters | Degrees of Freedom | Fit (-2lnL units)
Model: 10 5 -3660.5967
Saturated: 15 0 -3667.9905
Independence: 5 10 64.5342
Number of observations/statistics: 500/15

chi-square: χ² ( df=5 ) = 7.393793, p = 0.1929616
Information Criteria:
| df Penalty | Parameters Penalty | Sample-Size Adjusted
AIC: -2.606207 27.39379 27.84369
BIC: -23.679247 69.53987 37.79926
CFI: 0.9993569
TLI: 0.9987139 (also known as NNFI)
RMSEA: 0.03094378 [95% CI (0, 0.08143354)]
Prob(RMSEA <= 0.05): 0.7135768
timestamp: 2019-06-20 11:45:23
Wall clock time: 0.09138203 secs
optimizer: CSOLNP
OpenMx version number: 2.13.2
Need help? See help(mxSummary)

The A matrix's estimates are in its values slot:

> factorModelFit$A$values
x1 x2 x3 x4 x5 G
x1 0 0 0 0 0 0.3967544
x2 0 0 0 0 0 0.5031569
x3 0 0 0 0 0 0.5766635
x4 0 0 0 0 0 0.7020701
x5 0 0 0 0 0 0.7954528
G 0 0 0 0 0 0.0000000

and the same is true of the S matrix of covariance paths:

> factorModelFit$S$values
x1 x2 x3 x4 x5 G
x1 0.04073255 0.00000000 0.00000000 0.00000000 0.00000000 0
x2 0.00000000 0.03794394 0.00000000 0.00000000 0.00000000 0
x3 0.00000000 0.00000000 0.04074551 0.00000000 0.00000000 0
x4 0.00000000 0.00000000 0.00000000 0.03930827 0.00000000 0
x5 0.00000000 0.00000000 0.00000000 0.00000000 0.03621453 0
G 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 1

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:

> selA <- matrix(c(rep(0,5),1),1,6); omxSelectCols(factorModelFit$A$values,selA)
G
x1 0.3967544
x2 0.5031569
x3 0.5766635
x4 0.7020701
x5 0.7954528
G 0.0000000

So far, so good. We now want the covariance matrix of G from the S matrix, for which omxSelectRowsAndCols is handy:

> omxSelectRowsAndCols(factorModelFit$S$values,selA);
G
G 1

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 A*S*A' 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).

> omxSelectCols(factorModelFit$A$values,selA) %&% omxSelectRowsAndCols(factorModelFit$S$values,selA);
x1 x2 x3 x4 x5 G
x1 0.1574141 0.1996297 0.2287938 0.2785494 0.3155994 0
x2 0.1996297 0.2531669 0.2901522 0.3532514 0.4002376 0
x3 0.2287938 0.2901522 0.3325408 0.4048582 0.4587086 0
x4 0.2785494 0.3532514 0.4048582 0.4929024 0.5584636 0
x5 0.3155994 0.4002376 0.4587086 0.5584636 0.6327452 0
G 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0

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:

> mxGetExpected(factorModelFit,'covariance')
x1 x2 x3 x4 x5
x1 0.1981466 0.1996297 0.2287938 0.2785494 0.3155994
x2 0.1996297 0.2911108 0.2901522 0.3532514 0.4002376
x3 0.2287938 0.2901522 0.3732863 0.4048582 0.4587086
x4 0.2785494 0.3532514 0.4048582 0.5322107 0.5584636
x5 0.3155994 0.4002376 0.4587086 0.5584636 0.6689597

And finally we can calculate:

> factorModelFit$F$values %&% (omxSelectCols(factorModelFit$A$values,selA) %&% omxSelectRowsAndCols(factorModelFit$S$values,selA)) / mxGetExpected(factorModelFit,'covariance')
x1 x2 x3 x4 x5
x1 0.7944323 1.0000000 1.0000000 1.0000000 1.0000000
x2 1.0000000 0.8696581 1.0000000 1.0000000 1.0000000
x3 1.0000000 1.0000000 0.8908465 1.0000000 1.0000000
x4 1.0000000 1.0000000 1.0000000 0.9261415 1.0000000
x5 1.0000000 1.0000000 1.0000000 1.0000000 0.9458644

feeding this through diag() gives:

> diag(factorModelFit$F$values %&% (omxSelectCols(factorModelFit$A$values,selA) %&% omxSelectRowsAndCols(factorModelFit$S$values,selA)) / mxGetExpected(factorModelFit,'covariance'))
x1 x2 x3 x4 x5
0.7944323 0.8696581 0.8908465 0.9261415 0.9458644

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.

Replied on Sun, 07/07/2019 - 08:57
Picture of user. AdminNeale Joined: 03/01/2013

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!

Replied on Thu, 07/11/2019 - 21:54
No user picture. rranne2@uic.edu Joined: 12/10/2018

In reply to by AdminNeale

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:

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")

Finalmodelanxietystep2<-mxModel(model="finalmodelanxietystep2", type="RAM", manifestVars=manifests, latentVars=latents,
mxPath(from="Time2anxiety_1a", to="Time4anxiety_1a", arrows=2, free=TRUE, values=.12, labels=c("T2anx1aT4anx1a")),
mxPath(from="Time2anxiety_1b", to="Time4anxiety_1b", arrows=2, free=TRUE, values=.09, labels=c("T2anx1bT4anx1b")),
mxPath(from="Time2anxiety_1f", to="Time4anxiety_1f", arrows=2, free=TRUE, values=.09, labels=c("T2anx1fT4anx1f")),
mxPath(from="Time2anxiety_1h", to="Time4anxiety_1h", arrows=2, free=TRUE, values=.12, labels=c("T2anx1hT4anx1h")),
mxPath(from="Time2anxiety", to="Time2anxiety_1a", arrows=1, free=TRUE, values=0.8476268, labels=c("anxanx1a")),
mxPath(from="Time2anxiety", to="Time2anxiety_1b", arrows=1, free=TRUE, values=0.8577030, labels=c("anxanx1b")),
mxPath(from="Time2anxiety", to="Time2anxiety_1f", arrows=1, free=TRUE, values=0.8541549, labels=c("anxanx1f")),
mxPath(from="Time2anxiety", to="Time2anxiety_1h", arrows=1, free=TRUE, values=0.8378001, labels=c("anxanx1h")),
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 = "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.26, 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.47, labels=c("changeanxvar")),
mxPath(from = "Time2anxiety_1a", to = "Time2anxiety_1a", arrows = 2, free=TRUE, values = 0.5647315, labels=c("anx1avar")),
mxPath(from = "Time2anxiety_1b", to = "Time2anxiety_1b", arrows = 2, free=TRUE, values = 0.3528205, labels=c("T2anx1bvar")),
mxPath(from = "Time2anxiety_1f", to = "Time2anxiety_1f", arrows = 2, free=TRUE, values = 0.5079372, labels=c("anx1fvar")),
mxPath(from = "Time2anxiety_1h", to = "Time2anxiety_1h", arrows = 2, free=TRUE, values = 0.5221816, labels=c("anx1hvar")),
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("T4anx1bvar")),
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 = "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 = "L2", arrows=1, free=FALSE, values = 0, labels=c("mL2")),
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")),
mxData(observed=mydata8, type="raw"))

Finalmodelanxietystep2Run <-mxTryHard(Finalmodelanxietystep2)
refMods<-mxRefModels(Finalmodelanxietystep2Run, run = T)
summary(Finalmodelanxietystep2Run, refModels=refMods)

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.

Finalmodelanxietystep2Run$A$values

Finalmodelanxietystep2Run$S$values

L1 was the 12th of 13 so I used this

selA <- matrix(c(rep(0,11),1, 0),1,13); omxSelectCols(Finalmodelanxietystep2Run$A$values,selA)

And it gave me this:

L1
Time2anxiety_1a 0
Time2anxiety_1b 0
Time2anxiety_1f 0
Time2anxiety_1h 0
Time4anxiety_1a 0
Time4anxiety_1b 0
Time4anxiety_1f 0
Time4anxiety_1h 0
Time2anxiety 1
Time4anxiety 0
changeanxiety 0
L1 0
L2 1


That seemed good, right? Because it has L1 and then all the other variables. Then I did this:

omxSelectRowsAndCols(Finalmodelanxietystep2Run$S$values,selA);

Which gave me:

L1
L1 1

omxSelectCols(Finalmodelanxietystep2Run$A$values,selA) %&% omxSelectRowsAndCols(Finalmodelanxietystep2Run$S$values,selA);

mxGetExpected(Finalmodelanxietystep2Run,'covariance')

Finalmodelanxietystep2Run$F$values %&% (omxSelectCols(Finalmodelanxietystep2Run$A$values,selA) %&% omxSelectRowsAndCols(Finalmodelanxietystep2Run$S$values,selA)) / mxGetExpected(Finalmodelanxietystep2Run,'covariance')

diag(Finalmodelanxietystep2Run$F$values %&% (omxSelectCols(Finalmodelanxietystep2Run$A$values,selA) %&% omxSelectRowsAndCols(Finalmodelanxietystep2Run$S$values,selA)) / mxGetExpected(Finalmodelanxietystep2Run,'covariance'))

This yielded:

Time2anxiety_1a Time2anxiety_1b Time2anxiety_1f Time2anxiety_1h
0 0 0 0
Time4anxiety_1a Time4anxiety_1b Time4anxiety_1f Time4anxiety_1h
0 0 0 0

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!