Totally new to lavaan and R… trying to run CFA...

1,151 views
Skip to first unread message

Sgars

unread,
Sep 24, 2015, 12:54:28 AM9/24/15
to lavaan
Hi,

I am trying to run a CFA. I have defined my model and that seems to go ok. I then do the model fit and that seems to work. However, when I then use the summary() function the output reads:

Length     Class      Mode 
        1 character character 

This seems to be telling me that my model is a categorical variable, right? Am I using incorrect syntax? Why might this be occurring and what is the correct syntax to be using?

Thanks in advance for your help.

yrosseel

unread,
Sep 24, 2015, 2:18:42 AM9/24/15
to lav...@googlegroups.com
On 09/24/2015 06:54 AM, Sgars wrote:
> Hi,
>
> I am trying to run a CFA. I have defined my model and that seems to go
> ok. I then do the model fit and that seems to work. However, when I then
> use the summary() function the output reads:
>
> Length Class Mode
> 1 character character

Make sure that the first argument of the summary() function is your
fitted lavaan object (often called 'fit'), as in:

HS.model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'

fit <- cfa(HS.model, data=HolzingerSwineford1939)
summary(fit)


It would seem to me that you have typed

summary(HS.model)

(or something similar), which would give you the output you got.

Yves.

Sgars

unread,
Sep 24, 2015, 6:37:07 AM9/24/15
to lavaan
Ahhh…. problem solved. I knew someone here would immediately know what the problem was. Thanks!

Mahima Raina

unread,
Jun 19, 2017, 2:01:35 AM6/19/17
to lavaan
Hi,

I am getting similar error and when I try to fix it using above solution I get the following error :

Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") : cannot open file

The file is read properly as I have run codes other than the CFA() successfully. I am getting this error in the cfa sunction since yesterday, please help !
Thanks in advance !

Terrence Jorgensen

unread,
Jun 19, 2017, 7:09:57 AM6/19/17
to lavaan
Error in file(file, "r") : cannot open the connection

This does not look like you are running summary() on an object of class?lavaan.  Please provide the R syntax that leads to this error, as well as your sessionInfo() output.

Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Mahima Raina

unread,
Jun 25, 2017, 2:33:58 AM6/25/17
to lav...@googlegroups.com
Thanks Terrance.

The other day I reinstalled R and it worked. However, I am having the same issue again. I am writing the code I am using and the error I get. The code first :

model1<-'PowerT=~DP3+DP4+DP5
ObT=~DOR2+DOR3+DOR5
KnowT=~DK1+DK2+DK3+DK4
PowerT~~NA*PowerT
KnowT~~NA*KnowT
ObT~~NA*ObT'

fit<-cfa(model1,data=mydata1)
summary(model1,fit.measures=TRUE,standardized=TRUE)

However, I get error in the fit command (above) only and of course cannot ask for a summary then.


The error I get is :

> model1<-'PowerT=~DP3+DP4+DP5
+ ObT=~DOR2+DOR3+DOR5
+ KnowT=~DK1+DK2+DK3+DK4
+ PowerT~~NA*PowerT
+ KnowT~~NA*KnowT
+ ObT~~NA*ObT'
> fit<-cfa(model1,data=mydata1)
Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") : cannot open file 'PowerT=~DP3+DP4+DP5
ObT=~DOR2+DOR3+DOR5
KnowT=~DK1+DK2+DK3+DK4
PowerT~~NA*PowerT
KnowT~~NA*KnowT
ObT~~NA*ObT': Invalid argument

In this model, I had to fix the values of latent variables to zero. Please let me know also if this is the correct code for the same.

Thanks in advance,

Mahima



--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/7a9OeZFYQrw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+unsubscribe@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at https://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

Mahima Raina

unread,
Jun 25, 2017, 3:32:56 AM6/25/17
to lav...@googlegroups.com
Also , please see this

Attaching package: ‘sem’

The following objects are masked from ‘package:lavaan’:

    cfa, sem


Is this relevant ?

Thanks,
Mahima

Yves Rosseel

unread,
Jun 25, 2017, 5:16:58 AM6/25/17
to lav...@googlegroups.com
On 06/25/2017 09:32 AM, Mahima Raina wrote:
> Also , please see this
>
> Attaching package: ‘sem’ The following objects are masked from
> ‘package:lavaan’: cfa, sem
>
>
> Is this relevant ?


Yes, very much so. It means that you have loaded another package (sem),
which overwrites the lavaan functions cfa/sem.

Either do not load the sem package (at least not in the same R session
as you are using lavaan), or use the lavaan:: prefix to call lavaan's
cfa() function, as in

fit <- lavaan::cfa(model1,data=mydata1)

Yves.

Mahima Raina

unread,
Jun 25, 2017, 5:32:38 AM6/25/17
to lav...@googlegroups.com
Thanks Yves ! So what command can I use to get model summary if I use the lavaan command ??
Thanks a lot :)

Terrence Jorgensen

unread,
Jun 26, 2017, 4:46:52 AM6/26/17
to lavaan
Thanks Yves ! So what command can I use to get model summary if I use the lavaan command ??

summary()

That function will choose the correct summary method based on the type of object you give it.  If you don't load the sem package (only load the lavaan package), then you won't encounter an issue.  Here is a tutorial on lavaan you can read:

Mahima Raina

unread,
Jun 26, 2017, 5:10:12 AM6/26/17
to lav...@googlegroups.com
Thanks so much !

Reply all
Reply to author
Forward
0 new messages