anonymous-submission-acl2025 commited on
Commit
f69cee1
·
1 Parent(s): 8deff36
Files changed (33) hide show
  1. 27/paper.pdf +3 -0
  2. 27/replication_package/4-20-20_HH-KNearest_DeID_demed.RDS +3 -0
  3. 27/replication_package/4-20-20_Network-KNearest_DeID_demed.RDS +3 -0
  4. 27/replication_package/4-20-20_deid_nearestK.csv +3 -0
  5. 27/replication_package/4-21-20_1000x_sample_nearest.RDS +3 -0
  6. 27/replication_package/BBMP_ward_censusdata_2-12-19.RDS +3 -0
  7. 27/replication_package/Balance-tables_histograms_script.R +100 -0
  8. 27/replication_package/Bangalore_District_censusdata_2-25-19.RDS +3 -0
  9. 27/replication_package/Bihar_District_censusdata_2-25-19.RDS +3 -0
  10. 27/replication_package/Bihar_district_censusdata_2-22-19.RDS +3 -0
  11. 27/replication_package/Codebook.pdf +3 -0
  12. 27/replication_package/Figure3_4-28-20.R +64 -0
  13. 27/replication_package/Figure4_script.R +464 -0
  14. 27/replication_package/Figure5_script.R +485 -0
  15. 27/replication_package/Figure6_script.R +239 -0
  16. 27/replication_package/Figure_A5_script.R +188 -0
  17. 27/replication_package/Jaipur_District_censusdata_2-25-19.RDS +3 -0
  18. 27/replication_package/Jaipur_ward_censusdata_2-14-19.RDS +3 -0
  19. 27/replication_package/Karnataka_district_censusdata_2-22-19.RDS +3 -0
  20. 27/replication_package/Network-sample-calculations_script.R +271 -0
  21. 27/replication_package/Rajastan_district_censusdata_2-22-19.RDS +3 -0
  22. 27/replication_package/Results_List.xlsx +3 -0
  23. 27/replication_package/Table3_script.R +202 -0
  24. 27/replication_package/Table4_script.R +125 -0
  25. 27/replication_package/Table5_script.R +40 -0
  26. 27/replication_package/Table6_script.R +64 -0
  27. 27/replication_package/Table_1-2.R +157 -0
  28. 27/replication_package/india_pop_4-20-20.csv +3 -0
  29. 27/replication_package/india_sc_4-20-20.csv +3 -0
  30. 27/replication_package/map_dataset_4-28-20.RDS +3 -0
  31. 27/replication_package/map_out_4-28-20.RDS +3 -0
  32. 27/replication_package/readme.txt +3 -0
  33. 27/should_reproduce.txt +3 -0
27/paper.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36ea63a459ee4c4df540cbe455343dba765a05886e4cb8b7ea60fbb74800aaf8
3
+ size 802268
27/replication_package/4-20-20_HH-KNearest_DeID_demed.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6c68be737680e952b7cb15a9906f58fa329091e3fcc79835bd01fab18d47208
3
+ size 209454
27/replication_package/4-20-20_Network-KNearest_DeID_demed.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:128e222bd10684630b70f86a54c6a57160ca7dccefdcefc64fd8bf5a4543a0f5
3
+ size 83793
27/replication_package/4-20-20_deid_nearestK.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:06e21ef837882357b7cda548efee529d5031fd4b5d576fde9b8dacfa648577b1
3
+ size 1756501
27/replication_package/4-21-20_1000x_sample_nearest.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:37b2546534ebf83157b79536de8750ca2d6bc231036a6ff462d2c3d2aa552981
3
+ size 2649724
27/replication_package/BBMP_ward_censusdata_2-12-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7fbd4a74991019b35dae8455fa2ac90b427a0088fd30949177f4a11043c37f2
3
+ size 19670
27/replication_package/Balance-tables_histograms_script.R ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Create balance tables and histograms for Appendix
2
+
3
+ #package installation
4
+ # install.packages('plyr')
5
+ # install.packages('dplyr')
6
+ # install.packages('tidyr')
7
+ # install.packages('ggplot2')
8
+ # install.packages('lmtest')
9
+ # install.packages('multiwayvcov')
10
+ # install.packages('stargazer')
11
+
12
+ rm(list=ls())
13
+ library(plyr);library(dplyr, warn.conflicts = F)
14
+ library(tidyr)
15
+ library(ggplot2)
16
+ suppressMessages( library(lmtest) )
17
+ suppressMessages( library(multiwayvcov) )
18
+ suppressMessages(library(stargazer))
19
+
20
+ s = function(x){summary(factor(x))}
21
+
22
+ #setwd() #set working directory
23
+ dir.create(paste0(getwd(), '/Output/'))
24
+ dir.create(paste0(getwd(), '/Output/Balance-tables_histograms/'))
25
+ path0 = paste0(getwd(), '/Output/Balance-tables_histograms/', Sys.Date(),'/') #Directory for output files
26
+ dir.create(path0)
27
+
28
+ A = readRDS('4-20-20_HH-KNearest_DeID_demed.RDS') #
29
+ ##############################################################################################################################
30
+ #Balance table
31
+
32
+ A = A[A$A.A7_Area.Neighborhood %in% names(which(table(A$A.A7_Area.Neighborhood) >= 30)),]#DROP PLACES WITH <30 OBSERVATIONS
33
+
34
+ CandConjoint = A %>% filter(Wave %in% c('Bangalore 2016','Jai-Pat 2015'), is.na(L.Candidate_Question_1) == F) #just the people in the analysis.
35
+ CandConjoint$HiSeg = CandConjoint$Nearest10_OwnReligion == 10
36
+ CandConjoint$HiSeg_DeMed = CandConjoint$DeMedNearest10_OwnReligion >= 0
37
+ CandConjoint$LoSeg_DeMed = CandConjoint$DeMedNearest10_OwnReligion < 0
38
+
39
+ CandConjoint$LowCaste = CandConjoint$C.C8_Caste == 'SC/ST/RM'
40
+
41
+ CandConjoint$Muslim = CandConjoint$C.C6_Religion == 'Muslim'
42
+
43
+ CandConjoint$Male = CandConjoint$C.C5_Gender == 1
44
+
45
+ CandConjoint$Migrant = CandConjoint$C.C14_Permanent.Residence.of.Jaipur. == 0
46
+
47
+ CandConjoint$Jaipur = CandConjoint$City == 'Jaipur'
48
+ CandConjoint$Patna = CandConjoint$City == 'Patna'
49
+
50
+ CandConjoint$C.C4_Age = as.numeric(as.character(CandConjoint$C.C4_Age))
51
+
52
+ bal.vars = c('AssetSum','LowCaste','Muslim','Male','C.C4_Age', 'Migrant','Jaipur','Patna')
53
+ bal.table = data.frame('Segregated' = apply(CandConjoint[CandConjoint$HiSeg,bal.vars],2,function(x){mean(x,na.rm=T)}),
54
+ 'Integrated' = apply(CandConjoint[!CandConjoint$HiSeg,bal.vars],2,function(x){mean(x,na.rm=T)}),
55
+ 'p' = apply(CandConjoint[,bal.vars],2,function(x){t.test(x[CandConjoint$HiSeg],
56
+ x[!CandConjoint$HiSeg])[['p.value']]}) ) %>%
57
+ round(2)
58
+ bal.table = rbind(bal.table, data.frame('Segregated' = sum(CandConjoint$HiSeg == 1, na.rm = T),
59
+ 'Integrated' = sum(CandConjoint$HiSeg == 0, na.rm = T), 'p' = ''))
60
+ row.names(bal.table) = c('Asset Index','Low Caste','Muslim','Male','Age','Migrant','Jaipur','Patna','n')
61
+
62
+ out = stargazer(bal.table, summary = F, digits = 2,
63
+ title = 'Balance Table, Segregated vs. Integrated',
64
+ label = 'table:Nearest10Religion_Balance')
65
+ writeLines(out,con = paste0(path0,'Nearest10Religion_Balance.tex'));rm(out, bal.vars,bal.table)
66
+
67
+ #De-medianed
68
+ bal.vars = c('AssetSum','LowCaste','Muslim','Male','C.C4_Age', 'Migrant','Jaipur','Patna')
69
+ bal.table = data.frame('Segregated' = apply(CandConjoint[CandConjoint$HiSeg_DeMed,bal.vars],2,function(x){mean(x,na.rm=T)}),
70
+ 'Integrated' = apply(CandConjoint[CandConjoint$LoSeg_DeMed,bal.vars],2,function(x){mean(x,na.rm=T)}),
71
+ 'p' = apply(CandConjoint[,bal.vars],2,function(x){t.test(x[CandConjoint$HiSeg_DeMed],
72
+ x[CandConjoint$LoSeg_DeMed])[['p.value']]}) ) %>%
73
+ round(2)
74
+ bal.table = rbind(bal.table, data.frame('Segregated' = sum(CandConjoint$HiSeg_DeMed == 1, na.rm = T),
75
+ 'Integrated' = sum(CandConjoint$LoSeg_DeMed == 1, na.rm = T), 'p' = ''))
76
+ row.names(bal.table) = c('Asset Index','Low Caste','Muslim','Male','Age','Migrant','Jaipur','Patna','n')
77
+ bal.table
78
+
79
+ out = stargazer(bal.table, summary = F, digits = 2,
80
+ title = 'Balance Table, Segregated vs. Integrated (De-Medianed)',
81
+ label = 'table:Nearest10Religion_Balance_DeMed')
82
+ writeLines(out,con = paste0(path0,'DeMed_Nearest10Religion_Balance.tex'));rm(out, bal.vars,bal.table)
83
+
84
+ ########################################################################################
85
+ #Histograms
86
+
87
+ #Nearest 10 religion, full sample
88
+ ggplot(data=CandConjoint, aes(CandConjoint$Nearest10_OwnReligion)) + geom_bar(aes(y = (..count..)/sum(..count..))) + theme_minimal() +
89
+ labs(x = '10-nearest same religion', y = 'Proportion') + theme(axis.title=element_text(size=14),
90
+ axis.text = element_text(size = 12)) +
91
+ ggtitle('10-nearest same religion, Full sample') + theme(plot.title = element_text(hjust = 0.5, size = 16))
92
+ ggsave(filename = paste0(path0,'/Nearest10SameReligion.jpg'), height = 150, width = 150, units = 'mm')
93
+
94
+ #De-Medianned Nearest 10 religion, full sample
95
+ ggplot(data=CandConjoint, aes(CandConjoint$DeMedNearest10_OwnReligion)) + geom_bar(aes(y = (..count..)/sum(..count..))) + theme_minimal() +
96
+ labs(x = '10-nearest same religion (de-medianed)', y = 'Proportion') + theme(axis.title=element_text(size=14),
97
+ axis.text = element_text(size = 12)) +
98
+ ggtitle('De-Medianed 10-nearest same religion,\n Full sample') + theme(plot.title = element_text(hjust = 0.5, size = 16))
99
+ ggsave(filename = paste0(path0,'/DeMedNearest10SameReligion.jpg'), height = 150, width = 150, units = 'mm')
100
+
27/replication_package/Bangalore_District_censusdata_2-25-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d80ad2b97f953a39c8e6098a415378b7f8516afb3e44e14ed9624e897e97a6c1
3
+ size 942
27/replication_package/Bihar_District_censusdata_2-25-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:550499afae38697114545e0198444b2b6f7d293a2f517ebf323e8a0db62464d7
3
+ size 3255
27/replication_package/Bihar_district_censusdata_2-22-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75e41ff2628989212cf1a0b11044929b95d1537fd5805a8042a701de18be7f3e
3
+ size 5381
27/replication_package/Codebook.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:30f58182e9aba29da74e3991a87df64da230c81bd09e1db90817c3aaac55793f
3
+ size 172554
27/replication_package/Figure3_4-28-20.R ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #replicate figure 3
2
+ rm(list=ls())
3
+
4
+ #SET WORKING DIRECTORY
5
+
6
+ #devtools::install_github("dkahle/ggmap")
7
+ #devtools::install_github("hadley/ggplot2")
8
+ # install.packages('ggrepel')
9
+ # install.packages('plyr')
10
+ # install.packages('dplyr')
11
+ # install.packages('RColorBrewer')
12
+ # install.packages('reshape2')
13
+
14
+ library(ggplot2)
15
+ library(ggrepel)
16
+ library(plyr)
17
+ library(dplyr)
18
+ library(ggmap)
19
+ library(RColorBrewer)
20
+ library(reshape2)
21
+
22
+ s = function(x){summary(factor(x))}
23
+
24
+ #Read household survey data for mapping
25
+ #NOTE: Geolocation data (lat/long) has been offset by a random number to avoid compromising respondent anonymity
26
+ C = readRDS('map_dataset_4-28-20.RDS')
27
+ #Read map file
28
+ #NOTE: Geolocation data (lat/long) has been offset by a random number to avoid compromising respondent anonymity
29
+ map_19_cluster_offset = readRDS('map_out_4-28-20.RDS')
30
+
31
+ C = C %>% rename(Religion = C.C6_Religion.x)
32
+
33
+ #Create categories from KNN values
34
+ C$Cat = NA
35
+ C$Cat[which(C$Nearest10_OwnReligion <= 5)] = 0
36
+ C$Cat[which(C$Nearest10_OwnReligion > 5 & C$Nearest10_OwnReligion <= 8)] = 1
37
+ C$Cat[which(C$Nearest10_OwnReligion > 8)] = 2
38
+ C$Cat = factor(C$Cat)
39
+
40
+ #Switch order of data points for more attractive plot
41
+ D = C[seq(dim(C)[1],1),] #
42
+
43
+ #Set limits of scale bar
44
+ sc_x_lo = min(D$pseudo_lon) + (max(D$pseudo_lon) - min(D$pseudo_lon)) * (1/10)
45
+ sc_y = min(D$pseudo_lat) + (max(D$pseudo_lat) - min(D$pseudo_lat)) * (9.34/10)
46
+
47
+ #Create map
48
+ #Note again that lat-long values are PSEUDO, ie they have been offset by a random value to preserve respondent anonymity
49
+ ggmap(map_19_cluster_offset) + geom_point(data = D, aes(x=pseudo_lon, y=pseudo_lat, shape = Religion, fill=Cat),
50
+ size=6.7, alpha=1, color = 'white') +
51
+ geom_segment(aes(x=sc_x_lo, xend=sc_x_lo + 0.0005/1.0835,y=sc_y,yend=sc_y),color='white', size = 1.05) + #scale bar +
52
+ geom_text(aes(x=sc_x_lo + 0.00065,y=sc_y-0.0000),label='50 m',color='white', size = 6.5) + #scale bar label
53
+ theme(axis.line=element_blank(),axis.text.x=element_blank(), #suppress axes
54
+ axis.text.y=element_blank(),axis.ticks=element_blank(),
55
+ axis.title.x=element_blank(),
56
+ axis.title.y=element_blank(),
57
+ legend.title = element_text(size = 14),
58
+ legend.text = element_text(size = 12)) +
59
+ scale_fill_grey('10-nearest-\nneighbors', labels = c('(0,5]','(5,8]','(8,10]')) +
60
+ scale_shape_manual('Religion', values = c(24, 21), labels = c('Hindu','Muslim')) +
61
+ guides(fill=guide_legend(override.aes=list(shape=21)),
62
+ shape=guide_legend(override.aes=list(fill='black')))
63
+ ggsave(filename = 'figure3.png',
64
+ device = 'png', width = 6, height = 4, units = 'in', dpi = 'print')
27/replication_package/Figure4_script.R ADDED
@@ -0,0 +1,464 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #figure 4: Coethnic voting preferences in candidate conjoint experiment,
2
+ #comparing high- to low-exposure respondents.
3
+ #This also includes candidate-comparison results for Appendix
4
+
5
+ # install.packages('plyr')
6
+ # install.packages('dplyr')
7
+ # install.packages('tidyr')
8
+ # install.packages('ggplot2')
9
+ # install.packages('multiwayvcov')
10
+ # install.packages('lmtest')
11
+ # install.packages('stargazer')
12
+
13
+ rm(list=ls())
14
+ #setwd() #set working directory
15
+ dir.create(paste0(getwd(), '/Output/'))
16
+ dir.create(paste0(getwd(), '/Output/Figure_4/'))
17
+ path0 = paste0(getwd(), '/Output/Figure_4/', Sys.Date(),'/') #Directory for output files
18
+ dir.create(path0)
19
+
20
+ s = function(x){summary(factor(x))}
21
+ Num = function(x){as.numeric(as.factor(x))}
22
+
23
+ library(plyr);library(dplyr, warn.conflicts = FALSE)
24
+ library(tidyr);library(ggplot2)
25
+ suppressMessages(library(multiwayvcov, warn.conflicts = F))
26
+ suppressMessages(library(lmtest, warn.conflicts = F))
27
+ suppressMessages(library(stargazer))
28
+
29
+ A = readRDS('4-20-20_HH-KNearest_DeID_demed.RDS')
30
+
31
+ #Data cleaning
32
+ Aprime = A[A$A.A7_Area.Neighborhood %in% names(which(table(A$A.A7_Area.Neighborhood) >= 30)),]#DROP PLACES WITH <30 OBSERVATIONS
33
+ Aprime = Aprime[which(!is.na(Aprime$A.A7_Area.Neighborhood)),] #drop NA neighborhoods
34
+ Aprime = Aprime[which(Aprime$A.A7_Area.Neighborhood != 'Bangalore NA'),]
35
+
36
+ Q = Aprime
37
+
38
+ ###################################################################################################
39
+ #Conjoint analysis
40
+ #Rename variables
41
+ Q = Q %>% dplyr::rename(Q1A1 = L.Candidate_Random_A1_Candidate.Preferance, #first question, candidate 1, characteristic A
42
+ Q1A2 = L.Candidate_Random_A2_Candidate.Preferance, #first question, candidate 2, characteristic A
43
+ Q1B1 = L.Candidate_Random_B1_Candidate.Preferance, #first question, candidate 1, characteristic B
44
+ Q1B2 = L.Candidate_Random_B2_Candidate.Preferance, #first question, candidate 2, characteristic B
45
+ Q2A1 = L.Candidate_Random_A3, #second question, candidate 1, characteristic A
46
+ Q2A2 = L.Candidate_Random_A4, #second question, candidate 2, characteristic A
47
+ Q2B1 = L.Candidate_Random_B3, #second question, candidate 1, characteristic B
48
+ Q2B2 = L.Candidate_Random_B4, #second question, candidate 2, characteristic B
49
+ Q3A1 = L.Candidate_Random_A5, #third question, candidate 1, characteristic A
50
+ Q3A2 = L.Candidate_Random_A6, #third question, candidate 2, characteristic A
51
+ Q3B1 = L.Candidate_Random_B5, #third question, candidate 1, characteristic B
52
+ Q3B2 = L.Candidate_Random_B6, #third question, candidate 2, characteristic B
53
+ Q1 = L.Candidate_Question_1, #first question, choose candidate 1 or 2
54
+ Q2 = L.Candidate_Question_2, #first question, choose candidate 1 or 2
55
+ Q3 = L.Candidate_Question_3 #first question, choose candidate 1 or 2
56
+ )
57
+
58
+ #rearrange so one row is one conjoint observation. 3x as many rows as A
59
+ #new variables: A1, B1 are two traits for candidate 1; similar for 2; and y is responent's choice between candidates
60
+ B = Q %>% unite('Q1', matches('Q1')) %>% unite('Q2', matches('Q2')) %>% unite('Q3', matches('Q3')) %>%
61
+ gather(Question, b, starts_with('Q')) %>% arrange(X) %>% separate( 'b', c('A1','A2','B1','B2','y') )
62
+
63
+ B = B %>% filter(! (A1 == A2 & B1 == B2)) #drop observations where candidates have same profile
64
+
65
+ #make new data frame wheer each row is one PROFILE, ie each question becomes two rows (one for each candidate)
66
+ #New variables: A1, A2 are combined as A: trait A for either candidate
67
+ C = B %>% unite('AB1',c(A1,B1)) %>% unite('AB2',c(A2,B2)) %>% gather(Neighbor, AB, c(AB1, AB2)) %>%
68
+ arrange(X) %>% separate('AB', c('A','B')) %>%
69
+ mutate(Neighbor = as.numeric(mapvalues(Neighbor, from = c('AB1', 'AB2'), to = c(1,2))))
70
+
71
+ #function to make dummies for trait levels
72
+ ModFn = function(x,f){
73
+ data.frame(x, model.matrix(as.formula(f), data=x)) }
74
+
75
+ C = C %>% ModFn('~ A - 1') %>% ModFn('~ B - 1') #function to make dummies for trait levels
76
+ C = C %>% mutate(Y = y == Neighbor) #1 when that candidate is picked
77
+ C = C %>% dplyr::rename(A_0 = A0, #rename variables to be consistent with earlier version of code
78
+ A_1 = A1,
79
+ A_2 = A2,
80
+ A_3 = A3,
81
+ B_0 = B0,
82
+ B_1 = B1,
83
+ B_2 = B2,
84
+ B_3 = B3,
85
+ B_4 = B4)
86
+
87
+ B = C; rm(C) #rename
88
+ ###########################################################################################
89
+
90
+ ###########################################################################################
91
+ #Setup for subgroup analysis
92
+
93
+ #regression formula
94
+ form1 = as.formula(paste0('Y ~ ',
95
+ paste(strsplit('A_0 A_1 A_2 A_3 B_0 B_1 B_2 B_3 B_4',
96
+ split = ' ')[[1]], collapse = ' + ' )))
97
+
98
+ #function to make data frame from regression results
99
+ DF_C = function(l_m, id){
100
+ l_m_pl = data.frame(Parameter = rownames(l_m[-1,])) %>% #-1 drops intercept
101
+ mutate(Coef = l_m[-1,1]) %>%
102
+ mutate(Lo = Coef - 1.96 * l_m[-1,2]) %>%
103
+ mutate(Hi = Coef + 1.96 * l_m[-1,2]) %>%
104
+ mutate(ID = id) %>%
105
+ rbind(data.frame(Parameter = c('A_3','B_4'),
106
+ Coef = c(0,0), Lo = c(0,0), Hi = c(0,0), ID = c(id, id)) ) %>%
107
+ mutate(Parameter = as.character(Parameter)) %>%
108
+ arrange(Parameter)
109
+ }
110
+ ###########################################################################################
111
+
112
+ ###########################################################################################
113
+ #Make function to extract p- and z-values for each choice of k
114
+
115
+ calc_p_relig = function(k, Dat){
116
+ var <- paste0('Nearest',k,'_OwnReligion')
117
+ if(median(Dat[,var],na.rm=T)==k){ #split the sample at the median value of KNN (or at K - 1, if the median is K)
118
+ var_break <- k-1
119
+ }else{var_break = floor(median(Dat[,var],na.rm=T))}
120
+ dat_lo = Dat[ which(Dat[,var] <= var_break ) ,] # low knn group
121
+ dat_hi = Dat[ which(Dat[,var] > var_break ) ,] #high kn group
122
+ if(nrow(dat_lo) == 0 | nrow(dat_hi) == 0){p_a2 = NA #set p-value to NA if there are no respondents in high- or low-exposure group
123
+ }else{
124
+ lm_lo = lm(form1, data = dat_lo ) #run regression on lo-knn group
125
+ lm_hi = lm(form1, data = dat_hi ) #run regression on hi-knn group
126
+ lm_clus_lo = coeftest(lm_lo, cluster.vcov(lm_lo, dat_lo[,c('X','A.A7_Area.Neighborhood')])) #calculate cluster-robust std errors
127
+ lm_clus_hi = coeftest(lm_hi, cluster.vcov(lm_hi, dat_hi[,c('X','A.A7_Area.Neighborhood')])) #calculate cluster-robust std errors
128
+ #z-test for difference in coefficients between lo- and hi-exposure groups
129
+ z_a2 = (lm_clus_lo['A_2','Estimate'] - lm_clus_hi['A_2','Estimate']) / sqrt(lm_clus_lo['A_2','Std. Error']^2 + lm_clus_hi['A_2','Std. Error']^2)
130
+ #calculate p-value, based on outcome of z-test above
131
+ p_a2 = 2 * pnorm( abs(z_a2), mean = 0, sd = 1, lower.tail = F)
132
+ coef_lo = lm_clus_lo['A_2','Estimate']; coef_hi = lm_clus_hi['A_2','Estimate'] #extract coefficients for inclusion in output data frame
133
+ sd_lo = lm_clus_lo['A_2','Std. Error']; sd_hi = lm_clus_hi['A_2','Std. Error'] #extract se's for inclusion in output data frame
134
+ #output data frame with coefficients, std errors, and p-value for difference in coefficients between hi- and lo-exposure groups
135
+ return(data.frame(coef_lo, coef_hi, sd_lo, sd_hi, p_a2)) }
136
+ }
137
+
138
+ #Calculate results for k in 1:30
139
+ relig_results = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B)) %>% t() %>% data.frame() %>%
140
+ mutate(k = 1:30,
141
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
142
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
143
+ dif_lobd = dif - 1.96*dif_sd,
144
+ dif_hibd = dif + 1.96*dif_sd
145
+ )
146
+
147
+ #Create data frame for plotting
148
+ rrc = relig_results[seq(from = 2, to = 30, by = 3),] %>%
149
+ rename(Low = coef_lo, High = coef_hi) %>%
150
+ select(-starts_with('dif')) %>%
151
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
152
+ sd_lo = as.numeric(sd_lo),
153
+ sd_hi = as.numeric(sd_hi),
154
+ p_a2 = as.numeric(p_a2),
155
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
156
+ lo = Coef - 1.96*sd,
157
+ hi = Coef + 1.96*sd,
158
+ sig = factor(p_a2 < 0.05))
159
+
160
+ #################################################################
161
+ #Figure 4: main result
162
+ ggplot(data = rrc, aes(x = k, y = Coef)) +
163
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
164
+ position = position_dodge(width = 0.9)) +
165
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
166
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
167
+ labs(shape = 'Exposure') +
168
+ theme_minimal() + ylab('Coethnicity Coefficients') +
169
+ theme(text = element_text(size = 16),
170
+ plot.title = element_text(hjust = 0.5)) +
171
+ ggtitle('k-Nearest Own Religion') +
172
+ guides(shape = guide_legend(order = 1),
173
+ alpha = guide_legend(order = 0))
174
+
175
+ ggsave(filename = paste0(path0, 'Fig_4_k-z_coefficients.png'), height = 150, width = 150, units = 'mm')
176
+
177
+ #################################################################
178
+
179
+ #################################################################
180
+ #Main result in table form
181
+ #TABLE A4
182
+ relig_results_tab = relig_results %>% apply(2, function(x) as.numeric(x)) %>% data.frame() %>%
183
+ select(k, coef_lo, sd_lo, coef_hi, sd_hi, p_a2) %>%
184
+ rename(Coef_LowSeg = coef_lo, SD_LowSeg = sd_lo, Coef_HiSeg = coef_hi, SD_HiSeg = sd_hi, p = p_a2) %>%
185
+ round(3)
186
+ out = stargazer(relig_results_tab, summary = F, rownames = F,
187
+ title = 'Results for co-ethnicity attribute in candidate experiment compared between
188
+ high- and low-segregation subsamples, based on religious segregation.',
189
+ label = 'table:ReligResults')
190
+ writeLines(out,con = paste0(path0,'ReligResults.tex'));rm(out, relig_results_tab)
191
+
192
+ #################################################################################################################################################
193
+
194
+ #################################################################################################################################################
195
+ #De-medianed version
196
+
197
+ zpk_relig = function(data, Split, k){ # nearest k same religion
198
+ var = paste0('Nearest',k,'_OwnReligion')
199
+ VarLo = Split# - 1
200
+ VarHi = Split# - 1
201
+ lm_lo = lm( form1, data = data[which(data[,var] < VarLo),] )
202
+ lm_hi = lm( form1, data = data[which(data[,var] >= VarHi),] )
203
+ lm_lo_sum = coeftest(lm_lo, cluster.vcov(lm_lo, data[which(data[,var] < VarLo),c('X','A.A7_Area.Neighborhood')] ) )
204
+ lm_hi_sum = coeftest(lm_hi, cluster.vcov(lm_hi, data[which(data[,var] >= VarHi),c('X','A.A7_Area.Neighborhood')] ) )
205
+ diff = lm_lo_sum['A_2','Estimate'] - lm_hi_sum['A_2','Estimate']
206
+ z = (lm_lo_sum['A_2','Estimate'] - lm_hi_sum['A_2','Estimate']) / sqrt(lm_lo_sum['A_2','Std. Error']^2 + lm_hi_sum['A_2','Std. Error']^2)
207
+ p = (2*pnorm(abs(z), mean = 0, sd = 1, lower.tail = FALSE))
208
+ return(data.frame(diff = diff,z=z,p=p,split=Split, k = k))
209
+ }
210
+ zpk_demed_relig = function(data, Split, k){ #
211
+ var = paste0('DeMedNearest',k,'_OwnReligion')
212
+ VarLo = Split
213
+ VarHi = Split
214
+ lm_lo = lm( form1, data = data[which(data[,var] < VarLo),] )
215
+ lm_hi = lm( form1, data = data[which(data[,var] >= VarHi),] ) #
216
+ lm_lo_sum = coeftest(lm_lo, cluster.vcov(lm_lo, data[which(data[,var] < VarLo),c('X','A.A7_Area.Neighborhood')] ) )
217
+ lm_hi_sum = coeftest(lm_hi, cluster.vcov(lm_hi, data[which(data[,var] >= VarHi),c('X','A.A7_Area.Neighborhood')] ) ) #
218
+ diff = lm_lo_sum['A_2','Estimate'] - lm_hi_sum['A_2','Estimate']
219
+ z = (lm_lo_sum['A_2','Estimate'] - lm_hi_sum['A_2','Estimate']) / sqrt(lm_lo_sum['A_2','Std. Error']^2 + lm_hi_sum['A_2','Std. Error']^2)
220
+ p = (2*pnorm(abs(z), mean = 0, sd = 1, lower.tail = FALSE))
221
+ return(data.frame(diff = diff,z=z,p=p,split=Split, k = k))
222
+ }
223
+
224
+ varlist = as.list(seq(5,30,5))
225
+ NR_out = ldply(varlist, function(x) zpk_relig(data = B %>% filter(Wave %in% c( 'Bangalore 2016','Jai-Pat 2015') ),
226
+ Split = x,
227
+ k = as.character(x)) )
228
+
229
+ NR_out_demed = ldply(varlist, function(x) zpk_demed_relig(data = B %>% filter(Wave %in% c( 'Bangalore 2016','Jai-Pat 2015') ),
230
+ Split = 0,
231
+ k = as.character(x)) )
232
+
233
+ NR_out$se = NR_out$diff / NR_out$z
234
+ NR_out$lo = NR_out$diff - 1.96*NR_out$se
235
+ NR_out$hi = NR_out$diff + 1.96*NR_out$se
236
+ NR_out$id = 'k-Nearest Neighbors'
237
+
238
+ NR_out_demed$se = NR_out_demed$diff / NR_out_demed$z
239
+ NR_out_demed$lo = NR_out_demed$diff - 1.96*NR_out_demed$se
240
+ NR_out_demed$hi = NR_out_demed$diff + 1.96*NR_out_demed$se
241
+ NR_out_demed$id = 'De-Medianed k-Nearest Neighbors'
242
+
243
+ out = rbind(NR_out, NR_out_demed)
244
+
245
+ #FIGURE A4
246
+ ggplot(out, group = id, aes(x=k)) +
247
+ geom_point(aes(y = diff, color = id), position=position_dodge( width = 0.5 )) +
248
+ geom_errorbar(aes(ymin = lo, ymax = hi, group = id), position=position_dodge( width = 0.5 )) +
249
+ labs(x = 'k', y = 'Diff. of Coefficients (Lo - Hi Seg)') +
250
+ scale_color_discrete(name='Metric', labels=c('De-Med. K-Nearest', 'K-Nearest')) +
251
+ theme_minimal() + theme(text=element_text(size=16)) + theme(legend.title.align=0.5)
252
+ ggsave(filename = paste0(path0, 'demed-k-z-full.png'), height = 150, width = 150, units = 'mm')
253
+ #################################################################################################################################################
254
+
255
+ #################################################################################################################################################
256
+ #Plots for non-balanced attributes
257
+
258
+ relig_results_hin = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$C.C6_Religion == 'Hindu'),])) %>% t() %>% data.frame() %>%
259
+ mutate(k = 1:30,
260
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
261
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
262
+ dif_lobd = dif - 1.96*dif_sd,
263
+ dif_hibd = dif + 1.96*dif_sd
264
+ )
265
+ rrc_hin =
266
+ relig_results_hin[seq(from = 2, to = 30, by = 3),] %>%
267
+ rename(Low = coef_lo, High = coef_hi) %>%
268
+ select(-starts_with('dif')) %>%
269
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
270
+ sd_lo = as.numeric(sd_lo),
271
+ sd_hi = as.numeric(sd_hi),
272
+ p_a2 = as.numeric(p_a2),
273
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
274
+ lo = Coef - 1.96*sd,
275
+ hi = Coef + 1.96*sd,
276
+ sig = factor(p_a2 < 0.05))
277
+ #FIGURE A6
278
+ ggplot(data = rrc_hin, aes(x = k, y = Coef)) +
279
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
280
+ position = position_dodge(width = 0.9)) +
281
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) + #
282
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
283
+ labs(shape = 'Exposure') +
284
+ theme_minimal() + ylab('Coethnicity Coefficients') +
285
+ theme(text = element_text(size = 16),
286
+ plot.title = element_text(hjust = 0.5)) +
287
+ ggtitle('Nearest-k Own Religion (Hindu)') +
288
+ guides(shape = guide_legend(order = 1),
289
+ alpha = guide_legend(order = 0))
290
+ ggsave(filename = paste0(path0, 'k-z_coefficients_hindu.png'), height = 150, width = 150, units = 'mm')
291
+
292
+ relig_results_mus = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$C.C6_Religion == 'Muslim'),])) %>% t() %>% data.frame() %>%
293
+ mutate(k = 1:30,
294
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
295
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
296
+ dif_lobd = dif - 1.96*dif_sd,
297
+ dif_hibd = dif + 1.96*dif_sd
298
+ )
299
+
300
+ rrc_mus =
301
+ relig_results_mus[seq(from = 2, to = 30, by = 3),] %>%
302
+ rename(Low = coef_lo, High = coef_hi) %>%
303
+ select(-starts_with('dif')) %>%
304
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
305
+ sd_lo = as.numeric(sd_lo),
306
+ sd_hi = as.numeric(sd_hi),
307
+ p_a2 = as.numeric(p_a2),
308
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
309
+ lo = Coef - 1.96*sd,
310
+ hi = Coef + 1.96*sd,
311
+ sig = factor(p_a2 < 0.05))
312
+ #FIGURE A7
313
+ ggplot(data = rrc_mus, aes(x = k, y = Coef)) +
314
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
315
+ position = position_dodge(width = 0.9)) +
316
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
317
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
318
+ labs(shape = 'Exposure') +
319
+ theme_minimal() + ylab('Coethnicity Coefficients') +
320
+ theme(text = element_text(size = 16),
321
+ plot.title = element_text(hjust = 0.5)) +
322
+ ggtitle('Nearest-k Own Religion (Muslim)') +
323
+ guides(shape = guide_legend(order = 1),
324
+ alpha = guide_legend(order = 0))
325
+ ggsave(filename = paste0(path0, 'k-z_coefficients_muslim.png'), height = 150, width = 150, units = 'mm')
326
+
327
+ relig_results_hiassets = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$AssetSum >= 10),])) %>% t() %>% data.frame() %>%
328
+ mutate(k = 1:30,
329
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
330
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
331
+ dif_lobd = dif - 1.96*dif_sd,
332
+ dif_hibd = dif + 1.96*dif_sd
333
+ )
334
+
335
+ rrc_hiassets =
336
+ relig_results_hiassets[seq(from = 2, to = 30, by = 3),] %>%
337
+ rename(Low = coef_lo, High = coef_hi) %>%
338
+ select(-starts_with('dif')) %>%
339
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
340
+ sd_lo = as.numeric(sd_lo),
341
+ sd_hi = as.numeric(sd_hi),
342
+ p_a2 = as.numeric(p_a2),
343
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
344
+ lo = Coef - 1.96*sd,
345
+ hi = Coef + 1.96*sd,
346
+ sig = factor(p_a2 < 0.05))
347
+ #FIGURE A8
348
+ ggplot(data = rrc_hiassets, aes(x = k, y = Coef)) +
349
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
350
+ position = position_dodge(width = 0.9)) +
351
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
352
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
353
+ labs(shape = 'Exposure') +
354
+ theme_minimal() + ylab('Coethnicity Coefficients') +
355
+ theme(text = element_text(size = 16),
356
+ plot.title = element_text(hjust = 0.5)) +
357
+ ggtitle('Nearest-k Own Religion (Hi Assets)') +
358
+ guides(shape = guide_legend(order = 1),
359
+ alpha = guide_legend(order = 0))
360
+ ggsave(filename = paste0(path0, 'k-z_coefficients_hiassets.png'), height = 150, width = 150, units = 'mm')
361
+
362
+ relig_results_hicaste = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$C.C8_Caste == 'General/BC/OBC'),])) %>% t() %>% data.frame() %>%
363
+ mutate(k = 1:30,
364
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
365
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
366
+ dif_lobd = dif - 1.96*dif_sd,
367
+ dif_hibd = dif + 1.96*dif_sd
368
+ )
369
+
370
+ rrc_hicaste =
371
+ relig_results_hicaste[seq(from = 2, to = 30, by = 3),] %>%
372
+ rename(Low = coef_lo, High = coef_hi) %>%
373
+ select(-starts_with('dif')) %>%
374
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
375
+ sd_lo = as.numeric(sd_lo),
376
+ sd_hi = as.numeric(sd_hi),
377
+ p_a2 = as.numeric(p_a2),
378
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
379
+ lo = Coef - 1.96*sd,
380
+ hi = Coef + 1.96*sd,
381
+ sig = factor(p_a2 < 0.05))
382
+ ggplot(data = rrc_hicaste, aes(x = k, y = Coef)) +
383
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
384
+ position = position_dodge(width = 0.9)) +
385
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
386
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
387
+ labs(shape = 'Exposure') +
388
+ theme_minimal() + ylab('Coethnicity Coefficients') +
389
+ theme(text = element_text(size = 16),
390
+ plot.title = element_text(hjust = 0.5)) +
391
+ ggtitle('Nearest-k Own Religion (Hi Caste)') +
392
+ guides(shape = guide_legend(order = 1),
393
+ alpha = guide_legend(order = 0))
394
+ ggsave(filename = paste0(path0, 'k-z_coefficients_hicaste.png'), height = 150, width = 150, units = 'mm')
395
+
396
+ relig_results_jaipur = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$City == 'Jaipur'),])) %>% t() %>% data.frame() %>%
397
+ mutate(k = 1:30,
398
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
399
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
400
+ dif_lobd = dif - 1.96*dif_sd,
401
+ dif_hibd = dif + 1.96*dif_sd
402
+ )
403
+
404
+ rrc_jaipur =
405
+ relig_results_jaipur[seq(from = 2, to = 30, by = 3),] %>%
406
+ rename(Low = coef_lo, High = coef_hi) %>%
407
+ select(-starts_with('dif')) %>%
408
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
409
+ sd_lo = as.numeric(sd_lo),
410
+ sd_hi = as.numeric(sd_hi),
411
+ p_a2 = as.numeric(p_a2),
412
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
413
+ lo = Coef - 1.96*sd,
414
+ hi = Coef + 1.96*sd,
415
+ sig = factor(p_a2 < 0.05))
416
+ ggplot(data = rrc_jaipur, aes(x = k, y = Coef)) +
417
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
418
+ position = position_dodge(width = 0.9)) +
419
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
420
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
421
+ labs(shape = 'Exposure') +
422
+ theme_minimal() + ylab('Coethnicity Coefficients') +
423
+ theme(text = element_text(size = 16),
424
+ plot.title = element_text(hjust = 0.5)) +
425
+ ggtitle('Nearest-k Own Religion (Jaipur)') +
426
+ guides(shape = guide_legend(order = 1),
427
+ alpha = guide_legend(order = 0))
428
+ ggsave(filename = paste0(path0, 'k-z_coefficients_jaipur.png'), height = 150, width = 150, units = 'mm')
429
+
430
+ relig_results_patna = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$City == 'Patna'),])) %>% t() %>% data.frame() %>%
431
+ mutate(k = 1:30,
432
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
433
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
434
+ dif_lobd = dif - 1.96*dif_sd,
435
+ dif_hibd = dif + 1.96*dif_sd
436
+ )
437
+
438
+ rrc_patna =
439
+ relig_results_patna[seq(from = 2, to = 30, by = 3),] %>%
440
+ rename(Low = coef_lo, High = coef_hi) %>%
441
+ select(-starts_with('dif')) %>%
442
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
443
+ sd_lo = as.numeric(sd_lo),
444
+ sd_hi = as.numeric(sd_hi),
445
+ p_a2 = as.numeric(p_a2),
446
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
447
+ lo = Coef - 1.96*sd,
448
+ hi = Coef + 1.96*sd,
449
+ sig = factor(p_a2 < 0.05))
450
+ ggplot(data = rrc_patna, aes(x = k, y = Coef)) +
451
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
452
+ position = position_dodge(width = 0.9)) +
453
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
454
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
455
+ labs(shape = 'Exposure') +
456
+ theme_minimal() + ylab('Coethnicity Coefficients') +
457
+ theme(text = element_text(size = 16),
458
+ plot.title = element_text(hjust = 0.5)) +
459
+ ggtitle('Nearest-k Own Religion (Patna)') +
460
+ guides(shape = guide_legend(order = 1),
461
+ alpha = guide_legend(order = 0))
462
+ ggsave(filename = paste0(path0, 'k-z_coefficients_patna.png'), height = 150, width = 150, units = 'mm')
463
+
464
+ ############################################################################################
27/replication_package/Figure5_script.R ADDED
@@ -0,0 +1,485 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Figure 5
2
+ #Coethnic voting preferences in candidate conjoint experiment,
3
+ #comparing high- to low-exposure respondents in network dataset.
4
+
5
+ #install packages
6
+ # install.packages('plyr')
7
+ # install.packages('dplyr')
8
+ # install.packages('tidyr')
9
+ # install.packages('ggplot2')
10
+ # install.packages('multiwayvcov')
11
+ # install.packages('lmtest')
12
+ # install.packages('stargazer')
13
+
14
+ rm(list=ls())
15
+ #setwd() #set working directory
16
+ dir.create(paste0(getwd(), '/Output/'))
17
+ dir.create(paste0(getwd(), '/Output/Figure_5/'))
18
+ path0 = paste0(getwd(), '/Output/Figure_5/', Sys.Date(),'/') #Directory for output files
19
+ dir.create(path0)
20
+
21
+ library(plyr);library(dplyr, warn.conflicts = FALSE)
22
+ library(tidyr);library(ggplot2)
23
+ suppressMessages(library(multiwayvcov, warn.conflicts = F))
24
+ suppressMessages(library(lmtest, warn.conflicts = F))
25
+ suppressMessages(library(stargazer))
26
+
27
+ s = function(x){summary(factor(x))}
28
+
29
+ A = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
30
+ ########################################################################################################################
31
+ #Balance tables
32
+
33
+ A$HiSeg = A$Nearest10_SameRel == 10
34
+ A$HiSeg_DeMed = A$DeMedNearest10_SameRel >= 0
35
+ A$LoSeg_DeMed = A$DeMedNearest10_SameRel < 0
36
+
37
+ A$LowCaste = A$C.C8_Caste == 'SC/ST'
38
+
39
+ A$Muslim = A$C.C6 == 'Muslim'
40
+
41
+ A$Male = A$C.C5_Gender == 'M'
42
+
43
+ A$Migrant = A$C.C14_Live.in.Jaipur. == 0
44
+
45
+ A$Jaipur = A$City == 'Jaipur'
46
+ A$Patna = A$City == 'Patna'
47
+
48
+ A$C.C4_Age = as.numeric(as.character(A$C.C4_Age))
49
+
50
+ A$Income1 = mapvalues(A$F.F1_Monthly.Income, from = c(-888, 0, 888, 999), to = c(NA, NA, NA, NA)) / 1000
51
+ A$Income2 = A$Income1 > 10000
52
+
53
+ #Non-de-medianed
54
+ bal.vars = c('Income1','LowCaste','Muslim','Male','C.C4_Age', 'Migrant','Jaipur')
55
+ bal.table = data.frame('Segregated' = apply(A[A$HiSeg,bal.vars],2,function(x){mean(x,na.rm=T)}),
56
+ 'Integrated' = apply(A[!A$HiSeg,bal.vars],2,function(x){mean(x,na.rm=T)}),
57
+ 'p' = apply(A[,bal.vars],2,function(x){t.test(x[A$HiSeg],
58
+ x[!A$HiSeg])[['p.value']]}) ) %>%
59
+ round(2)
60
+ bal.table = rbind(bal.table, data.frame('Segregated' = sum(A$HiSeg == 1, na.rm = T),
61
+ 'Integrated' = sum(A$HiSeg == 0, na.rm = T), 'p' = ''))
62
+ row.names(bal.table) = c('Income (k INR/mo.)','Low Caste','Muslim','Male','Age','Migrant','Jaipur','n')
63
+ bal.table
64
+
65
+ out = stargazer(bal.table, summary = F, digits = 2,
66
+ title = 'Balance Table, Segregated vs. Integrated',
67
+ label = 'table:Nearest10Religion_Balance')
68
+ writeLines(out,con = paste0(path0,'Network_Nearest10Religion_Balance.tex'));rm(out, bal.vars,bal.table)
69
+ ########################################################################################################################
70
+ #Begin conjoint analysis
71
+
72
+ Q = A
73
+
74
+ #Rename variables
75
+
76
+ Q = Q %>% dplyr::rename(Q1A1 = I.Neta_Random_A1, #first question, candidate 1, characteristic A
77
+ Q1A2 = I.Neta_Random_A2, #first question, candidate 2, characteristic A
78
+ Q1B1 = I.Neta_Random_B1, #first question, candidate 1, characteristic B
79
+ Q1B2 = I.Neta_Random_B2, #first question, candidate 2, characteristic B
80
+ Q2A1 = I.Neta_Random_A3, #second question, candidate 1, characteristic A
81
+ Q2A2 = I.Neta_Random_A4, #second question, candidate 2, characteristic A
82
+ Q2B1 = I.Neta_Random_B3, #second question, candidate 1, characteristic B
83
+ Q2B2 = I.Neta_Random_B4, #second question, candidate 2, characteristic B
84
+ Q3A1 = I.Neta_Random_A5, #third question, candidate 1, characteristic A
85
+ Q3A2 = I.Neta_Random_A6, #third question, candidate 2, characteristic A
86
+ Q3B1 = I.Neta_Random_B5, #third question, candidate 1, characteristic B
87
+ Q3B2 = I.Neta_Random_B6, #third question, candidate 2, characteristic B
88
+ Q1 = I.Neta_Question_1, #first question, choose candidate 1 or 2
89
+ Q2 = I.Neta_Question_2, #first question, choose candidate 1 or 2
90
+ Q3 = I.Neta_Question_3 #first question, choose candidate 1 or 2
91
+ )
92
+
93
+ #rearrange so one row is one conjoint observation. 3x as many rows as A
94
+ #new variables: A1, B1 are two traits for candidate 1; similar for 2; and y is responent's choice between candidates
95
+ B = Q %>% unite('Q1', matches('Q1')) %>% unite('Q2', matches('Q2')) %>% unite('Q3', matches('Q3')) %>%
96
+ gather(Question, b, starts_with('Q')) %>% arrange(X) %>% separate( 'b', c('A1','A2','B1','B2','y') )
97
+
98
+ B = B %>% filter(! (A1 == A2 & B1 == B2)) #drop observations where candidates have same profile
99
+
100
+ #make new data frame wheer each row is one PROFILE, ie each question becomes two rows (one for each candidate)
101
+ #New variables: A1, A2 are combined as A: trait A for either candidate
102
+ C = B %>% unite('AB1',c(A1,B1)) %>% unite('AB2',c(A2,B2)) %>% gather(Neighbor, AB, c(AB1, AB2)) %>%
103
+ arrange(X) %>% separate('AB', c('A','B')) %>%
104
+ mutate(Neighbor = as.numeric(mapvalues(Neighbor, from = c('AB1', 'AB2'), to = c(1,2))))
105
+
106
+
107
+ #function to make dummies for trait levels
108
+ ModFn = function(x,f){
109
+ data.frame(x, model.matrix(as.formula(f), data=x)) }
110
+
111
+ C = C %>% ModFn('~ A - 1') %>% ModFn('~ B - 1') #function to make dummies for trait levels
112
+ C = C %>% mutate(Y = y == Neighbor) #1 when that candidate is picked
113
+ C = C %>% dplyr::rename(A_0 = A0, #reanme variables to be consistent with earlier version of code
114
+ A_1 = A1,
115
+ A_2 = A2,
116
+ A_3 = A3,
117
+ B_0 = B0,
118
+ B_1 = B1,
119
+ B_2 = B2,
120
+ B_3 = B3,
121
+ B_4 = B4)
122
+
123
+ B = C; rm(C) #rename to be consistent with earlier version
124
+ ###########################################################################################
125
+
126
+ ###########################################################################################
127
+ #SUBGROUP ANALYSIS: do analysis and make plots
128
+ #Subgroups: Nearest10_SameRel; Caste
129
+
130
+ form1 = as.formula(paste0('Y ~ ',
131
+ paste(strsplit('A_0 A_1 A_3 B_0 B_1 B_2 B_3 B_4',
132
+ split = ' ')[[1]], collapse = ' + ' )))
133
+
134
+
135
+ #function to make data frame from regression results
136
+ DF_C = function(l_m, id){
137
+ l_m_pl = data.frame(Parameter = rownames(l_m[-1,])) %>% #-1 drops intercept
138
+ mutate(Coef = l_m[-1,1]) %>%
139
+ mutate(Lo = Coef - 1.96 * l_m[-1,2]) %>%
140
+ mutate(Hi = Coef + 1.96 * l_m[-1,2]) %>%
141
+ mutate(ID = id) %>%
142
+ rbind(data.frame(Parameter = c('A_2','B_4'), #6-22-18
143
+ Coef = c(0,0), Lo = c(0,0), Hi = c(0,0), ID = c(id, id)) ) %>%
144
+ mutate(Parameter = as.character(Parameter)) %>%
145
+ arrange(Parameter)
146
+ }
147
+
148
+ #Make functions to extract p and z as a function of k
149
+ calc_p_relig = function(k, Dat){
150
+ var <- paste0('Nearest',k,'_SameRel')
151
+ if(median(Dat[,var],na.rm=T)==k){
152
+ var_break <- k-1
153
+ }else{var_break = floor(median(Dat[,var],na.rm=T))}
154
+ dat_lo = Dat[ which(Dat[,var] <= var_break ) ,]
155
+ dat_hi = Dat[ which(Dat[,var] > var_break ) ,]
156
+ if(nrow(dat_lo) == 0 | nrow(dat_hi) == 0){p_a2 = NA
157
+ }else{
158
+ lm_lo = lm(form1, data = dat_lo )
159
+ lm_hi = lm(form1, data = dat_hi )
160
+ lm_clus_lo = coeftest(lm_lo, cluster.vcov(lm_lo, dat_lo[,c('X','A.A7')]))
161
+ lm_clus_hi = coeftest(lm_hi, cluster.vcov(lm_hi, dat_hi[,c('X','A.A7')]))
162
+ z_a2 = (lm_clus_lo[3,1] - lm_clus_hi[3,1]) / sqrt(lm_clus_lo[3,2]^2 + lm_clus_hi[3,2]^2)
163
+ p_a2 = 2 * pnorm( abs(z_a2), mean = 0, sd = 1, lower.tail = F)
164
+ coef_lo = lm_clus_lo[3,1]; coef_hi = lm_clus_hi[3,1]
165
+ sd_lo = lm_clus_lo[3,2]; sd_hi = lm_clus_hi[3,2]
166
+ return(data.frame(coef_lo, coef_hi, sd_lo, sd_hi, p_a2)) }
167
+ }
168
+
169
+ #calculate results for all k
170
+ relig_results = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B)) %>% t() %>% data.frame() %>%
171
+ mutate(k = 1:30,
172
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
173
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
174
+ dif_lobd = dif - 1.96*dif_sd,
175
+ dif_hibd = dif + 1.96*dif_sd
176
+ )
177
+
178
+ #Create table
179
+ relig_results_tab = relig_results %>% apply(2, function(x) as.numeric(x)) %>% data.frame() %>%
180
+ select(k, coef_lo, sd_lo, coef_hi, sd_hi, p_a2) %>%
181
+ rename(Coef_LowSeg = coef_lo, SD_LowSeg = sd_lo, Coef_HiSeg = coef_hi, SD_HiSeg = sd_hi, p = p_a2) %>%
182
+ round(3)
183
+ out = stargazer(relig_results_tab, summary = F, rownames = F,
184
+ title = 'Results for co-ethnicity attribute in candidate experiment compared between
185
+ high- and low-segregation subsamples, based on religious exposure.',
186
+ label = 'table:ReligResults_Network')
187
+ writeLines(out,con = paste0(path0,'ReligResults_Network.tex'));rm(out, relig_results_tab)
188
+
189
+ #Create data frame for coefficient plots
190
+ rrc =
191
+ relig_results[seq(from = 1, to = 30, by = 1),] %>%
192
+ rename(Low = coef_lo, High = coef_hi) %>%
193
+ select(-starts_with('dif')) %>%
194
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
195
+ sd_lo = as.numeric(sd_lo),
196
+ sd_hi = as.numeric(sd_hi),
197
+ p_a2 = as.numeric(p_a2),
198
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
199
+ lo = Coef - 1.96*sd,
200
+ hi = Coef + 1.96*sd,
201
+ sig = factor(p_a2 < 0.05))
202
+
203
+ #Create plot
204
+ ggplot(data = rrc, aes(x = k, y = Coef)) +
205
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
206
+ position = position_dodge(width = 0.9)) +
207
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
208
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
209
+ labs(shape = 'Exposure') +
210
+ theme_minimal() + ylab('Coethnicity Coefficients') +
211
+ theme(text = element_text(size = 16),
212
+ plot.title = element_text(hjust = 0.5)) +
213
+ ggtitle('k-Nearest Own Religion') +
214
+ guides(shape = guide_legend(order = 1),
215
+ alpha = guide_legend(order = 0))
216
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_all.png'), height = 150, width = 150, units = 'mm')
217
+ ################################################################################################################################
218
+
219
+ #Income: median is 10k
220
+ relig_results_rich = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$Income1 >= 10),])) %>% t() %>% data.frame() %>%
221
+ mutate(k = 1:30,
222
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
223
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
224
+ dif_lobd = dif - 1.96*dif_sd,
225
+ dif_hibd = dif + 1.96*dif_sd
226
+ )
227
+ rrc_rich =
228
+ relig_results_rich[seq(from = 2, to = 30, by = 3),] %>%
229
+ rename(Low = coef_lo, High = coef_hi) %>%
230
+ select(-starts_with('dif')) %>%
231
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
232
+ sd_lo = as.numeric(sd_lo),
233
+ sd_hi = as.numeric(sd_hi),
234
+ p_a2 = as.numeric(p_a2),
235
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
236
+ lo = Coef - 1.96*sd,
237
+ hi = Coef + 1.96*sd,
238
+ sig = factor(p_a2 < 0.05))
239
+
240
+ #FIGURE A12
241
+ ggplot(data = rrc_rich, aes(x = k, y = Coef)) +
242
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
243
+ position = position_dodge(width = 0.9)) +
244
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
245
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) + #
246
+ labs(shape = 'Exposure') +
247
+ theme_minimal() + ylab('Coethnicity Coefficients') +
248
+ theme(text = element_text(size = 16),
249
+ plot.title = element_text(hjust = 0.5)) +
250
+ ggtitle('k-Nearest Own Religion (High Income)') +
251
+ guides(shape = guide_legend(order = 1), #
252
+ alpha = guide_legend(order = 0))
253
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_rich.png'), height = 150, width = 150, units = 'mm')
254
+
255
+ #Patna
256
+ relig_results_patna = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$City == 'Patna'),])) %>% t() %>% data.frame() %>%
257
+ mutate(k = 1:30,
258
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
259
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
260
+ dif_lobd = dif - 1.96*dif_sd,
261
+ dif_hibd = dif + 1.96*dif_sd
262
+ )
263
+ rrc_patna =
264
+ relig_results_patna[seq(from = 2, to = 30, by = 3),] %>%
265
+ rename(Low = coef_lo, High = coef_hi) %>%
266
+ select(-starts_with('dif')) %>%
267
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
268
+ sd_lo = as.numeric(sd_lo),
269
+ sd_hi = as.numeric(sd_hi),
270
+ p_a2 = as.numeric(p_a2),
271
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
272
+ lo = Coef - 1.96*sd,
273
+ hi = Coef + 1.96*sd,
274
+ sig = factor(p_a2 < 0.05))
275
+
276
+ #FIGURE A13
277
+ ggplot(data = rrc_patna, aes(x = k, y = Coef)) +
278
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
279
+ position = position_dodge(width = 0.9)) +
280
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
281
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
282
+ labs(shape = 'Exposure') +
283
+ theme_minimal() + ylab('Coethnicity Coefficients') +
284
+ theme(text = element_text(size = 16),
285
+ plot.title = element_text(hjust = 0.5)) +
286
+ ggtitle('k-Nearest Own Religion (Patna)') +
287
+ guides(shape = guide_legend(order = 1),
288
+ alpha = guide_legend(order = 0))
289
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_patna.png'), height = 150, width = 150, units = 'mm')
290
+
291
+ #Female
292
+ relig_results_female = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$Male == FALSE),])) %>% t() %>% data.frame() %>%
293
+ mutate(k = 1:30,
294
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
295
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
296
+ dif_lobd = dif - 1.96*dif_sd,
297
+ dif_hibd = dif + 1.96*dif_sd
298
+ )
299
+ rrc_female =
300
+ relig_results_female[seq(from = 2, to = 30, by = 3),] %>%
301
+ rename(Low = coef_lo, High = coef_hi) %>%
302
+ select(-starts_with('dif')) %>%
303
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
304
+ sd_lo = as.numeric(sd_lo),
305
+ sd_hi = as.numeric(sd_hi),
306
+ p_a2 = as.numeric(p_a2),
307
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
308
+ lo = Coef - 1.96*sd,
309
+ hi = Coef + 1.96*sd,
310
+ sig = factor(p_a2 < 0.05))
311
+
312
+ #Figure A14
313
+ ggplot(data = rrc_female, aes(x = k, y = Coef)) +
314
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
315
+ position = position_dodge(width = 0.9)) +
316
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
317
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
318
+ labs(shape = 'Exposure') +
319
+ theme_minimal() + ylab('Coethnicity Coefficients') +
320
+ theme(text = element_text(size = 16),
321
+ plot.title = element_text(hjust = 0.5)) +
322
+ ggtitle('k-Nearest Own Religion (Female)') +
323
+ guides(shape = guide_legend(order = 1),
324
+ alpha = guide_legend(order = 0))
325
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_female.png'), height = 150, width = 150, units = 'mm')
326
+
327
+ #Migrant
328
+ relig_results_migrant = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$Migrant == TRUE),])) %>% t() %>% data.frame() %>%
329
+ mutate(k = 1:30,
330
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
331
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
332
+ dif_lobd = dif - 1.96*dif_sd,
333
+ dif_hibd = dif + 1.96*dif_sd
334
+ )
335
+ rrc_migrant =
336
+ relig_results_migrant[seq(from = 2, to = 30, by = 3),] %>%
337
+ rename(Low = coef_lo, High = coef_hi) %>%
338
+ select(-starts_with('dif')) %>%
339
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
340
+ sd_lo = as.numeric(sd_lo),
341
+ sd_hi = as.numeric(sd_hi),
342
+ p_a2 = as.numeric(p_a2),
343
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
344
+ lo = Coef - 1.96*sd,
345
+ hi = Coef + 1.96*sd,
346
+ sig = factor(p_a2 < 0.05))
347
+
348
+ #Figure A15
349
+ ggplot(data = rrc_migrant, aes(x = k, y = Coef)) +
350
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
351
+ position = position_dodge(width = 0.9)) +
352
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
353
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
354
+ labs(shape = 'Exposure') +
355
+ theme_minimal() + ylab('Coethnicity Coefficients') +
356
+ theme(text = element_text(size = 16),
357
+ plot.title = element_text(hjust = 0.5)) +
358
+ ggtitle('k-Nearest Own Religion (Migrant)') +
359
+ guides(shape = guide_legend(order = 1),
360
+ alpha = guide_legend(order = 0))
361
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_migrant.png'), height = 150, width = 150, units = 'mm')
362
+
363
+ #High Caste
364
+ relig_results_hicaste = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$LowCaste == FALSE),])) %>% t() %>% data.frame() %>%
365
+ mutate(k = 1:30,
366
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
367
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
368
+ dif_lobd = dif - 1.96*dif_sd,
369
+ dif_hibd = dif + 1.96*dif_sd
370
+ )
371
+ rrc_hicaste =
372
+ relig_results_hicaste[seq(from = 2, to = 30, by = 3),] %>%
373
+ rename(Low = coef_lo, High = coef_hi) %>%
374
+ select(-starts_with('dif')) %>%
375
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
376
+ sd_lo = as.numeric(sd_lo),
377
+ sd_hi = as.numeric(sd_hi),
378
+ p_a2 = as.numeric(p_a2),
379
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
380
+ lo = Coef - 1.96*sd,
381
+ hi = Coef + 1.96*sd,
382
+ sig = factor(p_a2 < 0.05))
383
+
384
+ #Figure A16
385
+ ggplot(data = rrc_hicaste, aes(x = k, y = Coef)) +
386
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
387
+ position = position_dodge(width = 0.9)) +
388
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
389
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
390
+ labs(shape = 'Exposure') +
391
+ theme_minimal() + ylab('Coethnicity Coefficients') +
392
+ theme(text = element_text(size = 16),
393
+ plot.title = element_text(hjust = 0.5)) +
394
+ ggtitle('k-Nearest Own Religion (Hi Caste)') +
395
+ guides(shape = guide_legend(order = 1),
396
+ alpha = guide_legend(order = 0))
397
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_hicaste.png'), height = 150, width = 150, units = 'mm')
398
+
399
+ #Muslim
400
+ relig_results_mus = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$C.C6 == 'Muslim'),])) %>% t() %>% data.frame() %>%
401
+ mutate(k = 1:30,
402
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
403
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
404
+ dif_lobd = dif - 1.96*dif_sd,
405
+ dif_hibd = dif + 1.96*dif_sd
406
+ )
407
+ rrc_mus =
408
+ relig_results_mus[seq(from = 2, to = 30, by = 3),] %>%
409
+ rename(Low = coef_lo, High = coef_hi) %>%
410
+ select(-starts_with('dif')) %>%
411
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
412
+ sd_lo = as.numeric(sd_lo),
413
+ sd_hi = as.numeric(sd_hi),
414
+ p_a2 = as.numeric(p_a2),
415
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
416
+ lo = Coef - 1.96*sd,
417
+ hi = Coef + 1.96*sd,
418
+ sig = factor(p_a2 < 0.05))
419
+
420
+ #Figure A17
421
+ ggplot(data = rrc_mus, aes(x = k, y = Coef)) +
422
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
423
+ position = position_dodge(width = 0.9)) +
424
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
425
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
426
+ labs(shape = 'Exposure') +
427
+ theme_minimal() + ylab('Coethnicity Coefficients') +
428
+ theme(text = element_text(size = 16),
429
+ plot.title = element_text(hjust = 0.5)) +
430
+ ggtitle('k-Nearest Own Religion (Muslim)') +
431
+ guides(shape = guide_legend(order = 1),
432
+ alpha = guide_legend(order = 0))
433
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_muslim.png'), height = 150, width = 150, units = 'mm')
434
+
435
+ #Hindu
436
+ relig_results_hin = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[which(B$C.C6 == 'Hindu'),])) %>% t() %>% data.frame() %>%
437
+ mutate(k = 1:30,
438
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
439
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
440
+ dif_lobd = dif - 1.96*dif_sd,
441
+ dif_hibd = dif + 1.96*dif_sd
442
+ )
443
+ rrc_hin =
444
+ relig_results_hin[seq(from = 2, to = 30, by = 3),] %>%
445
+ rename(Low = coef_lo, High = coef_hi) %>%
446
+ select(-starts_with('dif')) %>%
447
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
448
+ sd_lo = as.numeric(sd_lo),
449
+ sd_hi = as.numeric(sd_hi),
450
+ p_a2 = as.numeric(p_a2),
451
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
452
+ lo = Coef - 1.96*sd,
453
+ hi = Coef + 1.96*sd,
454
+ sig = factor(p_a2 < 0.05))
455
+
456
+ #Figure A18
457
+ ggplot(data = rrc_hin, aes(x = k, y = Coef)) +
458
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
459
+ position = position_dodge(width = 0.9)) +
460
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(1, 0.5), labels = c('Yes','No')) +
461
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
462
+ labs(shape = 'Exposure') +
463
+ theme_minimal() + ylab('Coethnicity Coefficients') +
464
+ theme(text = element_text(size = 16),
465
+ plot.title = element_text(hjust = 0.5)) +
466
+ ggtitle('k-Nearest Own Religion (Hindu)') +
467
+ guides(shape = guide_legend(order = 1),
468
+ alpha = guide_legend(order = 0))
469
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NETWORK_hindu.png'), height = 150, width = 150, units = 'mm')
470
+
471
+ ######################################################################################################
472
+ #Main network results in table
473
+
474
+ #Table A7
475
+ relig_results_tab = relig_results %>% apply(2, function(x) as.numeric(x)) %>% data.frame() %>%
476
+ select(k, coef_lo, sd_lo, coef_hi, sd_hi, p_a2) %>%
477
+ rename(Coef_LowSeg = coef_lo, SD_LowSeg = sd_lo, Coef_HiSeg = coef_hi, SD_HiSeg = sd_hi, p = p_a2) %>%
478
+ round(3)
479
+ out = stargazer(relig_results_tab, summary = F, rownames = F,
480
+ title = 'Results for co-ethnicity attribute in candidate experiment compared between
481
+ high- and low-segregation subsamples, based on religious segregation.',
482
+ label = 'table:ReligResults_Network')
483
+ writeLines(out,con = paste0(path0,'ReligResults_Network.tex'));rm(out, path0, relig_results_tab)
484
+
485
+ #######################################################################################################################
27/replication_package/Figure6_script.R ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Figure 6: Preferences for non-coethnic neighbor in neighbor conjoint experiment,
2
+ #comparing high- to low-exposure respondents.
3
+
4
+ #install packges
5
+ # install.packages('plyr')
6
+ # install.packages('dplyr')
7
+ # install.packages('tidyr')
8
+ # install.packages('ggplot2')
9
+ # install.packages('lmtest')
10
+ # install.packages('multiwayvcov')
11
+ # install.packages('stargazer')
12
+
13
+ rm(list=ls())
14
+ library(plyr);library(dplyr, warn.conflicts = F)
15
+ library(tidyr)
16
+ library(ggplot2)
17
+ suppressMessages( library(lmtest) )
18
+ suppressMessages( library(multiwayvcov) )
19
+ suppressMessages(library(stargazer))
20
+
21
+ s = function(x){summary(factor(x))}
22
+ #setwd() #set working directory
23
+ dir.create(paste0(getwd(), '/Output/'))
24
+ dir.create(paste0(getwd(), '/Output/Figure_6/'))
25
+ path0 = paste0(getwd(), '/Output/Figure_6/', Sys.Date(),'/') #Directory for output files
26
+ dir.create(path0)
27
+
28
+ Q = read.csv('4-20-20_deid_nearestK.csv',
29
+ na.strings=c('','NA'),strip.white=T,stringsAsFactors = F)
30
+ Q = Q[which(Q$Wave == 'Bangalore 2017'),]
31
+
32
+ ##############################################################################################################################
33
+
34
+ Q = Q %>% dplyr::rename(Q2A1 = L.Neighbor_Random_2_A1, #second question, candidate 1, characteristic A
35
+ Q2A2 = L.Neighbor_Random_2_A2,
36
+ Q2B1 = L.Neighbor_Random_2_B1,
37
+ Q2B2 = L.Neighbor_Random_2_B2,
38
+ Q2C1 = L.Neighbor_Random_2_C1,
39
+ Q2C2 = L.Neighbor_Random_2_C2,
40
+ Q3A1 = L.Neighbor_Random_3_A1,
41
+ Q3A2 = L.Neighbor_Random_3_A2,
42
+ Q3B1 = L.Neighbor_Random_3_B1,
43
+ Q3B2 = L.Neighbor_Random_3_B2,
44
+ Q3C1 = L.Neighbor_Random_3_C1,
45
+ Q3C2 = L.Neighbor_Random_3_C2,
46
+ # Q1 = L.Neighbor_Question_1, #first question: choose candidate 1 or 2?
47
+ Q2 = L.Neighbor_Question_2,
48
+ Q3 = L.Neighbor_Question_3)
49
+
50
+ #rearrange so one row is one conjoint observation. 3x as many rows as A
51
+ #new variables: A1, B1 are two traits for candidate 1; similar for 2; and y is respondent's choice between candidates
52
+ B = Q %>%
53
+ unite('Q2',matches('Q2')) %>% unite('Q3',matches('Q3')) %>%
54
+ gather(Question,b,starts_with('Q')) %>% arrange(X) %>% separate('b', c('A1','A2','B1','B2','C1','C2','y'))
55
+
56
+ B = B %>% filter(!(A1 == A2 & B1 == B2 & C1 == C2)) #Drop observations where candidates have same profile
57
+
58
+ #Make new data frame where each row is one PROFILE, ie each question becomes two rows (one for each candidate)
59
+ #New variables: A1, A2 are combined as A: trait A for either candidate
60
+ C = B %>% unite('ABC1',c(A1,B1,C1)) %>% unite('ABC2',c(A2,B2,C2)) %>% gather(Neighbor, ABC, c(ABC1,ABC2)) %>%
61
+ arrange(X) %>% separate('ABC',c('A','B','C')) %>% mutate(Neighbor = as.numeric(mapvalues(Neighbor, from = c('ABC1','ABC2'), to = c(1,2))))
62
+
63
+ C$B_revised = NA
64
+ C$B_revised[which(C$C.C6_Religion == 'Hindu' & C$B == 0)] = 1 #Respondent and neighbor both Hindu
65
+ C$B_revised[which(C$C.C6_Religion == 'Hindu' & C$B == 1)] = 0 #Respondent Hindu, neighbor Muslim
66
+ C$B_revised[which(C$C.C6_Religion == 'Hindu' & C$B == 2)] = 2 #non-kannada-speaker: keep it the same
67
+ C$B_revised[which(C$C.C6_Religion == 'Muslim' & C$B == 0)] = 0 #Respondent Muslim, neighbor Hindu
68
+ C$B_revised[which(C$C.C6_Religion == 'Muslim' & C$B == 1)] = 1 #Respondent Hindu, neighbor Muslim
69
+ C$B_revised[which(C$C.C6_Religion == 'Muslim' & C$B == 2)] = 2 #non-kannada-speaker: keep it the same
70
+ C$B = as.character(C$B_revised) #this is necessary so model-matrix step below works
71
+ #B0: other religion. B1: same religion. B2: non kannada speaker
72
+
73
+ #this introduces NA's (people who are not Hindus or Muslims); drop these here
74
+ C = C[which(!is.na(C$B)),]
75
+
76
+ #function to make dummies for trait levels
77
+ ModFn = function(x,f){
78
+ data.frame(x, model.matrix(as.formula(f), data=x))}
79
+
80
+ C = C %>% ModFn('~ A - 1') %>% ModFn('~ B - 1') %>% ModFn('~ C - 1') #function to make dummies for trait levels
81
+ C = C %>% mutate(Y = y == Neighbor) #1 when that candidate is picked
82
+ C = C %>% dplyr::rename(A_0 = A0, #rename variables to be consistent with earlier version of code
83
+ A_1 = A1,
84
+ B_0 = B0,
85
+ B_1 = B1,
86
+ C_0 = C0,
87
+ C_1 = C1,
88
+ C_2 = C2,
89
+ C_3 = C3,
90
+ C_4 = C4)
91
+
92
+ B = C; rm(C) #rename variables to be consistent with earlier version of code
93
+ ##############################################################################################################
94
+
95
+ ##########do analysis and make plots######################################################################
96
+ #regression formula
97
+ form1 = as.formula(paste0('Y ~ ',
98
+ paste(strsplit('A_0 A_1 B_0 B_1 C_0 C_1 C_2 C_3 C_4', split = ' ')[[1]], collapse=' + ')))
99
+
100
+ DF_C_v2 = function(l_m,id){ #
101
+ l_m_pl = data.frame(Parameter = rownames(l_m[-1,])) %>% #-1 drops intercept
102
+ mutate(Coef = l_m[-1,1]) %>%
103
+ mutate(Lo = Coef - 1.96*l_m[-1,2]) %>%
104
+ mutate(Hi = Coef + 1.96*l_m[-1,2]) %>%
105
+ rbind(data.frame(Parameter = c('A_3','B_4'),
106
+ Coef = c(0,0),
107
+ Lo = c(0,0),
108
+ Hi = c(0,0)
109
+ )) %>%
110
+ mutate(ID = id) %>%
111
+ mutate(Parameter = as.character(Parameter)) %>%
112
+ arrange(Parameter)
113
+ }
114
+
115
+ ###########################################################################################
116
+ #Make functions to extract p and z as a function of k
117
+
118
+ calc_p_relig_neigh = function(k, Dat){ #
119
+ var <- paste0('Nearest',k,'_SameReligion') #
120
+ if(median(Dat[,var],na.rm=T)==k){
121
+ var_break <- k-1
122
+ }else{var_break = floor(median(Dat[,var],na.rm=T))}
123
+ dat_lo = Dat[ which(Dat[,var] <= var_break ) ,]
124
+ dat_hi = Dat[ which(Dat[,var] > var_break ) ,]
125
+ if(nrow(dat_lo) == 0 | nrow(dat_hi) == 0){p_a2 = NA
126
+ }else{
127
+ lm_lo = lm(form1, data = dat_lo )
128
+ lm_hi = lm(form1, data = dat_hi )
129
+ lm_clus_lo = coeftest(lm_lo, cluster.vcov(lm_lo, dat_lo[,c('X','A.A7_Area.Neighborhood')]))
130
+ lm_clus_hi = coeftest(lm_hi, cluster.vcov(lm_hi, dat_hi[,c('X','A.A7_Area.Neighborhood')]))
131
+ z_a2 = (lm_clus_lo[3,1] - lm_clus_hi[3,1]) / sqrt(lm_clus_lo[3,2]^2 + lm_clus_hi[3,2]^2)
132
+ p_a2 = 2 * pnorm( abs(z_a2), mean = 0, sd = 1, lower.tail = F)
133
+ coef_lo = lm_clus_lo[3,1]; coef_hi = lm_clus_hi[3,1]
134
+ sd_lo = lm_clus_lo[3,2]; sd_hi = lm_clus_hi[3,2]
135
+ return(data.frame(coef_lo, coef_hi, sd_lo, sd_hi, p_a2)) }
136
+ }
137
+
138
+ relig_results = sapply(1:30, function(x) calc_p_relig_neigh(k=x, Dat = B)) %>% t() %>% data.frame() %>%
139
+ mutate(k = 1:30,
140
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
141
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
142
+ dif_lobd = dif - 1.96*dif_sd,
143
+ dif_hibd = dif + 1.96*dif_sd
144
+ )
145
+
146
+ #Create data frame for plotting
147
+ rrc = relig_results[seq(from = 2, to = 30, by = 3),] %>%
148
+ rename(Low = coef_lo, High = coef_hi) %>%
149
+ select(-starts_with('dif')) %>%
150
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
151
+ sd_lo = as.numeric(sd_lo),
152
+ sd_hi = as.numeric(sd_hi),
153
+ p_a2 = as.numeric(p_a2),
154
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
155
+ lo = Coef - 1.96*sd,
156
+ hi = Coef + 1.96*sd,
157
+ sig = factor(p_a2 < 0.05))
158
+
159
+ #FIGURE 6
160
+ ggplot(data = rrc, aes(x = k, y = Coef)) +
161
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
162
+ position = position_dodge(width = 0.9)) +
163
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
164
+ scale_alpha_manual('p < 0.05', c(FALSE), values=c(0.5), labels = c('No')) +
165
+ labs(shape = 'Exposure') +
166
+ theme_minimal() + ylab('Non-Coethnicity Coefficients') +
167
+ theme(text = element_text(size = 16),
168
+ plot.title = element_text(hjust = 0.5)) +
169
+ ggtitle('k-Nearest Own Religion') +
170
+ guides(shape = guide_legend(order = 1),
171
+ alpha = guide_legend(order = 0))
172
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NEIGHBOR.png'), height = 150, width = 150, units = 'mm')
173
+ ################################################################################################################################
174
+
175
+ ################################################################################################################################
176
+ #De-medianed version
177
+
178
+ calc_p_relig_neigh_demed = function(k, Dat){
179
+ var <- paste0('DeMedNearest',k,'_SameReligion')
180
+ var_break = 0
181
+ dat_lo = Dat[ which(Dat[,var] < var_break ) ,]
182
+ dat_hi = Dat[ which(Dat[,var] >= var_break ) ,]
183
+ if(nrow(dat_lo) == 0 | nrow(dat_hi) == 0){p_a2 = NA
184
+ }else{
185
+ lm_lo = lm(form1, data = dat_lo )
186
+ lm_hi = lm(form1, data = dat_hi )
187
+ lm_clus_lo = coeftest(lm_lo, cluster.vcov(lm_lo, dat_lo[,c('X','A.A7_Area.Neighborhood')]))
188
+ lm_clus_hi = coeftest(lm_hi, cluster.vcov(lm_hi, dat_hi[,c('X','A.A7_Area.Neighborhood')]))
189
+ z_a2 = (lm_clus_lo[3,1] - lm_clus_hi[3,1]) / sqrt(lm_clus_lo[3,2]^2 + lm_clus_hi[3,2]^2)
190
+ p_a2 = 2 * pnorm( abs(z_a2), mean = 0, sd = 1, lower.tail = F)
191
+ coef_lo = lm_clus_lo[3,1]; coef_hi = lm_clus_hi[3,1]
192
+ sd_lo = lm_clus_lo[3,2]; sd_hi = lm_clus_hi[3,2]
193
+ return(data.frame(coef_lo, coef_hi, sd_lo, sd_hi, p_a2)) }
194
+ }
195
+
196
+ relig_results_demed = sapply(c(5,10,15,20,25,30), function(x) calc_p_relig_neigh_demed(k=x, Dat = B)) %>% t() %>% data.frame() %>%
197
+ mutate(k = c(5,10,15,20,25,30),
198
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
199
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
200
+ dif_lobd = dif - 1.96*dif_sd,
201
+ dif_hibd = dif + 1.96*dif_sd
202
+ )
203
+
204
+ rrc_demed = relig_results_demed %>%
205
+ rename(Low = coef_lo, High = coef_hi) %>%
206
+ select(-starts_with('dif')) %>%
207
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
208
+ sd_lo = as.numeric(sd_lo),
209
+ sd_hi = as.numeric(sd_hi),
210
+ p_a2 = as.numeric(p_a2),
211
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
212
+ lo = Coef - 1.96*sd,
213
+ hi = Coef + 1.96*sd,
214
+ sig = factor(p_a2 < 0.05))
215
+
216
+ #FIGURE A19
217
+ ggplot(data = rrc_demed, aes(x = k, y = Coef)) +
218
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
219
+ position = position_dodge(width = 0.9)) +
220
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
221
+ scale_alpha_manual('p < 0.05', c(FALSE), values=c(0.5), labels = c('No')) +
222
+ labs(shape = 'Exposure') +
223
+ theme_minimal() + ylab('Non-Coethnicity Coefficients') +
224
+ theme(text = element_text(size = 16),
225
+ plot.title = element_text(hjust = 0.5)) +
226
+ ggtitle('k-Nearest Own Religion (De-medianed)') +
227
+ guides(shape = guide_legend(order = 1),
228
+ alpha = guide_legend(order = 0))
229
+ ggsave(filename = paste0(path0, 'k-z_coefficients_NEIGHBOR_demed.png'), height = 150, width = 150, units = 'mm')
230
+
231
+ relig_results_tab = relig_results %>% apply(2, function(x) as.numeric(x)) %>% data.frame() %>%
232
+ select(k, coef_lo, sd_lo, coef_hi, sd_hi, p_a2) %>%
233
+ rename(Coef_HiExp = coef_lo, SD_HiExp = sd_lo, Coef_LoExp = coef_hi, SD_LoExp = sd_hi, p = p_a2) %>%
234
+ round(3)
235
+ out = stargazer(relig_results_tab, summary = F, rownames = F,
236
+ title = 'Results for co-ethnicity attribute in neighbor experiment compared between
237
+ high- and low-exposure subsamples, based on religious exposure',
238
+ label = 'table:ReligResults_Neighbor')
239
+ writeLines(out,con = paste0(path0,'ReligResults_Neighbor.tex'));rm(out, relig_results_tab)
27/replication_package/Figure_A5_script.R ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Figure A5, results excluding Bangalore
2
+
3
+ #package installation
4
+ # install.packages('plyr')
5
+ # install.packages('dplyr')
6
+ # install.packages('tidyr')
7
+ # install.packages('ggplot2')
8
+ # install.packages('multiwayvcov')
9
+ # install.packages('lmtest')
10
+
11
+ rm(list=ls())
12
+ #setwd() #set working directory
13
+ dir.create(paste0(getwd(), '/Output/'))
14
+ dir.create(paste0(getwd(), '/Output/Figure_A5/'))
15
+ path0 = paste0(getwd(), '/Output/Figure_A5/', Sys.Date(),'/') #Directory for output files
16
+ dir.create(path0)
17
+
18
+ s = function(x){summary(factor(x))}
19
+ Num = function(x){as.numeric(as.factor(x))}
20
+
21
+ library(plyr);library(dplyr, warn.conflicts = FALSE)
22
+ library(tidyr);library(ggplot2)
23
+ suppressMessages(library(multiwayvcov, warn.conflicts = F))
24
+ suppressMessages(library(lmtest, warn.conflicts = F))
25
+
26
+ A = readRDS('4-20-20_HH-KNearest_DeID_demed.RDS')
27
+
28
+ Aprime = A[A$A.A7_Area.Neighborhood %in% names(which(table(A$A.A7_Area.Neighborhood) >= 30)),]#DROP PLACES WITH <30 OBSERVATIONS
29
+ Aprime = Aprime[which(!is.na(Aprime$A.A7_Area.Neighborhood)),] #drop NA neighborhoods
30
+ Aprime = Aprime[which(Aprime$A.A7_Area.Neighborhood != 'Bangalore NA'),]
31
+
32
+ Q = Aprime
33
+
34
+ #Rename variables
35
+
36
+ Q = Q %>% dplyr::rename(Q1A1 = L.Candidate_Random_A1_Candidate.Preferance, #first question, candidate 1, characteristic A
37
+ Q1A2 = L.Candidate_Random_A2_Candidate.Preferance, #first question, candidate 2, characteristic A
38
+ Q1B1 = L.Candidate_Random_B1_Candidate.Preferance, #first question, candidate 1, characteristic B
39
+ Q1B2 = L.Candidate_Random_B2_Candidate.Preferance, #first question, candidate 2, characteristic B
40
+ Q2A1 = L.Candidate_Random_A3, #second question, candidate 1, characteristic A
41
+ Q2A2 = L.Candidate_Random_A4, #second question, candidate 2, characteristic A
42
+ Q2B1 = L.Candidate_Random_B3, #second question, candidate 1, characteristic B
43
+ Q2B2 = L.Candidate_Random_B4, #second question, candidate 2, characteristic B
44
+ Q3A1 = L.Candidate_Random_A5, #third question, candidate 1, characteristic A
45
+ Q3A2 = L.Candidate_Random_A6, #third question, candidate 2, characteristic A
46
+ Q3B1 = L.Candidate_Random_B5, #third question, candidate 1, characteristic B
47
+ Q3B2 = L.Candidate_Random_B6, #third question, candidate 2, characteristic B
48
+ Q1 = L.Candidate_Question_1, #first question, choose candidate 1 or 2
49
+ Q2 = L.Candidate_Question_2, #first question, choose candidate 1 or 2
50
+ Q3 = L.Candidate_Question_3 #first question, choose candidate 1 or 2
51
+ )
52
+
53
+ #rearrange so one row is one conjoint observation. 3x as many rows as A
54
+ #new variables: A1, B1 are two traits for candidate 1; similar for 2; and y is responent's choice between candidates
55
+ B = Q %>% unite('Q1', matches('Q1')) %>% unite('Q2', matches('Q2')) %>% unite('Q3', matches('Q3')) %>%
56
+ gather(Question, b, starts_with('Q')) %>% arrange(X) %>% separate( 'b', c('A1','A2','B1','B2','y') )
57
+
58
+ B = B %>% filter(! (A1 == A2 & B1 == B2)) #7767 -> 7346; drop observations where candidates have same profile
59
+
60
+ #make new data frame wheer each row is one PROFILE, ie each question becomes two rows (one for each candidate)
61
+ #New variables: A1, A2 are combined as A: trait A for either candidate
62
+ C = B %>% unite('AB1',c(A1,B1)) %>% unite('AB2',c(A2,B2)) %>% gather(Neighbor, AB, c(AB1, AB2)) %>%
63
+ arrange(X) %>% separate('AB', c('A','B')) %>%
64
+ mutate(Neighbor = as.numeric(mapvalues(Neighbor, from = c('AB1', 'AB2'), to = c(1,2))))
65
+
66
+ #function to make dummies for trait levels
67
+ ModFn = function(x,f){
68
+ data.frame(x, model.matrix(as.formula(f), data=x)) }
69
+
70
+ C = C %>% ModFn('~ A - 1') %>% ModFn('~ B - 1') #function to make dummies for trait levels
71
+ C = C %>% mutate(Y = y == Neighbor) #1 when that candidate is picked
72
+ C = C %>% dplyr::rename(A_0 = A0, #rename variables to be consistent with earlier version of code
73
+ A_1 = A1,
74
+ A_2 = A2,
75
+ A_3 = A3,
76
+ B_0 = B0,
77
+ B_1 = B1,
78
+ B_2 = B2,
79
+ B_3 = B3,
80
+ B_4 = B4)
81
+
82
+ B = C; rm(C) #rename to be consistent with earlier version
83
+ ###########################################################################################
84
+
85
+ ###########################################################################################
86
+ #SUBGROUP ANALYSIS: Religion 10
87
+
88
+ #regression formula
89
+ form1 = as.formula(paste0('Y ~ ',
90
+ paste(strsplit('A_0 A_1 A_2 A_3 B_0 B_1 B_2 B_3 B_4',
91
+ split = ' ')[[1]], collapse = ' + ' )))
92
+
93
+ #function to make data frame from regression results
94
+ DF_C = function(l_m, id){
95
+ l_m_pl = data.frame(Parameter = rownames(l_m[-1,])) %>% #-1 drops intercept
96
+ mutate(Coef = l_m[-1,1]) %>%
97
+ mutate(Lo = Coef - 1.96 * l_m[-1,2]) %>%
98
+ mutate(Hi = Coef + 1.96 * l_m[-1,2]) %>%
99
+ mutate(ID = id) %>%
100
+ rbind(data.frame(Parameter = c('A_3','B_4'),
101
+ Coef = c(0,0), Lo = c(0,0), Hi = c(0,0), ID = c(id, id)) ) %>%
102
+ mutate(Parameter = as.character(Parameter)) %>%
103
+ arrange(Parameter)
104
+ }
105
+
106
+ ###########################################################################################
107
+
108
+ ###########################################################################################
109
+ #Make functions to extract p and z as a function of k
110
+
111
+ calc_p_relig = function(k, Dat){
112
+ var <- paste0('Nearest',k,'_OwnReligion')
113
+ if(median(Dat[,var],na.rm=T)==k){
114
+ var_break <- k-1
115
+ }else{var_break = floor(median(Dat[,var],na.rm=T))}
116
+ dat_lo = Dat[ which(Dat[,var] <= var_break ) ,]
117
+ dat_hi = Dat[ which(Dat[,var] > var_break ) ,]
118
+ if(nrow(dat_lo) == 0 | nrow(dat_hi) == 0){p_a2 = NA
119
+ }else{
120
+ lm_lo = lm(form1, data = dat_lo )
121
+ lm_hi = lm(form1, data = dat_hi )
122
+ lm_clus_lo = coeftest(lm_lo, cluster.vcov(lm_lo, dat_lo[,c('X','A.A7_Area.Neighborhood')]))
123
+ lm_clus_hi = coeftest(lm_hi, cluster.vcov(lm_hi, dat_hi[,c('X','A.A7_Area.Neighborhood')]))
124
+ z_a2 = (lm_clus_lo['A_2','Estimate'] - lm_clus_hi['A_2','Estimate']) / sqrt(lm_clus_lo['A_2','Std. Error']^2 + lm_clus_hi['A_2','Std. Error']^2)
125
+ p_a2 = 2 * pnorm( abs(z_a2), mean = 0, sd = 1, lower.tail = F)
126
+ coef_lo = lm_clus_lo['A_2','Estimate']; coef_hi = lm_clus_hi['A_2','Estimate']
127
+ sd_lo = lm_clus_lo['A_2','Std. Error']; sd_hi = lm_clus_hi['A_2','Std. Error']
128
+ return(data.frame(coef_lo, coef_hi, sd_lo, sd_hi, p_a2)) }
129
+ }
130
+
131
+ relig_results = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B)) %>% t() %>% data.frame() %>%
132
+ mutate(k = 1:30,
133
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
134
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
135
+ dif_lobd = dif - 1.96*dif_sd,
136
+ dif_hibd = dif + 1.96*dif_sd
137
+ )
138
+
139
+ rrc =
140
+ relig_results[seq(from = 2, to = 30, by = 3),] %>%
141
+ rename(Low = coef_lo, High = coef_hi) %>%
142
+ select(-starts_with('dif')) %>%
143
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
144
+ sd_lo = as.numeric(sd_lo),
145
+ sd_hi = as.numeric(sd_hi),
146
+ p_a2 = as.numeric(p_a2),
147
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
148
+ lo = Coef - 1.96*sd,
149
+ hi = Coef + 1.96*sd,
150
+ sig = factor(p_a2 < 0.05))
151
+
152
+ #Results without Bangalore
153
+ relig_results_2 = sapply(1:30, function(x) calc_p_relig(k=x, Dat = B[-which(B$Wave == 'Bangalore 2016'),])) %>% t() %>% data.frame() %>%
154
+ mutate(k = 1:30,
155
+ dif = as.numeric(coef_lo) - as.numeric(coef_hi),
156
+ dif_sd = sqrt(as.numeric(sd_lo)^2 + as.numeric(sd_hi)^2),
157
+ dif_lobd = dif - 1.96*dif_sd,
158
+ dif_hibd = dif + 1.96*dif_sd
159
+ )
160
+
161
+ #Data frame for plotting
162
+ rrc_2 =
163
+ relig_results_2[seq(from = 2, to = 30, by = 3),] %>%
164
+ rename(Low = coef_lo, High = coef_hi) %>%
165
+ select(-starts_with('dif')) %>%
166
+ gather(Seg, Coef, c(Low, High)) %>% mutate(Coef = as.numeric(Coef),
167
+ sd_lo = as.numeric(sd_lo),
168
+ sd_hi = as.numeric(sd_hi),
169
+ p_a2 = as.numeric(p_a2),
170
+ sd = sd_lo*(Seg == 'Low') + sd_hi*(Seg == 'High'),
171
+ lo = Coef - 1.96*sd,
172
+ hi = Coef + 1.96*sd,
173
+ sig = factor(p_a2 < 0.05))
174
+
175
+ #Figure A5
176
+ ggplot(data = rrc_2, aes(x = k, y = Coef)) +
177
+ geom_pointrange(aes(ymin = lo, ymax = hi, x = k, shape = Seg, alpha = sig),
178
+ position = position_dodge(width = 0.9)) +
179
+ scale_shape_manual('Exposure', values = c(16, 17), labels = c('Low','High')) +
180
+ scale_alpha_manual('p < 0.05', c(TRUE, FALSE), values=c(0.5, 1), labels = c('Yes','No')) +
181
+ labs(shape = 'Exposure') +
182
+ theme_minimal() + ylab('Coethnicity Coefficients') +
183
+ theme(text = element_text(size = 16),
184
+ plot.title = element_text(hjust = 0.5)) +
185
+ ggtitle('k-Nearest Own Religion,\nExcluding Bangalore') +
186
+ guides(shape = guide_legend(order = 1),
187
+ alpha = guide_legend(order = 0))
188
+ ggsave(filename = paste0(path0, 'k-z_coefficients_no-bangalore.png'), height = 150, width = 150, units = 'mm'); rm(path0)
27/replication_package/Jaipur_District_censusdata_2-25-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0378738e7586686664d5502cfd0f315af3e7674fe748087dc227da2380b4639a
3
+ size 2111
27/replication_package/Jaipur_ward_censusdata_2-14-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f9c452aa07befa647adaa6695e9dff3391cd3603d21ff3c4d2fc110f1bade65
3
+ size 6270
27/replication_package/Karnataka_district_censusdata_2-22-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4c43e16fc33a638484d107f7fbbdc3402c6a062f9ac3711142f4e4839cb84cb4
3
+ size 4368
27/replication_package/Network-sample-calculations_script.R ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #package installation
2
+ # install.packages('plyr')
3
+ # install.packages('dplyr')
4
+ # install.packages('tidyr')
5
+ # install.packages('igraph')
6
+ # install.packages('geosphere')
7
+ # install.packages('ggplot2')
8
+ # install.packages('tictoc')
9
+
10
+ rm(list=ls())
11
+ library(plyr);library(dplyr, warn.conflicts = F)
12
+ library(tidyr)
13
+ library(igraph)
14
+ library(geosphere)
15
+ library(ggplot2, warn.conflicts = F)
16
+ library(tictoc)
17
+
18
+ #setwd() #set working directory
19
+ dir.create(paste0(getwd(), '/Output/'))
20
+ dir.create(paste0(getwd(), '/Output/Network-sample-calculations/'))
21
+ path0 = paste0(getwd(), '/Output/Network-sample-calculations/', Sys.Date(),'/') #Directory for output files
22
+ dir.create(path0)
23
+
24
+ s = function(x){summary(factor(x))}
25
+
26
+ dat = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
27
+ ################################################################################################
28
+
29
+ tic()
30
+ out_sim_list = readRDS('4-21-20_1000x_sample_nearest.RDS')
31
+ toc() #
32
+
33
+ cor_df = data.frame(Nearest5_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
34
+ Nearest10_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
35
+ Nearest15_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
36
+
37
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
38
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
39
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
40
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
41
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
42
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
43
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
44
+ Nearest5_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list)),
45
+
46
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
47
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
48
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
49
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
50
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
51
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
52
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
53
+ Nearest10_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list)),
54
+
55
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
56
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
57
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
58
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
59
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
60
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
61
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
62
+ Nearest15_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list))
63
+ )
64
+
65
+ tic()
66
+ for(i in 1:length(out_sim_list)){
67
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig[i] = #nearest 5 religion correlation
68
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
69
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig[i] = #nearest 10 religion correlation
70
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
71
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig[i] = #nearest 5 religion correlation
72
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
73
+
74
+
75
+ #nearest-5
76
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh1[i] =
77
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")],
78
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")], use = 'pairwise.complete.obs')
79
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh2[i] =
80
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")],
81
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")], use = 'pairwise.complete.obs')
82
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh3[i] =
83
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")],
84
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")], use = 'pairwise.complete.obs')
85
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh4[i] =
86
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")],
87
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")], use = 'pairwise.complete.obs')
88
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh5[i] =
89
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 42")],
90
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 42")], use = 'pairwise.complete.obs')
91
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh6[i] =
92
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 51")],
93
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 51")], use = 'pairwise.complete.obs')
94
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh7[i] =
95
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 82")],
96
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 82")], use = 'pairwise.complete.obs')
97
+ cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh8[i] =
98
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 93")],
99
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 93")], use = 'pairwise.complete.obs')
100
+
101
+ #nearest-10
102
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh1[i] =
103
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")],
104
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")], use = 'pairwise.complete.obs')
105
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh2[i] =
106
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")],
107
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")], use = 'pairwise.complete.obs')
108
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh3[i] =
109
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")],
110
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")], use = 'pairwise.complete.obs')
111
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh4[i] =
112
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")],
113
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")], use = 'pairwise.complete.obs')
114
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh5[i] =
115
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 42")],
116
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 42")], use = 'pairwise.complete.obs')
117
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh6[i] =
118
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 51")],
119
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 51")], use = 'pairwise.complete.obs')
120
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh7[i] =
121
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 82")],
122
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 82")], use = 'pairwise.complete.obs')
123
+ cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh8[i] =
124
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 93")],
125
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 93")], use = 'pairwise.complete.obs')
126
+
127
+ #nearest-15
128
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh1[i] =
129
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")],
130
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 1")], use = 'pairwise.complete.obs')
131
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh2[i] =
132
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")],
133
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 145")], use = 'pairwise.complete.obs')
134
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh3[i] =
135
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")],
136
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 30")], use = 'pairwise.complete.obs')
137
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh4[i] =
138
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")],
139
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Jaipur 68")], use = 'pairwise.complete.obs')
140
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh5[i] =
141
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 42")],
142
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 42")], use = 'pairwise.complete.obs')
143
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh6[i] =
144
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 51")],
145
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 51")], use = 'pairwise.complete.obs')
146
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh7[i] =
147
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 82")],
148
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 82")], use = 'pairwise.complete.obs')
149
+ cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh8[i] =
150
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 == "Patna 93")],
151
+ out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 == "Patna 93")], use = 'pairwise.complete.obs')
152
+
153
+ } #throws a warning about "standard deviation is 0", because sometimes by chance everyone has the same Nearest10 score
154
+ toc() #2.5 sec
155
+
156
+
157
+ #histograms for nearest-k vs contact correlations from neighborhood samples
158
+ #FIGURE A3
159
+ ggplot(data = cor_df, aes(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig)) + geom_histogram(aes(y = (..count..)/sum(..count..) ), bins = 50) +
160
+ theme_bw() + labs(x = 'Correlation, Nearest-k vs Outgroup Contacts', y = 'Proportion') +
161
+ ggtitle('Nearest-10 Same Religion vs. Outgroup Contacts\nSample of 60 per neighborhood\nAll neighborhoods together') +
162
+ theme(plot.title = element_text(hjust = 0.5, size = 14))
163
+ ggsave(filename = paste0(path0, 'k_contact_networksamp_histogram.png'), height = 150, width = 150, units = 'mm'); rm(path0)
164
+
165
+ #*******RESULT******#
166
+ #Correlations over 1k iterations between k-nearest score calculated for random sam- ple of 60 individuals from each neighborhood, versus contact with individuals from another religion.
167
+ #TABLE A2
168
+ mean(cor_df$Nearest5_SameRel_samp_AllLinksSameRelig) #0.4181599
169
+ sd(cor_df$Nearest5_SameRel_samp_AllLinksSameRelig) #0.05066349
170
+ mean(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig) #0.4312065
171
+ sd(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig) #0.0466517
172
+ mean(cor_df$Nearest15_SameRel_samp_AllLinksSameRelig) #0.4375551
173
+ sd(cor_df$Nearest15_SameRel_samp_AllLinksSameRelig) #0.04528044
174
+ ###############################################################################################################################################
175
+
176
+
177
+ ################################################################################################################################################
178
+
179
+ #*******RESULT******#
180
+ #Relationship between proportion of the 10 nearest neighbors of another religion, and proportion who have any contacts with members of another religion.
181
+ #TABLE A3
182
+
183
+ sum(dat$AllLinksSameRelig < 1, na.rm = T) / sum(dat$Nearest10_SameRel <= 10, na.rm = T) # 15% of whole sample have non-homogeneous contacts
184
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel == 10, na.rm = T) / sum(dat$Nearest10_SameRel == 10, na.rm = T) #1% of samerel = 10 have hetero contacts
185
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 10, na.rm = T) / sum(dat$Nearest10_SameRel < 10, na.rm = T) #26% of samerel < 10 have hetero contacts
186
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 9, na.rm = T) / sum(dat$Nearest10_SameRel < 9, na.rm = T) #30% of samerel < 9 have hetero contacts
187
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 8, na.rm = T) / sum(dat$Nearest10_SameRel < 8, na.rm = T) #33% of samerel < 8 have hetero contacts
188
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 7, na.rm = T) / sum(dat$Nearest10_SameRel < 7, na.rm = T) #37% of samerel < 7 have hetero contacts
189
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 6, na.rm = T) / sum(dat$Nearest10_SameRel < 6, na.rm = T) #38% of samerel < 6 have hetero contacts
190
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 5, na.rm = T) / sum(dat$Nearest10_SameRel < 5, na.rm = T) #41% of samerel < 5 have hetero contacts
191
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 4, na.rm = T) / sum(dat$Nearest10_SameRel < 4, na.rm = T) #46% of samerel < 4 have hetero contacts
192
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 3, na.rm = T) / sum(dat$Nearest10_SameRel < 3, na.rm = T) #54% of samerel < 3 have hetero contacts
193
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 2, na.rm = T) / sum(dat$Nearest10_SameRel < 2, na.rm = T) #66% of samerel < 2 have hetero contacts
194
+ sum(dat$AllLinksSameRelig < 1 & dat$Nearest10_SameRel < 1, na.rm = T) / sum(dat$Nearest10_SameRel < 1, na.rm = T) #75% of samerel < 1 have hetero contacts
195
+ s(dat$Nearest10_SameRel)
196
+
197
+ sum(dat$Nearest10_SameRel == 10, na.rm = T) #1159
198
+ sum(dat$Nearest10_SameRel < 10, na.rm = T) #1422
199
+ sum(dat$Nearest10_SameRel < 9, na.rm = T) #1134
200
+ sum(dat$Nearest10_SameRel < 8, na.rm = T) #909
201
+ sum(dat$Nearest10_SameRel < 7, na.rm = T) #734
202
+ sum(dat$Nearest10_SameRel < 6, na.rm = T) #539
203
+ sum(dat$Nearest10_SameRel < 5, na.rm = T) #378
204
+ sum(dat$Nearest10_SameRel < 4, na.rm = T) #240
205
+ sum(dat$Nearest10_SameRel < 3, na.rm = T) #135
206
+ sum(dat$Nearest10_SameRel < 2, na.rm = T) #68
207
+ sum(dat$Nearest10_SameRel < 1, na.rm = T) #28
208
+
209
+ ################################################################################################################################################
210
+ #What is relationship between census-nearest-K and sample-nearest-K?
211
+
212
+ k_cor_df = data.frame(SameRel_5_cor = rep(NA, length(out_sim_list)),
213
+ SameRel_10_cor = rep(NA, length(out_sim_list)),
214
+ SameRel_15_cor = rep(NA, length(out_sim_list)),
215
+
216
+ SameRel_10_cor_neigh1 = rep(NA, length(out_sim_list)),
217
+ SameRel_10_cor_neigh2 = rep(NA, length(out_sim_list)),
218
+ SameRel_10_cor_neigh3 = rep(NA, length(out_sim_list)),
219
+ SameRel_10_cor_neigh4 = rep(NA, length(out_sim_list)),
220
+ SameRel_10_cor_neigh5 = rep(NA, length(out_sim_list)),
221
+ SameRel_10_cor_neigh6 = rep(NA, length(out_sim_list)),
222
+ SameRel_10_cor_neigh7 = rep(NA, length(out_sim_list)),
223
+ SameRel_10_cor_neigh8 = rep(NA, length(out_sim_list))
224
+
225
+ )
226
+ for(i in 1:length(out_sim_list)){
227
+ k_cor_df$SameRel_5_cor[i] = #nearest 5 religion correlation
228
+ cor(out_sim_list[[i]]$Nearest5_SameRel_samp, out_sim_list[[i]]$Nearest5_SameRel, use = 'pairwise.complete.obs')
229
+ k_cor_df$SameRel_10_cor[i] = #nearest 10 religion correlation
230
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp, out_sim_list[[i]]$Nearest10_SameRel, use = 'pairwise.complete.obs')
231
+ k_cor_df$SameRel_15_cor[i] = #nearest 15 religion correlation
232
+ cor(out_sim_list[[i]]$Nearest15_SameRel_samp, out_sim_list[[i]]$Nearest15_SameRel, use = 'pairwise.complete.obs')
233
+
234
+
235
+ k_cor_df$SameRel_10_cor_neigh1 = #nearest 10 religion correlation, neigh x
236
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 1')],
237
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 1')], use = 'pairwise.complete.obs')
238
+ k_cor_df$SameRel_10_cor_neigh2 = #nearest 10 religion correlation, neigh x
239
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 145')],
240
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 145')], use = 'pairwise.complete.obs')
241
+ k_cor_df$SameRel_10_cor_neigh3 = #nearest 10 religion correlation, neigh x
242
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 30')],
243
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 30')], use = 'pairwise.complete.obs')
244
+ k_cor_df$SameRel_10_cor_neigh4 = #nearest 10 religion correlation, neigh x
245
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 68')],
246
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 68')], use = 'pairwise.complete.obs')
247
+ k_cor_df$SameRel_10_cor_neigh5 = #nearest 10 religion correlation, neigh x
248
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 42')],
249
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 42')], use = 'pairwise.complete.obs')
250
+ k_cor_df$SameRel_10_cor_neigh6 = #nearest 10 religion correlation, neigh x
251
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 51')],
252
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 51')], use = 'pairwise.complete.obs')
253
+ k_cor_df$SameRel_10_cor_neigh7 = #nearest 10 religion correlation, neigh x
254
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 82')],
255
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 82')], use = 'pairwise.complete.obs')
256
+ k_cor_df$SameRel_10_cor_neigh8 = #nearest 10 religion correlation, neigh x
257
+ cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 93')],
258
+ out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 93')], use = 'pairwise.complete.obs')
259
+
260
+ }#throws a warning about "standard deviation is 0", because sometimes by chance everyone in random sample has the same Nearest10 score
261
+
262
+ #*******RESULT******#
263
+ #Correlation between k-nearest score calculated for random sample of 60 individuals from each neighborhood, versus the same metric calculated for the entire neighborhood. The reported means and standard deviations of the correlations are for 500 random samples of 60 individuals from each neighborhood.
264
+ #TABLE A1
265
+ mean(k_cor_df$SameRel_5_cor) # 0.7706546
266
+ sd(k_cor_df$SameRel_5_cor) # 0.02571546
267
+ mean(k_cor_df$SameRel_10_cor) # 0.8508894
268
+ sd(k_cor_df$SameRel_10_cor) # 0.01996775
269
+ mean(k_cor_df$SameRel_15_cor) # 0.8735986
270
+ sd(k_cor_df$SameRel_15_cor) # 0.01815468
271
+ ################################################################################################################################################
27/replication_package/Rajastan_district_censusdata_2-22-19.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:356016eb904e008c10c263c19132d85ed430ed3d564f1ffa65303b3e08b678f5
3
+ size 4774
27/replication_package/Results_List.xlsx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cdef9515f25adae322bb577a6242e7814544c2260e8d29871438f0226e29608
3
+ size 9765
27/replication_package/Table3_script.R ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #install packages
2
+ # install.packages('plyr')
3
+ # install.packages('dplyr')
4
+ # install.packages('stringr')
5
+
6
+ rm(list=ls())
7
+ #setwd() #set working directory
8
+
9
+ library('plyr');library('dplyr')
10
+ library('stringr')
11
+
12
+ #India as a whole
13
+ InSc = read.csv('india_sc_4-20-20.csv',
14
+ stringsAsFactors = F) %>%
15
+ rename(PopSc = Population)
16
+ InTo = read.csv('india_pop_4-20-20.csv',
17
+ stringsAsFactors = F)
18
+ In = merge(InSc, InTo, by = 'State') %>%
19
+ mutate(PropSc = ( PopSc %>% str_replace_all(',','') %>% as.numeric() ) /
20
+ ( Population %>% str_replace_all(',','') %>% as.numeric() ),
21
+ Pop = Population %>% str_replace_all(',','') %>% as.numeric(),
22
+ PopSc = PopSc %>% str_replace_all(',','') %>% as.numeric()) %>%
23
+ filter(State != 'India')
24
+ rm(InSc, InTo)
25
+
26
+ #Karnataka district data
27
+ KaL = readRDS('Karnataka_district_censusdata_2-22-19.RDS')
28
+ Ka = data.frame(matrix(unlist(KaL), nrow=length(KaL), byrow=T))
29
+ names = Ka[1,1:13]
30
+ Ka = Ka %>% select(X27:X39)
31
+ names(Ka) = names[1,] %>% apply(1,as.character)
32
+ Ka = Ka %>% rename(STprF = 'Scheduled Tribes (ST) %',
33
+ SCprF = 'Scheduled Caste (SC) %')
34
+ Ka$Pop = Ka$Population %>% as.character() %>% as.numeric()
35
+ Ka$SCpr = Ka$SCprF %>% as.character() %>% as.numeric()
36
+ Ka$SCpo = Ka$SCpr * Ka$Pop
37
+ rm(names,KaL)
38
+
39
+ #Rajasthan district data
40
+ RaL = readRDS('Rajastan_district_censusdata_2-22-19.RDS')
41
+ Ra = data.frame(matrix(unlist(RaL), nrow=length(RaL), byrow=T))
42
+ names = Ra[1,1:13]
43
+ Ra = Ra %>% select(X27:X39)
44
+ names(Ra) = names[1,] %>% apply(1,as.character)
45
+ Ra = Ra %>% rename(STprF = 'Scheduled Tribes (ST) %',
46
+ SCprF = 'Scheduled Caste (SC) %')
47
+ Ra$Pop = Ra$Population %>% as.character() %>% as.numeric()
48
+ Ra$SCpr = Ra$SCprF %>% as.character() %>% as.numeric()
49
+ Ra$SCpo = Ra$SCpr * Ra$Pop
50
+ rm(names,RaL)
51
+
52
+ #Bihar district data
53
+ BiL = readRDS('Bihar_district_censusdata_2-22-19.RDS')
54
+ Bi = data.frame(matrix(unlist(BiL), nrow=length(BiL), byrow=T))
55
+ names = Bi[1,1:13]
56
+ Bi = Bi %>% select(X27:X39)
57
+ names(Bi) = names[1,] %>% apply(1,as.character)
58
+ Bi = Bi %>% rename(STprF = 'Scheduled Tribes (ST) %',
59
+ SCprF = 'Scheduled Caste (SC) %')
60
+ Bi$Pop = Bi$Population %>% as.character() %>% as.numeric()
61
+ Bi$SCpr = Bi$SCprF %>% as.character() %>% as.numeric()
62
+ Bi$SCpo = Bi$SCpr * Bi$Pop
63
+ rm(names,BiL)
64
+
65
+ #Bangalore-District data
66
+ BdL = readRDS('Bangalore_District_censusdata_2-25-19.RDS')
67
+ Bd = data.frame(matrix(unlist(BdL), nrow=length(BdL), byrow=T))
68
+ names = Bd[1,1:13]
69
+ Bd = Bd %>% select(X27:X39)
70
+ names(Bd) = names[1,] %>% apply(1,as.character)
71
+ Bd = Bd %>% rename(STprF = 'Scheduled Tribes (ST) %',
72
+ SCprF = 'Scheduled Caste (SC) %')
73
+ Bd$Pop = Bd$Population %>% as.character() %>% as.numeric()
74
+ Bd$SCpr = Bd$SCprF %>% as.character() %>% as.numeric()
75
+ Bd$SCpo = Bd$SCpr * Bd$Pop
76
+ rm(names,BdL)
77
+
78
+ #Jaipur-District data
79
+ JdL = readRDS('Jaipur_District_censusdata_2-25-19.RDS')
80
+ Jd = data.frame(matrix(unlist(JdL), nrow=length(JdL), byrow=T))
81
+ names = Jd[1,1:13]
82
+ Jd = Jd %>% select(X27:X39)
83
+ names(Jd) = names[1,] %>% apply(1,as.character)
84
+ Jd = Jd %>% rename(STprF = 'Scheduled Tribes (ST) %',
85
+ SCprF = 'Scheduled Caste (SC) %')
86
+ Jd$Pop = Jd$Population %>% as.character() %>% as.numeric()
87
+ Jd$SCpr = Jd$SCprF %>% as.character() %>% as.numeric()
88
+ Jd$SCpo = Jd$SCpr * Jd$Pop
89
+ rm(names,JdL)
90
+
91
+ #Patna-District data
92
+ PdL = readRDS('Bihar_District_censusdata_2-25-19.RDS')
93
+ Pd = data.frame(matrix(unlist(PdL), nrow=length(PdL), byrow=T))
94
+ names = Pd[1,1:13]
95
+ Pd = Pd %>% select(X27:X39)
96
+ names(Pd) = names[1,] %>% apply(1,as.character)
97
+ Pd = Pd %>% rename(STprF = 'Scheduled Tribes (ST) %',
98
+ SCprF = 'Scheduled Caste (SC) %')
99
+ Pd$Pop = Pd$Population %>% as.character() %>% as.numeric()
100
+ Pd$SCpr = Pd$SCprF %>% as.character() %>% as.numeric()
101
+ Pd$SCpo = Pd$SCpr * Pd$Pop
102
+ rm(names,PdL)
103
+
104
+ #BBMP data
105
+ BaL = readRDS('BBMP_ward_censusdata_2-12-19.RDS')
106
+ Ba = data.frame(matrix(unlist(BaL), nrow=length(BaL), byrow=T))
107
+ names = Ba[1,1:11]
108
+ Ba = Ba %>% select(X23:X33)
109
+ names(Ba) = names[1,] %>% apply(1,as.character)
110
+ Ba = Ba %>% rename(STprF = 'Scheduled Tribes (ST) %',
111
+ SCprF = 'Scheduled Caste (SC) %')
112
+ Ba$Pop = Ba$Population %>% as.character() %>% as.numeric()
113
+ Ba$SCpr = Ba$SCprF %>% as.character() %>% as.numeric()
114
+ Ba$SCpo = Ba$SCpr * Ba$Pop
115
+ rm(names,BaL)
116
+
117
+ #Jaipur data
118
+ JaL = readRDS('Jaipur_ward_censusdata_2-14-19.RDS')
119
+ Ja = data.frame(matrix(unlist(JaL), nrow=length(JaL), byrow=T))
120
+ names = Ja[1,1:11]
121
+ Ja = Ja %>% select(X23:X33)
122
+ names(Ja) = names[1,] %>% apply(1,as.character)
123
+ Ja = Ja %>% rename(STprF = 'Scheduled Tribes (ST) %',
124
+ SCprF = 'Scheduled Caste (SC) %')
125
+ Ja$Pop = Ja$Population %>% as.character() %>% as.numeric()
126
+ Ja$SCpr = Ja$SCprF %>% as.character() %>% as.numeric()
127
+ Ja$SCpo = Ja$SCpr * Ja$Pop
128
+ rm(names,JaL)
129
+
130
+ #############################################################################################################
131
+ #SEGREGATION CALCULATIONS
132
+
133
+ #DISSIMILARITY INDEX FOR INDIA AS A WHOLE (SC)
134
+ #P: total minority proportion; ti: total pop in gridspace; pi: prop minority in gridspace
135
+ P = sum(In$PopSc) / sum(In$Pop)
136
+ T0 = sum(In$Pop)
137
+ In$D = In$Pop * abs(In$PropSc - P) / (2 * T0 * P * (1 - P))
138
+ D_India = sum(In$D) #This gives final value for dissimilarity index, based on SC population, for India as a whole
139
+ rm(T0,P)
140
+
141
+ #DISSIMILARITY INDEX FOR KARNATAKA AS A WHOLE (SC)
142
+ P = sum(Ka$SCpo) / sum(Ka$Pop)
143
+ T0 = sum(Ka$Pop)
144
+ Ka$D = Ka$Pop * abs(Ka$SCpr - P) / (2 * T0 * P * (1 - P))
145
+ D_Karnataka = sum(Ka$D)
146
+ rm(T0,P)
147
+
148
+ #DISSIMILARITY INDEX FOR RAJASTHAN AS A WHOLE (SC)
149
+ P = sum(Ra$SCpo) / sum(Ra$Pop)
150
+ T0 = sum(Ra$Pop)
151
+ Ra$D = Ra$Pop * abs(Ra$SCpr - P) / (2 * T0 * P * (1 - P))
152
+ D_Rajasthan = sum(Ra$D)
153
+ rm(T0,P)
154
+
155
+ #DISSIMILARITY INDEX FOR BIHAR AS A WHOLE (SC)
156
+ P = sum(Bi$SCpo) / sum(Bi$Pop)
157
+ T0 = sum(Bi$Pop)
158
+ Bi$D = Bi$Pop * abs(Bi$SCpr - P) / (2 * T0 * P * (1 - P))
159
+ D_Bihar = sum(Bi$D)
160
+ rm(T0,P)
161
+
162
+ #DISSIMILARITY INDEX FOR BANGALORE-DISTRICT BY TEHSIL
163
+ P = sum(Bd$SCpo) / sum(Bd$Pop)
164
+ T0 = sum(Bd$Pop)
165
+ Bd$D = Bd$Pop * abs(Bd$SCpr - P) / (2 * T0 * P * (1 - P))
166
+ D_BaD = sum(Bd$D) #
167
+ rm(T0,P)
168
+
169
+ #DISSIMILARITY INDEX FOR JAIPUR-DISTRICT BY TEHSIL
170
+ P = sum(Jd$SCpo) / sum(Jd$Pop)
171
+ T0 = sum(Jd$Pop)
172
+ Jd$D = Jd$Pop * abs(Jd$SCpr - P) / (2 * T0 * P * (1 - P))
173
+ D_JaD = sum(Jd$D)
174
+ rm(T0,P)
175
+
176
+ #DISSIMILARITY INDEX FOR PATNA-DISTRICT BY TEHSIL
177
+ P = sum(Pd$SCpo) / sum(Pd$Pop)
178
+ T0 = sum(Pd$Pop)
179
+ Pd$D = Pd$Pop * abs(Pd$SCpr - P) / (2 * T0 * P * (1 - P))
180
+ D_PdD = sum(Pd$D)
181
+ rm(T0,P)
182
+
183
+ #DISSIMILARITY INDEX FOR BBMP (198) BY WARD
184
+ P = sum(Ba$SCpo) / sum(Ba$Pop)
185
+ T0 = sum(Ba$Pop)
186
+ Ba$D = Ba$Pop * abs(Ba$SCpr - P) / (2 * T0 * P * (1 - P))
187
+ D_Bbmp = sum(Ba$D) #very dissimilar; min 0 to max 0.5
188
+ rm(T0,P)
189
+
190
+ #DISSIMILARITY INDEX FOR JAIPUR BY WARD
191
+ P = sum(Ja$SCpo) / sum(Ja$Pop)
192
+ T0 = sum(Ja$Pop)
193
+ Ja$D = Ja$Pop * abs(Ja$SCpr - P) / (2 * T0 * P * (1 - P))
194
+ D_Jaipur = sum(Ja$D)
195
+ rm(T0,P)
196
+
197
+ #********THIS IS THE BASIS FOR TABLE 3********#
198
+ data.frame(Bangalore = c(D_India, D_Karnataka, D_BaD, D_Bbmp),
199
+ Jaipur = c(D_India, D_Rajasthan, D_JaD, D_Jaipur),
200
+ Patna = c(D_India, D_Bihar, D_PdD, NA))
201
+ #############################################################################################################
202
+
27/replication_package/Table4_script.R ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Table 4: Comparison of most common occupations for network and household data sets.
2
+
3
+ #install packages
4
+ # install.packages('plyr')
5
+ # install.packages('dplyr')
6
+ # install.packages('tidyr')
7
+ # install.packages('ggplot2')
8
+ # install.packages('multiwayvcov')
9
+ # install.packages('lmtest')
10
+ # install.packages('stargazer')
11
+
12
+ rm(list=ls())
13
+
14
+ #setwd() #set working directory
15
+
16
+ library(plyr);library(dplyr, warn.conflicts = FALSE)
17
+ library(tidyr);library(ggplot2)
18
+ suppressMessages(library(multiwayvcov, warn.conflicts = F))
19
+ suppressMessages(library(lmtest, warn.conflicts = F))
20
+ suppressMessages(library(stargazer))
21
+
22
+ s = function(x){summary(factor(x))}
23
+
24
+ #Read network data
25
+ A = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
26
+ #Read and clean household sample data
27
+ H = read.csv('4-20-20_deid_nearestK.csv',
28
+ na.strings=c('','NA'),strip.white=T,stringsAsFactors = F)
29
+ Hprime = H[H$A.A7_Area.Neighborhood %in% names(which(table(H$A.A7_Area.Neighborhood) >= 30)),]#Drop neighborhoods with < 30 observations
30
+ Hprime = Hprime[which(!is.na(Hprime$A.A7_Area.Neighborhood)),] #drop NA neighborhoods
31
+ Hprime = Hprime[which(Hprime$A.A7_Area.Neighborhood != 'Bangalore NA'),]
32
+ H = Hprime; rm(Hprime)
33
+
34
+ ########################################################################################################################
35
+
36
+ ############################################################################################################
37
+ #Map values to occupations
38
+ A$Job = mapvalues(A$D.D1_Occupation,
39
+ from = c(12,
40
+ 13,
41
+ 19,
42
+ 2,
43
+ 20,
44
+ 21,
45
+ 3,
46
+ 4,
47
+ 6,
48
+ 7,
49
+ 9),
50
+ to = c('Garbage',
51
+ 'Gardener',
52
+ 'Security',
53
+ 'Butcher',
54
+ 'Tailor',
55
+ 'Vendor',
56
+ 'Carpenter',
57
+ 'Construction',
58
+ 'Cook',
59
+ 'Corporate',
60
+ 'Electrician'))
61
+
62
+ H$Job = mapvalues(H$D.D1_Occupation.,
63
+ from = c(1,
64
+ 2,
65
+ 3,
66
+ 4,
67
+ 5,
68
+ 6,
69
+ 7,
70
+ 8,
71
+ 9,
72
+ 10,
73
+ 11,
74
+ 12,
75
+ 13,
76
+ 14,
77
+ 15,
78
+ 16,
79
+ 17,
80
+ 18,
81
+ 19,
82
+ 20,
83
+ 21,
84
+ 22,
85
+ 23,
86
+ 24,
87
+ 25,
88
+ 26, #B17: retired
89
+ 27), #B17: unemployed
90
+ to = c('Agriculture',
91
+ 'Butcher',
92
+ 'Carpenter',
93
+ 'Construction',
94
+ 'Labour',
95
+ 'Cook',
96
+ 'Corporation',
97
+ 'Driver',
98
+ 'Electrical',
99
+ 'Factory',
100
+ 'Flower',
101
+ 'Garbage',
102
+ 'Gardener',
103
+ 'Maid',
104
+ 'Mechanic',
105
+ 'Painter',
106
+ 'ProfessionalSvc',
107
+ 'Grocessory',
108
+ 'Security',
109
+ 'Tailor',
110
+ 'Vendor',
111
+ 'Government',
112
+ 'Housewife',
113
+ 'Student',
114
+ 'Other',
115
+ NA, NA )) #Map retired/unemployed to NA so they don't affect denominator
116
+
117
+ #Note that B17 includes retired/unemployed; these are mapped above to NA and dropped from table (and not included in denominator)
118
+
119
+ #Network
120
+ sort (s(A$Job) / sum(!is.na(A$Job )) * 100 , decreasing = T) %>% round(2)
121
+ #Household
122
+ sort (s(H$Job) / sum(!is.na(H$Job )) * 100 , decreasing = T) %>% round(2)
123
+ #These are basis for manually-created Table 4
124
+
125
+ ################################################################################################
27/replication_package/Table5_script.R ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Table 5: Correlations of k-nearest-neighbors metric,
2
+ #calculated from neighborhood census network data,
3
+ #with the proportion of social links to members of the same group.
4
+
5
+ #install packages
6
+ # install.packages('plyr')
7
+ # install.packages('dplyr')
8
+ # install.packages('tidyr')
9
+ # install.packages('ggplot2')
10
+ # install.packages('igraph')
11
+ # install.packages('geosphere')
12
+ # install.packages('tictoc')
13
+
14
+ rm(list=ls())
15
+ library(plyr);library(dplyr, warn.conflicts = F)
16
+ library(tidyr)
17
+ library(igraph, warn.conflicts = F)
18
+ library(geosphere)
19
+ library(ggplot2)
20
+ library(tictoc)
21
+
22
+ #setwd() #set working directory
23
+
24
+ s = function(x){summary(factor(x))}
25
+
26
+ #Read data
27
+ dat3 = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
28
+ ##########################################################################################
29
+
30
+ #Relationships between census-nearest-K and census-contact
31
+ #These are basis for manually-created Table 5
32
+
33
+ cor(dat3$Nearest5_SameRel, dat3$AllLinksSameRelig, use = 'pairwise.complete.obs') #0.3779793
34
+ cor(dat3$Nearest10_SameRel, dat3$AllLinksSameRelig, use = 'pairwise.complete.obs') #0.4163651
35
+ cor(dat3$Nearest15_SameRel, dat3$AllLinksSameRelig, use = 'pairwise.complete.obs') #0.4222002
36
+
37
+ cor(dat3$Nearest5_SameCaste, dat3$AllLinksSameCaste, use = 'pairwise.complete.obs') #0.47723
38
+ cor(dat3$Nearest10_SameCaste, dat3$AllLinksSameCaste, use = 'pairwise.complete.obs') #0.5185767
39
+ cor(dat3$Nearest15_SameCaste, dat3$AllLinksSameCaste, use = 'pairwise.complete.obs') #0.5329627
40
+
27/replication_package/Table6_script.R ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #install packages
2
+ # install.packages('plyr')
3
+ # install.packages('dplyr')
4
+ # install.packages('tidyr')
5
+ # install.packages('ggplot2')
6
+ # install.packages('multiwayvcov')
7
+ # install.packages('lmtest')
8
+ # install.packages('stargazer')
9
+
10
+ rm(list=ls())
11
+ library(plyr);library(dplyr, warn.conflicts = F)
12
+ library(tidyr)
13
+ library(ggplot2)
14
+ suppressMessages( library(lmtest) )
15
+ suppressMessages( library(multiwayvcov) )
16
+ suppressMessages(library(stargazer))
17
+
18
+ s = function(x){summary(factor(x))}
19
+ #setwd() #set working directory
20
+
21
+ #read data
22
+ A = read.csv('4-20-20_deid_nearestK.csv',
23
+ na.strings=c('','NA'),strip.white=T,stringsAsFactors = F)
24
+ Q = A[which(A$Wave == 'Bangalore 2017'),]
25
+
26
+ ####################################################################################
27
+
28
+ ####################################################################################
29
+ #clean data
30
+ Aprime = A[A$A.A7_Area.Neighborhood %in% names(which(table(A$A.A7_Area.Neighborhood) >= 30)),]#Drop places with < 30 observations
31
+ Aprime = Aprime[which(!is.na(Aprime$A.A7_Area.Neighborhood)),] #drop NA neighborhoods
32
+ Aprime = Aprime[which(Aprime$A.A7_Area.Neighborhood != 'Bangalore NA'),] #Still 8273
33
+ A = Aprime; rm(Aprime)
34
+ ####################################################################################
35
+
36
+ ########################################
37
+ #Count observations and neighborhoods
38
+
39
+ #THIS IS BASIS FOR TABLE 6
40
+
41
+ #2017 neighborhoods
42
+ length(levels(factor(Q$A.A7_Area.Neighborhood))) #50
43
+ #2017 respondents
44
+ nrow(Q) #1948
45
+
46
+ #Bangalore '16 neighborhoods
47
+ length(levels(factor(A$A.A7_Area.Neighborhood[which(A$Wave == 'Bangalore 2016')]))) #20
48
+ #Bangalore '16 respondents
49
+ nrow(A[which(A$Wave == 'Bangalore 2016'),]) #609
50
+
51
+ #Jaipur neighborhoods
52
+ length(levels(factor(A$A.A7_Area.Neighborhood[which(A$City == 'Jaipur')]))) #45
53
+ #Jaipur respondents
54
+ nrow(A[which(A$City == 'Jaipur'),]) #2669
55
+
56
+ #Patna neighborhoods
57
+ length(levels(factor(A$A.A7_Area.Neighborhood[which(A$City == 'Patna')]))) #34
58
+ #Patna respondents
59
+ nrow(A[which(A$City == 'Patna'),]) #1972
60
+
61
+ #LAST TWO ROWS OF TABLE 6: Network data
62
+ N = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
63
+ nrow(N[which(N$City == 'Jaipur'),]) #1593
64
+ nrow(N[which(N$City == 'Patna'),]) #988
27/replication_package/Table_1-2.R ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #install packages
2
+ # install.packages('plyr')
3
+ # install.packages('dplyr')
4
+ # install.packages('tidyr')
5
+ # install.packages('ggplot2')
6
+ # install.packages('multiwayvcov')
7
+ # install.packages('lmtest')
8
+ # install.packages('stargazer')
9
+
10
+ ######################################################################
11
+ rm(list=ls())
12
+
13
+ s = function(x){summary(factor(x))}
14
+ Num = function(x){as.numeric(as.factor(x))}
15
+
16
+ library(plyr);library(dplyr, warn.conflicts = FALSE)
17
+ library(tidyr);library(ggplot2)
18
+ suppressMessages(library(multiwayvcov, warn.conflicts = F))
19
+ suppressMessages(library(lmtest, warn.conflicts = F))
20
+ library(stargazer)
21
+
22
+ #setwd() #set working directory
23
+ dir.create(paste0(getwd(), '/Output/'))
24
+ dir.create(paste0(getwd(), '/Output/Table_1-2/'))
25
+ path0 = paste0(getwd(), '/Output/Table_1-2/', Sys.Date(),'/') #Directory for output files
26
+ dir.create(path0)
27
+
28
+ A = readRDS('4-20-20_HH-KNearest_DeID_demed.RDS')
29
+
30
+ A = A[-which(A$Wave == 'Bangalore 2015'),] #6101
31
+
32
+ Aprime = A[A$A.A7_Area.Neighborhood %in% names(which(table(A$A.A7_Area.Neighborhood) >= 30)),]#DROP PLACES WITH <30 OBSERVATIONS
33
+ Aprime = Aprime[which(!is.na(Aprime$A.A7_Area.Neighborhood)),] #drop NA neighborhoods
34
+ Aprime = Aprime[which(Aprime$A.A7_Area.Neighborhood != 'Bangalore NA'),]
35
+
36
+ A = Aprime
37
+ ######################################################################
38
+
39
+ ##############################################################################
40
+
41
+ A$Muslim = A$C.C6_Religion == 'Muslim'
42
+
43
+ #Water
44
+ A$HtH = A$J.J3_Source.of.water == 4
45
+ WaterReg = glm('HtH ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A, family = 'binomial')
46
+
47
+ #Voter
48
+ A$Voter = A$L.L8_Voter.ID == 1
49
+ VoterIDReg = glm('Voter ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A, family = 'binomial')
50
+
51
+ #Ration Card
52
+ A$Ration = A$L.L9_Ration.card == 1
53
+ RationCardReg = glm('Ration ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A, family = 'binomial')
54
+
55
+ #Security
56
+ A$Security = mapvalues(A$J.J24_Eviction,
57
+ from = c(888, 999),
58
+ to = c(NA, NA))
59
+ SecurityReg = lm('Security ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A)
60
+
61
+ #Primary School
62
+ A$L20 = mapvalues(A$L.L20_Primary.School,
63
+ from = c(777, 888, 999),
64
+ to = c(NA, NA, NA))
65
+ PrimSchReg = lm('-L20 ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A) #Switch scale so negative is LESS satisfied
66
+
67
+ #Secondary school
68
+ A$L21 = mapvalues(A$L.L21_Secondary.School,
69
+ from = c(777, 888, 999),
70
+ to = c(NA, NA, NA))
71
+ SecSchReg = lm('-L21 ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A)
72
+
73
+ #Waste satisfaction
74
+ A$L24 = mapvalues(A$L.L24_Waste.Disposal,
75
+ from = c(777, 888, 999),
76
+ to = c(NA, NA, NA))
77
+ WasteSatReg = lm('-L24 ~ AssetSum + Muslim + City + A.A7_Area.Neighborhood', data = A)
78
+ ##############################################################################
79
+
80
+ ##############################################################################
81
+ #Make tables
82
+
83
+ #Neighborhood services
84
+ MuslimServices_1 = stargazer(WaterReg,
85
+ VoterIDReg,
86
+ RationCardReg,
87
+ dep.var.labels.include = T,
88
+ model.names = FALSE,
89
+ digits = 2,
90
+ omit = c("A.A7_Area.Neighborhood","City"),
91
+ omit.labels = c("Neighborhood dummies?","City dummies?"),
92
+ omit.stat = c("rsq","ll","ser","f"),
93
+ order=c(2,1,3),
94
+ covariate.labels = c('Muslim',
95
+ 'Assets',
96
+ 'Constant'),
97
+ dep.var.labels = c("Water Connection","Voter ID","Ration Card"),
98
+ title = 'Public Services by Religion',
99
+ label = 'table:Muslim_Services_1')
100
+ writeLines(MuslimServices_1,con = paste0(path0,'Muslim_Services_1.tex'))
101
+ #NOTE: Columns must be renamed manually in LaTeX file to match version in paper
102
+ #Replace this line: \\[-1.8ex] & \multicolumn{3}{c}{Water Connection} \\
103
+ #With this line: \\[-1.8ex] & {Water Connection} & {Voter ID} & {Ration Card} \\
104
+
105
+ MuslimServices_2 = stargazer(SecurityReg,
106
+ PrimSchReg,
107
+ SecSchReg,
108
+ WasteSatReg,
109
+ dep.var.labels.include = T,
110
+ model.names = FALSE,
111
+ digits = 2,
112
+ omit = c("A.A7_Area.Neighborhood","City"),
113
+ omit.labels = c("Neighborhood dummies?","City dummies?"),
114
+ omit.stat = c("rsq","ll","ser","f"),
115
+ order=c(2,1,3),
116
+ covariate.labels = c('Muslim',
117
+ 'Assets',
118
+ 'Constant'),
119
+ dep.var.labels = c("Tenure Security","Prim. Sch. Satis.","Sec. Sch. Satis.", "Waste Remov. Satis."), #Why does only the first one show up?
120
+ title = 'Services Satisfaction by Religion',
121
+ label = 'table:Muslim_Services_2')
122
+ writeLines(MuslimServices_2,con = paste0(path0,'Muslim_Services_2.tex'))
123
+ #NOTE: Columns must be renamed manually in LaTeX file to match version in paper
124
+ #Replace this line: \\[-1.8ex] & \multicolumn{4}{c}{Tenure Security} \\
125
+ #With this line: \\[-1.8ex] & {Tenure Sec.} & {Prim. School} & {Sec. School} & {Waste Remov.} \\
126
+
127
+ #########################
128
+
129
+ #########################
130
+ #Hindu-Muslim support for same leader
131
+ #Cited in theory section, p 7 of article
132
+ #99 neighborhoods, 58 had responses for both Hindu and Muslim, 37 differ between religions
133
+
134
+ A$L.L50_Neighborhood.Leader[which(A$L.L50_Neighborhood.Leader == '-999')] = NA #5 of these
135
+ A$L.L50_Neighborhood.Leader = factor(A$L.L50_Neighborhood.Leader)
136
+
137
+ L = A %>% group_by(A.A7_Area.Neighborhood) %>% summarize(
138
+ HinLeaderName = names(sort(table(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Hindu')], useNA = 'no'),decreasing=T))[1],
139
+ MusLeaderName = names(sort(table(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Muslim')], useNA = 'no'),decreasing=T))[1],
140
+ nHin = sum(C.C6_Religion == 'Hindu', na.rm = T),
141
+ nMus = sum(C.C6_Religion == 'Muslim', na.rm = T),
142
+ nHinAns = sum(!is.na(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Hindu')])),
143
+ nMusAns = sum(!is.na(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Muslim')])),
144
+ nHinLeader = sort(table(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Hindu')], useNA = 'no'),decreasing=T)[1],
145
+ nMusLeader = sort(table(L.L50_Neighborhood.Leader[which(C.C6_Religion == 'Muslim')], useNA = 'no'),decreasing=T)[1]
146
+ )
147
+
148
+ L = L %>% data.frame() %>%
149
+ mutate(Match = HinLeaderName == MusLeaderName,
150
+ PropHin = nHin / (nHin + nMus),
151
+ PropMus = nMus / (nHin + nMus))
152
+
153
+
154
+ #Neighborhoods where both Hindus and Muslims answer
155
+ s(L$Match[which(L$nMusLeader > 0 & L$nHinLeader > 0)]) #false 37, true 21
156
+ 37 / (37 + 21) # 0.64
157
+ 37 + 21 #58
27/replication_package/india_pop_4-20-20.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:add747a7a183a2650ba7b9f4719391b4e4fa4fc22e6bddb61ee8e9d252c230ed
3
+ size 1002
27/replication_package/india_sc_4-20-20.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:87c4377433ca10dd23d0dc50da38b9f3fbd8f634a6cb64328db36dd8bda7fb99
3
+ size 834
27/replication_package/map_dataset_4-28-20.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9381ed7b3b35b1604c46d641762983f4cf9d2faf0d58aa08a2f42837c2d608d8
3
+ size 1085
27/replication_package/map_out_4-28-20.RDS ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:974c5c445d0d218f4e28d700f3d6e5da87e1f365557428c23a3c92eed9cde7d1
3
+ size 1377902
27/replication_package/readme.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:026c16a9681672c20b9ef1b012aee3c8f76df7d9a24e70181a98217a3d513832
3
+ size 3298
27/should_reproduce.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c849f10d98acd706dba048d9ecea61a8abf58e6a54c0dbf0a94d40257fb3a53d
3
+ size 67