# Extract data from growth curve model

134 views

### Peter Hilpert

Jan 8, 2019, 11:50:57 AM1/8/19
to lavaan
Hi all,

I would like to extract data from lavaan but failed so far. In the simplest way, I compute a growth curve model (latent intercept, latent slope) and I try to extract each intercept and each slope per row. This is possible in MPlus with the fscore command. Is this also possible with lavaan? Or is there a similar command like the fscore in lavaan?
The model I actually run is more complicated (a latent differential equation model, where I use a time embedded matrix with 600 data points per person) but that does not change the main idea that I use a growth curve model and try to extract the intercept and slope per row (there are 3 data point per row, which gives me nearly 600 intercepts and slope per person).

Thanks a lot!

Best,
Peter

### Mauricio Garnier-Villarreal

Jan 8, 2019, 6:33:56 PM1/8/19
to lavaan
Peter

In lavaan you can use the predict function, predict will give you a matrix of nrow equal to the nrow of the data used for the analysis, so in your case would be the embedded matrix. The ncol would be the number of factors you have, in your case I imagine you have the zeroth, first, and second derivatives. Also, this will only you you the factor scores for rows with complete data

library(lavaan)

?growth

model.syntax <- '
# intercept and slope with fixed coefficients
i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4

# regressions
i ~ x1 + x2
s ~ x1 + x2

# time-varying covariates
t1 ~ c1
t2 ~ c2
t3 ~ c3
t4 ~ c4
'

fit <- growth(model.syntax, data=Demo.growth)
summary(fit, standardized=T)

dim(Demo.growth)
predict(fit) ## Factor scores

### Mauricio Garnier-Villarreal

Jan 10, 2019, 2:59:42 PM1/10/19
to lavaan
Also, predict is the generic R function. It calls the lavPredict function with its defaults, you can look at the lavaan specific function for more details and options

### Peter Hilpert

Jan 11, 2019, 9:21:06 AM1/11/19
Hi Mauricio,

Oh, very cool!!! That is super helpful. Is there also an easy way to include participants ID directly into this object or would you extract from the original data file?

Thanks a lot!

Best,
Peter
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.

### Mauricio Garnier-Villarreal

Jan 11, 2019, 1:21:20 PM1/11/19
to lavaan
Peter

In the ?lavPredict page there is the following example to help adding them back to the original data

library(lavaan)

## The famous Holzinger and Swineford (1939) example
HS.model <- ' visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data=HolzingerSwineford1939)
summary(fit, fit.measures=TRUE)

## merge factor scores to original data.frame
idx <- lavInspect(fit, "case.idx")
fscores <- lavPredict(fit)
## loop over factors
for (fs in colnames(fscores)) {
HolzingerSwineford1939[idx, fs] <- fscores[ , fs]
}