Re: [R] SEM polychoric lavaan

2013-08-20 Thread yrosseel

On 08/12/2013 05:55 PM, Pablo Menese Camargo wrote:


fitcfa <- cfa(mcfa, data = data_analisis)
summary(fitcfa, standardized = TRUE, fit.measure=TRUE)
standardizedSolution(fitcfa, type = "std.all")

anyone know what y should do to apply polychoric?


See the manual:

http://lavaan.ugent.be/tutorial/cat.html

Yves.

__
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.


Re: [R] CFA with lavaan or with SEM

2013-01-25 Thread yrosseel

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.


That is a lot of variables, and a rather small sample size (for binary 
data).



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))


To avoid having to type "var?" 25 times, you can say

ordered=paste("var",1:25,sep="")


Is it right that I define the variables as ordered (the output
returns thresholds suggesting I should).


Yes!

Does the cfa command

calculate tetrachoric correlations in the background?


Yes, indeed. You can 'see' it by typing

inspect(fit, "sampstat")

lavaan also computes an asymptotic variance matrix of these 
correlations, so you should get correct standard errors and a correct 
test statistic. By default, lavaan will provide robust standard errors 
and a mean and variance adjusted test statistic (estimator="WLSMV").



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?


You can NOT force these to be equal (at least not in the current version 
of lavaan - 0.5-11, where the residual variance is a function of other 
model parameters). I don't think this is caused by a non-pd correlation 
matrix (you should get a big warning if this was the case). Perhaps the 
sample size is too small. Could you remove some items, or regroup them?



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 will work only if you omit the 'ordered' argument. Perhaps in 
combination with estimator="ULS". But do not trust/report the standard 
errors in this case.



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).


You could do this, and use estimator="ULS". But again, you can not use 
the standard errors.


Yves.
--
http://lavaan.org

__
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.


Re: [R] total indirect effects in structural equation modeling using lavaan

2013-02-22 Thread yrosseel

On 02/21/2013 03:59 PM, Marios wrote:

My question...I would like to calculate the total indirect effects of
all variables on the right-hand-side of the regression eqn's so that i can
work out the total effect (indirect effects + direct effect)

I know the direct effect and i can calculate the total indirect effects by
hand but i was wondering if there is a function that can calculate this?


In the current version of lavaan (0.5-11), there is no function that 
will compute all possible indirect/total effects automatically.


What you can do is to label the coefficients, and then use the ':=' 
operator to define the total/indirect effects that you are interested 
in. Next, you could use se="boot" to get a bootstrap based SE.


See the lavaan paper (http://www.jstatsoft.org/v48/i02/) section 7.4 for 
an example.


Yves.

__
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.


Re: [R] total indirect effects in structural equation modeling using lavaan

2013-02-22 Thread yrosseel

On 02/22/2013 11:40 AM, Marios wrote:

Thank you very much Yves!

I have managed to get the total indirect effects that i wanted but it
seems to only work on the unstandardized coefficients. I use
"standardized =TRUE" in the "summary" command but the "std.all" column
has the same values as the "Estimate" (unstandardized) column for the
new parameters defined by the ":=" operator. All other parameters i.e.
using "~" have been standardized.

How do i go about calculating the indirect effects based on standardized
coefficients?


You need to install 0.5-12, where the newly 'defined' parameters 
(defined by the ":=" operator) are standardized too (just like the other 
parameters). This version is not on CRAN yet, but you can install it by 
typing in R:


install.packages("lavaan", repos="http://www.da.ugent.be";, type="source")

Yves.

--
Yves Rosseel -- http://www.da.ugent.be
Department of Data Analysis, Ghent University
http://lavaan.org

__
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.


Re: [R] two indirect effects of path analysis

2012-10-07 Thread yrosseel

On 10/07/2012 02:17 AM, Elaine Kuo wrote:

Hello,

This is Elaine.

I am trying a path analysis using lavaan Package.

There are three explanatory variables: X, Z, and M.
The response variable is Y.
A, b, and c have direct effects on Y.

On the other hand, X and Z also have direct effects on M.
In other words, X and Z have indirect effects on Y.

I found the code example of lavaan package describes only one indirect
effect as below.
Please kindly advise how to modify it as two indirect effects.


You need to write down all the regressions that are involved in your 
model. For each 'dependent' variable, there is a regression formula:


model <- '
Y ~ X + Z + M
M ~ X + Z
'

Optionally, you can label the parameters, and define some indirect effects:

model <- '
Y ~ c1*X + c2*Z + b*M
M ~ a1*X + a2*Z

ab1 := a1*b
ab2 := a2*b

totalx := ab1 + c1
totalz := ab2 + c2
'

Hope this helps,

Yves.

__
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.


Re: [R] Error in rowMeans function

2012-10-14 Thread yrosseel

On 10/15/2012 08:28 AM, PIKAL Petr wrote:

Hi



-Original Message- From: r-help-boun...@r-project.org
[mailto:r-help-bounces@r- project.org] On Behalf Of Gunsalus,
Catherine Sent: Friday, October 12, 2012 8:52 PM To:
r-help@r-project.org Subject: [R] Error in rowMeans function

Hello, I am trying to create parcels for a CFA model.  I am trying
to average 6 sets of 3 variables each into parcels.  I don't
understand why I am getting an error message as follows: Thanks for
your help, Catherine



atds1par <- rowMeans(semHW1dat1[, c("atds1", "atds2", "atds3")],
na.rm=TRUE)

atds2par <- rowMeans(semHW1dat1[, c("atds4", "atds5", "atds6")],
na.rm=TRUE)

atds3par <- rowMeans(semHW1dat1[, c("atds7", "atds8", "atds9")],
na.rm=TRUE)



sgs1par <- rowMeans(semHW1dat1[, c("sgs1", "sgs2", "sgs3")],
na.rm=TRUE)

sgs2par <- rowMeans(semHW1dat1[, c("sgs4", "sgs5", "sgs6")],
na.rm=TRUE)

sgs3par <- rowMeans(semHW1dat1[, c("sgs7", "sgs8", "sgs9")],
na.rm=TRUE)


The cfa() function requires that all variables that are specified in the 
model syntax are included in a data.frame. So after constructing  your 
parcels, you need to create a new data.frame containing them:


Data <- data.frame(ads1par=atds1par, atds2par=atds2par, 
atds3par=atds3par, sgs1par=sgs1par, sgs2par=sgs2par, sgs3par=sgs3par)


then, you can call the cfa() function but with 'Data' as your data 
argument (note: no need to provide sample.nobs, we get it from the 
data.frame):


parout1 <- cfa(parmod1, data=Data, std.lv=TRUE)

Hope this helps,

Yves
http://lavaan.org

__
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.


Re: [R] Lavaan model

2012-10-31 Thread yrosseel

On 10/31/2012 02:47 PM, sylvain.giroud wrote:

Dear R-users,
Does somebody know what does the "Estimate" reported by the Lavaan model
tell us?
I assume this tells the relative strength of the dyadic relations.


The 'Estimate' column contains the estimated model parameters. There are 
many different kinds of model parameters. Under the heading 'Latent 
variables', you may have factor loadings. Under the heading 
'Regressions' you may have regression coefficients. Other parameters are 
(residual) variances and (residual) covariances.


Yves Rosseel.
http://lavaan.org

__
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.


Re: [R] polycor package

2012-11-17 Thread yrosseel

Dear Laura,

John is correct. The error is produced by the sem() function in the 
lavaan package. The reason is that you did not use proper names for the 
function arguments. The correct call should be:


sem.cdu= sem(cdu, sample.cov=hetcor, sampl.nobs=1861, 
meanstructure=F,fixed.x=F)


But more importantly, using a polychoric correlation as input, and using 
ML estimation will not produce correct standard errors and test 
statistics. A better approach is to use the categorical capabilities of 
lavaan and provide the full data to sem(), indicating which variables 
can be considered as 'ordered'. For example:


sem.cdu= sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE", 
"CDULIKE", "MERKELPROX", "CDUPROX"))


Here, I assumed all variables can be considered as ordered. If some of 
them should be treated as conintuous/numeric, omit them from the 
ordered= argument.


By default, lavaan will use the WLSMV estimator when ordered variables 
are involved.


Yves.


On 11/16/2012 10:13 PM, John Fox wrote:

Dear Laura,

As I explained to you when you wrote to me directly, you're not
having trouble with the polycor package, since you have AFAICS
successfully computed polychoric correlation among your variables.
The error is produced when you call sem(), apparently in the lavaan
package (though you don't mention that), and you should address your
question to the author of that package (or use an appropriate title
line in your r-help posting).

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 Fri, 16 Nov 2012 12:58:03 +
Laura Maria Schwirz  wrote:

I am currently working with R's polycor package and I have
encountered a problem. I tried to follow the steps as outlined in
the sem.pdf file where a CFA model is run using polychoric
correlations. Every time I run the command sem(model, data, N=.), I
get the following warning message: Warning message: In if
(orthogonal) { : the condition has length > 1 and only the first
element will be used

Below is the code that I am using for my dataset. I also tried to
copy and paste the code from the sem.pdf file using data from CNES
but I still get an error message. Any help would be much
appreciated.




germany2009 <- read.dta("StataforR.dta") #attach(germany2009)
viewData(germany2009) # hcor <- function(data) hetcor(data,
std.err=F, pd=T)$correlations hetcor=hcor(germany2009) hetcor

cdupid MERKELLIKECDULIKE MERKELPROXCDUPROX cdupid
1.000  0.5073577  0.6070514 -0.4064925 -0.4146810 MERKELLIKE
0.5073577  1.000  0.7832340 -0.4405571 -0.4200360 CDULIKE
0.6070514  0.7832340  1.000 -0.4868895 -0.5042006 MERKELPROX
-0.4064925 -0.4405571 -0.4868895  1.000  0.8789219 CDUPROX
-0.4146810 -0.4200360 -0.5042006  0.8789219  1.000

viewData(hetcor) cdu= ' CDULIKE ~ cdupid + CDUPROX

+   MERKELLIKE ~ cdupid + MERKELPROX +   CDUPROX ~ ~ cdupid +
cdupid ~ ~ MERKELPROX +   CDUPROX ~ ~ MERKELPROX +   MERKELLIKE ~
1 +   CDULIKE ~1 '

sem.cdu= sem(cdu, hetcor, N=1861, meanstructure=F,fixed.x=F)

Warning message: In if (orthogonal) { : the condition has length >
1 and only the first element will be used


__
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.


Re: [R] polycor package

2012-11-18 Thread yrosseel

The reason I was working with a correlation matrix is because I wanted
to calculate polychoric correlations first before submitting it to the
actual sem command. I was unsure whether R would use polychoric
correlations when indicating which of the variables are ordered. A final
question on the sample.cov=hetcor command. Does this not assume it is a
sample variance-covariance rather than a correlation matrix (as
calculated using the polycor package)?


Yes. Sample.cov should always be a covariance matrix, not a correlation 
matrix. It is pretty useless for categorical data.



I also tried using the command (thanks for the tip!)

sem.cdu= sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE",
"CDULIKE", "MERKELPROX", "CDUPROX"))

but I always get the error result:

semcdu=sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE",
"CDULIKE", "MERKELPROX", "CDUPROX"), fixed.x=F)
Error in tmp[cbind(REP$row[idx], REP$col[idx])] <- partable$free[idx] :
   NAs are not allowed in subscripted assignments


It looks like you have some issues with your model specification. Could 
you remove all '~~' statements and try again?


If the problem remains, please send me your R script, and a snippet of 
the data (just enough to replicate the error).


Yves.

__
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.


Re: [R] error in model specification for cfa with lavaan-package

2011-06-01 Thread yrosseel

Dear Alain,

As for the first error ("sample covariance can not be inverted"): Mike 
is right: with only 10 observations and 16 variables, the ML estimation 
of the sample cov produces a covariance matrix that is not positive 
definite, and hence the inversion (deliberately) fails.


The lesson for me is that lavaan should simply check for this situation 
(p > N), and refuse to proceed (with a gentle and more informative error 
message).


As for the second error ("Error in start.idx[i]:end.idx[i] : NA/NaN 
Argument"): the first argument of cfa() must be a model syntax, not a 
lavaanified object.


The 'lavaanify' function is useful to learn about lavaan, and to export 
the model specification to other software packages, but you should not 
pass a 'lavaanified' object to the cfa/sem/growth functions directly 
(although this may work in the future).


Moreover, to get a 'complete' parameter list, you need to specify a 
number of additional arguments:


model <- model <- lavaanify(cfa.model, auto.fix.first = TRUE, auto.var = 
TRUE, auto.cov.lv.x = TRUE)


(see the 'Details' section in the man page of cfa() for a complete list).

Hope this helps,

Yves.


On 06/01/2011 12:31 PM, D. Alain wrote:

Dear R-List,

(I am not sure whether this list is the right place for my
question...)

I have a dataframe df.cfa

df.cfa<-data.frame(x1=c(5,4,1,5,5,NA,4,NA,NA,5),x2=c(2,3,3,3,NA,1,2,1,2,1),x3=c(5,3,4,1,5,5,5,5,5,5),x4=c(5,3,4,5,5,5,5,5,5,5),x5=c(5,4,3,3,4,4,4,5,NA,5),x6=c(3,5,2,1,4,NA,NA,5,3,4),x7=c(5,4,3,4,4,3,4,NA,NA,4),x8=c(5,5,3,4,5,4,5,4,5,5),x9=c(5,1,1,1,NA,3,3,2,2,3),x10=c(5,5,2,3,4,3,1,3,2,5),x11=c(5,4,4,5,5,4,5,4,5,5),x12=c(5,4,5,5,5,4,5,5,5,5),x13=c(5,4,4,5,5,5,5,5,5,5),x14=c(5,3,3,NA,5,4,5,3,NA,5),x15=c(4,4,4,3,4,5,2,4,5,5),x16=c(5,4,3,3,4,4,3,3,4,5))





and I want to carry out a confirmatory factor analysis using the
"cfa" function (lavaan).

library(lavaan)

then I specify my model

cfa.model<-'  f1=~x1+x2+x3+x4+x5 f2=~x6+x7+x8+x9+x10+x11
f3=~x12+x13+x14+x15+x16 '

and run the cfa routine

fit<-cfa(cfa.model,data=df.cfa,missing="ml")

The output is an error message (here partly in german)

"Error in chol.default(S) : der führende Minor der Ordnung 4 ist
nicht positiv definit Fehler in Sample(data = data, group = group,
sample.cov = sample.cov, sample.mean = sample.mean,  : sample
covariance can not be inverted"

Then I tried to "lavaanify" my model specification first

cfa.model<- lavaanify(cfa.model)

id lhs op rhs user group free ustart fixed.x   label eq.id
free.uncon 1   1  f1 =~  x11 11 NA   0  f1=~x1 0
1 2   2  f1 =~  x21 12 NA   0  f1=~x2 0
2 3   3  f1 =~  x31 13 NA   0  f1=~x3 0
3 4   4  f1 =~  x41 14 NA   0  f1=~x4 0
4 5   5  f1 =~  x51 15 NA   0  f1=~x5 0
5 6   6  f2 =~  x61 16 NA   0  f2=~x6 0
6 7   7  f2 =~  x71 17 NA   0  f2=~x7 0
7 8   8  f2 =~  x81 18 NA   0  f2=~x8 0
8 9   9  f2 =~  x91 19 NA   0  f2=~x9 0
9 10 10  f2 =~ x101 1   10 NA   0 f2=~x10 0
10 11 11  f2 =~ x111 1   11 NA   0 f2=~x11 0
11 12 12  f3 =~ x121 1   12 NA   0 f3=~x12 0
12 13 13  f3 =~ x131 1   13 NA   0 f3=~x13 0
13 14 14  f3 =~ x141 1   14 NA   0 f3=~x14 0
14 15 15  f3 =~ x151 1   15 NA   0 f3=~x15 0
15 16 16  f3 =~ x161 1   16 NA   0 f3=~x16 0
16

I run cfa again

fit<-cfa(cfa.model,data=df.cfa,missing="ml")

And this time I get another error information

"Error in start.idx[i]:end.idx[i] : NA/NaN Argument"

I must admit that I am stuck, can anyone help?

Thanks, Alain [[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.


__
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.


Re: [R] Results of CFA with Lavaan

2011-06-09 Thread yrosseel

On 06/08/2011 11:56 PM, R Help wrote:

Yes, that is the difference.  For the last SEM I built I fixed the
factor variances to 1, and I think that's what I want to do for the
CFA I'm doing now.  Does that make sense for a CFA?


If you have a latent variable in your model (like a factor in CFA), you 
need to define its metric/scale. There are typically two ways to do 
this: 1) fix the variance of the latent variable to a constant 
(typically 1.0), or 2) fix the factor loading of one of the indicators 
of the factor (again to 1.0). For CFA with a single group, it should not 
matter which method you choose. The fit measures will be identical.


Lavaan by default uses the second option. If you prefer the first 
(fixing the variances), you can simply add the 'std.lv=TRUE' option to 
the cfa() call, and lavaan will take care of the rest.



I'll try figuring out how to do that with lavaan later, but my model
takes so long to fit that I can't try it right now.


You can use the 'verbose=TRUE' argument to monitor progress. You may 
also use the options se="none" (no standard errors) and test="none" (no 
test statistic) to speed things up, if you are still constructing your 
model. Or the model does not convergence, but I should see both the 
model and the data to determine the possible cause.


Hope this helps,

Yves Rosseel
http://lavaan.org

__
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.


Re: [R] Results of CFA with Lavaan

2011-06-09 Thread yrosseel

On 06/09/2011 05:21 PM, R Help wrote:

Ok, I think this is the last question I have.  My model is producing
an estimate of intercepts for my variables along with my loadings.
 From the documentation it appears that this is controlled by the
meanstructure   option in cfa.  It says that setting it to TRUE includes
the intercepts, and setting it to "default" means thatthe value is set
based on the user-specified model, and/or the values of other
arguments.  I've included my model specification below, and I would
prefer not to fit intercepts, but setting it to FALSE does not seem to
achieve this.


Several arguments of the cfa() function force meanstructure=TRUE (and 
indeed, silently overriding the meanstructure=FALSE option if specified 
by the user; perhaps, lavaan should spit out a warning if this happens).


The following argument choices force meanstructure to be TRUE (if there 
is only a single group):


- estimator = "mlm" or "mlf" or "mlr"
- missing = "ml" or "fiml"

Did you use any one of those arguments?

But why would you prefer not to fit the intercepts? If there are no 
restrictions on the intercepts/means, fitting them should have no effect 
on your model fit whatsoever.


Yves Rosseel
http://lavaan.org

__
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.


Re: [R] Results of CFA with Lavaan

2011-06-09 Thread yrosseel

On 06/09/2011 06:06 PM, R Help wrote:

I am using missing = 'fiml', which would require estimating intercepts.

I figured they would effect my overall model fit, but can I still
estimate my loading coefficients the same way?


Yes, no problem.

Yves Rosseel
http://lavaan.org

__
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.


Re: [R] confirmatory factor analysis program in R

2011-03-24 Thread yrosseel
the cfa() function is in the 'lavaan' package

--
View this message in context: 
http://r.789695.n4.nabble.com/confirmatory-factor-analysis-program-in-R-tp3386133p3401847.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] binary exogenous variable in path analysis in sem or lavaan

2011-03-24 Thread yrosseel
Rob,

If 'sex' is indeed an exogenous variable (ie. predictor only), you can
simply code it as (1=male, 2=female) and use it as a covariate in any sem
model. In lavaan, you can explicitly use the argument 'fixed.x=TRUE', which
will regard all exogenous covariates as fixed variables. Their
means/variances/covariances are not included in the model estimation (so
they do not have to be normally distributed), but they are simply set at
their sample values. In essence, this is exactly what happens in ordinary
regression, when a categorical predictor is recoded as a dummy variable.

Yves.

--
View this message in context: 
http://r.789695.n4.nabble.com/binary-exogenous-variable-in-path-analysis-in-sem-or-lavaan-tp3355987p3401870.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Structural equation modeling in R(lavaan,sem)

2011-03-28 Thread yrosseel

On 03/28/2011 04:18 AM, jouba wrote:


Jeremy thanks a lot for your response I have read sem package help
and I currently reading the help of lavaan I see that there is also
an other function called lavaan can do the SEM analysis So I wonder
what is the difference between this function and the sem function


The 'sem()' function (in the lavaan package) is more user-friendly, in 
the sence that it sets a number of reasonable options by default, before 
calling the lower-level 'lavaan()' function (which has the 'feature' of 
doing nothing automatically, but expects that you really know what your 
are doing).


Most users should only use the 'sem()' function (or the 'cfa()' 
function). For non-standard models, the 'lavaan()' function gives more 
control.



Also I am wondering in the case where we have categorical variables
and discreet variables??


Currently, the lavaan package (0.4-7) has no support for categorical 
variables.



calculate the correlation matrix , mainly when we have to calculate
these between a quantitative and qualitative variables, I wonder if
polycor package is the best solution for this


It depends. The 'hetcor()' function in the polycor package may provide a 
suitable correlation matrix that can be used with the 'sem' package or 
the 'lavaan' package. However, AFAIK, the polycor does not compute the 
corresponding asymptotic weight matrix which you need for getting proper 
standard errors and test statistics (in a WLS context).


The OpenMx package (http://openmx.psyc.virginia.edu/) has some support 
for categorical (ie binary/ordinal) observed variables (although I'm not 
sure if they can handle the joint analysis of ordinal and continuous 
variables yet).


But none of this is needed _if_ the categorical variables are all 
exogenous (ie predictor variables only) in which case you can still use 
the methods for continuous data.


Yves.

--
Yves Rosseel -- http://www.da.ugent.be
Department of Data Analysis, Ghent University
Henri Dunantlaan 1, B-9000 Gent, Belgium

__
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.


Re: [R] Structural equation modeling in R(lavaan,sem)

2011-03-30 Thread yrosseel

On 03/29/2011 10:49 PM, jouba wrote:


Dear all

I have an  error mesage
« error message : the MLM estimator can not be used when data are incomplete »
When i use the function sem(package lavaan) and  when i  fill  the  paramters  
estimator  and missing
estimator="MLM", missing="ml"

i understand by this that i am not allowed to use estimator= ``mlm ``  in 
parralle to the parameter missing


You understood correctly. Estimator 'MLM' (=ML estimation + 
Satorra-Bentler scaled test statistic) is only defined for complete 
data. The equivalent for incomplete data is the estimator 'MLR' (=ML 
estimation + Yuan-Bentler scaled test statistic).


Yves.

__
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.


Re: [R] Structural equation modeling in R(lavaan,sem)

2011-04-04 Thread yrosseel

On 04/03/2011 09:38 PM, jouba wrote:


Daer all, I have a question concerning longitudinal data: When we
have a longitudinal data and we have to do sem analysis there is in
the package lavaan some functions,options in this package that help
to do this or we can treat these data like non longitudinal data


The function 'growth' (in the lavaan package) can be used for (standard) 
growth modeling. Good material about growth modeling (using Mplus) can 
be found here:


http://statistics.ats.ucla.edu/stat/mplus/seminars/gm/default.htm

Next, you can read how to do growth modeling with lavaan by reading 
section 7 in the lavaan intro, which you can download from the 
documentation section on the lavaan website (http://lavaan.org).


Yves.

--
Yves Rosseel -- http://www.da.ugent.be
Department of Data Analysis, Ghent University
Henri Dunantlaan 1, B-9000 Gent, Belgium

__
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.


Re: [R] Structural equation modeling in R(lavaan,sem)

2011-04-05 Thread yrosseel

On 04/04/2011 07:14 PM, jouba wrote:



Thanks you for your response
For lavaan package can i have more information about this example you have 
applied in the section 7
the meanings  of The variables (c1,c2,c3,c4, i ,s ,x1,x2)
I think i have need more information to learn  more about how  able to apply 
growth model  in my data (longitudianl data)


In the example, c1-c4 are time-varying covariates, i and s are the 
random intercept and slope respectively, and x1 and x2 are two exogenous 
covariates influencing the intercept and slope.


Please note: the lavaanIntroduction document is hardly useful to _learn_ 
about growth models (or any SEM model for that matter). It only explains 
how to fit them using the lavaan package. To learn about growth models, 
you may want to read any one of the books below:


Latent Curve Models: A Structural Equation Perspective (Wiley Series in 
Probability and Statistics) by Kenneth A. Bollen and Patrick J. Curran 
(Hardcover - Dec 23, 2005)


Latent Growth Curve Modeling (Quantitative Applications in the Social 
Sciences) by Dr. Kristopher J. Preacher, Aaron Lee Wichman, Robert 
Charles MacCallum and Dr. Nancy E. Briggs (Paperback - Jun 27, 2008)


An Introduction to Latent Variable Growth Curve Modeling: Concepts, 
Issues, and Applications (Quantitative Methodology) (Quantitative 
Methodology Series) by Terry E. Duncan, Susan C. Duncan and Lisa A. 
Strycker (Paperback - May 23, 2006)


Yves.

__
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.


Re: [R] lavaan: how to analyse residuals of a latent variable

2011-08-09 Thread yrosseel



My question is: how can I analyse the part of the variation in
fire.setting that is not included in the latent variable criminality?
Ideally I would want a new variable that captures just this. Then I
could model regressions with this variable as the dependent variable.


You can add a regression line to your model syntax with 'fire.setting' 
as the dependent variable:


fire.setting ~ x1 + x2 + x3

were x1-x3 are additional predictors that might influence the variable 
'fire.setting'.


Yves Rosseel
http://lavaan.org

__
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.


Re: [R] lavaan: how to analyse residuals of a latent variable

2011-08-09 Thread yrosseel



Can I include criminality among those and thereby get the common part
of criminality and fire.setting "out of the way"?


No. You already regress fire.setting on criminality since it is an 
indicator in the measurement model of criminality. In other words, the 
'criminality' part is already regressed out.


Yves Rosseel
http://lavaan.org

__
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.


Re: [R] [Solved] Re: lavaan: how to analyse residuals of a latent variable

2011-08-10 Thread yrosseel



Maybe this kind of usage of lavaan is not very common, but in order to
help others in my situation, is this documented somewhere? My
understanding of latent variable analysis is indeed limited, but I did
not understand that lavaan worked liked this when I read the
documentation.


This is not specific to lavaan; the same strategy would work in other 
(commercial) software as well.


But of course, lavaan needs better documentation. If only there was more 
time...


Yves Rosseel
http://lavaan.org

__
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.


Re: [R] FIML using lavaan returns zeroes for coefficients

2012-07-21 Thread yrosseel

On 07/20/2012 10:35 PM, Andrew Miles wrote:

Hello!

I am trying to reproduce (for a publication) analyses that I ran
several months ago using lavaan, I'm not sure which version, probably
0.4-12. A sample model is given below:

pathmod='mh30days.log.w2 ~ mh30days.log + joingroup + leavegroup +
alwaysgroup + grp.partic.w2 + black + age + bivoc + moved.conf +
local.noretired + retired + ds + ministrytime + hrswork +
nomoralescore.c + negint.c + cong.conflict.c + nomoraleXjoin +
nomoraleXleave + nomoraleXalways + negintXjoin + negintXleave +
negintXalways + conflictXjoin + conflictXleave + conflictXalways '
mod1 = sem(pathmod, data=sampledat, missing="fiml", se="robust")

At the time, the model ran fine.  Now, using version 0.4-14, the
model returns all 0's for coefficients.


What happened is that since 0.4-14, lavaan tries to 'detect' models that 
are just univariate regression, and internally calls lm.fit, instead of 
the lavaan estimation engine, at least when the missing="ml" argument is 
NOT used. (BTW, I fail to understand why you would use lavaan if you 
just want to fit a univariate regression).


When missing="ml" is used, lavaan normally checks if you have fixed x 
covariates (which you do), and if fixed.x=TRUE (which is the default). 
In 0.4, lavaan internally switches to fixed.x=FALSE (which implicitly 
assumes that all your predictors are continuous, but I assume you would 
not using missing="ml" otherwise). Unfortunately, for the 'special' case 
of univariate regression, it fails to do this. This behavior will likely 
change in 0.5, where, by default, only endogenous/dependent variables 
will be handled by missing="ml", not exogenous 'x' covariates.


To fix it: simply add the fixed.x=FALSE argument, or revert to 0.4-12 to 
get the old behavior.


Hope this helps,

Yves.
http://lavaan.org

__
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.


Re: [R] easy way to fit saturated model in sem package?

2012-07-23 Thread yrosseel



I will check out the lavaan package.


Dear Joshua,

The lavaan package may help you. The FIML estimator typically starts 
with the EM algorithm to estimate the moments of the unrestricted model. 
There is no 'one-shot' function for it, at the moment, but if you only 
need those moments, you can do something like this:


Suppose your data is a data.frame called 'HS.missing', then the 
following commands can be used to get the estimated moments:


library(lavaan)

Data <- lavaan:::lavData(HS.missing, ov.names=names(HS.missing), 
missing="fiml")


# we assume only 1 group
Missing <- lavaan:::getMissingPatternStats(X = Data@X[[1L]], Mp = 
Data@Mp[[1L]])


# compute moments using EM algorithm
Moments <- lavaan:::estimate.moments.EM(X=Data@X[[1L]], M=Missing, 
verbose=TRUE)


# estimated covariance matrix
Moments$sigma

# estimated mean vector
Moments$mu


Hope this helps,

Yves Rosseel
http://lavaan.org

__
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.


Re: [R] covariance estimate in function sem (Lavaan)

2012-08-10 Thread yrosseel

On 07/30/2012 11:00 PM, Luna wrote:

Dear R users,
I have a hard time interpreting the covariances in the parameter estimates
output (standardized), even in the example documented (PoliticalDemocracy).
Can anyone tell me if the estimated covariances are residual covariances
(unexplained by the model), or the covariances of the observable variables?


They are *residual* covariances.


I haved checked the data and it does not look like the covariances of the
observable variables, however when I tried to find out using simulated data
( with correlated residuals) the estimates did not seem to be the covariance
of the residuals either (much much underestimated). Can anyone help?


How did you simulate your data? It is rather tricky to generate data 
under a known CFA/SEM model with pre-specified residual (co)variances.


Yves Rosseel.
http://lavaan.org

__
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.


Re: [R] FIML with missing data in sem package

2011-12-01 Thread yrosseel

On 12/01/2011 05:25 PM, Dustin Fife wrote:


What is your goal?  I have used and like mice pretty well, but using
mice + sem to try to address missingness seems like more work than
using FIML in OpenMx or lavaan to try to address it.  Is there a
reason you want to use the sem package or a reason you do not want to
use the others?

I tried lavaan and couldn't get it to work. I noticed it was in beta so I

figured it wasn't a good idea to use for simulations I intend to publish
(especially when I can't get them to work properly). Either way, whether I
use OpenMX or lavaan, it will require me to learn a new syntax, and I guess
I'm just lazy. I'm comfortable with sem.


The 'beta' status of lavaan implies that things (eg function arguments) 
may change from one version to another, until most planned features are 
implemented. However, the computations/results themselves are pretty 
stable. Many SEM reseachers do use lavaan today for simulation studies, 
and some have been published already. I can provide you with some 
references off-list if you are interested.


Yves Rosseel.
http://lavaan.org

__
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.