Suppose I have a vector id <- c(1:10) and I want to create random pairs coming from this vector, with the restriction that each pairing has to be unique and each number/id can not be paired with itself. Like:
1-2,2-4,3-7 and so on.
I found a solution to this problem in this stackoverflow answer:
pairOff <- function(x) { x <- sample(x) # to get first column into random order M <- matrix(c(x, x[(seq_along(x) + sample(seq_along(x)[- (length(x) - 1)], 1)) %% length(x) + 1]), ncol=2) return(M) }
returning for example:
[,1] [,2]
[1,] 1 10
[2,] 10 4
[3,] 4 9
[4,] 9 5
[5,] 5 2
[6,] 2 8
[7,] 8 6
[8,] 6 3
[9,] 3 7
[10,] 7 1
Now assume that each id belongs to a group (either 1 or 2, but can also be > 2):
group_ID <- c(1,1,1,1,1,2,2,2,2,2)
and there is a probability for each id to be paired with either another id from the same group or the other group:
p_owngroup <- 0.7
p_othergroup <- 1 - p_owngroup_ID
In the pairOff function, the probability is always p = 0.5.
My question: How can I create a function to create unique pairings (not allowing pairings with oneself and each id has only one, unique partner) where the underlying probability for each id to be paired with an id from the same group is p_owngroup and to be paired with an id from a different group is p_othergroup?
Aucun commentaire:
Enregistrer un commentaire