Currently, lavaan will not provide casewise residuals from residuals(,"casewise") for an sem that does not have latent variables (see open issue #44). Until this capability is added, does anyone have a way to work-around the problem?
predictors <- c("X","M")outcomes <- c("M","Y")X <- as.matrix(DATA1[predictors])
X <- cbind(intercept = 1, X)
Y <- as.matrix(DATA1[outcomes])B <- lavInspect(ModelFit, "coef")$beta[outcomes, predictors]
int <- lavInspect(ModelFit, "coef")$alpha[outcomes, "intercept"]
B <- t(cbind(intercept = int, B))
Y.hat <- X %*% B # regression equationE <- Y - Y.hat # casewise residuals
DATA1$M.resid <- E[ , "M"]DATA1$Y.resid <- E[ , "Y"]
Hello,
Thank you for this! I also performed a path analysis in lavaan based on observed variables. This code was exactly what I needed to calculate my casewise model residuals!
It seems that the following adaptation to my data and model was successful. I was able to run it and obtained a residual data matrix. (Note to other beginners like me: I had to make sure that all my variables were as.numeric. I also created a subset data frame with only my model variables and in the exact order in which they appear below - but maybe that was overkill).
I have two questions to obtain additional information through this procedure.
Would it be possible to know how to adapt the syntax to obtain standardized residuals?
Also, is there a way to obtain Cook values through this easy procedure? I am not really familiar with matrix algebra and I only recently started using R and lavaan.
Here is the adapted code:
In my model, I have 2 exogenous variables, 3 mediators, and 2 outcomes (1 is binary, so I used WLSMV). So: X1, X2, M1, M2, M3, Y1, Y2
predictors <- c("X1", "X2","M1", "M2", "M3")
outcomes <- c("M1", "M2", "M3","Y1", "Y2")
X <- as.matrix(mydata[predictors])
X <- cbind(intercept = 1, X)
Y <- as.matrix(mydata[outcomes])
B <- lavInspect(mymodelfit, "coef")$beta[outcomes, predictors]
int <- lavInspect(mymodelfit, "coef")$alpha[outcomes, "intercept"]
B <- t(cbind(intercept = int, B))
Y.hat <- X %*% B # regression equation
E <- Y - Y.hat # casewise residuals
Thank you again!
Myriam
mydata$M1.resid <- E[ , "M1"]
#standardizing residuals
mydata$M1.zres <- as.numeric(
(mydata$M1.resid-mean(mydata$M1.resid))
/sd(mydata$M1.resid))
--
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/a951aa54-0345-4654-8f3b-b8c244bda55fo%40googlegroups.com.