File size: 20,363 Bytes
3af64c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#package installation
# install.packages('plyr')
# install.packages('dplyr')
# install.packages('tidyr')
# install.packages('igraph')
# install.packages('geosphere')
# install.packages('ggplot2')
# install.packages('tictoc')

rm(list=ls())
library(plyr);library(dplyr, warn.conflicts = F)
library(tidyr)
library(igraph)
library(geosphere)
library(ggplot2, warn.conflicts = F)
library(tictoc)

#setwd() #set working directory
dir.create(paste0(getwd(), '/Output/'))
dir.create(paste0(getwd(), '/Output/Network-sample-calculations/'))
path0 = paste0(getwd(), '/Output/Network-sample-calculations/', Sys.Date(),'/') #Directory for output files
dir.create(path0)

s = function(x){summary(factor(x))}

dat = readRDS('4-20-20_Network-KNearest_DeID_demed.RDS')
################################################################################################

tic()
out_sim_list = readRDS('4-21-20_1000x_sample_nearest.RDS')
toc() #

cor_df = data.frame(Nearest5_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig = rep(NA, length(out_sim_list)),
                    
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
                    Nearest5_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list)),
                    
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
                    Nearest10_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list)),
                    
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh1 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh2 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh3 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh4 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh5 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh6 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh7 = rep(NA, length(out_sim_list)),
                    Nearest15_SameRel_samp_AllLinksSameRelig_Neigh8 = rep(NA, length(out_sim_list))
                    )

tic()
for(i in 1:length(out_sim_list)){
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig[i] =  #nearest 5 religion correlation
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig[i] =  #nearest 10 religion correlation
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig[i] =  #nearest 5 religion correlation
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp, out_sim_list[[i]]$AllLinksSameRelig, use = 'pairwise.complete.obs')
  
  
  #nearest-5
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh1[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh2[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh3[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh4[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh5[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh6[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh7[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")], use = 'pairwise.complete.obs')
  cor_df$Nearest5_SameRel_samp_AllLinksSameRelig_Neigh8[i] = 
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")], use = 'pairwise.complete.obs')
  
  #nearest-10
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh1[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh2[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh3[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh4[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh5[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh6[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh7[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")], use = 'pairwise.complete.obs')
  cor_df$Nearest10_SameRel_samp_AllLinksSameRelig_Neigh8[i] = 
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")], use = 'pairwise.complete.obs')
  
  #nearest-15
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh1[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 1")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh2[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 145")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh3[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 30")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh4[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Jaipur 68")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh5[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 42")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh6[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 51")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh7[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 82")], use = 'pairwise.complete.obs')
  cor_df$Nearest15_SameRel_samp_AllLinksSameRelig_Neigh8[i] = 
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")],
        out_sim_list[[i]]$AllLinksSameRelig[which(out_sim_list[[i]]$A.A7 ==  "Patna 93")], use = 'pairwise.complete.obs')
  
} #throws a warning about "standard deviation is 0", because sometimes by chance everyone has the same Nearest10 score
toc() #2.5 sec


#histograms for nearest-k vs contact correlations from neighborhood samples
#FIGURE A3
ggplot(data = cor_df, aes(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig)) + geom_histogram(aes(y = (..count..)/sum(..count..)  ), bins = 50) +
  theme_bw() + labs(x = 'Correlation, Nearest-k vs Outgroup Contacts', y = 'Proportion') + 
  ggtitle('Nearest-10 Same Religion vs. Outgroup Contacts\nSample of 60 per neighborhood\nAll neighborhoods together') + 
  theme(plot.title = element_text(hjust = 0.5, size = 14))
ggsave(filename = paste0(path0, 'k_contact_networksamp_histogram.png'), height = 150, width = 150, units = 'mm'); rm(path0)

#*******RESULT******#
#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.
#TABLE A2
mean(cor_df$Nearest5_SameRel_samp_AllLinksSameRelig) #0.4181599 
sd(cor_df$Nearest5_SameRel_samp_AllLinksSameRelig) #0.05066349 
mean(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig) #0.4312065 
sd(cor_df$Nearest10_SameRel_samp_AllLinksSameRelig) #0.0466517 
mean(cor_df$Nearest15_SameRel_samp_AllLinksSameRelig) #0.4375551 
sd(cor_df$Nearest15_SameRel_samp_AllLinksSameRelig) #0.04528044 
###############################################################################################################################################


################################################################################################################################################

#*******RESULT******#
#Relationship between proportion of the 10 nearest neighbors of another religion, and proportion who have any contacts with members of another religion.
#TABLE A3

sum(dat$AllLinksSameRelig < 1, na.rm = T) / sum(dat$Nearest10_SameRel <= 10, na.rm = T) # 15% of whole sample have non-homogeneous contacts
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
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
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
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
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
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
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
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
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
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
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
s(dat$Nearest10_SameRel)

sum(dat$Nearest10_SameRel == 10, na.rm = T) #1159
sum(dat$Nearest10_SameRel < 10, na.rm = T) #1422
sum(dat$Nearest10_SameRel < 9, na.rm = T) #1134
sum(dat$Nearest10_SameRel < 8, na.rm = T) #909
sum(dat$Nearest10_SameRel < 7, na.rm = T) #734
sum(dat$Nearest10_SameRel < 6, na.rm = T) #539
sum(dat$Nearest10_SameRel < 5, na.rm = T) #378
sum(dat$Nearest10_SameRel < 4, na.rm = T) #240
sum(dat$Nearest10_SameRel < 3, na.rm = T) #135
sum(dat$Nearest10_SameRel < 2, na.rm = T) #68
sum(dat$Nearest10_SameRel < 1, na.rm = T) #28

################################################################################################################################################
#What is relationship between census-nearest-K and sample-nearest-K?

k_cor_df = data.frame(SameRel_5_cor = rep(NA, length(out_sim_list)),
                      SameRel_10_cor = rep(NA, length(out_sim_list)),
                      SameRel_15_cor = rep(NA, length(out_sim_list)),
                      
                      SameRel_10_cor_neigh1 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh2 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh3 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh4 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh5 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh6 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh7 = rep(NA, length(out_sim_list)),
                      SameRel_10_cor_neigh8 = rep(NA, length(out_sim_list))
                      
                      )
for(i in 1:length(out_sim_list)){
  k_cor_df$SameRel_5_cor[i] =  #nearest 5 religion correlation
    cor(out_sim_list[[i]]$Nearest5_SameRel_samp, out_sim_list[[i]]$Nearest5_SameRel, use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor[i] =  #nearest 10 religion correlation
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp, out_sim_list[[i]]$Nearest10_SameRel, use = 'pairwise.complete.obs')
  k_cor_df$SameRel_15_cor[i] =  #nearest 15 religion correlation
    cor(out_sim_list[[i]]$Nearest15_SameRel_samp, out_sim_list[[i]]$Nearest15_SameRel, use = 'pairwise.complete.obs')
  
  
  k_cor_df$SameRel_10_cor_neigh1  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 1')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 1')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh2  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 145')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 145')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh3  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 30')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 30')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh4  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Jaipur 68')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Jaipur 68')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh5  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 42')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 42')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh6  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 51')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 51')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh7  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 82')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 82')], use = 'pairwise.complete.obs')
  k_cor_df$SameRel_10_cor_neigh8  =  #nearest 10 religion correlation, neigh x
    cor(out_sim_list[[i]]$Nearest10_SameRel_samp[which(out_sim_list[[i]]$A.A7 == 'Patna 93')], 
        out_sim_list[[i]]$Nearest10_SameRel[which(out_sim_list[[i]]$A.A7 == 'Patna 93')], use = 'pairwise.complete.obs')

}#throws a warning about "standard deviation is 0", because sometimes by chance everyone in random sample has the same Nearest10 score

#*******RESULT******#
#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.
#TABLE A1
mean(k_cor_df$SameRel_5_cor) # 0.7706546 
sd(k_cor_df$SameRel_5_cor) # 0.02571546 
mean(k_cor_df$SameRel_10_cor) # 0.8508894
sd(k_cor_df$SameRel_10_cor) # 0.01996775 
mean(k_cor_df$SameRel_15_cor) #  0.8735986 
sd(k_cor_df$SameRel_15_cor) # 0.01815468 
################################################################################################################################################