vendredi 20 octobre 2023

Randomizing 1s and 0s by groups while specifiying proportion of 1 and 0 within groups

First, I want to create a column that randomize 1s and 0s by group while maintaining the same proportion of 1s and 0s in another column.

Second, I want to repeat the above procedure many times (say 1000) and calculate the expected value.

Let me clarify with hypothetical data.

library(data.table) 

district <- c(1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3)                                       
village <- c(1,2,3,4,1,2,3,4,5,1,2,3,4,5,6,7)                              
status <- c(1,0,1,0, 1,1,1,0,0,1,1,1,1,0,0,0) 

datei <- data.table(district, village, status) 

What I want to do is I want to create a column that randomize 1s and 0s within a district while maintaining the same proportion of 1s and 0s in status; the proportions of 1:0 are 2:2, 3:2 and 4:3 in district 1, 2 and 3 respectively.

Second, I also want to repeat this randomization many times (say 1000 times) and calculate the expected value for each row.

I know how to randomize 1s and 0s based on district.

datei[, random_status := sample(c(1,0), .N, replace=TRUE), keyby = district]

However, I do not know how to have the same proportion of 1s and 0s as in status and how to repeat and calculate the expected values for each row.

Many thanks.




Aucun commentaire:

Enregistrer un commentaire