'Randomly sample by group based on numeric variables

Is it possible to randomly sample patients by group so that they have similar distributions based on other variables? To me, this sounds like a matching problem, but there's no "treatment" here, so I'm not sure if the concept applies.

Sample data:

structure(list(id = c(8350L, 22543L, 24144L, 9392L, 27648L, 2943L, 
34686L, 27153L, 11143L, 15209L, 11952L, 22669L, 8211L, 27765L, 
28671L, 9693L, 30274L, 25807L, 14839L, 22400L, 24494L, 6540L, 
6861L, 31825L, 34190L, 19606L, 21077L, 5037L, 25943L, 20530L, 
23730L, 34774L, 7210L, 2051L, 28410L, 18318L, 34848L, 26596L, 
8973L, 24885L, 9652L, 8387L, 16168L, 36893L, 24048L, 17769L, 
1273L, 22734L, 36796L, 25497L, 28300L, 166L, 21172L, 20026L, 
16265L, 1699L, 33140L, 23997L, 10216L, 27408L, 6813L, 10196L, 
15015L, 2748L, 34979L, 21763L, 27438L, 6255L, 17047L, 30593L, 
30723L, 7914L, 218L, 20134L, 29952L, 27126L, 3795L, 1367L, 33585L, 
5940L, 26250L, 22519L, 35611L, 26168L, 26848L, 21276L, 8971L, 
22554L, 16655L, 5315L, 18121L, 32526L, 21513L, 9262L, 36882L, 
7408L, 18873L, 17238L, 15216L, 23667L, 30138L, 2978L, 25451L, 
2492L, 30983L, 7677L, 22880L, 29674L, 7093L, 24910L, 20839L, 
18176L, 23031L, 17197L, 4613L, 35801L, 30822L, 3889L, 11752L, 
11314L, 22317L, 12825L, 17433L, 4407L, 3986L, 10173L, 32409L, 
2697L, 3410L, 26834L, 3203L, 5474L, 34678L, 35336L, 19462L, 15835L, 
7888L, 27897L, 9245L, 16524L, 13316L, 21604L, 30458L, 9191L, 
1220L, 1779L, 1724L, 26382L, 11566L, 21310L, 12600L, 25063L, 
30912L, 31189L, 9480L, 16804L, 2372L, 26238L, 20113L, 33753L, 
32711L, 11543L, 10578L, 4475L, 13187L, 23395L, 35342L, 6903L, 
26905L, 12026L, 5697L, 15352L, 33985L, 1132L, 15806L, 13611L, 
29930L, 15896L, 6057L, 10849L, 12944L, 25561L, 3328L, 27481L, 
28790L, 3260L, 24986L, 22177L, 26580L, 11639L, 2256L, 4839L, 
22805L, 616L, 6702L, 18360L, 4439L, 1300L, 33779L, 24940L, 10043L, 
21268L, 35127L, 36621L, 17618L, 6688L, 15937L, 31057L, 2144L, 
30866L, 12500L, 29753L, 36497L, 21247L, 9481L, 36465L, 20665L, 
15017L, 21234L, 34258L, 576L, 31187L, 4528L, 15314L, 3657L, 24489L, 
33871L, 106L, 24916L, 2524L, 17469L, 2799L, 13311L, 26585L, 7131L, 
21401L, 6191L, 22338L, 11647L, 11681L, 22744L, 14000L, 5356L, 
2892L, 24481L, 24116L, 21461L, 13992L, 22751L, 11129L, 8802L, 
29963L, 4660L, 29020L, 20843L, 21796L, 3607L, 10692L, 29168L, 
25034L, 3307L, 35010L, 20280L, 31894L, 7276L, 24259L, 34059L, 
35867L, 11165L, 16010L, 34082L, 26586L, 30958L, 25030L, 34851L, 
29185L, 25721L, 8968L, 29427L, 20213L, 34667L, 28721L, 21472L, 
17132L, 35247L, 9798L, 36826L, 21226L, 28335L, 16077L, 2654L, 
20466L, 21324L, 36969L, 22553L, 5895L, 16514L, 10644L, 4376L, 
13592L, 11206L, 32440L, 13413L, 31416L, 22540L, 15986L, 11506L, 
16928L, 18652L, 17858L, 13522L, 8566L, 10665L, 29442L, 28219L, 
22549L, 2209L, 8017L, 6066L, 21718L, 21930L, 11540L, 4100L, 35236L, 
240L, 24900L, 425L, 26880L, 21409L, 18885L, 5803L, 33335L, 25597L, 
12547L, 8930L, 4328L, 17360L, 4696L, 25198L, 26469L, 14679L, 
1691L, 32989L, 6099L, 14427L, 31797L, 23408L, 29296L, 23928L, 
31889L, 31737L, 6420L, 11304L, 34798L, 20785L, 9806L, 35018L, 
35008L, 1450L, 3246L, 15123L, 19603L, 8519L, 32012L, 3397L, 11682L, 
27102L, 18022L, 20408L, 15836L, 18284L, 12897L, 29580L, 14510L, 
23925L, 28821L, 35825L, 14922L, 36643L, 10948L, 4220L, 23791L, 
65L, 35772L, 1423L, 29386L, 755L, 23627L, 27201L, 12353L, 3578L, 
1914L, 35373L, 16702L, 13057L, 3021L, 27531L, 1990L, 205L, 21559L, 
29081L, 26301L, 18894L, 3088L, 9782L, 10522L, 12570L, 8948L, 
36240L, 33943L, 33022L, 2750L, 32649L, 30134L, 13920L, 11498L, 
8314L, 16849L, 15559L, 22529L, 31406L, 5680L, 17908L, 14931L, 
2122L, 2581L, 33546L, 12143L, 17220L, 16713L, 7454L, 13659L, 
15973L, 20116L, 27689L, 35285L, 36106L, 21834L, 29850L, 29030L, 
7957L, 31698L, 12307L, 23642L, 5615L, 12016L, 1161L, 15291L, 
32738L, 1089L, 32988L, 33382L, 3642L, 18661L, 35584L, 8009L, 
24000L, 30587L, 25870L, 19944L, 34970L, 29983L, 24774L, 28702L, 
21199L, 17292L, 29831L, 476L, 18881L, 29923L, 31476L, 4570L, 
31081L, 10544L, 3373L, 13435L, 22651L, 17861L, 3818L, 35387L, 
11459L, 35637L, 308L, 35697L, 12696L, 15175L, 7990L, 16691L, 
19494L, 9008L, 30695L, 28889L, 446L, 22178L, 13000L, 26166L, 
15431L, 19332L, 35991L, 2840L), race_f = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 4L, 2L, 3L, 4L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 
3L, 1L, 3L, 3L, 1L, 1L, 3L, 2L, 2L, 1L, 4L, 5L, 1L, 4L, 1L, 1L, 
5L, 1L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 3L, 2L, 1L, 1L, 
2L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 
1L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 3L, 4L, 4L, 1L, 1L, 3L, 1L, 2L, 
3L, 4L, 1L, 1L, 1L, 3L, 1L, 1L, 5L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 
3L, 1L, 4L, 1L,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
1L, 4L, 4L, 1L, 3L, 4L, 1L, 4L, 1L, 1L, 1L, 3L, 2L, 1L, 2L, 4L, 
1L, 1L, 5L, 4L, 1L, 1L, 4L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 
1L, 3L, 3L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 
1L, 3L, 4L, 1L, 1L, 2L, 5L, 3L, 3L, 1L, 1L, 4L, 1L, 4L, 1L, 4L, 
2L, 3L, 3L, 1L, 1L, 1L, 4L, 1L, 4L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 
3L, 1L, 1L, 1L, 1L, 4L, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 1L, 1L, 1L, 
3L, 5L, 2L, 5L, 1L, 1L, 4L, 3L, 1L, 3L, 2L, 1L, 1L, 5L, 1L, 3L, 
3L, 4L, 1L, 1L, 1L, 2L, 5L, 1L, 1L, 4L, 3L, 1L, 1L, 1L, 2L, 1L, 
1L, 3L, 3L, 1L, 1L, 4L, 3L, 2L, 4L, 4L, 1L, 1L, 2L, 3L, 1L, 3L, 
3L, 1L), .Label = c("White", "Black", "Hispanic", "Asian", "Other"
), class = "factor"), cops2_avg_12mo = c(82.9166666666667, 66, 
23.3333333333333, 28, 9.33333333333333, 69.9166666666667, 6, 
33.3333333333333, 0, 12, 102, NA, 66, 6, 45, 58.5, 10, 55.9166666666667, 
19.5, 6, 10, 234.666666666667, 28, 23, 51.5833333333333, 10, 
38, 123.5, 0, 24, 10, 0, 73, 10, 25, 6, 20, 13.4166666666667, 
13.8333333333333, 8, 14.8333333333333, 53.5, 42, NA, 57.1666666666667, 
0, 24.6666666666667, 10, NA, 54.6666666666667, 38.75, 41, 22, 
0.833333333333333, 13, 113.083333333333, 27.3333333333333, 9, 
33.1666666666667, 18.75, 57.75, 30, 60.3333333333333, 23.1666666666667, 
37, 16.5, 0, 145.5, 45, 31.3333333333333, 0, 10, 187.5, 27.4166666666667, 
10, 54.9166666666667, 78.8333333333333, 103.75, 6.66666666666667, 
30.4166666666667, 10, 10, 24.6666666666667, 10, 118.333333333333, 
61.25, 17, 10, 28, 51, 6, 32.0833333333333, 80.75, 8.83333333333333, 
NA, 10, 74.25, 42.25, 47, 60, 41.6666666666667, 19.0833333333333, 
98.5, 73.5, 10, 6.66666666666667, 49.8333333333333, 10, 79.8333333333333, 
10, 42, 95.8333333333333, 130.583333333333, 5.41666666666667, 
47.25, 6, 8, 17.8333333333333, 10, 73.9166666666667, 10, 8, 27.8333333333333, 
125.916666666667, 134.166666666667, 88, 10, 58, 62.5, 10.3333333333333, 
28.8333333333333, 100.083333333333, 35.5, 0, 0, 10, 105, 7.33333333333333, 
35, 9.66666666666667, 10, 4.16666666666667, 10, 8.33333333333333, 
70.6666666666667, 28.4166666666667, 38.1666666666667, 8, 101.5, 
26.75, 61.1666666666667, 14, 95.5833333333333, 35, 65, 0, 51.75, 
57.5, 10, 13.6666666666667, 10, 67.5, 10, 62.3333333333333, 72.6666666666667, 
10, 45.5, 20.8333333333333, 31, 84.5, 10, 98.1666666666667, 47.5, 
56, 126, 14, 10, 10, 8, 36, 111.5, 54.5, 45.5, 8, 37.5, 84.8333333333333, 
39.1666666666667, 56.25, 37.9166666666667, 37.75, 27, 55.6666666666667, 
10, 34, 5.83333333333333, 37, 80.0833333333333, 57, 102.166666666667, 
12.6666666666667, 10, 19.3333333333333, 10, NA, 51, 25.9166666666667, 
14, 36.9090909090909, 38.6666666666667, 0, 6.33333333333333, 
NA, 31, 43, 26.5, 10, 34.4166666666667, 77.1666666666667, 10, 
10, 89.9166666666667, 59, 37, 77.3333333333333, 64, 52, 19.6666666666667, 
66.5, 24, 106.083333333333, 29.6666666666667, 38.1666666666667, 
6.66666666666667, 10, 16.75, NA, 86.75, 1, 14, 20.3333333333333, 
8, 21, 38.9166666666667, 50.8333333333333, 57.5, 29, 0, 26.5, 
51.9166666666667, 71.25, 42.6666666666667, 82, 58.0833333333333, 
11.3333333333333, 82, 9.5, 78.6666666666667, 102.5, 71, 10, 70.6666666666667, 
NA, 33.8333333333333, 61.25, 87, 36.5, 10, 40.4166666666667, 
51.8333333333333, 23, 9.66666666666667, 44.5, 8, 10, 4.16666666666667, 
0, 48.8333333333333, 49.25, 15, 70, 10, 6, 10, 34.8333333333333, 
108.75, 36, NA, 31, 51, 69.5, 122.5, 48, 43.5833333333333, NA, 
10, 20, 80.75, 54.75, 106.916666666667, 53.5, 90.6666666666667, 
8.33333333333333, 85.5, 40.5833333333333, 5.5, 10, 61.3333333333333, 
69.8333333333333, 10, 51, 0, 49.0833333333333, 13.6666666666667, 
13.3333333333333, 5.83333333333333, 33.8333333333333, 14.4166666666667, 
11.25, 14, 6, 14.5833333333333, 36, 21, 10, 29.5833333333333, 
13, 34, 10, 2.5, 10, 211.916666666667, 19.75, 7.33333333333333, 
6, 59.6666666666667, 30.25, 34.25, 16.1666666666667, 10, NA, 
NA, 97, 75, 26.5, 8, 32.25, 0, 39, 37, 165.333333333333, 45, 
33.1666666666667, 21, 10, 57, 70.3333333333333, 10, 10, 62, 79.1666666666667, 
38, 26.1666666666667, 13, 8, 69.6666666666667, 40.5, 100, 0.833333333333333, 
8, 82.5, 10, 19.8333333333333, 20.0833333333333, 8, 25.8333333333333, 
16.75, 10, 36, NA, 12.8333333333333, 31.4166666666667, 10, 61.4166666666667, 
14, 67.5, 3, 83.1666666666667, 48, 43.75, 35.4166666666667, 73, 
44.1666666666667, 8, 29.75, 10, 10, 62.6666666666667, 26.9166666666667, 
29.6666666666667, 10, NA, 15, 19.4166666666667, 112, 29, 3, 33.5, 
62.5, 10, 84.6666666666667, 8, 84.4166666666667, 81.5, 56.1666666666667, 
10, 101.416666666667, 16, 10, 19.6666666666667, 60, 73.6666666666667, 
74.9166666666667, 21, 5, 15.0833333333333, 17.0833333333333, 
17.5, 46, 61.8333333333333, 115.333333333333, 92, 30, 0, 22.75, 
16.6666666666667, 15, 15, 10, NA, 56.25, 54, 10, 40, 9.83333333333333, 
10.9166666666667, 22.25, 84.75, 80, 1.66666666666667, 99.8333333333333, 
10, 38.6666666666667, 169.75, 35.0833333333333, 8, 78.5, 6.33333333333333, 
21, 10, 42, 105.166666666667, 162.416666666667, 14, 69.25, 35.8333333333333, 
13, 5.83333333333333, 34, 51, 12.75, 44.3333333333333, 39.5, 
10, 23, 46.8333333333333, 89.9166666666667, 15, 28, 128.416666666667, 
10, 91.6666666666667, 3.5, 54, 23, NA, 29.75, 37.1666666666667, 
12.6666666666667, 31.9166666666667, 23, 0, 11, 67.9166666666667, 
3.16666666666667, 8.33333333333333, 51, NA, 10, 0, 58.8333333333333
), AGE = c(86, 82, 83, 92, 45, 81, 52, 64, 71, 96, 79, 64, 76, 
37, 81, 79, 72, 79, 74, 46, 45, 71, 89, 76, 53, 48, 52, 77, 63, 
52, 57, 62, 84, 88, 55, 69, 67, 63, 67, 51, 86, 53, 65, 59, 71, 
60, 70, 20, 78, 62, 58, 73, 68, 71, 66, 72, 71, 65, 95, 67, 79, 
70, 86, 77, 81, 54, 44, 66, 80, 71, 30, 77, 67, 75, 48, 65, 83, 
85, 70, 70, 74, 58, 81, 28, 78, 66, 79, 47, 74, 41, 74, 58, 73, 
55, 53, 56, 84, 74, 62, 85, 68, 47, 78, 72, 57, 56, 64, 55, 86, 
76, 77, 58, 74, 55, 71, 61, 74, 62, 65, 75, 81, 68, 39, 58, 65, 
76, 27, 79, 86, 61, 87, 52, 72, 58, 53, 69, 78, 65, 81, 69, 66, 
68, 61, 72, 74, 80, 88, 46, 53, 77, 89, 83, 41, 67, 83, 62, 90, 
70, 60, 62, 33, 78, 80, 62, 81, 37, 55, 90, 81, 73, 67, 97, 32, 
71, 70, 69, 46, 57, 60, 79, 79, 56, 75, 60, 52, 78, 61, 51, 70, 
67, 71, 36, 53, 70, 53, 74, 89, 78, 70, 56, 58, 83, 50, 77, 70, 
50, 75, 53, 86, 65, 45, 63, 62, 78, 65, 69, 75, 79, 71, 56, 88, 
63, 72, 85, 68, 72, 45, 81, 46, 70, 84, 71, 82, 63, 57, 77, 70, 
42, 87, 84, 61, 64, 79, 53, 65, 64, 69, 68, 71, 89, 49, 70, 82, 
63, 79, 65, 64, 54, 73, 36, 80, 38, 68, 62, 84, 80, 65, 73, 91, 
59, 35, 80, 67, 68, 65, 47, 60, 67, 72, 81, 22, 35, 58, 57, 68, 
94, 38, 77, 75, 73, 78, 71, 78, 53, 58, 61, 77, 44, 95, 53, 72, 
68, 72, 73, 78, 41, 75, 80, 60, 53, 68, 79, 80, 74, 25, 79, 55, 
68, 85, 64, 72, 78, 78, 71, 73, 82, 73, 73, 58, 69, 58, 72, 78, 
56, 74, 67, 66, 72, 38, 58, 62, 77, 81, 37, 46, 88, 55, 76, 50, 
57, 72, 39, 56, 29, 76, 77, 36, 31, 70, 70, 70, 54, 74, 47, 81, 
46, 81, 55, 53, 70, 28, 71, 79, 68, 78, 81, 30, 83, 43, 70, 79, 
47, 94, 60, 64, 82, 81, 92, 57, 90, 86, 58, 61, 69, 50, 64, 79, 
56, 76, 52, 55, 53, 85, 89, 64, 86, 58, 82, 64, 74, 45, 64, 71, 
75, 61, 79, 82, 63, 81, 60, 70, 79, 63, 59, 80, 53, 80, 41, 83, 
67, 90, 60, 82, 74, 75, 52, 62, 35, 53, 49, 71, 69, 73, 67, 44, 
77, 81, 96, 52, 75, 30, 83, 74, 56, 62, 78, 63, 63, 62, 71, 62, 
89, 83, 77, 66, 64, 24, 96, 63, 51, 65, 71, 50, 68, 83, 82, 90, 
91, 84, 90, 76, 62, 79, 20, 75, 79, 80, 62, 62, 71, 51, 81, 84, 
65, 65, 55, 65, 51, 26, 70)), row.names = c(NA, -500L), class = c("tbl_df", 
"tbl", "data.frame"))

I'm hoping to sample by race_f so that the different race groups are similar in AGE and cops2_avg_12mo. Is this at all possible? Thank you!

r


Solution 1:[1]

The answer depends on if you want to ensure that their ages/cops2_avg_12mo will always be within a specific range - in which case you would simply create a subset of your data frame with only the patients whose age and cops2_avg_12mo are within some range. I do think that this is the safer thing to do in terms of quality control. You can view a plot of the two columns of your data (AGE and cops2_avg_12mo) to get an idea of what ranges of values most of the patients fall into:

plot(x[,c("AGE", "cops2_avg_12mo")])

Pick ranges for these values that contain enough patients to sample from. (I don't know how many samples you need). Basically, draw a box in the dot plot which contains enough patients to sample from.

plot of age versus cops2_avg_12mo

So once you determine the ranges/boundaries of the box, just create indexes like so:

idx = (x[,"AGE"] > 50) & (x[,"AGE"] < 75) & (x[,"cops2_avg_12mo"] > 0) & (x[,"cops2_avg_12mo"] < 75) & !is.na(x[,"cops2_avg_12mo"])

then get the subset of your data:

subsetX = x[idx,]

After you create that subset, you can randomly sample using R's sample() function. If you want to do sampling from each race equally, then call sample() with the subsetX data, with each race selected at a time, to get n samples at a time:

sample(subsetX[subsetX[,"race_f"]=="Asian",], n, replace=FALSE)

Alternatively, if you are ok with sampling patients that have outlier values (but I feel like this will produce more variation in your results), then you can create a histogram of each of the columns - for example, AGE - then get the histogram bin counts, divide them by the total number of patients to get a probability distribution, then create a vector the same length as the number of patients where each value is the probability we calculated for the bin it belongs to (found by getting bin indexes when calculating the histogram), then pass that vector into the sample() function as the prob input argument so that values are sampled with their specified probability.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 benson23