Help extract residuals for SEM with two paths

55 views
Skip to first unread message

Jenn Lau

unread,
Apr 18, 2020, 2:36:14 AM4/18/20
to lavaan
Hi, all. I'm new to R and am bugged by extracting residual values for each observation of the two dependent variables using lavaan package. 

My model is a two-path SEM. In the model, I have one independent variable x1, two dependent variables y1 and y2, two mediators m1 and m2, one moderator z, one control x2. For convenience, I've mean-centered the moderator z and got the product result named as x1z. 

List of variables:
y1, y2, x1, m1, m2, z, x1z, x2

Number of observations: 200

Model: 
m1 = (intercept) + a1*x1 + (coef.)*z + (coef.)*x1z + (coef.)*x2
y1 = (intercept) + b1*m1
m2 = (intercept) + a2*x1 + (coef.)*z + (coef.)*x1z + (coef.)*x2
y2 = (intercept) + b2*m2

The R codes are from online:

> semModel <- '
>   y1 ~ c(a1,b1)*x1 + z + x1z + x2
>   y2 ~ c(a2,b2)*x1 + z + x1z + x2
>   '
> fits <- lapply(semMdoel,data=dt, meanstructure=T)
> Resids <- lapply(residuals, function(x1){lapply(x1,"obs")}) 
# or
# Resids <- lapply(residuals, function(x1) { lapply(x1, as.data.frame) })

However, the above model cannot be idenfitied and error message is "lavaan ERROR: wrong number of arguments in modifier (a1,b1) of element y1~x1".

I also tried other ways:
# Model specification
> semModel <- '
>      m1 ~ x1 + z + x1z + x2
>      y1 ~ x1
>      m2 ~ x1 + z + x1z + x2
>      y2 ~ m2
> '
> fit <- sem(semModel, meanstructure=TRUE, data=dt)
> summary(fit, standardized=TRUE, fit.measures=TRUE, rsq=TRUE)
> Resids <- lavResiduals(fit)
> dt$Resids

Can only return residual matrix. But I need to extract residuals for each observation:  (the ith observed y1 - predicted y1), and (the ith observed y2 - predicted y2), and then return two new columns for the two sets of residuals.

Another try:
# Model specification
> model2 <- '
>      m1 ~ a*x1 + z + x1z + x2
>      y1 ~ b*m1
>      m2 ~ a*x1 + z + x1z + x2
>      y2 ~ b*m1
> indirect := a*b
> '
> funEstim<- function(model2){
>    sem(model2, data=dt, estimator="MLM")
> }
> fit2<- lapply(model2, funEstim)
summary(fit, standardized=TRUE, fit.measures=TRUE, rsq=TRUE)
> Resids <- lapply(residuals, function(x1){lapply(x1,"obs")}) 

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'obs' of mode 'function' was not found
Called from: get(as.character(FUN), mode = "function", envir = envir)

I was also suggested to use predict(fit) to get the estimates of each observation first, and then deduct them by the observed values. The problem is the returned values of predict(fit) is empty , something like [1,], [2,], ......, [200,]. 

Can anyone please chek where I am wrong? Or tell me how I can do this correctly? I know it should be easy if I had learned R in the previous.

Thanks a ton!!!!!!

Jarrett Byrnes

unread,
Apr 18, 2020, 8:33:30 AM4/18/20
to lav...@googlegroups.com
I think I’ve got you covered! See the fitted_lavaan.r code at https://jebyrnes.github.io/semclass/schedule.html

On Apr 18, 2020, at 2:36 AM, Jenn Lau <lihua...@gmail.com> wrote:


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/077e5258-71ef-44d8-8939-a9843098dd21%40googlegroups.com.

Jenn Lau

unread,
Apr 18, 2020, 9:48:28 AM4/18/20
to lavaan
Thank you so much, Professor Byrnes. Your code works brilliantly in my case.

I got some error when running this line of code "pred <- predict_lavaan(dpfit, newdata=newdata)":

Error in predict_lavaan(dpfit, newdata = newdata) : 
  Not all exogenous variables supplied!
Called from: predict_lavaan(dpfit, newdata = newdata)

Before the error, everything is smooth and amazing.

> fitted_lavaan(dpfit)
         m1        m2        y1           y2
1   3.142491 4.133351 3.319520 4.453942
2   3.174461 3.989005 3.342207 4.390141
3   3.101387 4.318939 3.290352 4.535971
4   3.014612 4.710735 3.228773 4.709145
5   3.101387 4.318939 3.290352 4.535971
6   3.165326 4.030247 3.335725 4.408369
7   3.179028 3.968384 3.345448 4.381026
8   3.151625 4.092109 3.326002 4.435713
.
.
.

Have you any idea why the above error occurred and how I can have it solved?

Thank you!!!!!

Jenn
To unsubscribe from this group and stop receiving emails from it, send an email to lav...@googlegroups.com.
Message has been deleted

Terrence Jorgensen

unread,
Apr 21, 2020, 6:55:13 AM4/21/20
to lavaan
I got some error when running this line of code "pred <- predict_lavaan(dpfit, newdata=newdata)":

Error in predict_lavaan(dpfit, newdata = newdata) : 
  Not all exogenous variables supplied!
Called from: predict_lavaan(dpfit, newdata = newdata)

If you look at head(newdata), do you see all the variables in your model syntax?  The message is telling you some predictors are missing.

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Lihua Liu

unread,
Apr 22, 2020, 5:01:43 AM4/22/20
to lav...@googlegroups.com

Thank you so much for pointing this out, Professor Jorgensen. The error occurred because original variables from the model are not included. Only the generated casewise residuals are seen. I cannot proceed to the next steps of combining two data frames (“cbind()”) without solving the above error first. I appreciate if you could guide more.

--

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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/dfebdc82-499a-43ca-8b28-6ee297271a8b%40googlegroups.com.

 

Terrence Jorgensen

unread,
Apr 23, 2020, 1:51:22 AM4/23/20
to lavaan

I cannot proceed to the next steps of combining two data frames (“cbind()”) without solving the above error first. I appreciate if you could guide more.


Then could you provide more information?  Are you trying to merge the residuals to your original data (or predicted values to newdata=)?  Does the residuals_lavaan() output not have the same number of rows as your original data?

Jennifer Liu

unread,
Apr 26, 2020, 10:33:30 PM4/26/20
to lav...@googlegroups.com
I simply want to merge the residuals to the original data and then do some correlation test. There is no missing value. Thank you so much for your help!

--
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.
Reply all
Reply to author
Forward
0 new messages