lundi 10 octobre 2016

andom sampling over values of a specified variable in STATA

Hi: I have a dataset of 1.3 million observations of partnerships between two sets of organizations, i and j, where the variable tie==1 indicates i and j have a partnership and tie==0 indicates no partnership. For most i-j observations, tie==0 because most organizations do not partner with each other. For analytical purposes, I need to keep all the i-j observations of tie==1 and sample a subset of the i-j observations of tie==0 . Here is an illustration of the data structure, illustrated using dataex but modified to highlight the structure of the observations while minimizing the number of rows. To conceptualize the full dataset imagine that each block/group (separated with an empty row for illustrative purposes) has 1095 additional rows of id_school id_partner combinations and three additional time periods (i.e. six time periods total).

input long id_school int id_partner byte(period tie)

171445 952 1 0

171445 404 1 1

171445 1203 1 0

171445 797 1 0

171445 361 1 0

171445 952 2 0

171445 404 2 1

171445 1203 2 1

171445 797 2 0

171445 361 2 0

171490 952 1 1

171490 404 1 1

171490 1203 1 1

171490 797 1 0

171490 361 1 0

What I need is to randomly sample a subset of the tie==0 observations for each group, where group is defined by id_school period. So, let’s say I wanted a subset of 2 (n=2) tie == 0 observations. The code I would use is "bysort id_school period: sample 2 if tie! == 1, count". The result for the data is below. Note that there are two tie=0 observations in every block/group.

input long id_school int id_partner byte(period tie)

171445 952 1 0

171445 404 1 1

171445 1203 1 0

171445 952 2 0

171445 404 2 1

171445 1203 2 1

171445 797 2 0

171490 952 1 1

171490 404 1 1

171490 1203 1 1

171490 797 1 0

171490 361 1 0

BUT I am trying to accomplish something slightly different. What I need is to sample n (i.e. n=2) tie==0 observations for every tie == 1 observation. An example of the desired data is below (note: for the example, I used the imaginary 1095 rows to illustrate the full, which means there are more observations below than in the initial illustration above). Notice how in block/group 1 there are two tie==0 observations because there is one tie==1 observation but in block/group 2 there are four tie==0 observations because there are two tie==1 observations. Likewise in block/group 3 there are six tie==0 observations.

input long id_school int id_partner byte(period tie)

171445 952 1 0

171445 404 1 1

171445 1203 1 0

171445 952 2 0

171445 404 2 1

171445 1203 2 1

171445 797 2 0

171445 361 2 0

171445 123 2 0

171490 952 1 1

171490 404 1 1

171490 1203 1 1

171490 797 1 0

171490 361 1 0

171490 123 1 0

171490 223 1 0

171490 323 1 0

171490 423 1 0

It seems like the most straightforward way to accomplish the above is to use the conventional code (bysort id_school period: sample 2 if tie! == 1, count) but then create some kind of loop so that the code would be executed for each group for whatever the total tie count is for that group (i.e. once for block/group 1, twice for block/group 2 and three times for block/group 3). However, I can’t figure out how to correctly construct the loop.

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire