1

R-novice here. I am running a CFA using the Lavaan package. It is giving me everything I need, except my p-value is coming up at .000 and I would like to find the exact p-value (i.e. .0000009 or whatever it might be).

I have tried to use:

format.pval(p, digits=6)

and

options(digits=10)

but neither of these changed my output.

The code I am using is:

fit<-cfa(model,data=fulldata,estimator = "WLSMV") 
summary(fit,fit.measures=TRUE,standardized=TRUE)  
dbc
  • 104,963
  • 20
  • 228
  • 340

2 Answers2

1

lavaan has special classes for its output, with dedicated print methods. The number of digits is controlled by the nd= argument (see the class?lavaan help page for more details). For example:

example(cfa)
summary(cfa, nd = 5)

PE <- parameterEstimates(fit)
print(PE, nd = 6)

FM <- fitMeasures(fit, "pvalue")
print(FM, nd = 7)
Terrence
  • 780
  • 4
  • 7
  • AMAZING! That worked! Thank you so much -- I have literally banging my head against the wall trying to figure this out -- I very much appreciate it! – Nichole Elizabeth Jul 09 '22 at 12:13
0

You can generally control the number of decimal places displayed using sprintf(). Here is an example displaying the first 20 decimal places of 1/7:

sprintf("%.20f", 1/7)

In your case, you would replace 1/7 by p. Make sure that p is a numeric value, not a string. If necessary, coerce (i.e., convert) p to numeric using as.numeric(). Also note that the output of sprintf() will be a string, not a number.

lhdjung
  • 1
  • 2
  • Thank you for helping -- I am still a bit lost, because I am not sure where in my code to add this. The p-value is stored within the "fit" variable I have created -- so I am not sure at what point I can add in specifiers to modify values that are part of that variable. – Nichole Elizabeth Jul 07 '22 at 18:22
  • Sorry, I sidestepped lavaan in my earlier post. It seems you can access the p-value from the model like this: `p <- fit@baseline$test$standard$pvalue`. After that, try `sprintf("%.20f", p)`. (20 is just an example for a sufficiently large number of decimal places.) Reaching directly into an S4 object as I just did with `fit` is [not always good practice](https://stackoverflow.com/questions/9900134/is-it-bad-practice-to-access-s4-objects-slots-directly-using) but it should be fine for reading access, as in your case. `sprintf()` also works (for me) with the `FM` object from Terrence's answer. – lhdjung Jul 08 '22 at 14:40
  • Thank you!! I very much appreciate the follow up! – Nichole Elizabeth Jul 09 '22 at 12:14