# Appendix 2: R syntaxes for the regression and causal analyses.

There are several SEM packages in R. This appendix illustrates how the correction for measurement errors can be applied using the ‘lavaan’ package [Ros12] of R. In particular, this appendix focuses on the reproduction of the syntax we have provided in Chapters 5 and 6 for the regression and the causal models, respectively, in both LISREL and Stata.

**Syntax A2.1:**R syntax for the regression model with correction for measurement errors.

#Regression model with corrections

#setwd("~/Edunet")

install.packages("lavaan") #To install "lavaan"

library("lavaan") #To require "lavaan"

#To estimate the model with correction for measurement errors, we only need to define the covariance matrix corrected for measurement errors (Table 4.7) as a new R object

lower.corrected<- '

.710

0.395 .643

0.268 0.333 .604

0.310 0.160 0.114 .605

0.112 0.07 0.018 0.094 .682

0.163 0.277 0.174 0.064 0.009 .624'

rho <- getCov(lower.corrected, names=c("stfdem","fairelcc","oppcrgvc","cttresac","lrscale","hinctnta"))

rho.corrected<- cov2cor(rho) #Function 'cov2cor' transforms a covariance matrix into a correlation matrix

#Regression model

model<- '

stfdem ~ fairelcc + oppcrgvc + cttresac + lrscale + hinctnta

'

fit.corrected<-sem(model,

sample.cov=rho.corrected,

sample.nobs= 1468) #mimic= ‘’EQS’ gives the results as close as possible to LISREL

summary(fit.corrected, standardized=T) #Prints in addition standardized results

#In addition, the option 'modindices=T' prints the modification indices

inspect(fit.corrected) #This is to recall the matrices we are estimating in a LISREL format

**Syntax A2.2:**R syntax for the causal model with correction for measurement errors.

#Causal model with corrections

setwd("~/Edunet")

install.packages("lavaan") #To install "lavaan"

library("lavaan") #To require "lavaan"

#To estimate the model with correction for measurement error, we only need to define the covariance matrix corrected for measurement errors (Table 4.7) as a new R object

lower.corrected<- '

.710

0.395 .643

0.268 0.333 .604

0.310 0.160 0.114 .605

0.112 0.07 0.018 0.094 .682

0.163 0.277 0.174 0.064 0.009 .624'

rho <- getCov(lower.corrected, names=c("stfdem","fairelcc","oppcrgvc","cttresac","lrscale","hinctnta"))

rho.corrected<- cov2cor(rho) #Function 'cov2cor' transforms a covariance matrix into a correlation matrix

#Causal model

model<-'

stfdem ~ fairelcc + oppcrgvc + cttresac + lrscale

fairelcc ~ lrscale + hinctnta

oppcrgvc ~ lrscale + hinctnta

cttresac ~ lrscale + hinctnta

'

fit.corrected<-sem(model,

sample.cov=rho.corrected,

sample.nobs= 1468) #This is to recall the matrices we are estimating in a LISREL format

inspect(fit.corrected) #This is to recall the matrices we are estimating in a LISREL format

summary(fit.corrected, standardized=T) #Prints in addition standardized results

#In addition, the option 'modindices=T' prints the modification indices

#### References

- [Ros12] Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling.
*Journal of Statistical Software, 48(2), 1-36*. URL: http://www.jstatsoft.org/v48/i02/