Read in Summary Data (Correlation Matrix)

115 views
Skip to first unread message

Arne Floh

unread,
Apr 15, 2020, 9:29:36 AM4/15/20
to lavaan
I have problems using a correlation matrix in lavaan. I have read the tutorial on lavaan.org and I can replicate the example. However, when I want to read in my LOWER correlation matrix I always get an error message.

Here's my code:

matrix_ex5 <- read_csv("MASEM/ex5.csv", col_names = FALSE)
view(matrix_ex5) 

ex5_cov <- getCov(matrix_ex5, names = c("a", "b", "c", "d"))

Error code:
Error in lav_matrix_lower2full(x, diagonal = diagonal) : 
  p == round(p, 0) is not TRUE

When I read in the matrix and look at it, it seems that R has replaced the upper triangle with NA's. Not sure if this is causing the error message. Nevertheless, I don't know how to remove the NA's of the upper triangle. I have tried packages such as matrixcalc, etc.

Help appreciated.

ex5.csv
Message has been deleted

Brandon McCormick

unread,
Apr 15, 2020, 12:49:13 PM4/15/20
to lavaan
I had a similar problem recently. One thing to note, you input a correlation matrix, and not a covariance matrix. I recommend converting it to a covariance matrix if you have the standard deviations of each variable. 
To resolve this issue, I input the full correlation matrix (not too hard with your data because it is a small number of vars), then converted it to covariance. 

Here Is my code: 
library(lavaan)

##input correlation matrix


matrix = matrix(

c(

1, .57, .25, .33, .29, .19, .47, .44, .30,

.57,   1, .64, .23, .37, .20, .37, .58, .40, 

.25, .64,   1, .15, .25, .27, .28, .43, .52, 

.33, .23, .15,   1, .38, .26, .22, .21, .12, 

.29, .37, .25, .38,   1, .38, .23, .37, .20,

.19, .20, .27, .26, .38,   1, .13, .20, .31,

.47, .37, .28, .22, .23, .13,   1, .59, .40, 

.44, .58, .43, .21, .37, .20, .59,   1, .60,

.30, .40, .52, .12, .20, .31, 40, .60,   1

), 

nrow=9, 

ncol=9,

byrow=TRUE)


##assign names


dimnames(matrix)= list(

c("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8", "var9"), c("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8", "var9"))

##input SD

SD <- c(.74, .80, .76, .63, .68, .67, .91, 1.11, 1.17)


#convert to covariance matrix

matrix_cov <- cor2cov(matrix,SD)

Arne Floh

unread,
Apr 15, 2020, 2:08:22 PM4/15/20
to lavaan
Thanks Brandon for your help and Sorry for not being clear. I need to use a correlation matrix since I do not have sd's. I've tried using a full matrix but I get the same error message.

Terrence Jorgensen

unread,
Apr 16, 2020, 9:17:13 AM4/16/20
to lavaan
I need to use a correlation matrix since I do not have sd's.

Then to make sure you can trust your SEs and test statistics, you need your model to acknowledge the fact that your total variances must == 1.  For exogenous variables, this is simply a matter of fixing the variance to 1.  For endogenous variables, you will need to label all parameters and constrain the residual variance to be 1 minus the explained variance.
 
I want to read in my LOWER correlation matrix I always get an error message.

I don't know what package's alternative CSV reader you were using, but make sure it is imported as a matrix rather than a data.frame

(M <- as.matrix(read.csv("Desktop/ex5.csv", header = FALSE)))
dimnames
(M) <- list(c("a", "b", "c", "d"), c("a", "b", "c", "d"))
(tM <- t(M)) # save the transpose
upper
.tri(M) # this is how you flag upper/lower triangles
M
[upper.tri(M)] <- tM[upper.tri(tM)]
M
isSymmetric
(M) # TRUE

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

Edward Rigdon

unread,
Apr 16, 2020, 9:35:57 AM4/16/20
to lav...@googlegroups.com
You could also consider copy and paste to get your lower triangular matrix into R. 
Here is an example that I use, involving data from Bagozzi (1980) and the 
lavaan package's getCov() function:

> library("lavaan", lib.loc="~/R/win-library/3.6")

 

> Bagcorstr<-' 1.000

   0.418   1.000

  0.394   0.627   1.000

  0.129   0.202   0.266   1.000

  0.189   0.284   0.208   0.365   1.000

  0.544   0.281   0.324   0.201   0.161   1.000

  0.507   0.225   0.314   0.172   0.174   0.546  1.000

  -0.357  -0.156  -0.038  -0.199  -0.277  -0.294 -0.174  1.000'

 

> Bagnames<-c("Perform1", "JobSat1", "JobSat2", "AchMot1", "AchMot2",
                             "SelfEst1", "SelfEst2", "VerbInt1")

> # Use default spec, first indicator loading fixed to 1

 

> Bagcor<-getCov(Bagcorstr,names=Bagnames)


This works fine, producing a square, symmetric matrix. (Bagozzi 1980 
analyzed the correlation matrix, without correction, because the "Perform" variable's 
variance is about 10,000 times the variances of the other observed variables, and 
analyzing the covariance matrix produces nonconvergence errors.. If you divide 
the "Perform" variable's standard deviation by 100, you can analyze the covariance matrix
without a problem.)

 


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/cb4a4b6b-b804-4990-863c-aecfae2865fb%40googlegroups.com.

Arne Floh

unread,
Apr 23, 2020, 3:34:45 PM4/23/20
to lavaan
Many Thanks Ed. You're always so helpful. By the way, I attended one your SEM PhD classes almost 15 years ago. Good old LISREL-days.
To unsubscribe from this group and stop receiving emails from it, send an email to lav...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages