Extract the Z-value and p-value in the calibrated model

55 views
Skip to first unread message

Seongho Bae

unread,
Jun 24, 2018, 3:13:27 AM6/24/18
to lavaan
Hi, all.
I want to extract the Z-value and p-value in the calibrated model to research.
However, I couldn't find out where they exist. How extract them?

Best,
Seongho

Terrence Jorgensen

unread,
Jun 24, 2018, 10:55:07 AM6/24/18
to lavaan

I want to extract the Z-value and p-value in the calibrated model to research.
However, I couldn't find out where they exist. How extract them?
 
Of parameter estimates?  They are in the parameterEstimates(fit) output.

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

Christopher Castille

unread,
Jun 26, 2018, 5:23:44 PM6/26/18
to lav...@googlegroups.com
As a follow up, is there a way to code the extraction of specific statistics from the output into an RMarkdown manuscript? I’m hoping to make my manuscript reproducible using the package “papaja” and, rather than manually typing in the, say, fit statistics, it would be nice to write a specific line of code that would automate this work.

Chris

Christopher M. Castille, Ph.D.
Assistant Professor of Management
College of Business Administration
Nicholls State University








--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@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.

Jeremy Miles

unread,
Jun 26, 2018, 5:44:46 PM6/26/18
to lavaan
You can use the fitMeasures function. E.g. :

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

fit <- lavaan(HS.model, data=HolzingerSwineford1939,
              auto.var=TRUE, auto.fix.first=TRUE,
              auto.cov.lv.x=TRUE)
summary(fit, fit.measures=TRUE)

fitMeasures(fit)[["chisq"]]
# OR 
cat(round(fitMeasures(fit)[["chisq"]], 1))

Christopher Castille

unread,
Jun 26, 2018, 5:57:02 PM6/26/18
to lav...@googlegroups.com
Thanks Jeremy! Much appreciated!

Christopher Castille

unread,
Jun 29, 2018, 7:30:38 PM6/29/18
to lav...@googlegroups.com
Hi Jeremy,

One more question here.

After testing for, say, measurement invariance using lavaan, one receives the "Chi-Square Difference Test” results in the form of a table. 

How might I print that data to a data frame? Also, if you happen to know “papaja” and can understand how to put that table into an APA formate form, that would be great.

Thanks in advance for you help!

Chris

Jeremy Miles

unread,
Jun 29, 2018, 8:26:35 PM6/29/18
to lavaan
Can you just do something like:

fitMeasures <- fitMeasures(fit)
as.data.frame(fitMeasures)

(Not used papaja, I"m afraid.)

Seongho Bae

unread,
Jun 30, 2018, 5:36:32 AM6/30/18
to lavaan
+1, I agree with you. Someone knows the markdown language, that would be great!

Seongho

2018년 6월 30일 토요일 오전 8시 30분 38초 UTC+9, Christopher Castille 님의 말:
Chris

Thanks Jeremy! Much appreciated!

Chris

Terrence Jorgensen

unread,
Jun 30, 2018, 6:11:41 AM6/30/18
to lavaan
+1, I agree with you. Someone knows the markdown language, that would be great!

Just embed the R syntax that yields the output you want within backticks + the letter "r".  For example,

## R Markdown Section

This is how you run a `lavaan` model:
```{r message=FALSE}
library(lavaan) 
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

fit <- lavaan(HS.model, data=HolzingerSwineford1939,
              auto.var=TRUE, auto.fix.first=TRUE,
              auto.cov.lv.x=TRUE)
summary(fit, fit.measures=TRUE)
```

Some text, blah blah.  This is how you can run some R syntax to create an object in your workspace without printing it to the document.
```{r echo=FALSE}
myFitMeasures <- fitMeasures(fit)
```

Now more text, where I say my model's $\chi^2$ value is `r myFitMeasures[["chisq"]]`, with a *p* value of `r myFitMeasures[[pvalue]]`.

## ------------- end RMarkdown example


That should work.

Christopher Castille

unread,
Jun 30, 2018, 8:37:55 AM6/30/18
to lav...@googlegroups.com
Thanks for the link, Terrence, and for spelling things out clearly as usual.

Unfortunately, it would appear that my situation is a little more complicated. The model that I’m testing examines measurement invariance using your measurementInvarianceCat() function. When I apply the fitMeasures() function to the mi object. My specific syntax is below:

mi <- measurementInvarianceCat(model = initial.cfa, ordered = c("PP1", "PP2", "PP3", "PP4", "PP5", "PP6", "PP7", "PP8", "PP9", "PP10", "IRB1", "IRB2", "IRB3", "IRB4", "IRB5", "IRB6", "IRB7", "OCBI1", "OCBI2", "OCBI3", "OCBI4", "OCBI5", "OCBI6", "OCBI7", "OCBO1", "OCBO2", "OCBO3", "OCBO4", "OCBO5", "OCBO6", "OCBO7"), data = data1, group="COND", estimator = "DWLS", strict = TRUE, parameterization = "theta", information = "expected”)

fitMeasures(mi)

The error that is thrown is:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘fitMeasures’ for signature ‘"list”’

Clearly this is because the mi function generates a list of model fit statistics corresponding to each tested model. I could print all of these statistics to their relative location in a table and then print that table, but I was wondering if a more convenient method was available.

Thanks again in advance for your help.

Chris

Terrence Jorgensen

unread,
Jun 30, 2018, 3:39:35 PM6/30/18
to lavaan
Clearly this is because the mi function generates a list of model fit statistics corresponding to each tested model.

No, it generates a list of fitted lavaan objects.  You can extract anything you need from those lavaan objects by accessing the elements of mi:

## view names of lavaan objects in the list
summary
(mi)
## extract fit measures from a model
fitMeasures
(mi$fit.loadings)
Reply all
Reply to author
Forward
0 new messages