[R] Help with SEM package: Error message

John Fox jfox at mcmaster.ca
Thu Nov 10 02:28:13 CET 2011


Dear Lisa,

Here's a little CFA simulation of my own. I used the development version
2.1-0 of the sem package (on R-Forge) to allow equation-style specification
of the model, but you should get the same results with version 2.0-0:

--------------- snip ------------

> library(MASS) # for mvrnorm()
> set.seed(12345) # for replication

> R <- matrix(1, 5, 5)
> R[lower.tri(R)] <- R[upper.tri(R)] <- round(runif(10, min=0.2, max=0.8),
3)
> R
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 1.000 0.633 0.725 0.732 0.395
[2,] 0.633 1.000 0.657 0.474 0.506
[3,] 0.725 0.474 1.000 0.300 0.637
[4,] 0.657 0.300 0.506 1.000 0.794
[5,] 0.732 0.395 0.637 0.794 1.000
> eigen(R)$values
[1] 3.3449466 0.7678584 0.4168595 0.3160630 0.1542725

> F <- mvrnorm(1000, rep(0, 5), R)

> Lambda <- matrix(0, 15, 5)
> Lambda[c(1:3, 18+1:3, 36+1:3, 54+1:3, 72+1:3)] <- round(runif(15, min=0.5,
max=0.8), 3)
> Lambda
       [,1]  [,2]  [,3]  [,4]  [,5]
 [1,] 0.637 0.000 0.000 0.000 0.000
 [2,] 0.505 0.000 0.000 0.000 0.000
 [3,] 0.579 0.000 0.000 0.000 0.000
 [4,] 0.000 0.602 0.000 0.000 0.000
 [5,] 0.000 0.574 0.000 0.000 0.000
 [6,] 0.000 0.712 0.000 0.000 0.000
 [7,] 0.000 0.000 0.692 0.000 0.000
 [8,] 0.000 0.000 0.611 0.000 0.000
 [9,] 0.000 0.000 0.749 0.000 0.000
[10,] 0.000 0.000 0.000 0.586 0.000
[11,] 0.000 0.000 0.000 0.648 0.000
[12,] 0.000 0.000 0.000 0.768 0.000
[13,] 0.000 0.000 0.000 0.000 0.692
[14,] 0.000 0.000 0.000 0.000 0.639
[15,] 0.000 0.000 0.000 0.000 0.559

> Y <- (F %*% t(Lambda)) + matrix(rnorm(1000*15), 1000, 15)
> colnames(Y) <- paste("y", 1:15, sep="")

> library(sem)
Loading required package: matrixcalc

> mod.cfa <- specifyEquations(covs=c("F1, F2, F3, F4, F5"))
1: y1 = lam1*F1
2: y2 = lam2*F1
3: y3 = lam3*F1
4: y4 = lam4*F2
5: y5 = lam5*F2
6: y6 = lam6*F2
7: y7 = lam7*F3
8: y8 = lam8*F3
9: y9 = lam9*F3
10: y10 = lam10*F4
11: y11 = lam11*F4
12: y12 = lam12*F4
13: y13 = lam13*F5
14: y14 = lam14*F5
15: y15 = lam15*F5
16: v(F1) = 1
17: v(F2) = 1
18: v(F3) = 1
19: v(F4) = 1
20: v(F5) = 1
21: 
Read 20 records
NOTE: adding 15 variances to the model

> (sem.cfa <- sem(mod.cfa, data=data.frame(Y)))

 Model Chisquare =  54.61949   Df =  80 

     lam1      lam2      lam3      lam4      lam5      lam6 
0.5529479 0.5126612 0.5365723 0.5533220 0.5852442 0.6283580 
     lam7      lam8      lam9     lam10     lam11     lam12 
0.6313437 0.5655048 0.8395153 0.5735607 0.6884884 0.8166616 
    lam13     lam14     lam15  C[F1,F2]  C[F1,F3]  C[F1,F4] 
0.6888107 0.6866472 0.5291979 0.7321806 0.7387977 0.6232119 
 C[F1,F5]  C[F2,F3]  C[F2,F4]  C[F2,F5]  C[F3,F4]  C[F3,F5] 
0.8189901 0.5298096 0.2800764 0.4247156 0.5145992 0.6809838 
 C[F4,F5]     V[y1]     V[y2]     V[y3]     V[y4]     V[y5] 
0.8249320 0.9865355 1.0098899 1.0390452 0.9690317 0.9421020 
    V[y6]     V[y7]     V[y8]     V[y9]    V[y10]    V[y11] 
1.0879510 0.9610472 0.9888304 0.9835167 1.0599708 1.0522614 
   V[y12]    V[y13]    V[y14]    V[y15] 
0.9653279 1.0703453 0.9421894 1.1277448 

 Iterations =  16 

--------------- snip ------------

Best,
 John

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Lisa Pham
> Sent: November-08-11 4:17 PM
> To: John Fox
> Cc: r-help at r-project.org
> Subject: Re: [R] Help with SEM package: Error message
> 
> Dear John,
> 
> Thank you for your reply.  My data is actually simulated under the
> model X = Lambda*F + E.
> 
> Since my post, I've simplified the simulation of my data and I still
> get the error.  This is what I've done since my last post.
> 
> I constructed Lambda apriori (so I know exactly which observed
> variables load onto which factors), E follows a Gaussian with mean 0
> and var-cov matrix given by the Identity matrix.
> 
> For my particular model, I sample the factor scores F_i (for sample i)
> from a multivariate normal
> 
> F_i ~ N(mu_i, Phi).
> 
> mu_i is fixed to Phi*z_i, where z_i is a 5x1 vector.
> 
> Thinking I could have an ill-conditioned var-cov matrix, I looked at
> the condition number of Phi (the factor var-cov matrix).  I recently
> adjusted Phi to ensure that the condition number was indeed small (it
> is now about 2).
> 
> I then sample Y_i ~ N(Lambda*F_i, Psi).
> 
> If the data I'm simulating is ill conditioned, I'm not even sure how to
> fix it because the simulation itself is pretty straightforward.  Even
> with a well conditioned factor var-cov matrix Phi that I used to sample
> my factor scores, I still get that same problem.
> 
> In any case, I am so grateful for your help- I've been working on this
> all day and I can't seem to figure out where I go wrong.  I made Lambda
> pretty sparse and with 150 samples, I certainly don't have too many
> parameters...
> besides identifiability, I'm not sure what to check for if its not a
> problem with my coding.  Your post has already helped me to think about
> this problem a little differently.
> 
> Sincerely,
> Lisa
> 
> 
> On Tue, Nov 8, 2011 at 9:32 PM, John Fox <jfox at mcmaster.ca> wrote:
> 
> > Dear Lisa,
> >
> > There doesn't seem to be anything logically wrong with your model.
> >
> > I don't have much time today to look into it, but trying different
> > optimizers in version 2.0-0 of sem, using the correlation matrix in
> > place of the covariance matrix, and setting the par.size parameter, I
> > was unable to obtain an admissible solution. I also was unable using
> > factanal() to fit an exploratory factor analysis for five factors to
> > your data. I expect that the problem is ill-conditioned.
> >
> > Best,
> >  John
> >
> > ------------------------------------------------
> > John Fox
> > Sen. William McMaster Prof. of Social Statistics Department of
> > Sociology McMaster University Hamilton, Ontario, Canada
> > http://socserv.mcmaster.ca/jfox/ On Tue, 8 Nov 2011 08:18:28 -0800
> > (PST)
> >  lisamp85 <lisamlpham at gmail.com> wrote:
> > > Hello.
> > >
> > > I started using the sem package in R and after a lot of searching
> > > and
> > trying
> > > things I am still having difficulty.  I get the following error
> > > message
> > when
> > > I use the sem() function:
> > >
> > > Warning message:
> > > In sem.default(ram = ram, S = S, N = N, param.names = pars,
> > > var.names = vars,  :
> > >   Could not compute QR decomposition of Hessian.
> > > Optimization probably did not converge.
> > >
> > > I started with a simple example using the specify.model() function,
> > > but
> > it
> > > is really straight forward.  I uploaded my specify.model script and
> > > my
> > data
> > > covariance matrix here too so I wouldn't clutter this email with
> the
> > entire
> > > model (20 observed variables, 5 factors).  Could this error message
> > > be
> > from
> > > the data itself and not from my path model?
> > >
> > > I have my observed variables X and my unobserved variables F.  I
> > > have
> > ONLY
> > > exogenous latent variables (i.e. they never appear on the right
> side
> > > of
> > the
> > > single head arrow ->).  I include all possible factor covariances
> > > FjFk,
> > and
> > > the only constraints I've made was to restrict the Factor variances
> to 1.
> > > My model follows in this basic format (as you can see from my
> > > uploaded
> > > file):
> > >
> > > # Factors (where I specify which observed variables load on to
> which
> > > factors)
> > > # I have only exogenous latent variables F.i -> X.j, lamj.i, NA .
> > > .
> > > .
> > > # Observed variable variances
> > > X.j <-> X.j, ej, NA
> > > .
> > > .
> > > .
> > > # Factor variances (I fixed all factor variances to 1) F.i <-> F.i,
> > > NA, 1 .
> > > .
> > > .
> > > # Factor covariances (I represent all factor covariances, i.e. the
> > > upper
> > or
> > > lower triangle of a covariance matrix) F.i <-> F.k, FiFk, NA .
> > > .
> > > .
> > >
> > > Did I do something wrong here?
> > > Here are my uploaded files:
> > > CFA script:
> > > http://r.789695.n4.nabble.com/file/n4016569/CFA_script.txt
> > > CFA_script.txt
> > > Covariance matrix:
> > > http://r.789695.n4.nabble.com/file/n4016569/covariance_matrix.RData
> > > covariance_matrix.RData
> > >
> > >
> > > Thank you so much for any and all of your help.
> > > Lisa
> > >
> > > --
> > > View this message in context:
> > http://r.789695.n4.nabble.com/Help-with-SEM-package-Error-message-
> tp40
> > 16569p4016569.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> >
> >
> 
> 
> --
> **************************
> Lisa Pham
> PhD Candidate
> Department of Biomedical Engineering
> Bioinformatics Program
> Boston University
> 
> To raise new questions, new possibilities, to regard old problems from
> a new angle, requires creative imagination and marks real advance in
> science.
> - Albert Einstein
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list