-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplacement_Network.R
More file actions
2752 lines (2493 loc) · 94 KB
/
Copy pathDisplacement_Network.R
File metadata and controls
2752 lines (2493 loc) · 94 KB
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Nepal Displacement Tracking Network Analysis
# author: Georgi D. Gospodinov
# date: "July 27, 2015"
#
# Data Sources:
#
# Tables:
#
# https://data.hdx.rwlabs.org/dataset/nepal-earthquake-severity-index
# https://data.hdx.rwlabs.org/dataset/io (sheet 3)
# https://data.hdx.rwlabs.org/dataset/population-movements-after-the-nepal-earthquake-v-3-up-to-11th-june-2015
# master_hlcit.csv
#
# Relevant materials and data can be found at:
#
# https://www.dropbox.com/sh/tb9854hzcof7x23/AACEDTGk8EmYQ6r4ukSFLBspa?dl = 0
#
# in the folder /Displacement Network Model/
#
#
#
# LOAD PACKAGES
library(plyr)
library(dplyr)
library(igraph)
library(RColorBrewer)
#
#
# SET FILE SOURCE PATH
DIR <- "/Users/ggospodinov/Desktop/UN_OCHA_project/data/"
#
#
# DEFINE FUNCTIONS
#
#
#
# FUNCTION TO DISPLAY RELATIVE PERCENTAGES FOR HSITOGRAM COLUMNS
histP <- function(x,breaks, ...) {
H <- hist(x, plot = FALSE, breaks=breaks)
H$density <- with(H, 100 * density* diff(breaks)[1])
labs <- paste(round(H$density), "%", sep="")
plot(H, freq = FALSE, labels = labs, ylim=c(0, 1.08*max(H$density)),...)
}
# FUNCTION TO DISPLAY RELATIVE PERCENTAGE VALUES GREATER THAN 0
histP1 <- function(x,breaks, ...) {
H <- hist(x, plot = FALSE, breaks=breaks)
H$density <- with(H, 100 * density* diff(breaks)[1])
labs <- ifelse(round(H$density)>0,paste(round(H$density), "%", sep=""),NA)
plot(H, freq = FALSE, labels = labs, ylim=c(0, 1.08*max(H$density)),...)
}
# FUNCITON TO DISPLAY RELATIVE PERCENTAGE VALUES GREATER THAN 5, CAN BE HARDCODED
histP2 <- function(x,breaks, ...) {
H <- hist(x, plot = FALSE, breaks=breaks)
H$density <- with(H, 100 * density* diff(breaks)[1])
labs <- ifelse(round(H$density)>5,paste(round(H$density), "%", sep=""),NA)
plot(H, freq = FALSE, labels = labs, ylim=c(0, 1.08*max(H$density)),...)
}
# FUNCTION THAT TRIMS LEADING WHITESPACE
trim.leading <- function (x) sub("^\\s+", "", x)
# FUNCTION THAT TRIMS TRAILING WHITESPACE
trim.trailing <- function (x) sub("\\s+$", "", x)
# FUNCTION THAT TRIMS LEADING OR TRAILING WHITESPACE
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
# FUNCITON TO REMOVE ALL SPACES FROM LEVEL NAMES OF A VARIABLE
rm_space <- function(df,col_name){
level_names <- unique(levels(df[,which(names(df) %in% col_name)]))
df[,which(names(df) %in% col_name)] <- mapvalues(df[,which(names(df) %in% col_name)],
from = level_names,
to = gsub("[[:space:]]","",level_names))
return(df)
}
# FUNCTION THAT DROPS ISOLATED VERTICES
drop_isolated <- function(graph, vertex_colors, vertex_names) {
# get the definitions
g <- graph
# filter to degree > 0 eliminate isolated vertices
g_f <- delete.vertices(g,V(g)[degree(g)==0])
v_g_f <- setdiff(V(g),V(g)[degree(g)==0])
# filter names and color
V(g_f)$name <- vertex_names[v_g_f]
V(g_f)$color <- vertex_colors[v_g_f]
return(g_f)
}
# FUNCITON THAT DROPS LOOPS
drop_loops <- function(graph, vertex_colors, vertex_names){
g <- simplify(graph,remove.loops=TRUE)
g_f <- delete.vertices(g,V(g)[degree(g)==0])
v_g_f <- setdiff(V(g),V(g)[degree(g)==0])
# filter names and color
V(g_f)$name <- vertex_names[v_g_f]
V(g_f)$color <- vertex_colors[v_g_f]
return(g_f)
}
# DEFINE EDGE-FILTRATION FUNCTION FOR THE NETWORKS
filter <- function(cutoff,edge_matrix,vertex_colors,vertex_names) {
# get the definitions
cut <- cutoff
adj <- edge_matrix
adj[adj<cut] <- 0
adj_0 <- adj
# define the filtered graph
g <- graph.adjacency(adj_0,mode="directed",weighted=TRUE)
V(g)$color <- vertex_colors
# filter to degree > 0 eliminate isolated vertices
g_f <- delete.vertices(g,V(g)[degree(g)==0])
v_g_f <- setdiff(V(g),V(g)[degree(g)==0])
V(g_f)$name <- vertex_names[v_g_f]
V(g_f)$color <- vertex_colors[v_g_f]
return(g_f)
}
# DEFINE DEGREE FILTRATION FUNCTION FOR THE NETWORKS
filter_deg <- function(cutoff,edge_matrix,vertex_colors,vertex_names) {
# get the definitions
cut <- cutoff
adj <- edge_matrix
# set the cut-off
adj_0 <- adj
adj_0[adj_0>0] <- 1
for (i in 1:dim(adj)[1]){
if (sum(adj_0[,i])<cutoff){
adj <- adj[,-i]
adj <- adj[-i,]
}
}
# define the filtered graph
g <- graph.adjacency(adj,mode="directed",weighted=TRUE)
V(g)$color <- vertex_color
# filter to degree > 0 eliminate isolated vertices
g_f <- delete.vertices(g,V(g)[degree(g)==0])
v_g_f <- setdiff(V(g),V(g)[degree(g)==0])
V(g_f)$name <- vertex_names[v_g_f]
# color the filtered graph with sources and endpoints for the directed edges
V(g_f)$color <- V(g)$colors[v_g_f]
return(g_f)
}
# DEFINE WEIGHTED DEGREE FILTRATION USING graph.strength
# DEFINE A FILTRATION OF GRAPH TO DISPLAY LARGEST CLUSTER (GIANT COMPONENT)
giant_comp <- function(graph, vertex_colors, vertex_names){
# get the definitions
g <- graph
# identify the largest cluster
clusters <- as.data.frame(table(clusters(g)$membership))
ind <- as.numeric(clusters[which(clusters$Freq==max(clusters$Freq)),]$Var1)
vertices <- which(clusters(g)$membership==ind)
vertices_complement <- which(clusters(g)$membership!=ind)
# filter to only include the giant component
g_f <- delete.vertices(g,V(g)[which(V(g) %in% vertices_complement)])
v_g_f <- setdiff(V(g),V(g)[which(V(g) %in% vertices_complement)])
V(g_f)$name <- vertex_names[v_g_f]
# color the filtered graph with sources and endpoints for the directed edges
V(g_f)$color <- vertex_colors[v_g_f]
return(g_f)
}
# INITIAL DATA INPUT AND TRANSFORMATIONS
#
#
# COLUMN NAMES FOR CCCM_Nepal_DTM_R2.csv
#
#
# 1.1.c.1 Site ID (SSID)
# 1.1.d.1 Site Name
# 1.1.a.2 Survey Round
# 1.1.a.1 Survey date (DD.MM.YYYY)
# 1.1.e.2 Zone
# 1.1.e.3 District
# 1.1.e.4 VDC
# 1.1.f.2 Site GPS Latitude
# 1.1.f.1 Site GPS Longitude
# 1.4.a.2 Site open?
# 1.4.c.1 Closing Date (DD.MM.YYYY)
# 1.3.a.1 Site Classification
# 1.3.b.1 Site Type
# If other, please specify
# 1.4.a.1 Site start/open date (DD.MM.YYYY)
# 1.4.b.1 Site Expected Closing Date
# 1.1.i.1 Accessibility to site
# 1.3.d.1 Ownership of land of site
# 1.3.c.1 What is the most common type of shelter
# If other or No Answer, please specify
# 1.2.b.1 Is there a Site Management Committee (SMC) at the site?
# 1.2.b.10 % of women participating in the Site Management Committee (SMC)
# 1.2.b.2 Is the site management committee (SMC) made up from the community at the site?
# 1.2.b.7 SMC member name or focal point at site
# 1.2.b.8 SMC member phone number
# 1.2.c.1 Is there a Site Management Agency (SMA) at the site?
# 1.2.c.2 Type of SMA
# If other, Specify.
# 1.2.c.3 Name of SMA
# 1.2.c.4 SMA member name or focal point at site
# 1.2.c.5 SMA member phone number
# 1.2.a.1 Is there any registration activity/IDP List Maintained
# 1.2.n.1 Is WASH support being provided at the site ?
# If yes who provides the service?
# 1.2.o.1 Is HEALTH support being provided at the site
# If yes who provides the service?
# 1.2.p.1 Is SHELTER/NFI support provided at the site ?
# If yes who provides the service?
# 1.2.q.1 Is FOOD support provided at the site ?
# If yes who provides the service?
# 1.2.r.1 Is PROTECTION support provided at the site ?
# If yes who provides the service?
# 1.2.s.1 Is EDUCATION support provided at the site ?
# If yes who provides the service?
# 1.2.t.1 Is LIVELIHOOD support being provided at the site?
# If yes who provides the service?
# 1.5.b.2 Place of Origin of the largest IDP group in camp (Zone)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (District)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (VDC)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (Ward)
# 1.5.c.2 Place of Origin of the second largest IDP group (Zone)
# 1.5.c.2 Place of Origin of the second largest IDP group District)
# 1.5.c.2 Place of Origin of the second largest IDP group (VDC)
# 1.5.c.2 Place of Origin of the second largest IDP group (Ward)
# 2.1.a.1 Total Number of IDP Families/HHs
# 2.1.c.1 Number of Males by age: <1 year
# 2.1.c.1 Number of Males by age: 1-5 year
# 2.1.c.1 Number of Males by age: 6-17 year
# 2.1.c.1 Number of Males by age: 18-59 year
# 2.1.c.1 Number of Males by age: 60+ year
# 2.1.b.2 Total number of IDP Male Individuals
# 2.1.d.1 Number of Females by age: <1 year
# 2.1.d.1 Number of Females by age: 1-5 year
# 2.1.d.1 Number of Females by age: 6-17 year
# 2.1.d.1 Number of Females by age: 18-59 year
# 2.1.d.1 Number of Females by age: 60+ year
# 2.1.b.2 Total number of IDP Female Individuals
# 2.1.b.2 Total number of IDP
# 2.2.c.1 Number of pregnant women
# 2.2.d.1 Number of breastfeeding mothers
# 2.2.g.1 Number of persons with Disabilities
# 2.2.f.1 Number of Persons with Chronic Diseases or Serious Medical Conditions
# 2.2.n.1 Number of single female-headed households
# 2.2.p.1 Number of child-headed households
# 2.2.x.3 Number of elderly-headed households
# 2.2.u.1 Number of members of marginalized caste/ethnicity
# 2.3.c.1 Date of arrival of first IDP group
# 2.3.c.2 Date of arrival of last IDP group
# 2.3.e.1 Area of intended return for largest IDP group
# 2.3.b.4 Have IDPs previously been displaced
# 2.3.e.7 What is preventing the largest IDP group from returning home?
# If other, please specify
# 2.3.g.1 Estimated % of IDPs sleeping in the site
# 2.3.f.1 Is there relocation plan for the IDPs?
# 3.1.a.1 Percentage of HH living outside (no shelter)
# 3.1.b.1 Percentage of HH living in tents
# 3.1.c.1 Percentage of HH living in makeshift shelters
# 3.1.d.1 Percentage of HH living indoors (solid walls)
# 3.2.a.1 Percentage of HH with access to electricity
# 3.2.b.1 Percentage of HH with access to safe cooking facilities
# 3.2.c.1 Percentage of HH with private living area
# 3.7.c.2 Percentage of HH with mosquito nets
# 3.7.j.1 Most needed type of NFI
# If other or No Answer, please specify
# 3.7.j.2 Second most needed type of NFI
# If other or No Answer, please specify
# 3.7.j.3 Third most needed type of NFI
# If other or No Answer, please specify
# 3.8.b.1 Is there a need for shelter repair materials
# 3.8.b.1 Is there a need for tools for shelter construction?
# 4.1.a.1 Location of site's main water source (walking, one-way)
# 4.1.b.1 Main non-drinking water source provided or available
# If other, please specify
# 4.1.f.1 What is the main drinking water source provided or available
# If other, please specify
# 4.2.a.1 Average amount of water available per day and per person
# 4.3.e.1 What is the main problem with the water?
# If other, please specify
# 4.4.j.1 Condition of most of the latrines
# 4.4.a.1 Number of functioning toilets available on-site
# 4.8.b.1 Do toilets and bathrooms have locks from the inside
# 4.1.g.1 Is the drinking water potable ?
# 4.4.b.1 Availability of separate male and female toilets
# 4.5.b.1 Availability of separate male and female bathing areas
# 4.6.a.1 Garbage Disposal Type
# If other, please specify
# 4.4.n.1 Evidence of open defecation?
# 4.7.g.1 Availability of hand-washing station filled in with water and soap close to the toilets?
# 4.7.g.2 Evidence of hand-washing practices?
# 5.1.a.1 Is there access to food (distribution, vouchers, trade, fishing…)
# 5.1.e.1 Is there access to a market near from the site?
# 5.1.d.1 Most common source for obtaining food
# If other, please specify
# 6.1.a.1 Screening for malnutrition conducted in the area? (i.e. weight, height, mid-upper arm circumference screening)
# 6.1.b.1 Availability of supplementary feeding for pregnant and lactating mothers
# 6.1.c.1 Availability of supplementary feeding for children
# 7.1.b.1 What is the most prevalent health problem at the site
# If other, please specify
# 7.1.b.2 What is the second most prevalent health problem at the site
# If other, please specify
# 7.1.b.3 What is the third most prevalent health problem at the site
# If other, please specify
# 7.2.a.1 Access to health facilities
# 7.2.a.4 Do most women utilize health facilities?
# 7.2.b.1 Location of health facilities/services
# 7.2.c.2 Who provides health facilities/services
# If other, please specify
# 7.2.a.3 Regular access to medicine
# 8.1.b.1 Access to formal/informal education services for children from displaced HHs
# 8.2.b.1 Distance to nearest education facility
# 8.3.a.2 Of children in site attending school, what % are girls?
# 8.3.a.2 Of children in site attending school, what % are boys?
# 8.2.a.1 Location of formal/informal education facilities/services for children from displaced HHs
# 9.1.a.1 Occupation/trade of majority of displaced households (coping mechanism)
# If other, please specify
# 9.2.h.1 Percentage of HH in the site with a source of income
# 9.2.i.1 Access to income generating activities
# 9.2.k.1 Do the majority of HHs in the site receive remittances?
# 9.3.a.2 Is there livestock on site
# 9.1.c.1 Do IDPs have access to land for cultivation?
# 11.3.b.1 Do any recruiters come to the site for day labour, domestic work or working abroad?
# 11.3.a.2 if yes, to which city/country
# 10.1.a.2 Is there security on-site/settlement areas
# 10.1.e.1 Are security incidents reported in the site
# 10.1.b.1 Who provides main security in the site
# If other, please specify
# 10.2.g.1 Most common type of security incidents reported/known occurring in the settlement area
# If others, please specify
# 10.2.g.5 Are security incidents commonly reported in the community before the earthquake?
# 10.2.i.4 Most reported problem in receiving support
# If other, please specify
# 10.1.c.1 Number of children friendly spaces
# 10.1.d.1 Number of women friendly spaces
# 10.1.f.1 Do the majority of people have identification card or other documentation?
# 10.2.j.2 Reporting/referral mechanism for GBV survivors
# 10.2.t.2 Are women segregated during menstruation?
# If yes, where do they go?
# 10.2.t.1 Are there issues of discrimination towards minority groups?
# 10.3.a.1 Do men feel safe in site?
# 10.3.a.2 Do children feel safe in site
# 10.3.a.3 Do women feel safe at the site
# 10.1.s.1 Is there adequate lighting in the majority of communal point (WASH facilities, public spaces…)
# 11.1.a.1 Where do residents mostly get their information from?
# If other, please specify
# 11.2.c.1 Are complaints being reported?
# 11.2.c.2 If yes, to whom?
# 11.1.c.1 What is the main topic on which the community is requesting information on?
# If other, please specify
# 11.1.h.1 Is everyone aware that donations do not need to be exchanged for anything?
# Site classification
# LOAD LAT/LON COORDINATES (OF CENTROIDS) AND HLCIT CODES
hlcit <- read.csv(paste0(DIR,"master_hlcit.csv"))
colnames(hlcit) <- c("lon","lat","vdc_name","vname","hlcit_code")
hlcit$hlcit_code <- as.factor(hlcit$hlcit_code)
hlcit$vname <- as.character(hlcit$vname)
hlcit$vdc_name <- as.character(hlcit$vdc_name)
hlcit <- rm_space(hlcit,"hlcit_code")
hlcit$hlcit_code <- as.numeric(levels(hlcit$hlcit_code))[hlcit$hlcit_code]
# READ IN THE DISPLACEMENT FILE DATA
dt_data <- read.csv(paste0(DIR,"CCCM_Nepal_DTM_R2.csv"), sep = ",")
# SET VAIRABLE NAMES THAT ARE SHORTER AND DO NOT CONTAIN SPACES AND OTHER SYMBOLS
nam <- c("ssid","site_name","survey_round","survey_date","zone","district","vdc","lat","lon","is_open","closing_date",
"site_class","site_type","if_other","site_start","expect_close","access","owner","shelter_type",
"specify","is_smc","women_smc","smc_locals","smc_members","member_phone","is_sma","sma_type",
"if_other2","sma_name","sma_member","sma_phone","is_idp","is_wash","wash_provider","is_health",
"health_provider","is_nfi","nfi_provider","is_food","food_provider","is_protect","protector",
"is_educate","educator","is_livelihood","livelihood_provider","idp_origin_zone","idp_origin_district",
"idp_origin_vdc","idp_origin_ward","idp2_origin_zone","idp2_origin_district","idp2_origin_vdc",
"idp2_origin_ward","idp_hh","males_age_1","males_age_1-5","males_age_6-17","males_age_18-59",
"males_age_over60","total_idp_males","females_age_1","females_age_1-5","females_age_6-17",
"females_age_18-59","females_age_over60","total_idp_females","total_idp","preg","breastfeed",
"disabled","chronical","female_hh","child_hh","elder_hh","marginal","idp_arrival","idp_last_arrival",
"return_area","prev_idp","why_idp","if_other3","idp_pct_sleep","is_relocation","hh_pct_out","hh_pct_tents",
"hh_pct_mkshift","hh_pct_indoors","hh_pct_electricity","hh_pct_cooking","hh_pct_private","hh_pct_nets",
"nfi_most_need","if_other4","nfi2_most_need","if_other5","nfi3_most_need","if_other6","is_repairs",
"is_tools","water_loc","non_drinking_water","if_other7","drinking_water","if_other8","ave_water_per_day",
"water_prob","if_other9","latrines","no_wc","wc_locks","water_potable","m_f_wc","m_f_bath","garbage",
"if_other10","open_wc","hand_wash","is_hand_wash","food_access","market_access","food_src","if_other11",
"screening","food_for_preg","food_child","illness","if_other12","illness2","if_other13","illness3",
"if_other14","access_hosp","women_use_hosp","hosp_loc","who_doc","if_other15","access_meds","access_school",
"dist_school","girls_pct_school","boys_pct_school","school_loc","coping_mech","if_other16","hh_pct_income",
"paid_work","is_remittance","is_livestock","is_land","recruit","to_which","is_security","reported",
"main_security","if_other17","common_crimes","if_other18","before_reported","biggest_prob","if_other19",
"child_friendly","women_friendly","id","gbv_survivors","w_segregated","where","discrimination","men_safe",
"children_safe","women_safe","lights","info","if_other20","complaints","to_whom","info_need","if_other21",
"donations","site_class2")
colnames(dt_data) <- nam
# DROP THE FIRST THREE ROWS DUE TO EXTRA NOTATION
dt_data <- dt_data[4:dim(dt_data)[1],]
# FORMAT THE POPULATION COUNT (IN HH) OF DISPLACEMENT
dt_data$idp_hh <- as.numeric(levels(dt_data$idp_hh))[dt_data$idp_hh]
# COLUMNS FOR THE INITIAL MODEL:
# 1.1.c.1 Site ID (SSID)
# 1.1.d.1 Site Name
# 1.1.a.2 Survey Round
# 1.1.a.1 Survey date (DD.MM.YYYY)
# 1.1.e.2 Zone
# 1.1.e.3 District
# 1.1.e.4 VDC
# 1.1.f.2 Site GPS Latitude
# 1.1.f.1 Site GPS Longitude
# 1.5.b.2 Place of Origin of the largest IDP group in camp (Zone)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (District)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (VDC)
# 1.5.b.2 Place of Origin of the largest IDP group in camp (Ward)
# 1.5.c.2 Place of Origin of the second largest IDP group (Zone)
# 1.5.c.2 Place of Origin of the second largest IDP group District)
# 1.5.c.2 Place of Origin of the second largest IDP group (VDC)
# 1.5.c.2 Place of Origin of the second largest IDP group (Ward)
# 2.1.a.1 Total Number of IDP Families/HHs
# SELECT INITIAL COLUMNS, SAVE THE FILE
names <- c("ssid","site_name","survey_round","survey_date","zone","district",
"vdc","lat","lon","idp_origin_zone","idp_origin_district","idp_origin_vdc",
"idp_origin_ward","idp2_origin_zone","idp2_origin_district","idp2_origin_vdc",
"idp2_origin_ward","idp_hh")
dt_data <- dt_data[,names]
write.csv(dt_data,file = paste0(DIR,"Nepal_DTM_Network_model1.csv"))
# READ IN DISPLACEMENT DATA
dt_data <- read.csv(paste0(DIR,"Nepal_DTM_Network_model1.csv"))
dt_data <- dt_data[,2:dim(dt_data)[2]]
# FORMAT THE VDC RELATED COLUMNS AS CHARACTER
dt_data$vdc <- trim(as.character(dt_data$vdc))
dt_data$idp_origin_vdc <- trim(as.character(dt_data$idp_origin_vdc))
dt_data$idp2_origin_vdc <- trim(as.character(dt_data$idp2_origin_vdc))
# FILTER TO USE DESTINATION VDC LEVELS AS WELL AS ORIGIN (1 OR 2) VDC ENTRIES THAT ARE NON-EMPTY
dt_data <- dt_data[nchar(dt_data$vdc)>0 & (nchar(dt_data$idp_origin_vdc) + nchar(dt_data$idp2_origin_vdc))>0,]
# RESOLVE VDC NAMES ACCORDING TO THE HLCIT MASTER LIST:
dt_data$vdc <- mapvalues(dt_data$vdc,
from = c("Barahbise","Barpak","Chandeni","Charikot","Gokarneshwar","Kathmandu",
"Kathmandu Municipality","Lalitpur","Mankha","Manmaijn","Naikap Naya",
"Puranagaun","Sanga","Sangkhu Suntol","Tokhasaraswati"),
to = c("Barhabise","Warpak","Chandeni Mandan","Narikot","Gokarneswor",
"Kathmandu Metropolitan","Kathmandu Metropolitan","Lalitpur Sub Metropolitan",
"Mangkha","Manmaiju","NaikapNayaBhanjyang","PuranogaunDapcha",
"Sangla","Sangkhu","TokhaSarswoti"))
dt_data$idp_origin_vdc <- mapvalues(dt_data$idp_origin_vdc,
from = c("barabise","Bhimeshwor Municipality","Chautara Municipality","Maanka"),
to = c("Barhabise","Bhimeswor Municipality","Chautara","Mahangkal"))
dt_data$idp2_origin_vdc <- mapvalues(dt_data$idp2_origin_vdc,
from = c("barabise","Bhimeshwor Municipality","Jhaku"),
to = c("Barhabise","Bhimeswor Municipality","Jhyanku"))
# CREATE A LIST OF UNIQUE VDC DESTINATION NAMES
vdc <- unique(c(dt_data$vdc, dt_data$idp_origin_vdc,dt_data$idp2_origin_vdc))
vdc <- vdc[-which(vdc == "")]
vdc1 <- intersect(vdc,hlcit$vdc_name)
vdc2 <- setdiff(vdc,hlcit$vdc_name)
# RESOLVE THE ONLY VDC NAME LEFT THAT IS NOT ON RECORD
index <- which(dt_data$vdc == setdiff(vdc2,hlcit$vname))
closest <- vector()
for (k in 1:dim(hlcit)[1]){
closest[k] <- (dt_data$lat[index]-hlcit$lat[k])^2+(dt_data$lon[index]-hlcit$lon[k])^2
}
dt_data$vdc[index] <- hlcit$vname[which(closest == min(closest))]
vdc <- unique(c(dt_data$vdc, dt_data$idp_origin_vdc,dt_data$idp2_origin_vdc))
vdc <- vdc[-which(vdc == "")]
vdc1 <- intersect(vdc,hlcit$vdc_name)
vdc2 <- setdiff(vdc,hlcit$vdc_name)
# EXTRAPOLATE LHCIT NUMBERS FROM LAT AND LON FOR VDCS FROM HLCIT_MASTER
hl <- vector()
xc <- vector()
yc <- vector()
for (k in 1:length(vdc)){
if (vdc[k] %in% vdc1){
hl[k] <- hlcit$hlcit_code[which(hlcit$vdc_name == vdc[k])[1]]
xc[k] <- hlcit$lat[which(hlcit$vdc_name == vdc[k])[1]]
yc[k] <- hlcit$lon[which(hlcit$vdc_name == vdc[k])[1]]
}
if (vdc[k] %in% vdc2){
hl[k] <- hlcit$hlcit_code[which(hlcit$vname == vdc[k])[1]]
xc[k] <- hlcit$lat[which(hlcit$vname == vdc[k])[1]]
yc[k] <- hlcit$lon[which(hlcit$vname == vdc[k])[1]]
}
}
koords <- cbind(xc,yc)
# FURTHER NARROW DOWN COLUMNS
dt_data <- dt_data[,c("vdc","idp_origin_vdc","idp2_origin_vdc","idp_hh")]
# VDC-LEVEL ADJACENCY MATRIX FOR THE DISPLACEMENT GRAPH, AT THE LEVEL OF DISPLACEMENT TRACK
vdc_m <- matrix(0,nrow = length(vdc),ncol = length(vdc))
# CREATE A MATRIX THAT TRACKS THE NUMBERS OF DISPLACED POPULATIONS AS WELL
# (THIS COULD BE USED TO DERIVE EDGE WEIGHTS)
dtm <- matrix(nrow = length(vdc),ncol = length(vdc))
# COMPUTE THE ENTRIES OF BOTH THE TRACKING DISPLACEMENT MATRIX
# AND THE WEIGHTED DISPLACEMENT TRACKING MATRIX
for (i in 1:length(vdc)){
for (j in 1:length(vdc)){
vdc_m[[i,j]] <- length(dt_data[dt_data$idp_origin_vdc == vdc[i] & dt_data$vdc == vdc[j],1])+
length(dt_data[dt_data$idp2_origin_vdc == vdc[i] & dt_data$vdc == vdc[j],1])
dtm[[i,j]] <- (2/3)*sum(dt_data[dt_data$idp_origin_vdc == vdc[i] & dt_data$vdc == vdc[j],]$idp_hh)+
(1/3)*sum(dt_data[dt_data$idp2_origin_vdc == vdc[i] & dt_data$vdc == vdc[j],]$idp_hh)
}
}
# BUILD THE DIRECTED WEIGHTED VDC NETWORK
gv <- graph.adjacency(vdc_m,
mode = "directed",
weighted = TRUE)
# COLOR VDC NAMES OF ORIGIN (GREEN) AND VDC NAMES OF DESTINATION (BLUE)
V(gv)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))+
length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gv)$color[k] <- "green"
}
}
# SET THE VERTEX LABELS
V(gv)$name <- vdc
# DROP ISOLATED VERTICES (NO DISPLACEMENT)
gv <- drop_isolated(graph = gv,
vertex_colors = V(gv)$color,
vertex_names= vdc)
plot(gv,
layout = layout.fruchterman.reingold(gv,
niter = 200,
area = 2000*vcount(gv)),
vertex.color = V(gv)$color,
vertex.size = 9,
vertex.label = V(gv)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.9,
edge.width = 0.2*(E(gv)$weight),
edge.arrow.size = 0.5,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Abstract Nepal Displacement Network Flow (VDC Level, with Self-Loops)")
legend("topright",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# DROP LOOPS ONLY VERTICES AS WELL ()
gv <- drop_loops(graph = gv,
vertex_colors = V(gv)$color,
vertex_names = vdc)
# RESULTING CLEANED UP GRAPH SHOWING NONTRIVIAL MIGRATION
plot(gv,
layout = layout.fruchterman.reingold(gv,
niter = 200,
area = 2000*vcount(gv)),
vertex.color = V(gv)$color,
vertex.size = 9,
vertex.label = V(gv)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.9,
edge.width = 2*(E(gv)$weight),
edge.arrow.size = 0.5,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Abstract Nepal Displacement Network Flow (VDC Level, No Self-Loops)")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
# PLOT THE GRAPH WITH SELECTED COORDINATES
gv_coords <- koords[which(vdc %in% V(gv)$name),]
plot(gv,
layout = gv_coords,
vertex.color = V(gv)$color,
vertex.size = 4,
vertex.label = V(gv)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.7,
edge.width = (E(gv)$weight),
edge.arrow.size = 0.4,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Nepal Displacement Geo-Network Flow (VDC Level)")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
# DEFINE THE WEIGHTED DISPLACEMENT GRAPH
gd <- graph.adjacency(dtm,
mode = "directed",
weighted = TRUE)
# SET VERTEX COLORS
V(gd)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))+
length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gd)$color[k] <- "green"
}
}
# SET THE VERTEX LABELS
V(gd)$name <- vdc
# PLOT THE WEIGHTED DISPLACEMENT GRAPH
gd <- drop_isolated(graph = gd,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
# DROP LOOPS ONLY VERTICES AS WELL
gd <- drop_loops(graph = gd,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
# RESULTING CLEANED UP GRAPH SHOWING NONTRIVIAL MIGRATION
plot(gd,
layout = layout.fruchterman.reingold(gd,
niter = 200,
area = 2000*vcount(gd)),
vertex.color = V(gd)$color,
vertex.size = 9,
vertex.label = V(gd)$name,
vertex.label.color = "black",
vertex.label.font = 2,
vertex.label.cex = 0.7,
edge.width = 0.02*(E(gd)$weight),
edge.arrow.size = 0.7,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Weighted Abstract Nepal Displacement Network Flow (VDC Level)")
legend("topright",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# SELECT THE COORDINATES
gd_coords <- koords[which(vdc %in% V(gd)$name),]
plot(gd,
layout = gd_coords,
vertex.color = V(gd)$color,
vertex.size = 4,
vertex.label = V(gd)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.6,
edge.width = 0.2*sqrt(E(gd)$weight),
edge.arrow.size = 0.2,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Weighted Nepal Displacement Geo-Network Flow (VDC Level)")
legend("topright",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# DISPLAY THE LARGEST CLUSTER (GIANT COMPONENT):
gd_c <- giant_comp(graph = gd,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
# PLOT THE WEIGHTED DISPLACEMENT GRAPH
gd_c_coords <- koords[which(V(gd)$name %in% V(gd_c)$name),]
plot(gd_c,
layout = gd_c_coords,
vertex.color = V(gd_c)$color,
vertex.size = 5,
vertex.label = V(gd_c)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.7,
edge.width = 0.2*sqrt(E(gd_c)$weight),
edge.arrow.size = 0.5,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Weighted Nepal Displacement Network Flow (Giant Component)")
legend("top",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# PLOT THE GIANT COMPONENT OF THE ABSTRACT GRAPH
plot(gd_c,
layout = layout.fruchterman.reingold(gd_c,
niter = 200,
area = 2000*vcount(gd_c)),
vertex.color = V(gd_c)$color,
vertex.size = 7,
vertex.label = V(gd_c)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 1,
edge.width = 0.2*sqrt(E(gd_c)$weight),
edge.arrow.size = 0.7,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Weighted Nepal Displacement Network Flow (Giant Component)")
legend("topleft",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# EDGE-FILTRATION BY EDGE WEIGHT OF THE WEIGHTED DISPLACEMENT GRAPH: CUT-OFF = 50% quantile
cut50 <- quantile(as.vector(dtm[dtm>0]),0.50)
gd <- graph.adjacency(dtm,
mode = "directed",
weighted = TRUE)
V(gd)$name <- vdc
V(gd)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))
+ length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gd)$color[k] <- "green"
}
}
gd_f <- filter(cutoff = cut50,
edge_matrix = dtm,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
gd_f <- drop_loops(graph = gd_f,
vertex_colors = V(gd_f)$color,
vertex_names = V(gd_f)$name)
gd_f_coords <- koords[which(vdc %in% V(gd_f)$name),]
# PLOT THE FILTRATION OF THE GEO GRAPH
plot(gd_f,
layout = gd_f_coords,
vertex.color = V(gd_f)$color,
vertex.size = 5,
vertex.label = V(gd_f)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.7,
edge.width = 0.2*sqrt(E(gd_f)$weight),
edge.arrow.size = 0.5,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Filtered Abstract Nepal Displacement Network (Cut-Off = 50%))")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
# PLOT THE FILTRATION OF THE ABSTRACT GRAPH
plot(gd_f,
layout = layout.fruchterman.reingold(gd_f,
niter = 200,
area = 2000*vcount(gd_f)),
vertex.color = V(gd_f)$color,
vertex.size = 10,
vertex.label = V(gd_f)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 1.2,
edge.width = 0.4*sqrt(E(gd_f)$weight),
edge.arrow.size = 01,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Filtered Nepal Displacement Geo-Network (Cut-Off = 50%))")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
# EDGE-FILTRATION BY EDGE WEIGHT OF THE WEIGHTED DISPLACEMENT GRAPH: CUT-OFF = 75% quantile
cut75 <- quantile(as.vector(dtm[dtm>0]),0.75)
gd <- graph.adjacency(dtm,
mode = "directed",
weighted = TRUE)
V(gd)$name <- vdc
V(gd)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))
+ length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gd)$color[k] <- "green"
}
}
gd_f <- filter(cutoff = cut75,
edge_matrix = dtm,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
gd_f <- drop_loops(graph = gd_f,
vertex_colors = V(gd_f)$color,
vertex_names = V(gd_f)$name)
gd_f_coords <- koords[which(vdc %in% V(gd_f)$name),]
# PLOT THE FILTRATION OF THE ABSTRACT GRAPH
plot(gd_f,
layout = layout.fruchterman.reingold(gd_f,
niter = 200,
area = 2000*vcount(gd_f)),
vertex.color = V(gd_f)$color,
vertex.size = 14,
vertex.label = V(gd_f)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 1.4,
edge.width = 0.05*(E(gd_f)$weight),
edge.arrow.size = 1.4,
edge.curved = TRUE,edge.color = gray.colors(1),
main = "Filtered Abstract Nepal Displacement Network (Cut-Off = 75%))")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
# PLOT THE FILTRATION OF THE GEO GRAPH
plot(gd_f,
layout = layout.fruchterman.reingold(gd_f,
niter = 200,
area = 2000*vcount(gd_f)),
vertex.color = V(gd_f)$color,
vertex.size = 14,
vertex.label = V(gd_f)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 1.4,
edge.width = 0.05*(E(gd_f)$weight),
edge.arrow.size = 1.4,
edge.curved = TRUE,edge.color = gray.colors(1),
main = "Filtered Nepal Displacement Geo-Network (Cut-Off = 75%))")
legend("topright",
c("Origins of Displacement","Destinations of Displacement"),
fill = c("green","SkyBlue2"),
bty = "n")
#
#
#
#
#
#
#
# ANALYSIS OF THE VDC NETWORK ITSELF
#
#
#
#
#
#
#
# DEFINE THE WEIGHTED DISPLACEMENT GRAPH
gd <- graph.adjacency(dtm,
mode = "directed",
weighted = TRUE)
# SET VERTEX COLORS
V(gd)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))+
length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gd)$color[k] <- "green"
}
}
# SET THE VERTEX LABELS
V(gd)$name <- vdc
# PLOT THE WEIGHTED DISPLACEMENT GRAPH
gd <- drop_isolated(graph = gd,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
# DROP LOOPS ONLY VERTICES AS WELL
gd <- drop_loops(graph = gd,
vertex_colors = V(gd)$color,
vertex_names = V(gd)$name)
# SELECT THE COORDINATES
gd_coords <- koords[which(vdc %in% V(gd)$name),]
plot(gd,
layout = gd_coords,
vertex.color = V(gd)$color,
vertex.size = 4,
vertex.label = V(gd)$name,
vertex.label.color = "black",
vertex.label.font = 1,
vertex.label.cex = 0.5,
edge.width = 0.15*sqrt(E(gd)$weight),
edge.arrow.size = 0.5,
edge.curved = TRUE,
edge.color = gray.colors(1),
main = "Weighted Nepal Displacement Network Flow (VDC Level)")
legend("topright",
c("Displacement Origin","Displacement Destination"),
fill = c("green","SkyBlue2"),
bty = "n")
# REGENERATE THE GRAPH AGAIN
gd <- graph.adjacency(dtm,
mode = "directed",
weighted = TRUE)
# SET VERTEX COLORS
V(gd)$color <- rep("SkyBlue2",length(vdc))
for (k in 1:length(vdc)){
o_count <- length(which(dt_data$idp_origin_vdc == vdc[k]))+
length(which(dt_data$idp2_origin_vdc == vdc[k]))
d_count <- length(which(dt_data$vdc == vdc[k]))
if(o_count>d_count){
V(gd)$color[k] <- "green"
}
}
# SET THE VERTEX LABELS
V(gd)$name <- vdc