1

I have some correlation matrices and would like to test whether they are statistically equal. For this, I am using the cortest.mat function from the psych package, but get the following error:

Error in solve.default(R1) : system is computationally singular: reciprocal condition number = 4.96434e-18

Using random numbers also yield the same error, i.e.:

Random<-cor(matrix(rnorm(400, 0, .25), nrow=(20), ncol=(20)))
cortest.mat(Random,Random,n1=400, n2=400)

Since this package was made to compare correlation matrices, I don't understand what I'm doing wrong.

Package: http://www.personality-project.org/r/html/cortest.mat.html

Thanks in advance.

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
NKGon
  • 55
  • 8

1 Answers1

0

You need your matrix to be an object that contains elements of classes psych and sim, which you can achieve with the sim.congeneric function from psych

#The code below results in a sample and population matrix for x and y
y <- sim.congeneric(loads =c(.20,.19,.18,.17,.16,.15,.14,.13,.12,.11,.10,
      .9,.8,.7,.6,.5,.4,.3,.2,.1),N=1000,short=FALSE)
x <- sim.congeneric(loads =c(.20,.19,.18,.17,.16,.15,.14,.13,.12,.11,.10,
      .9,.8,.7,.6,.5,.4,.3,.2,.1),N=1000,short=FALSE)

#To show the class
class(x)
[1] "psych" "sim" 
class(y)
[1] "psych" "sim" 

#Now you can run the test
cortest.mat(x$r,y$r,n1=1000,n2=1000) #here we extract the sample matrix using '$r' and run the test

Tests of correlation matrices 
Call:cortest.mat(R1 = x$r, R2 = y$r, n1 = 1000, n2 = 1000)
 Chi Square value 403.47  with df =  380   with probability < 0.2 

Let's generate a new correlation matrix with a smaller size so we can inspect:

sim.congeneric(loads =c(.5,.4,.3,.2,.1),N=1000,short=FALSE)

Call: NULL

 $model (Population correlation matrix) 
     V1   V2   V3   V4   V5
V1 1.00 0.20 0.15 0.10 0.05
V2 0.20 1.00 0.12 0.08 0.04
V3 0.15 0.12 1.00 0.06 0.03
V4 0.10 0.08 0.06 1.00 0.02
V5 0.05 0.04 0.03 0.02 1.00

$r  (Sample correlation matrix  for sample size =  1000 )
     V1    V2    V3     V4     V5
V1 1.00 0.151 0.124 0.1471 0.0303
V2 0.15 1.000 0.137 0.1083 0.0507
V3 0.12 0.137 1.000 0.0894 0.0159
V4 0.15 0.108 0.089 1.0000 0.0018
V5 0.03 0.051 0.016 0.0018 1.0000

Note that sim.congeneric creates an object with two matrices -one for the sample and the other for population -we used the sample matrices in the test (obviously).

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • Thanks! I think I got it. May I ask two more details: 1) Is there a way to traslate a normal corralation Matrix into a "phych""sim" class and 2) if i just do corttest instead of cortest.mat it works fine, why? Should the result be the same? – NKGon Sep 17 '16 at 18:37
  • @NKGon ``corttest`` defaults to using the ``cortest.normal`` method as opposed to ``cortest.jennrich`` or ``cortest.mat`` so you shouldn't be getting the same result. I just ran my code about using ``corttest`` and I got drastically different results (so check your results once more). ``corttest`` uses the steiger test. – Cyrus Mohammadian Sep 17 '16 at 18:57
  • As far as converting goes, I would accept my solution above by checking the mark next to my answer (if you feel your original question is resolved) and then open a new question in regards to that. Short answer is yes but not the available space in comments to do it, but basically you dont really need a matrix you can just provide the raw data instead of matrix of correlations – Cyrus Mohammadian Sep 17 '16 at 19:08