# [R] How to use pakcage SEM

John Fox jfox at mcmaster.ca
Tue May 20 17:54:29 CEST 2003

```Dear Mitsuo,

At 12:02 AM 5/21/2003 +0900, Mitsuo Igarashi wrote:
>Hello, Dear John.
>
>As you taught me, I obtained the result which I expect and is
>the same as that of EQS.
>I have started to learn structural equation modeling. This
>pushes my understanding SEM. Thank you very much.
>
>Would you permit me to ask a question?
>This EQS program sets an error covariance between V1 and V2.
>However,It seems to me that the package SEM does not have the
>method of error covariance. So I set a covariance between V1 and
>V2. What is the difference between these two settings of
>covariances?

Unlike EQS, the sem function uses the "ram" form of a structural-equation
model. Setting a covariance between endogenous variables V1 and V2 is
interpreted in this form of the model as an error covariance. See
<http://www.socsci.mcmaster.ca/jfox/Books/Companion/appendix-sems.pdf> for

John

>Best regards.
>--------========----------
>Mitsuo Igarashi
>mitsu5 at ruby.famille.ne.jp
>---------------
>
>John Fox <jfox at mcmaster.ca> wrote:
>
> > Dear Mitsuo,
> >
> > You appear to be trying to fit a confirmatory factor analysis model with
> > five observed variables and two factors. Two of the five variables load on
> > both factors. Two variables have correlated measurement errors. I haven't
> > checked that the model that you intend to fit is identified but let's
> > suppose that it is.
> >
> > As you've described the model to sem, there are 16 free parameters, but
> > there are only 5*6/2 = 15 observed covariances (counting the diagonal
> > entries of the correlation matrix), so clearly there's a mistake in the
> > specification -- the model has negative degrees of freedom (and sem should
> > complain, but doesn't).
> >
> > The problem is that you've not placed any constraints on the model to fix
> > the scales of the factors. The simplest thing to do would be to fix the
> > variances of the factors to 1, rather than specifying them as free
> > parameters; as well, you don't have to start the measurement-error
> > variances at 1 rather than using the start values that sem computes,
> though
> > it doesn't hurt to do so:
> >
> >  > model.mh<-matrix(c(
> > +         'F1 -> V1', 'a1',NA,
> > +         'F1 -> V3', 'a3',NA,
> > +         'F1 -> V4', 'a4',NA,
> > +         'F2 -> V2', 'b2',NA,
> > +         'F2 -> V3', 'b3',NA,
> > +         'F2 -> V4', 'b4',NA,
> > +         'F2 -> V5', 'b5',NA,
> > +         'V1 <-> V1','e1', NA,
> > +         'V2 <-> V2','e2', NA,
> > +         'V3 <-> V3','e3', NA,
> > +         'V4 <-> V4','e4', NA,
> > +         'V5 <-> V5','e5', NA,
> > +         'F1 <-> F1', NA, 1,
> > +         'F2 <-> F2', NA, 1,
> > +         'F1 <-> F2','c12',NA,
> > +         'V1 <-> V2','cv1', NA
> > +         ),ncol=3,byrow=T)
> >  > sem.mh <- sem(model.mh, data.mh, 100)
> >  > summary(sem.mh)
> >
> >   Model Chisquare =  2.2805   Df =  1 Pr(>Chisq) = 0.13101
> >   Goodness-of-fit index =  0.99097
> >   Adjusted goodness-of-fit index =  0.8646
> >   RMSEA index =  0.11373   90 % CI: (0, 0.31712)
> >   BIC =  -3.9341
> >
> >   Normalized Residuals
> >       Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
> > -3.92e-01 -5.50e-02 -5.27e-06  3.27e-02  1.09e-01  7.98e-01
> >
> >   Parameter Estimates
> >      Estimate Std Error z value   Pr(>|z|)
> > a1  0.970680  0.275852 3.51884 4.3343e-04 V1 <--- F1
> > a3  0.317133  0.198819 1.59509 1.1069e-01 V3 <--- F1
> > a4  0.437281  0.250375 1.74651 8.0723e-02 V4 <--- F1
> > b2  0.526620  0.116264 4.52951 5.9119e-06 V2 <--- F2
> > b3  0.452136  0.196716 2.29842 2.1538e-02 V3 <--- F2
> > b4  0.234373  0.247929 0.94532 3.4449e-01 V4 <--- F2
> > b5  0.759562  0.131164 5.79092 7.0001e-09 V5 <--- F2
> > e1  0.052702  0.512634 0.10281 9.1812e-01 V1 <--> V1
> > e2  0.722672  0.126106 5.73065 1.0004e-08 V2 <--> V2
> > e3  0.552314  0.103604 5.33101 9.7669e-08 V3 <--> V3
> > e4  0.651868  0.128687 5.06552 4.0728e-07 V4 <--> V4
> > e5  0.423067  0.163535 2.58702 9.6811e-03 V5 <--> V5
> > c12 0.497557  0.167763 2.96584 3.0186e-03 F2 <--> F1
> > cv1 0.108796  0.076515 1.42190 1.5506e-01 V2 <--> V1
> >
> >   Iterations =  37
> >  >
> >
> > How does that compare to your EQS output?
> >
> > John
> >
> > At 04:37 PM 5/20/2003 +0900, you wrote:
> > >Hi.
> > >
> > >I have tried to use Package "SEM".
> > >
> > >As a learning, I try to convert a program running well of EQS
> > >which is as follows to SEM:
> > >
> > >###  EQS  ###
> > >/SPECIFICATION
> > >CAS=100; VAR=5 MAT=COR; ANA=COR;
> > >/EQUATIONS
> > >V1=*F1+E1; V2=*F1+E2; V3=*F1+*F2+E3; V4=**F1+*F2*E4;
> > >V5=*F2+E5;
> > >/VAR
> > >E1 TO E5=*; F1*1.0; F2=1.0;
> > >/COV
> > >E1,E2=*; F1,F2=*:
> > >/PRINT
> > >FIT ALL;
> > >/MATRIX ......
> > >/END
> > >
> > >This is the converted SEM program.
> > >###
> > >data.mh<-matrix(c(
> > >1.00,0,0,0, 0,
> > >0.38,1.00,0,0, 0,
> > >0.52,0.28,1.00,0,0,
> > >0.55,0.32,0.38,1.00,0,
> > >0.36,0.40,0.48,0.31,1.00
> > >),ncol=5,byrow=T)
> > >
> > >model.mh<-matrix(c(
> > >         'F1 -> V1', 'a1',NA,
> > >         'F1 -> V3', 'a3',NA,
> > >         'F1 -> V4', 'a4',NA,
> > >         'F2 -> V2', 'b2',NA,
> > >         'F2 -> V3', 'b3',NA,
> > >         'F2 -> V4', 'b4',NA,
> > >         'F2 -> V5', 'b5',NA,
> > >         'V1 <-> V1','e1', 1,
> > >         'V2 <-> V2','e2', 1,
> > >         'V3 <-> V3','e3', 1,
> > >         'V4 <-> V4','e4', 1,
> > >         'V5 <-> V5','e5', 1,
> > >         'F1 <-> F1','d1', 1,
> > >         'F2 <-> F2','d2', 1,
> > >         'F1 <-> F2','c12',NA,
> > >         'V1 <-> V2','cv1', NA,
> > >),ncol=3,byrow=T)
> > >obs.vars.mh <- c('V1','V2','V3','V4','V5')
> > >rownames(data.mh) <- colnames(data.mh) <- obs.vars.mh
> > >
> > >sem.mh <- sem(model.mh, data.mh, 100)
> > >###
> > >
> > >At this stage, everything looks going well.
> > >By debug mode of "sem()" it finishes with no error.
> > >However,the process of "summary(sem.mh)" produces an error.
> > >
> > > > summary(sem.mh)
> > >Error in optim(0, function(lam) ((1 - conf.level)/2 - pchisq(chisq, df,  :
> > >         Function cannot be evaluated at initial parameters
> > >In addition: Warning message:
> > >NaNs produced in: sqrt(diag(object\$cov))
> > >
> > >It seems to me that there is something wrong at the conversion of
> > >"E1 TO E5=*;"  to " 'V1 <-> V2','cv1', NA ".
> > >
> > >Could anyone explain me how to manage this trouble?
> > >
> > >Thanks.
> > >
> > >--------========----------
> > >Mitsuo Igarashi
> > >mitsu5 at ruby.famille.ne.jp
> > >
> > >______________________________________________
> > >R-help at stat.math.ethz.ch mailing list
> > >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> > -----------------------------------------------------
> > John Fox
> > Department of Sociology
> > McMaster University
> > Hamilton, Ontario, Canada L8S 4M4
> > email: jfox at mcmaster.ca
> > phone: 905-525-9140x23604
> > web: www.socsci.mcmaster.ca/jfox
> > -----------------------------------------------------

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox

```