-1

I created a SEM model in lavaan that produced a good fit to my data. I inspected the resulting covariance matrix using:

cov1 <- lavInspect(my_fit, what="fitted")$cov

which produced this matrix (the variables themselves are not of interest)

             dpl    cns    par    cbi   med
dpl          0.046                            
cns          0.032  0.064                     
par          0.042  0.042  0.082              
cbi          0.104  0.112  0.164  0.488       
med          0.007  0.009  0.008  0.020 0.012 

I converted this into a correlation matrix with cov2cor

cor1 <- cov2cor(cov1)

which produced this:

             dpl    cns    par    cbi   med
dpl          1.000                            
cns          0.587  1.000                     
par          0.692  0.573  1.000              
cbi          0.692  0.630  0.821  1.000       
med          0.305  0.327  0.248  0.261 1.000 

So far, so good. Just to test things out, I decided to use cor2cov to see if it brought me back to the original corr matrix. For this, I needed both the correlations & the variable sd's out of cor1. So

sd1 <- describe(cor1)$sd
cov2 <- cor2cov(cor1, sd1)

Here's the cov2 matrix:

             dpl    cns    par    cbi   med
dpl          0.062                            
cns          0.035  0.058                     
par          0.049  0.039  0.080              
cbi          0.047  0.042  0.064  0.075       
med          0.024  0.025  0.023  0.023 0.103 

Alas, cov2 is not identical to cov1, which it probably should be. Any ideas as to why these functions produce different results, and more importantly, which one is the "right" cov matrix for further analysis?

steve---g
  • 365
  • 2
  • 11
  • When you go to a correlation matrix, the diagnoals necessarily become 1. That means there is no information left in the correlation matrix regarding the variances. So I would challenge you assumptions about the assumption the there is a unique inverse to the cov2cor operation. – IRTFM Aug 20 '21 at 23:11

1 Answers1

1

I'm guessing that the things you are getting for sd's may not be correct. Since I didn't have those, I built them from the variances. When I follow the example in ?lavaan::cor2cov I get this:

 vnames <- c('dpl' ,   'cns',    'par',    'cbi',   'med')
 entries <- '0.046                            
           0.032  0.064                     
           0.042  0.042  0.082              
           0.104  0.112  0.164  0.488       
           0.007  0.009  0.008  0.020 0.012'

 cov1 <- lavaan::getCov(entries, names=vnames)
 sds <- sqrt(diag(cov1))

 cor1 <- cov2cor(cov1)
 cor1
          dpl       cns       par       cbi       med
dpl 1.0000000 0.5897678 0.6838541 0.6941359 0.2979398
cns 0.5897678 1.0000000 0.5797655 0.6337502 0.3247595
par 0.6838541 0.5797655 1.0000000 0.8198360 0.2550307
cbi 0.6941359 0.6337502 0.8198360 1.0000000 0.2613542
med 0.2979398 0.3247595 0.2550307 0.2613542 1.0000000
 cov2 <- lavaan::cor2cov(cor1,sds=sds)
 cov2
      dpl   cns   par   cbi   med
dpl 0.046 0.032 0.042 0.104 0.007
cns 0.032 0.064 0.042 0.112 0.009
par 0.042 0.042 0.082 0.164 0.008
cbi 0.104 0.112 0.164 0.488 0.020
med 0.007 0.009 0.008 0.020 0.012

> all.equal(cov1,cov2)
[1] TRUE
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks! That indeed gets things back in sync. It's still a mystery to me, though, why `describe(cor1)$sd` produces a different output than `sqrt(diag(cov1))`. Part of the problem might be too many `cor2cov` and `cov2cor` functions masking each other. I might have been using `psych::cor2cov` instead of `lavaan::cor2cov`. – steve---g Aug 21 '21 at 00:23
  • 1
    Yet another reason why rhelp strong suggests including the results of sessionInfo(). What about a checkmark or upvote? (You really ought to go back over your other questions and put in check marks for all the correct answer. I don’t see that you ever have done so.) – IRTFM Aug 21 '21 at 05:03