Get exchange partners for fast_anticlustering()

generate_exchange_partners(
  n_exchange_partners,
  N = NULL,
  features = NULL,
  method = "random",
  categories = NULL
)

Arguments

n_exchange_partners

The number of exchange partners per element

N

The number of elements for which exchange partners; can be NULL if features is passed (it is ignored if features is passed).

features

The features for which nearest neighbours are sought if method = "RANN". May be NULL if random exchange partners are generated.

method

Currently supports "random" (default), "RANN" and "restricted_random". See details.

categories

A vector, data.frame or matrix representing one or several categorical constraints.

Value

A list of length N. Is usually used as input to the argument exchange_partners in

fast_anticlustering. Then, the i'th element of the list contains the indices of the exchange partners that are used for the i'th element.

Details

The method = "RANN" generates exchange partners using a nearest neighbour search via nn2 from the RANN package; methode = "restricted_random" generates random exchange partners but ensures that for each element, no duplicates are generated and that the element itself does not occur as exchange partner (this is the slowest method, and I would not recommend it for large N); method = "random" (default) does not impose these restrictions and generates unrescricted random partners (it may therefore generate duplicates and the element itself as exchange partner).

When setting the categories argument and using method = "RANN", exchange partners (i.e., nearest neighbours) will be generated from the same category; methode = "restricted_random" will also adhere to categorical constraints induced via categories (i.e. each element only receives exchange partners from the same category as itself); methode = "random" cannot incoorporate categorical restrictions.

Examples


# Restricted random method generates no duplicates per element and cannot return 
# the element itself as exchange partner
generate_exchange_partners(5, N = 10, method = "restricted_random")
#> [[1]]
#> [1] 8 4 3 6 7
#> 
#> [[2]]
#> [1] 3 4 5 9 8
#> 
#> [[3]]
#> [1] 4 6 7 8 9
#> 
#> [[4]]
#> [1]  6  8 10  5  2
#> 
#> [[5]]
#> [1] 3 6 2 4 1
#> 
#> [[6]]
#> [1]  9  7 10  5  4
#> 
#> [[7]]
#> [1] 8 9 5 4 6
#> 
#> [[8]]
#> [1] 10  1  9  7  5
#> 
#> [[9]]
#> [1]  3  1 10  7  5
#> 
#> [[10]]
#> [1] 2 7 9 8 3
#> 
# "random" simply randomizes with replacement and without restrictions
# (categorical restrictions are also not possible; is much faster for large data sets)
generate_exchange_partners(5, N = 10, method = "random")
#> [[1]]
#> [1] 4 6 6 4 6
#> 
#> [[2]]
#> [1] 3 1 4 5 3
#> 
#> [[3]]
#> [1] 8 4 1 6 6
#> 
#> [[4]]
#> [1]  2  2  4 10  7
#> 
#> [[5]]
#> [1] 8 9 9 6 6
#> 
#> [[6]]
#> [1] 8 2 4 8 2
#> 
#> [[7]]
#> [1]  4  1 10 10  6
#> 
#> [[8]]
#> [1]  9 10  1  9  5
#> 
#> [[9]]
#> [1] 7 6 2 3 1
#> 
#> [[10]]
#> [1] 4 7 3 7 7
#> 
# May return less than 5 exchange partners if there are not enough members 
# of the same category: 
generate_exchange_partners(
  5, N = 10, 
  method = "restricted_random", 
  categories = cbind(schaper2019$room, schaper2019$frequency)
)
#> [[1]]
#> [1] 12 37 41  6 71
#> 
#> [[2]]
#> [1] 67 69 72 65 70
#> 
#> [[3]]
#> [1] 46  4 34 14  8
#> 
#> [[4]]
#> [1]  3 14  8 44 34
#> 
#> [[5]]
#> integer(0)
#> 
#> [[6]]
#> [1] 41 12 71  1 66
#> 
#> [[7]]
#> [1] 35 80 73 11 76
#> 
#> [[8]]
#> [1] 14 34  4 13 46
#> 
#> [[9]]
#> integer(0)
#> 
#> [[10]]
#> [1] 39 45 75
#> 
#> [[11]]
#> [1] 73 80 48 15 42
#> 
#> [[12]]
#> [1]  6 41 66 37  1
#> 
#> [[13]]
#> [1]  4 14 46  3 34
#> 
#> [[14]]
#> [1] 44 34 13  3  8
#> 
#> [[15]]
#> [1] 38  7 73 48 47
#> 
#> [[16]]
#> [1] 79 40 33
#> 
#> [[17]]
#> [1] 85 28 96 63 18
#> 
#> [[18]]
#> [1] 63 96 28 30 17
#> 
#> [[19]]
#> [1] 61 60 84 54
#> 
#> [[20]]
#> [1] 22 89 53 52
#> 
#> [[21]]
#> [1] 57 90 23
#> 
#> [[22]]
#> [1] 20 52 89 53
#> 
#> [[23]]
#> [1] 21 57 90
#> 
#> [[24]]
#> integer(0)
#> 
#> [[25]]
#> [1] 27 29 59 81 82
#> 
#> [[26]]
#> [1] 92 87 83 51 55
#> 
#> [[27]]
#> [1] 82 59 25 58 81
#> 
#> [[28]]
#> [1] 18 17 85 88 30
#> 
#> [[29]]
#> [1] 25 81 59 82 58
#> 
#> [[30]]
#> [1] 28 85 88 63 18
#> 
#> [[31]]
#> [1] 49 87 51 26 83
#> 
#> [[32]]
#> [1] 86
#> 
#> [[33]]
#> [1] 16 79 40
#> 
#> [[34]]
#> [1] 46 14  3  8 13
#> 
#> [[35]]
#> [1] 15 38 73  7 47
#> 
#> [[36]]
#> [1] 67 72 77 65 70
#> 
#> [[37]]
#> [1] 41 71  1 12  6
#> 
#> [[38]]
#> [1] 73  7 80 76 48
#> 
#> [[39]]
#> [1] 75 45 10
#> 
#> [[40]]
#> [1] 16 33 79
#> 
#> [[41]]
#> [1]  1 71 37 66  6
#> 
#> [[42]]
#> [1] 35 11 73 80 76
#> 
#> [[43]]
#> [1] 68 74
#> 
#> [[44]]
#> [1]  4  3  8 46 14
#> 
#> [[45]]
#> [1] 10 39 75
#> 
#> [[46]]
#> [1] 14  8 44  3 34
#> 
#> [[47]]
#> [1] 11 76 38  7 42
#> 
#> [[48]]
#> [1] 42 73 47 11 76
#> 
#> [[49]]
#> [1] 51 92 83 31 87
#> 
#> [[50]]
#> [1] 94 95 91
#> 
#> [[51]]
#> [1] 55 62 92 26 83
#> 
#> [[52]]
#> [1] 89 22 53 20
#> 
#> [[53]]
#> [1] 22 52 89 20
#> 
#> [[54]]
#> [1] 60 84 61 19
#> 
#> [[55]]
#> [1] 51 31 92 83 87
#> 
#> [[56]]
#> integer(0)
#> 
#> [[57]]
#> [1] 23 90 21
#> 
#> [[58]]
#> [1] 82 25 29 27 81
#> 
#> [[59]]
#> [1] 29 58 25 81 82
#> 
#> [[60]]
#> [1] 61 19 54 84
#> 
#> [[61]]
#> [1] 60 54 19 84
#> 
#> [[62]]
#> [1] 49 51 87 55 31
#> 
#> [[63]]
#> [1] 88 30 96 17 18
#> 
#> [[64]]
#> [1] 93
#> 
#> [[65]]
#> [1] 36 77  2 67 70
#> 
#> [[66]]
#> [1] 41  6 12 37  1
#> 
#> [[67]]
#> [1]  2 72 69 77 36
#> 
#> [[68]]
#> [1] 74 43
#> 
#> [[69]]
#> [1] 77 65  2 72 36
#> 
#> [[70]]
#> [1] 65 77 36  2 72
#> 
#> [[71]]
#> [1]  6  1 66 37 12
#> 
#> [[72]]
#> [1] 36 67  2 77 70
#> 
#> [[73]]
#> [1] 47 35 38  7 15
#> 
#> [[74]]
#> [1] 43 68
#> 
#> [[75]]
#> [1] 45 39 10
#> 
#> [[76]]
#> [1] 15 11 35 47 73
#> 
#> [[77]]
#> [1]  2 70 65 72 36
#> 
#> [[78]]
#> integer(0)
#> 
#> [[79]]
#> [1] 40 16 33
#> 
#> [[80]]
#> [1] 48 76 11 47 73
#> 
#> [[81]]
#> [1] 25 29 82 58 59
#> 
#> [[82]]
#> [1] 27 81 29 58 25
#> 
#> [[83]]
#> [1] 62 55 49 87 26
#> 
#> [[84]]
#> [1] 54 19 61 60
#> 
#> [[85]]
#> [1] 28 17 18 63 96
#> 
#> [[86]]
#> [1] 32
#> 
#> [[87]]
#> [1] 92 55 49 83 26
#> 
#> [[88]]
#> [1] 18 30 63 17 85
#> 
#> [[89]]
#> [1] 53 20 52 22
#> 
#> [[90]]
#> [1] 23 21 57
#> 
#> [[91]]
#> [1] 94 50 95
#> 
#> [[92]]
#> [1] 87 49 55 31 26
#> 
#> [[93]]
#> [1] 64
#> 
#> [[94]]
#> [1] 95 50 91
#> 
#> [[95]]
#> [1] 94 91 50
#> 
#> [[96]]
#> [1] 30 18 17 88 63
#> 
# using nearest neighbour search (unlike RANN::nn2, this does not 
# return the ID of the element itself as neighbour)
generate_exchange_partners(5, features = schaper2019[, 3:5], method = "RANN")[1:3]
#> [[1]]
#> [1] 65 74 43 83 21
#> 
#> [[2]]
#> [1] 18 10 90 82 42
#> 
#> [[3]]
#> [1] 11 35  4 92 93
#> 
# compare with RANN directly:
RANN::nn2(schaper2019[, 3:5], k = 6)$nn.idx[1:3, ] # note k = 6
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1   65   74   43   83   21
#> [2,]    2   18   10   90   82   42
#> [3,]    3   11   35    4   92   93