NKGon - 1 year ago 71

R Question

I have some correlation matrices and would like to test whether they are statistically equal. For this, I am using the

`cortest.mat`

`psych`

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.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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