Import correlation matrix

125 views
Skip to first unread message

Jorge Sinval

unread,
May 31, 2018, 11:15:27 AM5/31/18
to lavaan
Hello!

I have this database with the correlation matrix, n, and means, how can I import it to lavaan? I saw the function getCov(), and the cor2cov, although I have no std...

Thanks.

Christopher David Desjardins

unread,
May 31, 2018, 11:22:41 AM5/31/18
to lav...@googlegroups.com
Hi Jorge,
Are you sure the attached is a correlation matrix? The diagonals are not 1.

If you don't have the standard deviations, I don't know how you can get a covariance matrix. However, you could just treat your correlation matrix as a covariance matrix, because a correlation matrix is a covariance matrix.

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



--

Jorge Sinval

unread,
May 31, 2018, 12:03:06 PM5/31/18
to lavaan
Yes, you are right!
Although I get:
Error: p == round(p, 0) is not TRUE


The code I used:
library(readxl)
df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")
df.full.sem

lower <- df.full.sem[2:9,3:10]
lower=as.matrix(lower)
library(lavaan)
wheaton.cov <- getCov(lower, names = c("P1","P2","C1","C2","V1","V2","S1","S2"), diagonal = F)




On Thursday, May 31, 2018 at 4:22:41 PM UTC+1, Christopher Desjardins wrote:
Hi Jorge,
Are you sure the attached is a correlation matrix? The diagonals are not 1.

If you don't have the standard deviations, I don't know how you can get a covariance matrix. However, you could just treat your correlation matrix as a covariance matrix, because a correlation matrix is a covariance matrix.
On Thu, May 31, 2018 at 10:15 AM, Jorge Sinval <jorge...@gmail.com> wrote:
Hello!

I have this database with the correlation matrix, n, and means, how can I import it to lavaan? I saw the function getCov(), and the cor2cov, although I have no std...

Thanks.

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

Terrence Jorgensen

unread,
Jun 2, 2018, 10:17:32 AM6/2/18
to lavaan

lower <- df.full.sem[2:9,3:10]
lower=as.matrix(lower)

This is not correct.  You are extracting a full, nonsymmetric matrix.  To extract only the lower triangle, use that function.

lower <- df.full.sem[lower.tri(df.full.sem)]

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


Jorge Sinval

unread,
Jun 2, 2018, 12:32:05 PM6/2/18
to lavaan
Dear Terrence,

The file contains a lower triangle nonsymmetric matrix of covariances with the diagonal. How can I import it to lavaan? I produced the numerical vector with the means and the vector with the names. But how to produce the correct cov matrix to lavaan?

Jorge Sinval

unread,
Jun 2, 2018, 1:04:15 PM6/2/18
to lavaan
I achieved a solution:

library(readxl)
df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")
mean.wwf=as.numeric(df.full.sem[10,3:10])

df.full.sem=as.matrix(df.full.sem[2:9,3:10])

rownames(df.full.sem) = c("P1","P2","C1","C2","V1","V2","S1","S2")
colnames(df.full.sem)=rownames(df.full.sem)
and then:

model.wwf='
performance =~ P1 + P2
knowledge =~ C1 + C2
value =~ V1 + V2
satisfaction =~ S1 + S2
performance ~ knowledge+value+satisfaction
'

fit.model.wwf=sem(model.wwf, sample.cov = df.full.sem, sample.nobs = 98, sample.mean =mean.wwf)

Thanks!

Christopher David Desjardins

unread,
Jun 2, 2018, 1:04:26 PM6/2/18
to lav...@googlegroups.com
what about?

library(readxl)
df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")

# won't work cause tibbles are awful ----
df.mat <- df.full.sem[2:9,3:10]
wheaton.cov <- getCov(df.mat[lower.tri(df.mat, diag = TRUE)],
       names = c("P1","P2","C1","C2","V1","V2","S1","S2"))

# works cause not a tibble ----
df.mat <- as.matrix(df.full.sem[2:9,3:10])
wheaton.cov <- getCov(df.mat[lower.tri(df.mat, diag = TRUE)],
       names = c("P1","P2","C1","C2","V1","V2","S1","S2"))
wheaton.cov


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

Christopher David Desjardins

unread,
Jun 2, 2018, 1:11:01 PM6/2/18
to lav...@googlegroups.com

This would have also worked because we remove these non-standard tibbles …

library(readxl)
df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")
df.full.sem <- data.frame(df.full.sem)  
df.mat <- df.full.sem[2:9,3:10]
wheaton.cov <- getCov(df.mat[lower.tri(df.mat, diag = TRUE)], 
       names = c("P1","P2","C1","C2","V1","V2","S1","S2"), diagonal= TRUE)
wheaton.cov

Jorge Sinval

unread,
Jun 2, 2018, 1:17:03 PM6/2/18
to lavaan
After all, there is no need of the "getCov" function. Strangely when I did this:

The original matrix:

df.full.sem=as.matrix(df.full.sem[2:9,3:10])

produces:

         P1     P2      C1     C2      V1      V2     S1     S2
[1,] 0.0271     NA      NA     NA      NA      NA     NA     NA
[2,] 0.0172 0.0222      NA     NA      NA      NA     NA     NA
[3,] 0.0219 0.0193  0.0876     NA      NA      NA     NA     NA
[4,] 0.0164 0.0130  0.0317 0.0568      NA      NA     NA     NA
[5,] 0.0284 0.0294  0.0383 0.0151  0.1826      NA     NA     NA
[6,] 0.0217 0.0185  0.0356 0.0230  0.0774  0.1473     NA     NA
[7,] 0.0083 0.0011 -0.0001 0.0055 -0.0087 -0.0069 0.1137     NA
[8,] 0.0074 0.0015  0.0035 0.0089 -0.0007 -0.0088 0.0722 0.1024

However the "getCov" produced

lower=df.full.sem[lower.tri(df.full.sem, diag = TRUE)]
cov.wff=getCov(lower, names=c("P1","P2","C1","C2","V1","V2","S1","S2"))

However the "getCov" produced a diffent matrix:

        P1      P2     C1      C2      V1      V2     S1      S2
P1  
0.0271  0.0172 0.0164  0.0083  0.0130  0.0876 0.0568 -0.0087
P2  
0.0172  0.0219 0.0284  0.0074  0.0294  0.0317 0.0151 -0.0007
C1  
0.0164  0.0284 0.0217  0.0222  0.0185  0.0383 0.0230  0.1473
C2  
0.0083  0.0074 0.0222  0.0193  0.0011  0.0356 0.0055 -0.0069
V1  
0.0130  0.0294 0.0185  0.0011  0.0015 -0.0001 0.0089 -0.0088
V2  
0.0876  0.0317 0.0383  0.0356 -0.0001  0.0035 0.1826  0.1137
S1  
0.0568  0.0151 0.0230  0.0055  0.0089  0.1826 0.0774  0.0722
S2
-0.0087 -0.0007 0.1473 -0.0069 -0.0088  0.1137 0.0722  0.1024

Which is a different matrix.

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.
Auto Generated Inline Image 1
Auto Generated Inline Image 2
Auto Generated Inline Image 3
Auto Generated Inline Image 4
Auto Generated Inline Image 5
Auto Generated Inline Image 6

Christopher David Desjardins

unread,
Jun 2, 2018, 1:20:19 PM6/2/18
to lav...@googlegroups.com
Hi Jorge,

This is not a different matrix. getCov() is just filling in the elements that were below the diagonals into the elements above the diagonal. This is perfectly fine because covariance matrices are symmetric. E.g., comparing row 2, column 1, with column 2, row 1. These are the same values! Which they better be since they are the covariance of P1 and P2. So getCov() works as advertised.


To unsubscribe from this group and stop receiving emails from it, 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.

Jorge Sinval

unread,
Jun 2, 2018, 1:20:23 PM6/2/18
to lavaan
This solution doesn't work:

Since:

fit.model.wwf=sem(model.wwf, sample.cov = wheaton.cov, sample.nobs = 98, sample.mean =mean.wwf)


produces:

Error in lav_samplestats_icov(COV = cov[[g]], ridge = ridge, x.idx = x.idx[[g]], : lavaan ERROR: sample covariance matrix is not positive-definite
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.

yros...@gmail.com

unread,
Jun 2, 2018, 2:52:11 PM6/2/18
to lavaan
getCov expects to see only the lower half of the covariance matrix.

This should work:

df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")
lower <- df.full.sem[2:9,3:10]

COV <- getCov(lav_matrix_vechr(as.matrix(lower)), names = c("P1","P2","C1","C2","V1","V2","S1","S2"), diagonal = TRUE)

Yves.

Jorge Sinval

unread,
Jun 2, 2018, 3:02:37 PM6/2/18
to lav...@googlegroups.com
Yes, that works perfectly. However, if I use just:

```

df.full.sem <- read_excel("BDAMOS.xls", sheet = "Warren")
df.full.sem=as.matrix(df.full.sem[2:9,3:10])
```
It work s also, no need of the getCov() function in this case. Same results.



--
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/2S8Wuu_qO74/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages