a 10x10 matrix contains "likelihoods" for any cell being selected in a given row during a draw.
id1 id2 id3 id4 id5 id6 id7 id8 id9 id10
id1 NA 0.5 0.7 0.5 0.5 0.4 0.4 0.4 0.4 0.4
id2 0.5 NA 0.5 0.5 0.5 0.4 0.4 0.4 0.4 0.4
id3 0.7 0.5 NA 0.5 0.5 0.4 0.4 0.4 0.4 0.4
id4 0.5 0.5 0.5 NA 0.5 0.4 0.4 0.4 0.4 0.4
id5 0.5 0.5 0.5 0.5 NA 0.4 0.4 0.4 0.4 0.4
id6 0.4 0.4 0.4 0.4 0.4 NA 0.5 0.7 0.5 0.5
id7 0.4 0.4 0.4 0.4 0.4 0.5 NA 0.5 0.5 0.5
id8 0.4 0.4 0.4 0.4 0.4 0.7 0.5 NA 0.5 0.5
id9 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 NA 0.5
id10 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 NA
Each draw is done by row, and the chance of a cell being chosen is the value of that cell divided by the sum of all cell values in a given row. For example, I need to pick a cell from id2
to id10
in the row id1
. The most likely choice is id3
because its value of 0.7
is the highest in the row.
I need a vector called result
that stores the choice for each row after I choose. My current plan is to:
- sum across rows and store the results as a vector
denom
- generate a random uniform variable between 0 and this sum for each row
- if the value is between 0.0 and 0.5, the chosen person in row 1 is id2; if 0.51-1.20, the chosen person is id3...etc.
This is obviously way too much work. What's a better way to sample with weights while ignoring the NA values in the diagonal?
Aucun commentaire:
Enregistrer un commentaire