-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustermax.ado
891 lines (818 loc) · 31.5 KB
/
clustermax.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
*********************
* clustermax.ado
* version 9
* Francisco Barba
* October 8, 2018
*********************
cap program drop clustermax
program clustermax, rclass
syntax varlist , seed(integer) GENerate(name) n(integer) [Within(real 0)] Between(real)
qui {
qui {
*************************************************************************************************************************
* PART 1: Find all combinations of points with MUTUAL maximum distance of `within'
*************************************************************************************************************************
cap gen `generate'
if _rc==110 {
di as err "variable `generate' already defined"
exit 110
}
if _rc==0 {
cap drop `generate'
}
set seed `seed'
tempvar sorttemp
gen double `sorttemp' = runiform()
sort `sorttemp'
tempvar id
qui gen `id'=_n
tempvar id_me
qui gen `id_me'=`id'
qui tempfile original0
qui save `original0'
local clustervarname `generate'
token `varlist'
local latitude `1'
local longitude `2'
* Define errors
if `n'>1 & "`within'"=="" {
di as errr "Error: must specift within if n>1"
continue, break
}
if `within'>`between'{
di as err "Error: between() must be greater or equal witin_dist()."
continue, break
}
if (`between'<=0) { //|
di as err "Error: between() must be positive."
continue, break
}
if `within'<0 {
di as err "Error: within() must be greater/equal zero."
continue, break
}
if `within'==0 & `n'>1 {
di as err "Error: within=0 only allowed for n=1."
continue, break
}
keep `id' `latitude' `longitude'
tempfile original
save `original', replace
tempvar dup3
qui duplicates tag `latitude' `longitude', gen(`dup3')
qui fsum `dup3'
if `r(max)'!=0 {
di as err "Error: duplicate lat/lon coordinates found. lat/lon must be unique."
exit
}
cap drop `dup3'
tempvar dup
qui duplicates tag `latitude' , gen(`dup')
} // ends qui
qui fsum `dup'
if `r(max)'!=0 {
di as text "Warning: duplicate lat coordinate found."
}
cap drop `dup'
tempvar dup2
qui duplicates tag `longitude' , gen(`dup2')
qui fsum `dup2'
if `r(max)'!=0 {
di as text "Warning: duplicate lon coordinate found."
}
cap qui drop `dup2'
qui count if `latitude'==.
if `r(N)'!=0 {
di as "Warning: missing lat coordinate found."
}
qui count if `longitude'==.
if `r(N)'!=0 {
di as "Warning: missing lon coordinate found."
}
qui {
tempvar latitude0
tempvar longitude0
tempvar id0
* Skip the loop that identifies neighbors if `within'=0 (allowed when n=1)
if `within'!=0 {
rename `latitude' `latitude0'
rename `longitude' `longitude0'
rename `id' `id0'
cross using `original'
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude', gen(`d')
* Get neighbors (for later)
tempvar x2
bys `id0': gen `x2'=`d'<=`within' if `d'!=0
tempvar points_close2
bys `id0': egen `points_close2'=sum(`x2')
* save number of points in potential conflict area
tempvar y
bys `id0': gen `y' =`d'<=`between' if `d'!=0 // &d>`within'
tempvar points_within_ring
bys `id0': egen `points_within_ring'=sum(`y')
drop `x2'
preserve
keep `id0' `points_close2' `points_within_ring'
duplicates drop `id0', force
tempfile temp_points_close
save `temp_points_close'
restore
drop `points_close' `points_within_ring'
* at least `n'-1 points in inner cluster
tempvar x
bys `id0': gen `x'=`d'<=`within' if `d'!=0
tempvar points_close
bys `id0': egen `points_close'=sum(`x')
drop `d' `x'
* This while-loop keeps dropping observations until every observation has a minimum number of `n' neighbor points.
//It needs to be done iteratively:
// example: p1 is (only) close to p2 and p3, but p2 is not close to p3
// hence, after dropping p2 or p3 (which only have 1 as a neighbor) p1 is only left with one neighbor.
// hence, the dropping of iterations has to be iterated again, until EACH point has at least `n' neighbors
fsum `points_close'
if `r(max)'!=0 {
local min_neigbor_points=-1
while `min_neigbor_points'<`n'-1 {
keep if `points_close'>=`n'-1
count
if `r(N)' == 0 {
di as err "Error: There don't exist `n' points with a max. pairwise distance of `within' in the current data."
exit 111
}
keep `id0' `latitude0' `longitude0'
qui duplicates drop `id0', force
tempfile temp1
qui save `temp1'
rename `latitude0' `latitude'
rename `longitude0' `longitude'
rename `id0' `id'
cross using `temp1'
cap drop d
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude', gen(`d')
tempvar x
bys `id0': gen `x'=`d'<=`within' if `d'!=0
tempvar points_close
bys `id0': egen `points_close'=sum(`x')
fsum `points_close'
local min_neigbor_points=`r(min)'
}
keep if `x'==1 | `x'==.
}
}
* Match potential points for clusters to each other (only if at least 2 points within cluster)
* seed point has at least `n'-1 neighbors
if `n' > 1 {
****************
* IMPORTANT SORT! Establishes reshape order
sort `id0' `id'
****************
keep `id0' `id'
tempvar nnn
bys `id0': gen `nnn'=_n
* This reshape gives the neighbor IDs in a row next to the source ID
reshape wide `id', i(`id0') j(`nnn')
local cols=`r(k)'-1
local c string(`id'1)
forval i= 2 / `cols' {
* This appending macro loop generates a string of original ID plus all neighbor IDs, separated by two blanks
local c `c' + " " + " " + string(`id'`i')
}
local c `" " " + " " + `c' + " " + " " "'
tempvar comb
gen `comb'= `c'
replace `comb'=subinstr(`comb', ".", "", .)
cap drop `nnn'
* Row counter
tempvar nnn
gen `nnn'=_n
* This loop starts the matching process of dyads, triads... if n is 2, 3... correspondingly
* Start with first row tuple
count
forval src_row=1/`r(N)' { // foreach seed (original point)
* get the source seed value in `src_row'
local src_seed=`id0' in `src_row'
* get neighbor combination string (including seed) of `src_row'
local src_comb_`src_seed'=`comb' in `src_row'
*find the column number of the seed within the string: (cond() value of the tuple)
local tuplelength: word count `src_comb_`src_seed''
forval j=1/`tuplelength' {
local column
local num`j': word `j' of `src_comb_`src_seed''
if `num`j''==`src_seed' {
local column`src_seed'=`j'
}
}
* run all tuples
cap tuples `src_comb_`src_seed'', display min(`n') max(`n') cond(`column`src_seed'')
if _rc==3300 {
di as err "Error: Mata out of memory. Too many neighbors to evaluate. Consider decreasing the within-cluster distance or n."
exit 3300
}
* Get the number of tuples of `src_row'
local count=" "
local num=1
while "`count'"!="" {
if "`tuple`num''"=="" {
local count=""
}
local src_tuple`num' `tuple`num''
local tuple`num'
local ++num
}
local num_tuples_`src_seed'=`num'-2
* Get neighbor elements of the each tuple of `src_seed'
forval src_tuple= 1/`num_tuples_`src_seed'' { // for all tuples of `src_seed'
local src_`src_seed'_`src_tuple'="`src_tuple`src_tuple''"
local nbrs_`src_seed'_`src_tuple'= subinstr(" `src_tuple`src_tuple'' ", " `src_seed' ", " ", .) // the n-1 neighbor seeds
local nbrseed=1
* Get the seed `nbrseed' of `src_tuple' in `src_seed'
local n_within_cluster_min1=`n'-1
forval nbrseed=1/`n_within_cluster_min1'{
local nbr_seed : word `nbrseed' of `nbrs_`src_seed'_`src_tuple''
* Get row of neighbor seed
cap drop `x'
tempvar x
qui gen `x'=`nnn' if `id0'==`nbr_seed'
qui fsum `x'
local nbr_row=`r(max)'
drop `x'
* get neighbor combination string (including seed) of `nbr_row'
local nbr_comb_`src_seed'_`src_tuple'_`nbr_seed' =`comb' in `nbr_row'
*find the column number of the seed within the string: (cond() value of the tuple)
local tuplelength: word count `nbr_comb_`src_seed'_`src_tuple'_`nbr_seed''
forval j=1/`tuplelength' {
local column
local num`j': word `j' of `nbr_comb_`src_seed'_`src_tuple'_`nbr_seed''
if `num`j''==`nbr_seed' {
local column`nbr_seed'=`j'
}
}
* Tuples of neighbor seed
cap tuples `nbr_comb_`src_seed'_`src_tuple'_`nbr_seed'', display min(`n') max(`n') cond(`column`nbr_seed'')
if _rc==3300 {
di as err "Error: Too many neighbors to evaluate. Consider decreasing the within-cluster distance"
exit 3300
}
* Get the number of tuples of `nbr_row'
local count2=" "
local num2=1
while "`count2'"!="" {
if "`tuple`num2''"=="" {
local count2=""
}
local nbr_tuple`num2' `tuple`num2''
local tuple`num2'
local ++num2
}
local num_tuples2=`num2'-1
}
* If the tuple of that neighbor seed is the same than the one from the originating source seed
* Generate a match and save the IDs into a local of matched tuples
forval i = 1/`num_tuples2' {
if "`src_`src_seed'_`src_tuple''"=="`nbr_tuple`i''" {
if `src_row'==1 & `src_tuple'==1 {
noisily di as text "matching neighbors..."
}
local matched_tuples `"`matched_tuples' "`src_`src_seed'_`src_tuple''""'
local tuple`num2'
}
}
}
}
* After the matching loop ran for each row, save the string of matched tuples into a dataset
local n_used_tuples: word count "`matched_tuples'"
preserve
clear all
set obs `n_used_tuples'
cap drop `clus'
tempvar clus
gen `clus'=""
* Select one tuple
forval xy=1/`n_used_tuples'{
local t: word `xy' of `matched_tuples'
replace `clus' ="`t'" in `xy'
}
* Save the tuple number as cluster id into dataset
fegen `clustervarname'=group(`clus')
cap drop `NNN'
tempvar NNN
bys `clustervarname': gen `NNN'=_N
drop if `clustervarname'==. // check why missings created
* keep only tuples that matched at least `n' times, e.g. if `n'=3 :
// 1 neighor of 2 and 3; 2 neighor of 1 and 3; 3 neighor of 1 and 2; : 3 matches
keep if `NNN'>=`n'
collapse (firstnm) `clus' (mean) `NNN' , by(`clustervarname')
* prepare reshape
tempvar clus_iid
split `clus', parse(" ") generate(`clus_iid')
destring `clus_iid'*, replace
drop `clus' `NNN'
tempvar new
* reshape to long (one id per row, several rows per cluster ID)
reshape long `clus_iid', i(`clustervarname') j(`new')
rename `clus_iid' `id0'
tempfile temp2
save `temp2', replace
restore
merge 1:m `id0' using `temp2', nogen
keep `id0' `clustervarname'
rename `id0' `id'
merge m:1 `id' using `original'
keep if _merge==3
drop _merge
}
}
* Special case: if only one cluster feasible in total, skip cluster conflict resolution section
if `n' != 1 {
distinct `clustervarname'
}
if `n' == 1 {
distinct `id'
}
local distinct_clusters `r(ndistinct)'
if `distinct_clusters'!= 1 {
*************************************************************************************************************************
* PART 2: Resolve between-cluster conflicts
*************************************************************************************************************************
if `n' == 1 & `within'!=0 {
duplicates drop `id', force
drop `latitude0' `longitude0'
drop `id0'
}
tempfile temp3
qui save `temp3'
if `n' != 1 {
rename `clustervarname' `clustervarname'0
}
rename `latitude' `latitude0'
rename `longitude' `longitude0'
rename `id' `id0'
cross using `temp3'
cap drop d
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude', gen(`d')
drop if `d'==0
if `n' != 1 {
drop if `clustervarname'0 == `clustervarname'
}
if `n' == 1 {
drop if `id0' == `id'
}
if `n' != 1 {
drop if `clustervarname' ==. | `clustervarname'0 ==.
}
if `n' == 1 {
drop if `id' ==. | `id0' ==.
}
count if `d'<`between'
local tot_conflicts `r(N)'
* indicator if any conflict between points of different clusters
tempvar conflict_distance
gen `conflict_distance'=`d'<`between'
* Save dataset with id sorting from seed
gen `id_me' = `id0'
merge m:1 `id_me' using `original0', keepusing( `sorttemp')
cap drop `id_me'
if `n' != 1 {
sort `clustervarname'0 , stable
}
tempvar sortorder
gen double `sortorder' = runiform()
tempvar sort_id
bys `id0': egen double `sort_id'=min(`sorttemp')
preserve
qui duplicates drop `id0', force
keep `id0' `sort_id'
//rename `id' `id0'
tempfile temp_sort_id
qui save `temp_sort_id'
restore
* Save dataset with cluster sorting from seed
if `n'!=1 {
tempvar min_sortorder
bys `clustervarname'0: egen double `min_sortorder'=min(`sortorder')
tempvar sort_clustervarname
fegen `sort_clustervarname'=group(`min_sortorder')
preserve
qui duplicates drop `clustervarname'0, force
keep `clustervarname'0 `sort_clustervarname'
//rename `clustervarname' `clustervarname'0
cap drop _merge
tempfile temp_sort_clustervarname
qui save `temp_sort_clustervarname'
restore
}
**********************************************
* loop for conflict resolution: initialization
if `n' != 1 {
cap drop `a'
tempvar a
bys `clustervarname'0 `clustervarname' `conflict_distance': gen `a'=_n if `conflict_distance'==1 & `clustervarname'!=.
merge m:1 `id0' using `temp_points_close', nogen // sort by number of points close
tempvar cluster_points_close
bys `clustervarname'0: egen `cluster_points_close'=sum(`points_close2')
tempvar rank_cluster_points_close
fegen `rank_cluster_points_close' =group(`cluster_points_close')
fsum `rank_cluster_points_close'
tempvar rev_rank_cluster_points_close
gen `rev_rank_cluster_points_close'=`r(max)'+`r(min)'-`rank_cluster_points_close'
tempvar rank_points_within_ring
fegen `rank_points_within_ring' =group(`points_within_ring')
fsum `rank_points_within_ring'
tempvar rev_rank_points_within_ring
gen `rev_rank_points_within_ring'=`r(max)'+`r(min)'-`rank_points_within_ring'
merge m:1 `clustervarname'0 using `temp_sort_clustervarname' , nogen
}
if `n' == 1 {
cap drop `a'
tempvar a
bys `id0' `id' `conflict_distance': gen `a'=_n if `conflict_distance'==1 & `id'!=.
if `within'!=0 {
merge m:1 `id0' using `temp_points_close', nogen // sort by number of points close
tempvar cluster_points_close
bys `id0': egen `cluster_points_close'=sum(`points_close2')
tempvar rank_cluster_points_close
fegen `rank_cluster_points_close' =group(`cluster_points_close')
fsum `rank_cluster_points_close'
tempvar rev_rank_cluster_points_close
gen `rev_rank_cluster_points_close'=`r(max)'+`r(min)'-`rank_cluster_points_close'
tempvar rank_points_within_ring
fegen `rank_points_within_ring' =group(`points_within_ring')
fsum `rank_points_within_ring'
tempvar rev_rank_points_within_ring
gen `rev_rank_points_within_ring'=`r(max)'+`r(min)'-`rank_points_within_ring'
}
merge m:1 `id0' using `temp_sort_id', nogen
}
tempvar c
gen `c'=`a'==1
if `n' != 1 {
tempvar cluster_conflicts
bys `clustervarname'0 : egen `cluster_conflicts'= sum(`c')
}
if `n' == 1 {
tempvar cluster_conflicts
bys `id0' : egen `cluster_conflicts'= sum(`c')
}
fsum `cluster_conflicts' if `cluster_conflicts'>0
if `r(N)'== 0 {
local no_conflicts 1
}
if `r(N)'!= 0 { // if there is any conflict: (otherwise go directly to end)
if `n' != 1 {
local max_temp=`r(max)'
local min_temp=`r(min)'
tempvar neighbor_to_min
gen `neighbor_to_min'=0
fsum `cluster_conflicts' if `cluster_conflicts'>0
local min_greater_one=`r(min)'
tempvar n_neighbors_min // number of neighors that have `cluster_conflicts'==`r(min)'
tempvar xx
bys `clustervarname'0 `clustervarname' `conflict_distance': gen `xx' =_n if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1
tempvar xx2
gen `xx2' =`xx' ==1
bys `clustervarname'0 : egen `n_neighbors_min'= sum(`xx2')
flevelsof `clustervarname' if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1, local(levels)
foreach v in `levels' {
replace `neighbor_to_min'=1 if `clustervarname'0==`v'
}
tempvar sortorder_cluster
fegen `sortorder_cluster' = group( `neighbor_to_min' `n_neighbors_min' `sort_clustervarname') //if `e'==1
}
if `n' == 1 {
local max_temp=`r(max)'
local min_temp=`r(min)'
tempvar neighbor_to_min
gen `neighbor_to_min'=0
fsum `cluster_conflicts' if `cluster_conflicts'>0
local min_greater_one=`r(min)'
tempvar n_neighbors_min // number of neighors that have `cluster_conflicts'==`r(min)'
tempvar xx
bys `id0' `id' `conflict_distance': gen `xx' =_n if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1
tempvar xx2
gen `xx2' =`xx' ==1
bys `id0' : egen `n_neighbors_min'= sum(`xx2')
flevelsof `id' if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1, local(levels)
foreach v in `levels' {
replace `neighbor_to_min'=1 if `id0'==`v'
}
tempvar sortorder_cluster
fegen `sortorder_cluster' = group( `neighbor_to_min' `n_neighbors_min' `sort_id') //if `e'==1
}
if `n' != 1 {
tempvar max_conflict_distance
bys `clustervarname'0 `clustervarname': egen `max_conflict_distance'=max(`conflict_distance')
duplicates drop `clustervarname'0 `clustervarname', force
replace `conflict_distance'=`max_conflict_distance'
drop `max_conflict_distance' `id' `d' `latitude' `longitude'
fsum `sortorder_cluster'
if `r(N)'!=0 {
flevelsof `clustervarname'0 if `sortorder_cluster'==`r(max)' , local(levels)
recode `conflict_distance'(1=0) if `clustervarname'==`levels'
drop if `clustervarname'0 == `levels'
}
}
if `n' == 1 {
tempvar max_conflict_distance
bys `id0' `id': egen `max_conflict_distance'=max(`conflict_distance')
duplicates drop `id0' `id', force
replace `conflict_distance'=`max_conflict_distance'
drop `max_conflict_distance' `d' `latitude' `longitude'
fsum `sortorder_cluster'
flevelsof `id0' if `sortorder_cluster'==`r(max)' , local(levels)
recode `conflict_distance'(1=0) if `id'==`levels'
drop if `id0' == `levels'
}
}
**********************************************************************************************************
* loop for conflict resolution: loop through all conflicts
fsum `cluster_conflicts'
if `r(N)'!=0 { // if there is any conflict: (otherwise go directly to end)
local max_conflicts=`r(max)'
while `max_conflicts'>0 {
qui {
if `n' != 1 {
cap drop `cluster_conflicts'
cap drop `a'
tempvar a
bys `clustervarname'0 `clustervarname' `conflict_distance': gen `a'=_n if `conflict_distance'==1 & `clustervarname'!=.
tempvar c
gen `c'=`a'==1
tempvar cluster_conflicts
bys `clustervarname'0 : egen `cluster_conflicts'= sum(`c')
}
if `n' == 1 {
cap drop `cluster_conflicts'
cap drop `a'
tempvar a
bys `id0' `id' `conflict_distance': gen `a'=_n if `conflict_distance'==1
tempvar c
gen `c'=`a'==1
tempvar cluster_conflicts
bys `id0' : egen `cluster_conflicts'= sum(`c')
}
fsum `cluster_conflicts' if `cluster_conflicts'>0
if `r(N)'!= 0 { //
if `n' != 1 {
drop `neighbor_to_min' `n_neighbors_min'
tempvar neighbor_to_min
gen `neighbor_to_min'=0
fsum `cluster_conflicts' if `cluster_conflicts'>0
local min_greater_one=`r(min)'
tempvar n_neighbors_min // number of neighors that have `cluster_conflicts'==`r(min)'
tempvar xx
bys `clustervarname'0 `clustervarname' `conflict_distance': gen `xx' =_n if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1
tempvar xx2
gen `xx2' =`xx' ==1
bys `clustervarname'0 : egen `n_neighbors_min'= sum(`xx2')
flevelsof `clustervarname' if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1 , local(levels)
foreach v in `levels' {
replace `neighbor_to_min'=1 if `clustervarname'0==`v'
}
drop `sortorder_cluster'
tempvar sortorder_cluster
fegen `sortorder_cluster' = group(`neighbor_to_min' `n_neighbors_min' `sort_clustervarname') //if `e'==1
fsum `sortorder_cluster'
if `r(N)'!=0 {
flevelsof `clustervarname'0 if `sortorder_cluster'==`r(max)' , local(levels)
local clustervarname_todrop= `levels'
recode `conflict_distance'(1=0) if `clustervarname'==`clustervarname_todrop'
drop if `clustervarname'0 == `clustervarname_todrop'
}
}
if `n' == 1 {
drop `neighbor_to_min' `n_neighbors_min'
tempvar neighbor_to_min
gen `neighbor_to_min'=0
fsum `cluster_conflicts' if `cluster_conflicts'>0
local min_greater_one=`r(min)'
tempvar n_neighbors_min // number of neighors that have `cluster_conflicts'==`r(min)'
tempvar xx
bys `id0' `id' `conflict_distance': gen `xx' =_n if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1
tempvar xx2
gen `xx2' =`xx' ==1
bys `id0' : egen `n_neighbors_min'= sum(`xx2')
flevelsof `id' if `cluster_conflicts'==`min_greater_one' & `conflict_distance'==1 , local(levels)
foreach v in `levels' {
replace `neighbor_to_min'=1 if `id0'==`v'
}
drop `sortorder_cluster'
fegen `sortorder_cluster' = group( `neighbor_to_min' `n_neighbors_min' `sort_id') //if `e'==1
fsum `sortorder_cluster'
flevelsof `id0' if `sortorder_cluster'==`r(max)' , local(levels)
local clustervarname_todrop= `levels'
recode `conflict_distance'(1=0) if `id'==`clustervarname_todrop'
drop if `id0' == `clustervarname_todrop'
}
} // ends qui
fsum `cluster_conflicts'
local max_conflicts_print `max_conflicts'
if `r(N)'!= 0 {
if `r(max)'==`max_conflicts' {
//local dots `dots'.
//local max_conflicts_print "resolving conflicts:`max_conflicts_print' `dots'"
//di as input "`max_conflicts_print'" _continue
//di as input "`dots'" _continue
//di as err "max_conflicts=`max_conflicts_print' `dots'" _continue
}
*
if `r(max)'!=`max_conflicts' {
local dots
noisily di as text "resolving conflicts:`r(max)'"
}
local max_conflicts=`r(max)'
}
}
if `r(N)'== 0 {
local max_conflicts 0
di as input "resolving conflicts:0"
}
}
}
if _rc==2000 {
di as err "Error: No observations."
di as err "Likely cause is a between-cluster distance which is too large."
exit 2000
}
if `n' != 1 {
cap duplicates drop `clustervarname'0 `id0' , force
keep `clustervarname'0 `id0'
}
if `n' == 1 {
cap duplicates drop `id0' , force
keep `id0'
}
rename `id0' `id'
merge 1:1 `id' using `original0'
keep if _merge==3
drop _merge
if `n' != 1 {
keep `clustervarname'0 `id' `latitude' `longitude'
}
if `n' == 1 {
keep `id' `latitude' `longitude'
}
*************************************************************************************************************************
* Part 3: After maximum number of clusters was chosen, keep adding points that respect constraints to clusters
*************************************************************************************************************************
if `within'!=0 {
qui {
//drop if `latitude0'==. | `longitude0'==.
if `n' == 1 {
gen `clustervarname'=_n
}
if `n' != 1 {
rename `clustervarname'0 `clustervarname'
}
merge 1:1 `latitude' `longitude' using `original0', nogen
* Find all remaining points within `within' distance
tempfile temp4
save `temp4'
rename `id' `id0'
rename `clustervarname' `clustervarname'0
rename `longitude' `longitude0'
rename `latitude' `latitude0'
cross using `temp4'
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude' , gen(`d')
drop if `id0'==`id'
* Distance from remaining points to old points in clusters (new points in: cluster_id, old points in:cluster_id0)
tempvar within_distance
bys `clustervarname'0: gen `within_distance'=1 if `d'<=`within' & `d'!=0 & `clustervarname'0!=. & `clustervarname'==.
tempvar max_within_dist
bys `clustervarname'0 `id': egen `max_within_dist'=max(`within_distance')
tempvar max_d_within
bys `clustervarname'0 `id': egen `max_d_within'=max(`d') if `max_within_dist'==1
tempvar new_point
gen `new_point'=`max_d_within'<=`within' // flag for potential new point
replace `clustervarname' = `clustervarname'0 if `new_point'==1
keep if `max_d_within'<=`within' | `clustervarname'!=. // keep potential new points and old assigned points
keep `id' `clustervarname' `latitude' `longitude' `new_point'
qui duplicates drop `id', force
* First drop points that are in conflict with points in existing clusters (would conflict with cluster maximization)
tempfile temp5
save `temp5'
rename `id' `id0'
rename `clustervarname' `clustervarname'0
rename `longitude' `longitude0'
rename `latitude' `latitude0'
rename `new_point' `new_point'0
cross using `temp5'
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude' , gen(`d')
drop if `id0'==`id'
tempvar conflict
gen `conflict'=1 if `d'< `between' & `clustervarname'0 !=`clustervarname' & `new_point'0==0 & `new_point'==1
tempvar max_conflict
bys `id': egen `max_conflict'=max(`conflict')
tempvar max_new_point
bys `id': egen `max_new_point'=max(`new_point')
drop if `max_conflict'==1 & `max_new_point'==1
* Second, sequentially drop new points that have most conflicts with other new points
keep `id' `clustervarname' `latitude' `longitude' `new_point'
qui duplicates drop `id', force
tempfile temp6
save `temp6'
rename `id' `id0'
rename `clustervarname' `clustervarname'0
rename `longitude' `longitude0'
rename `latitude' `latitude0'
rename `new_point' `new_point'0
cross using `temp6'
local max_sum_conflicts 1
while `max_sum_conflicts'>0 {
cap drop `d'
tempvar d
geodist `latitude0' `longitude0' `latitude' `longitude' , gen(`d')
drop if `id0'==`id'
tempvar conflict
gen `conflict'=1 if `d' < `between' & `clustervarname'0!=`clustervarname' & `new_point'0==1 & `new_point'==1 & `clustervarname'0!=. & `clustervarname'!=.
tempvar max_new_point
bys `id': egen `max_new_point'=max(`new_point')
tempvar sum_conflicts
bys `id': egen `sum_conflicts'=sum(`conflict')
fsum `sum_conflicts'
local max_sum_conflicts=`r(max)'
tempvar e
gen `e'=1 if `sum_conflicts'==`max_sum_conflicts'
set seed `seed'
tempvar sortorder
se seed `seed'
gen double `sortorder' = runiform()
tempvar sortorder_id
bys `id': egen `sortorder_id'=min(`sortorder')
sort `sum_conflicts' `id' `sortorder'
tempvar sortorder
fegen `sortorder' = group(`sum_conflicts' `sortorder_id') if `e'==1
fsum `sortorder'
drop if `sortorder'== `r(max)' & `max_new_point'==1
keep `id' `clustervarname' `latitude' `longitude' `new_point'
qui duplicates drop `id', force
tempfile temp7
save `temp7'
rename `id' `id0'
rename `clustervarname' `clustervarname'0
rename `longitude' `longitude0'
rename `latitude' `latitude0'
rename `new_point' `new_point'0
cross using `temp7'
}
keep `id' `clustervarname' `latitude' `longitude' `new_point'
qui duplicates drop `id', force
* Finally, drop if distance between two new points larger than `within' (a new point may be at `within' with respect to old points, but not to another newly added point)
local outside_local 1
while `outside_local' >0 {
tempfile temp8
save `temp8'
rename `id' `id0'
rename `clustervarname' `clustervarname'0
rename `longitude' `longitude0'
rename `latitude' `latitude0'
rename `new_point' `new_point'0
cross using `temp8'
geodist `latitude0' `longitude0' `latitude' `longitude' , gen(`d')
drop if `id0'==`id'
tempvar outside
gen `outside'=1 if `d' > `within' & `clustervarname'0==`clustervarname' & `d'!=.
fsum `outside'
local outside_local=`r(N)'
tempvar max_outside
bys `id': egen `max_outside'=max(`outside')
set seed `seed'
tempvar sortorder
gen double `sortorder' = runiform() if `max_outside'==1
tempvar max_id_sortorder
bys `id': egen double `max_id_sortorder'=max(`sortorder') if `max_outside'==1
tempvar to_drop
egen `to_drop'=group(`max_outside' `max_id_sortorder') if `max_outside'==1
fsum `to_drop'
if `r(N)'>0 {
drop if `to_drop'==`r(max)'
}
keep `id' `clustervarname' `latitude' `longitude' `new_point'
qui duplicates drop `id', force
}
}
}
} // closes "if distinct==1 bracket"
if `within'==0 {
gen `clustervarname'=_n
}
qui {
qui drop if `latitude'==. | `longitude'==.
tempvar sorted_clustervarname
fegen `sorted_clustervarname'=group(`clustervarname')
replace `clustervarname'= `sorted_clustervarname'
drop `sorted_clustervarname'
label var `clustervarname' "Cluster ID"
duplicates drop `id', force
merge 1:1 `latitude' `longitude' using `original0', nogen
sort `clustervarname'
}
if "`no_conflicts'"=="1" {
noisily di "Note: no conflicts found between eligible clusters"
}
} // ends first qui{
end