You are here

Can OpenMx plot predicted y hat of LGC automatically?

5 posts / 0 new
Last post
Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Can OpenMx plot predicted y hat of LGC automatically?

Hi all,

I am conducting latent basis models and have got estimations. I want to plot the functional form of individuals. Is there any way for OpenMx to plot those y hat's for each individual automatically? Or at least can I have those y hat values directly?

Thank you in advance!

AdminNeale's picture
Offline
Joined: 03/01/2013 - 14:09
Clarification

Hi Veronica

Can you clarify? Do you simply want the expected mean for each row? Or do you also want a function of the estimated factor scores, a la fitted.lme?

For the former, the mxGetExpected() will deliver the model expected means, and its defvar row argument could be apply'd to obtain the predicted means when there are definition variables involved that make the expected means differ between data rows.

Thanks
Mike

Veronica_echo's picture
Offline
Joined: 02/23/2018 - 01:57
Thanks

Dr. Neale,

Thanks for your kind reply. I tried the function mxGetExpected() at your suggestion and got mean vectors and variance-covariance matrix of outcomes (i.e., the variable BMI in my case, shown below). It is great to know the model-based mean and var-cov matrix of outcomes, which can be compared to the means and var-cov matrix from the data summary. However, we are more interested in such model-based values for each individual (I understand your point about using "defvar" to specify covariates, but we do not have such covariates/definition variables; we only have repeated outcomes). Can I get such values by some existed functions?

> mxGetExpected(latent.BMI.out, component = "means")
         BMI1     BMI2     BMI3     BMI4    BMI5
[1,] 13.45409 16.07842 16.78987 17.20335 16.8839
> mxGetExpected(latent.BMI.out, component = "covariance")
          BMI1      BMI2      BMI3      BMI4      BMI5
BMI1 2.0662547 0.4538267 0.4528134 0.4522246 0.4526795
BMI2 0.4538267 2.9940048 1.6378395 1.7846037 1.6712154
BMI3 0.4528134 1.6378395 3.5677893 2.1458102 2.0015591
BMI4 0.4522246 1.7846037 2.1458102 3.9644291 2.1935503
BMI5 0.4526795 1.6712154 2.0015591 2.1935503 3.6539106
AdminRobK's picture
Offline
Joined: 01/24/2014 - 12:15
It's not entirely clear to me

It's not entirely clear to me what you're trying to do. I'm not very familiar with the kind of LGC you're fitting, so it would help if you post your script and explain what it's meant to do.

AdminHunter's picture
Offline
Joined: 03/01/2013 - 11:03
4-line solution

If you start with something like the demo called LatentGrowthCurveModel_PathRaw which can be run with

demo(package='OpenMx', 'LatentGrowthCurveModel_PathRaw')

Then you only need a few extra lines to get what you want. The real code here is just four lines, but there's some extra inspection and comments.

fs <- mxFactorScores(growthCurveFit, type='regression')
# There's a message about the space in the model name, but everything is fine.
# See ?mxFactorScores for the structure returned
head(fs[,,1]) # these are the factor scores
#  intercept    slope
#1  7.304843 1.102765
#2  9.494752 2.357813
#3  9.343139 1.688075
#4  9.335320 1.904806
#5 10.397774 1.942586
#6 10.128553 1.350930
map <- mxEval(A, growthCurveFit)[growthCurveFit$manifestVars, growthCurveFit$latentVars]
# Note that this is the mapping for defvar.row=1. If you have different times of measurement
#  for different people, you'll need a different 'map' for each person.
obs <- t(map %*% t(fs[,,1]))
dim(obs) # structured like original data set
#[1] 500   5
matplot(t(obs), type='l')
# quick plot

The general idea here is to (1) get the regression factor scores, (2) get the latent-to-observed variable map (i.e. factor loadings), and then (3) compute the predicted observed scores based on the factor scores and the mapping. Finally, a simple plot is made with matplot.

Log in or register to post comments