Hello,
Here is my question. My data has stratified structure. And I want to create some stratified random samples from my data.
Suppose my original data look like this: I have three groups of people. Group 1 and group 3 has two people in it and group 2 has three people in it. For each people I have three variables: X, Y, and Z.
Group ID X Y Z
1 11 3 8 9
1 12 4 10 16
2 21 1 5 6
2 22 2 7 7
2 23 5 6 12
3 31 8 6 7
3 32 9 4 3
I want to select a random sample of groups with replacement, say 1,1,2, or 1, 3, 3. Then keep all observations within that group. So the sample I create should look this way (1, 1, 2):
Group ID X Y Z NewGroup
1 11 3 8 9 1
1 12 4 10 16 1
1 11 3 8 9 2
1 12 4 10 16 2
2 21 1 5 6 3
2 22 2 7 7 3
2 23 5 6 12 3
Note that I need a variable (i.e., NewGroup) indicating that the first and second two lines belong to different units.
Or (1, 3, 3):
Group ID X Y Z NewGroup
1 11 3 8 9 1
1 12 4 10 16 1
3 31 8 6 7 2
3 32 9 4 3 2
3 31 8 6 7 3
3 32 9 4 3 3
I am wondering how to get this type of random samples.
My second question is similar but slightly more complicated. In the first step I get a random sample of groups with replacement, say (1, 1, 2). In the second step, I randomly select people from the every group I get with replacement, say ((11, 12), (12, 12), (21, 22, 22)), or ((11, 11), (11, 12), (21, 21, 23)). And my output data should look like this ((11, 12), (12, 12), (21, 22, 22)):
Group ID X Y Z NewGroup
1 11 3 8 9 1
1 12 4 10 16 1
1 12 4 10 16 2
1 12 4 10 16 2
2 21 1 5 6 3
2 22 2 7 7 3
2 22 2 7 7 3
Or ((11, 11), (11, 12), (21, 21, 23)):
Group ID X Y Z NewGroup
1 11 3 8 9 1
1 11 3 8 9 1
1 11 3 8 9 2
1 12 4 10 16 2
2 21 1 5 6 3
2 21 1 5 6 3
2 23 5 6 12 3
How can I achieve that?
Thank you very much for your help!