dimanche 26 juillet 2015

How to extract a random sample with multiple conditions that vary by group?

I have a cross-national data set where each respondent has at least one diary. The number of diaries per respondent and diary completion day varies by country.

For example, in one country each respondent completed only 1 diary (half of the respondents completed only on a weekend, while the other half only on a weekday). In another country, each respondent completed 2 diaries (one weekend- one weekday), and in another one everyone completed 7 diaries (one for each day of the week). There are also surveys where some of the respondents returned 2 diaries, while others 3; and there are those where one everyone returned 4 diaries. The data look like this:

dat<-rbind(cbind(rep(1,8),11:18,111:118,1:0),cbind(rep(2,8),c(21,21,22,22,23,23,24,24),c(211,212,221,222,231,232,241,242),1:0),cbind(rep(3,7),c(rep(31,7),rep(32,7)),c(311:317,321:327),c(rep((c(1,1,rep(0,5))),2))),cbind(rep(4,10),c(41,41,41,42,42,42,43,43,44,44),c(411:413,421:423,431,432,441,442),1:0)  )
colnames(dat) <- c('country_id', 'diarist_id', 'diary_id','weekend')
dat

I am trying to draw a random sample of “one person-one diary” from each country. But at the country level I need -approximately- 29% of the diaries to be weekend diaries. How can I draw such a conditional random sample by group?




Aucun commentaire:

Enregistrer un commentaire