Re: [R] CFA with lavaan or with SEM

2013-01-23 Thread David Purves
Hi John

Thanks for your quick reply.

The full warning I got is

' Error in csem(model = model.description, start, opt.flag = 1, typsize = 
typsize,  :
  The matrix is non-invertable.'

The eigenvalues of the tetrachoric correlations are non negative. So it is must 
be how I am defining my model.

I have also tried it without having lavaan in the session.

A wee example of my error (whether it is sensible);

library(sem)

my.cor<-matrix(c( 1.000  ,  0.7600616  ,  0.3653309 ,   0.4377949 , 
0.2917927 ,   0.5133697,
0.7600616 ,   1.000,   0.6335519 ,   0.8288809 , 0.6223942  ,  
0.6355725,
 0.3653309 ,  0.6335519  ,  1.000 ,   0.9098309 , 0.9098309  ,  
0.7693395,
 0.4377949 , 0.8288809  ,  0.9098309  ,  1.000  ,0.9136967   , 
0.7829854,
  0.2917927  ,0.6223942  ,  0.9098309  ,  0.9136967  ,1.000   , 
0.7354562,
 0.5133697  ,0.6355725  ,  0.7693395  ,  0.7829854 , 0.7354562   , 
1.000),
nrow=6,byrow=T)

colnames(my.cor)<-rownames(my.cor)<-c("a","b","c","d","e","g")

eigen(my.cor)
solve(my.cor)

#i tried defining the model in two ways

model.1<-matrix(c(
#   arrow   #parameter  #start
"f -> a",   "g1",   NA,
"f -> b",   "g2",   NA,
"f -> c",   "g3",   NA,
"f -> d",   "g4",   NA,
"f -> e",   "g5",   NA,
"f -> g",   "g6",   NA,
"f <-> f",  NA, 1),
ncol=3,byrow=T)

out<-sem(model.1,S=my.cor,200)

model.1 <- specifyEquations()
 f1 = gam11*a + gam12*b + gam13*c + gam14*d + gam15*e + gam16*g
 f1 = 1* f1

out<-sem(model.1,S=my.cor,200)

But the same error.

I would be very grateful if you could indicate where the error in my code is 
please.


thanks, david




-Original Message-
From: John Fox [mailto:j...@mcmaster.ca]
Sent: 23 January 2013 14:00
To: David Purves
Cc: r-help@R-project.org
Subject: Re: [R] CFA with lavaan or with SEM

Dear David,

On Wed, 23 Jan 2013 11:19:09 +
 David Purves  wrote:
> Hi
>
> Sorry for the rather long message.
>

. . .

>
> I have tried the analysis using John Fox's SEM package / command.
>
> I calculate the correlation matrix with smoothing
>
> my.cor<-hetcor(north.dat.sub,use="pairwise.complete.obs")$correlations
>
> This returns the warning indicating that the correlation matrix was adjusted 
> to make it positive definite. However the following sem model does not run, 
> with the error message that the matrix is non-invertible.
>
> mod1<-sem::sem(sem .model.1, S=my.cor, 300)
>
> Should the smoothing not allow it to be inverted?
>

If the input correlation matrix is really positive definite, then it has an 
inverse. You could check directly, e.g., by looking at the eignevalues of the 
tetrachoric correlation matrix. There's very little here to go on, not even the 
error message produced by sem(). By the way, I assume that you didn't really 
call sem in the sem package as sem::sem in a session in which lavann was 
loaded. I'm not sure what would happen if you did that.

Best,
 John


John Fox
Sen. William McMaster Prof. of Social Statistics Department of Sociology 
McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/

>
> __
> R-help@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.




The University of Glasgow, charity number SC004401

__
R-help@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.


[R] CFA with lavaan or with SEM

2013-01-23 Thread David Purves
Hi

Sorry for the rather long message.

I am trying to use the cfa command in the lavaan package to run a CFA however I 
am unsure over a couple of issues.

I have @25 dichotomous variables, 300 observations and an EFA on a training 
dataset suggests a 3 factor model.

After defining the model I use the command

fit.dat <- cfa(model.1, data=my.dat, std.lv = T, estimator="WLSMV", 
ordered=c("var1","var2" and so on for the other 23 variables))

Is it right that I define the variables as ordered (the output returns 
thresholds suggesting I should). Does the cfa command calculate tetrachoric 
correlations in the background?

However, output for the command returns two variables with  small negative 
variances (-0.002) which I think is due to the correlation matrix not being 
positive definite. Is it reasonable to force these to be zero when defining the 
model or is this more a sign of problems with the model?

As an alternative is it possible to calculate the tetrachoric correlations 
using hetcor (which applies smoothing) and then use the smoothed sample 
correlation as the input to the model, such as

fit.cor <- cfa(model.1, sample.cov=my.hetcor, sample.nobs=300, std.lv = 
T,estimator="ML", ordered=c("var1","var2" and so on for the other 23 
variables)).

This however does not produce thresholds suggesting what I have tried is 
nonsense but is there a way to do this?

Final question is I have a lot of missing data - listwise deletion leaves 90 
subjects. Is there a way to calculate estimates using pairwise deletion (this 
is another reason why I tried using the correlation matrix as the input).



I have tried the analysis using John Fox's SEM package / command.

I calculate the correlation matrix with smoothing

my.cor<-hetcor(north.dat.sub,use="pairwise.complete.obs")$correlations

This returns the warning indicating that the correlation matrix was adjusted to 
make it positive definite. However the following sem model does not run, with 
the error message that the matrix is non-invertible.

mod1<-sem::sem(sem .model.1, S=my.cor, 300)

Should the smoothing not allow it to be inverted?

thanks for help, david




The University of Glasgow, charity number SC004401

[[alternative HTML version deleted]]

__
R-help@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.