[R] Lavaan Package - How to Extract Residuals in Data Values

Ed Merkle merklee at missouri.edu
Wed Jul 18 05:24:20 CEST 2012

Dear Emily,

The lavaan package is typically used to fit models with latent 
variables, and these models are typically fit to the covariance matrix 
(and not necessarily to the raw data).  Thus, it is usually not 
straightforward to get data residuals from the fitted models. In your 
case, it appears that all variables are observed, so you could use 
"meanstructure=TRUE" within the sem() command to get the intercept for 
your regression.  Then I believe the residuals could be obtained manually.

I also wonder whether your specified model is really what you want. I 
believe that, if you estimate error in all your variables and also 
specify some covariances between independent variables, the model will 
be unidentified.  It appears that you are handling this by fixing b1 to 
be zero, but then you are effectively excluding LOG_SR_A_D from the 
model.  I wonder whether you can get by with a simple regression model 
as estimated by lm().


Ed Merkle, PhD
Assistant Professor
Department of Psychological Sciences
University of Missouri
Columbia, MO, USA 65211

On 7/9/12 1:25 PM, r-help-request at r-project.org wrote:
> Date: Mon, 9 Jul 2012 11:41:33 -0400
> From: Emily Zimmerman<zimmerman.em.k at gmail.com>
> To:r-help at r-project.org
> Subject: [R] Lavaan Package - How to Extract Residuals in Data Values
> Message-ID:
> 	<CABC6T6d-ezgpwYCskaDpvwq3=pGRy8Tb9=4v3va-9gJBNnkTQw at mail.gmail.com>
> Content-Type: text/plain
> Hello R Community,
> I am using the Lavaan package in R 2.15.0 to analyze data collected from
> 1200 lakes across North America. My dataset includes 3 continuous
> independent variables (LOG_NTL, LOG_PTL, and LOG_SR_A_D) and 1 continuous
> dependent variable (BIOVOL) . I have successfully constructed structural
> equation models using the Lavaan package (example included below with
> code), but I have not been able to figure out how to extract the
> residuals in the data values themselves (the unexplained values) of my
> dependent variable, BIOVOL. For the last step of my analysis, I would like
> to plot the residuals for BIOVOL against one of the independent variables
> to see the relationship. I understand how to get the residuals for the
> covariance matrix, but I do not know how to get the residuals in the data
> values themselves for BIOVOL. Does anyone know how to extract residuals for
> data values themselves in the Lavaan package?
> Here is the code I am using to construct my model and the model that I am
> trying to get the residuals for:
> #Specify the model
>> >model2BIOVre <- 'BIOVOL ~ LOG_NTL + LOG_PTL + b1*LOG_SR_A_D
> + b1 == 0'
> #Fit the model with the sem function
>> >fit <- sem(model2BIOVre, data=lakes, fixed.x=FALSE, estimator="MLM")
> #Summarize model
>> >summary(fit, fit.measures=TRUE, standardize=TRUE, rsq=TRUE)
> Here is where I am stumped...I have read the package manuals, and tutorials
> located at lavaan.urgent.be, as well as some by James Grace. I have also
> tried to manipulate some other codes, but I can't get it.  I may have
> missed something as I am relatively new to R, but it is not clear to me how
> to do this.
> Any help would be very much appreciated.
> Thank you,
> Emily Zimmerman

More information about the R-help mailing list