# Matrix for MRQAP/network regression

Hey everyone!
I have data on the sex of the respondents with 1=male 2=female.

To performe MRQAP I need a relational matrix as independent variable where 1=same gender 0=different gender.
How can I generate a matrix like this from my initial data on sex?

For those of us not familiar with MRQAP it would help to provide an example of what you're trying to achieve.

If I understand correctly, you have a vector like this:

``````respondents <- sample(c(1, 2), size = 6, replace = TRUE)
respondents
#> [1] 2 1 2 1 1 1
``````

And you want to make a matrix by comparing the vector to itself. You can do that with `outer()`, telling it that the operation you are doing to compare these vectors is equality `==`

``````
outer(respondents, respondents, "==")
#>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
#> [1,]  TRUE FALSE  TRUE FALSE FALSE FALSE
#> [2,] FALSE  TRUE FALSE  TRUE  TRUE  TRUE
#> [3,]  TRUE FALSE  TRUE FALSE FALSE FALSE
#> [4,] FALSE  TRUE FALSE  TRUE  TRUE  TRUE
#> [5,] FALSE  TRUE FALSE  TRUE  TRUE  TRUE
#> [6,] FALSE  TRUE FALSE  TRUE  TRUE  TRUE
``````

Or if you want it with `0` and `1` instead of TRUE/FALSE:

``````1L * outer(respondents, respondents, "==")
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    0    1    0    0    0
#> [2,]    0    1    0    1    1    1
#> [3,]    1    0    1    0    0    0
#> [4,]    0    1    0    1    1    1
#> [5,]    0    1    0    1    1    1
#> [6,]    0    1    0    1    1    1
``````

Created on 2022-11-10 by the reprex package (v2.0.1)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.