-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strain_Sharing_Analysis.Rmd
1184 lines (983 loc) · 41.3 KB
/
Strain_Sharing_Analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Analysis"
output: html_notebook
date: "2022-12-12"
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)
if (!require("lmerTest")) install.packages("lmerTest")
library(lmerTest)
if (!require("pROC")) install.packages("pROC")
library(pROC)
if (!require("ggpubr")) install.packages("ggpubr")
library(ggpubr)
if (!require("svglite")) install.packages("svglite")
library(svglite)
```
```{r}
#Get Summary of shared strains by relationship type
shared_summary <- SN %>%
distinct(pair_key, relationship, .keep_all = TRUE) %>% #Symmetrize network to avoid reccipricol nominations
group_by(relationship) %>%
summarise(Proportion_With_Shared_Strains = round(mean(strain_sharing_rate>0,
na.rm = TRUE),2),
Median_Strain_Sharing_Rate = round(median(strain_sharing_rate,
na.rm = TRUE),2),
n = n()) %>%
arrange(desc(Proportion_With_Shared_Strains))
shared_summary_reciprocated_ties <- SN %>%
filter(pair_key %in% E(SN_mutual_conns)$pair_key) |>
distinct(pair_key, relationship, .keep_all = TRUE) %>%
group_by(relationship) %>%
summarise(Proportion_With_Shared_Strains = round(mean(strain_sharing_rate>0,
na.rm = TRUE),2),
Median_Strain_Sharing_Rate = round(median(strain_sharing_rate,
na.rm = TRUE),2),
n = n()) %>%
arrange(desc(Proportion_With_Shared_Strains))
#Get shared strain rates for unnominated individuals in the same village
count <- 0
for(i in 1:length(village_names)){
SN_Village <- SN %>% filter(village_code_w3 == village_names[i])
village_ids <- unique(c(SN_Village$ego, SN_Village$alter))
count <- count + length(village_ids) *(length(village_ids)-1)/2
}
for(i in 1:length(village_names)){
SN_Village <- SN %>% filter(village_code_w3 == village_names[i])
village_ids <- unique(c(SN_Village$ego, SN_Village$alter))
strain_rate_vil <- strain_rate[rownames(strain_rate) %in% village_ids ,
colnames(strain_rate) %in% village_ids]
for(j in 1:nrow(SN_Village)){
strain_rate_vil[rownames(strain_rate_vil) == SN_Village$ego[j] ,
colnames(strain_rate_vil) == SN_Village$alter[j]] <- NA
strain_rate_vil[rownames(strain_rate_vil) == SN_Village$alter[j] ,
colnames(strain_rate_vil) == SN_Village$ego[j]] <- NA
}
strain_rate_vil[lower.tri(strain_rate_vil, diag = TRUE)] <- NA
if(i ==1){
unrelated_rate <- c(na.omit(unlist(as.list(strain_rate_vil))))
}
else{
unrelated_rate <- c(unrelated_rate,na.omit(unlist(as.list(strain_rate_vil))))
}
}
#Get counts across for individuals in different villages
strain_2_rate <- strain_rate
for(i in 1:length(village_names)){
SN_Village <- SN %>% filter(village_code_w3 == village_names[i])
village_ids <- unique(c(SN_Village$ego, SN_Village$alter))
for(j in 1:(length(village_ids)-1)){
for(k in j:length(village_ids)){
strain_2_rate[rownames(strain_2_rate) == village_ids[j],
colnames(strain_2_rate) == village_ids[k]] <- NA
strain_2_rate[rownames(strain_2_rate) == village_ids[k],
colnames(strain_2_rate) == village_ids[j]] <- NA
}
}
}
#Make matrix lower triangular and in list form
strain_2_rate[lower.tri(strain_2_rate, diag = TRUE)] <- NA
strain_2_rate <- na.omit(unlist(as.list(strain_2_rate)))
#add on unnominated individuals and individuals in different villages to summary
shared_summary <- shared_summary %>%
add_row(
relationship = "No Nomination - Same Village",
Proportion_With_Shared_Strains = round(mean(unrelated_rate >0,
na.rm = TRUE), 2),
Median_Strain_Sharing_Rate = round(median(unrelated_rate,
na.rm = TRUE), 2),
n = length(unrelated_rate)
)
shared_summary <- shared_summary %>%
add_row(
relationship = "No Nomination - Different Village",
Proportion_With_Shared_Strains = round(mean(strain_2_rate > 0,
na.rm = TRUE), 2),
Median_Strain_Sharing_Rate = round(median(strain_2_rate,
na.rm = TRUE), 2),
n = length(strain_2_rate)
)
#add on row for non-kin in a different house relationships
#Creat non-kin and different house network for sensitivity analysis
family_house_pairs <- unique(SN$pair_key[SN$building_id_ego == SN$building_id_alter |
SN$relationship %in% c("Father", "Mother", "Sibling", "Child")])
SN_Non_Kin_House <- SN %>% filter(!pair_key %in% family_house_pairs)
non_kin_different_house_row <- SN_Non_Kin_House %>%
distinct(pair_key, .keep_all = TRUE) %>%
summarise(relationship = "Non-Kin and Different House",
Proportion_With_Shared_Strains = round(mean(strain_sharing_rate>0,
na.rm = TRUE),2),
Median_Strain_Sharing_Rate = round(median(strain_sharing_rate,
na.rm = TRUE),2),
n = n()) %>%
arrange(desc(Proportion_With_Shared_Strains))
shared_summary <- shared_summary %>% add_row(non_kin_different_house_row)
#Add on summary row for people living in the same house
same_house <- SN %>%
distinct(pair_key, .keep_all = TRUE) %>%
filter(building_id_ego == building_id_alter) %>%
summarise(relationship = "Same Building",
Proportion_With_Shared_Strains = round(mean(strain_sharing_rate>0,
na.rm = TRUE),2),
Median_Strain_Sharing_Rate = round(median(strain_sharing_rate,
na.rm = TRUE),2),
n = n()) %>%
arrange(desc(Proportion_With_Shared_Strains))
shared_summary <- shared_summary %>% add_row(same_house)
names(shared_summary) <- c("Relationship",
"Proportion_With_Shared_Strains",
"Median_Strain_Sharing_Rate",
"Count")
names(shared_summary_reciprocated_ties) <- c("Relationship",
"Proportion_With_Shared_Strains",
"Median_Strain_Sharing_Rate",
"Count")
shared_summary <- shared_summary %>% arrange(desc(Median_Strain_Sharing_Rate))
```
```{r}
comp_reciprocated <- SN %>%
filter(pair_key %in% E(SN_mutual_conns)$pair_key) |>
distinct(pair_key, relationship, .keep_all = TRUE) |>
select(relationship, pair_key, strain_sharing_rate) |>
mutate(status = 'Reciprocated') |>
bind_rows(
( SN |>
filter(!(pair_key %in% E(SN_mutual_conns)$pair_key)) |>
distinct(pair_key, relationship, .keep_all = TRUE) |>
select(relationship, pair_key, strain_sharing_rate) |>
mutate(status = 'Not\nReciprocated')
)) %>%
filter(!relationship %in% c('Child','Father','Mother', 'Sibling')) |>
mutate(status_collapsed = paste(relationship, status))
comp_reciprocated_pvals <- comp_reciprocated %>%
group_by(relationship) %>%
do(w = wilcox.test(strain_sharing_rate ~ status, data = ., paired = FALSE)) |>
summarize(relationship, w$p.value)
# compare_means(strain_sharing_rate ~ status,
# data = .,
# method = "wilcox.test",
# p.adjust.method = "BH")
# filter(str_sub(group1, 1, 5)==str_sub(group2, 1, 5))
comp_reciprocated_plot <- comp_reciprocated |>
ggplot() +
geom_boxplot(aes(status, strain_sharing_rate, fill = status)) +
facet_wrap(~relationship, strip.position = 'bottom', nrow = 1) +
# stat_pvalue_manual(comp_reciprocated_pvals, label = "p.signif", tip.length = 0) +
coord_cartesian(ylim = c(0, 55)) +
xlab("Relationship") +
ylab("Strain-Sharing Rate (%)") +
theme_pubr() +
labs_pubr() +
theme(
strip.background = element_blank(),
strip.text = element_text(size = 10),
axis.text.x = element_text(
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#axis.title.x = element_text(size = 10),
legend.position = "none"
) +
scale_fill_brewer(palette = 'Set1')
```
```{r}
svglite('Figures/comp_reciprocated_ties_ssr.svg', fix_text_size = FALSE, height = 6)
comp_reciprocated_plot
dev.off()
```
```{r}
left_join(shared_summary_reciprocated_ties, shared_summary, by = join_by('Relationship'), suffix = c('_reciprocated',''))
```
Create graphics of strain sharing rate by relationship
```{r}
#Get unique dataframes for all relationship types
SN_DF_simple <- SN %>%
distinct(pair_key,relationship,.keep_all = TRUE) %>%
dplyr::select(relationship, strain_sharing_rate)
SN_Non_Kin_House_simple <- SN_Non_Kin_House %>%
distinct(pair_key,.keep_all = TRUE) %>%
dplyr::select(relationship, strain_sharing_rate)
SN_Non_Kin_House_simple$relationship <- "Non-Kin and Different House"
SN_Same_Building_simple <- SN %>%
filter(building_id_alter == building_id_ego) %>%
distinct(pair_key, .keep_all = TRUE) %>%
dplyr::select(relationship, strain_sharing_rate)
SN_Same_Building_simple$relationship <- "Same Building"
same_vil_unrelated <- data.frame(relationship = rep("No Nomination - Same Village", length(unrelated_rate)),
strain_sharing_rate = unrelated_rate)
dif_vil_unrelated <- data.frame(relationship = rep("No Nomination - Different Village", length(strain_2_rate)),
strain_sharing_rate = strain_2_rate)
#Combine into dataframe with all relationship types relationships
SN_All_Relationship_DF <- rbind(SN_DF_simple,SN_Non_Kin_House_simple,SN_Same_Building_simple, same_vil_unrelated, dif_vil_unrelated)
readr::write_tsv(SN_All_Relationship_DF,'data/export/SN_All_Relationship_DF.tsv')
#Make relationship factor variable for plotting
SN_All_Relationship_DF$relationship <- reorder(as.factor(SN_All_Relationship_DF$relationship),
-SN_All_Relationship_DF$strain_sharing_rate,
FUN = median, na.rm=TRUE)
#Change names for plotting
levels(SN_All_Relationship_DF$relationship) <- c("Partner",
"Same Building",
"Mother",
"Child",
"Father",
"Free Time",
"Personal/\nPrivate",
"Sibling",
"Non-Kin\nDif-House",
"Close Friend",
"No-Nom\nSame-Vil",
"No-Nom\nDif-Vil")
#Get means by group or strain sharing rate
median_rels_ssr <- aggregate(strain_sharing_rate ~ relationship, SN_All_Relationship_DF, median, na.rm=TRUE)
#Get test statistics
kruskal.test(strain_sharing_rate ~ relationship, data = SN_All_Relationship_DF)
nrow(SN_All_Relationship_DF)
rels_pvals <- compare_means(strain_sharing_rate ~ relationship,
data = SN_All_Relationship_DF,
method = "wilcox.test",
p.adjust.method = "BH")
rels_pvals <- rels_pvals %>% filter(p.signif == "ns") %>%
mutate(y.position = c(40, 37.5, 35, 32.5, 27))
```
```{r}
relationships_all_plot <-
ggplot(SN_All_Relationship_DF, aes(relationship, strain_sharing_rate, color = relationship)) +
geom_boxplot(aes(color = relationship), outlier.shape = NA, size = 1) +
xlab("Relationship") +
ylab("Strain-Sharing Rate (%)") +
theme_pubr() +
labs_pubr() +
scale_y_continuous( limits = c(0,42), breaks = seq(0, 40, 5)) +
theme(
axis.text.x = element_text(
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
stat_pvalue_manual(rels_pvals, label = "p.signif", tip.length = 0, bracket.size = 0.7, size = 5) +
geom_text(data = median_rels_ssr,
color = 'black',
aes(label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
),"%"),
y = 42,
fontface = "bold")) +
scale_x_discrete(labels = paste0(
levels(SN_All_Relationship_DF$relationship),
"\n(N=",
table(SN_All_Relationship_DF$relationship),
")"
)) +
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#80d15d",
"#954ed1",
"#cdb753",
"#51347d",
"#d6593c",
"#81c9b8",
"#ca5395",
"#566e3e",
"#8692c8",
"#823f34",
"#d0a39e",
"#403642"))
# scale_color_manual(values = c('#a6cee3',
# '#b2df8a',
# '#1f78b4',
# '#fb9a99',
# '#33a02c',
# '#fdbf6f',
# '#e31a1c',
# '#cab2d6',
# '#ff7f00',
# '#fdfd37',
# '#6a3d9a',
# '#b15928'))
relationships_all_plot
```
Free Time and meals
```{r}
#Create dictionary with frequency mappings
Free_Time_Numeric_Dict <- c(
"Every day"=4,
"A few days a week"=3,
"A few days a month"=2,
"Rarely/never" = 1
)
Meals_Numeric_Dict <- c(
"Almost every day"=4,
"About once a week"=3,
"A few times a month"=2,
"About once a month or less" = 1
)
#Make frequency numeric from dictionary
SN$a2701_numeric <- Free_Time_Numeric_Dict[SN$a2701]
SN$a2702_numeric <- Meals_Numeric_Dict[SN$a2702]
SN_Non_Kin_House$a2701_numeric <- Free_Time_Numeric_Dict[SN_Non_Kin_House$a2701]
SN_Non_Kin_House$a2702_numeric <- Meals_Numeric_Dict[SN_Non_Kin_House$a2702]
#Symmetrize network to most frequent reported contact
SN_Symmetrized <- SN %>%
group_by(pair_key) %>%
filter(!is.na(a2701_numeric), !is.na(a2702_numeric)) %>%
mutate(a2701_numeric_max = max(a2701_numeric),
a2702_numeric_max = max(a2702_numeric)) %>%
ungroup() %>% distinct(pair_key, .keep_all = TRUE)
readr::write_tsv(SN_Symmetrized |>
select(a2701_numeric_max, a2702_numeric_max, strain_sharing_rate),'data/export/SN_Symmetrized.tsv')
SN_Non_Kin_House_Symmetrized <- SN_Non_Kin_House %>%
group_by(pair_key) %>%
filter(!is.na(a2701_numeric), !is.na(a2702_numeric)) %>%
mutate(a2701_numeric_max = max(a2701_numeric),
a2702_numeric_max = max(a2702_numeric)) %>%
ungroup() %>% distinct(pair_key, .keep_all = TRUE)
#Reverse map on values from survey
SN_Symmetrized <- SN_Symmetrized %>% mutate(
Free_Time = case_when(
a2701_numeric_max == 4 ~ "Every day",
a2701_numeric_max == 3 ~ "A few days a week",
a2701_numeric_max == 2 ~ "A few days a month",
a2701_numeric_max == 1 ~ "Rarely/never",
),
Meals = case_when(
a2702_numeric_max == 4 ~ "Almost every day",
a2702_numeric_max == 3 ~ "Once a week",
a2702_numeric_max == 2 ~ "Few times a month",
a2702_numeric_max == 1 ~ "Once a month"
))
SN_Non_Kin_House_Symmetrized <- SN_Non_Kin_House_Symmetrized %>% mutate(
Free_Time = case_when(
a2701_numeric_max == 4 ~ "Every day",
a2701_numeric_max == 3 ~ "A few days a week",
a2701_numeric_max == 2 ~ "A few days a month",
a2701_numeric_max == 1 ~ "Rarely/never",
),
Meals = case_when(
a2702_numeric_max == 4 ~ "Almost every day",
a2702_numeric_max == 3 ~ "Once a week",
a2702_numeric_max == 2 ~ "Few times a month",
a2702_numeric_max == 1 ~ "Once a month"
))
#Rarely/never category in Free time may be biased with a very low count
#drop these since there are only 18
table(SN_Symmetrized$Free_Time)
table(SN_Symmetrized$Meals)
#Rarely/never category in Free time may be biased with a very low count
#drop these since there are only 12
table(SN_Non_Kin_House_Symmetrized$Free_Time)
table(SN_Non_Kin_House_Symmetrized$Meals)
#Get median by group
meals_ssr_median <- aggregate(strain_sharing_rate ~ Meals, SN_Symmetrized,
median, na.rm=TRUE)
#Reorder relationship factor variable for plotting
SN_Symmetrized$Meals <- reorder(as.factor(SN_Symmetrized$Meals),
-SN_Symmetrized$strain_sharing_rate,
median,na.rm=TRUE)
#Get test-statistics
kruskal.test(strain_sharing_rate ~ Meals, data = SN_Symmetrized)
nrow(SN_Symmetrized)
meal_pvals <- compare_means(strain_sharing_rate ~ Meals,
data = SN_Symmetrized,
method = "wilcox.test",
p.adjust.method = "BH")
meal_pvals <- meal_pvals %>% filter(p.signif == "ns") %>%
mutate(y.position = c(55, 52))
#Plot differences between groups for meals based on strain sharing rate
meals_all_plot <- ggplot(SN_Symmetrized, aes(Meals, strain_sharing_rate, color = Meals)) +
geom_quasirandom(size = 1, alpha = 0.5) +
# geom_boxplot( outlier.shape = NA ) +
# geom_jitter(aes(color = Meals),
# alpha = .25,
# width = .25,
# show.legend = FALSE) +
xlab("Shared Meal Frequency") +
ylab("Strain Sharing Rate (%)") +
scale_y_continuous( limits = c(0, 60), breaks = seq(0, 60, 10)) +
theme_pubr() +
labs_pubr() +
theme(
axis.text.x = element_text(
angle = 0,
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
geom_text(data = meals_ssr_median,
color = 'black',
aes(label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
),"%"),
y = 60,
fontface = "bold")) +
scale_x_discrete(labels = paste0(
levels(SN_Symmetrized$Meals),
"\n(N=",
table(SN_Symmetrized$Meals),
")"
)) +
stat_pvalue_manual(meal_pvals, label = "p.signif", tip.length = 0, bracket.size = 0.7, size = 5)+
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#01b0f8",
"#33d600",
"#004672",
"#ff6438",
"#537200",
"#e3006f",
"#9216ff"
))
meals_all_plot
```
Meals non-kin dif-house
```{r}
#Get median by group
meals_ssr_median_nkh <- aggregate(strain_sharing_rate ~ Meals,
SN_Non_Kin_House_Symmetrized, median, na.rm=TRUE)
#Reorder relationship factor variable for plotting
SN_Non_Kin_House_Symmetrized$Meals <- reorder(as.factor(SN_Non_Kin_House_Symmetrized$Meals),
-SN_Non_Kin_House_Symmetrized$strain_sharing_rate,
median, na.rm=TRUE)
#Get test-statistics
kruskal.test(strain_sharing_rate ~ Meals,
data = SN_Non_Kin_House_Symmetrized)
nrow(SN_Non_Kin_House_Symmetrized)
meal_pvals_nkh <- compare_means(strain_sharing_rate ~ Meals,
data = SN_Non_Kin_House_Symmetrized,
method = "wilcox.test",
p.adjust.method = "BH")
meal_pvals_nkh <- meal_pvals_nkh %>%
filter(p.signif != "ns") %>%
mutate(y.position = c(27, 29))
#Plot differences between groups for meals based on strain sharing rate
meals_all_plot_nkh <- ggplot(SN_Non_Kin_House_Symmetrized, aes(Meals, strain_sharing_rate, color = Meals)) +
geom_quasirandom(size = 3, alpha = 0.5) +
xlab("Shared Meal Frequency (Non-Kin Dif-House)") +
ylab("Strain Sharing Rate (%)") +
scale_y_continuous( limits = c(0, 30), breaks = seq(0, 60, 10)) +
theme_pubr() +
labs_pubr() +
theme(
axis.text.x = element_text(
angle = 0,
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
geom_text(data = meals_ssr_median_nkh,
color = 'black',
aes(label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
), "%"),
y = 30,
fontface = "bold")) +
scale_x_discrete(labels = paste0(
levels(SN_Non_Kin_House_Symmetrized$Meals),
"\n(N=",
table(SN_Non_Kin_House_Symmetrized$Meals),
")"
)) +
stat_pvalue_manual(meal_pvals_nkh, label = "p.adj", tip.length = 0, bracket.size = 0.7, size = 5) +
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#01b0f8",
"#33d600",
"#004672",
"#ff6438",
"#537200",
"#e3006f",
"#9216ff"
))
```
```{r}
#Same analysis but for free time
SN_Symmetrized_2 <- SN_Symmetrized %>% filter(Free_Time != "Rarely/never")
SN_Non_Kin_House_Symmetrized_2 <- SN_Non_Kin_House_Symmetrized %>% filter(Free_Time != "Rarely/never")
free_time_ssr_median <- aggregate(strain_sharing_rate ~ Free_Time, SN_Symmetrized_2,
median, na.rm = TRUE)
#Reorder relationship factor variable for plotting
SN_Symmetrized_2$Free_Time <- reorder(as.factor(SN_Symmetrized_2$Free_Time),
-SN_Symmetrized_2$strain_sharing_rate,
median, na.rm = TRUE)
table(SN_Symmetrized_2$Free_Time)
#Get test statistics
kruskal.test(strain_sharing_rate ~ Free_Time, data = SN_Symmetrized_2)
nrow(SN_Symmetrized_2)
free_time_pvals <- compare_means(strain_sharing_rate ~ Free_Time,
data = SN_Symmetrized_2,
method = "wilcox.test",
p.adjust.method = "BH")
free_time_pvals <- free_time_pvals %>% filter(p.signif == "ns") %>%
mutate(y.position = c(45))
free_time_all_plot <- ggplot(SN_Symmetrized_2, aes(Free_Time, strain_sharing_rate, color = Free_Time)) +
geom_quasirandom(size = 1, alpha = 0.5) +
xlab("Frequency of Contact") +
ylab("Strain-Sharing Rate (%)") +
scale_y_continuous( limits = c(0, 60), breaks = seq(0, 60, 10)) +
theme_pubr() +
labs_pubr() +
theme(
axis.text.x = element_text(
angle = 0,
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
geom_text(data = free_time_ssr_median,
color = 'black',
aes(label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
),"%"),
y = 60,
fontface = "bold")) +
scale_x_discrete(labels = paste0(
levels(SN_Symmetrized_2$Free_Time),
"\n(N=",
table(SN_Symmetrized_2$Free_Time),
")"
)) +
stat_pvalue_manual(free_time_pvals, label = "p.signif", tip.length = 0, bracket.size = 0.7, size = 5) +
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#01b0f8",
"#33d600",
"#004672",
"#ff6438",
"#537200",
"#e3006f",
"#9216ff"
))
```
Free-time Non-Kin Dif-House
```{r}
free_time_ssr_median_nkh <- aggregate(strain_sharing_rate ~ Free_Time,
SN_Non_Kin_House_Symmetrized_2, median, na.rm = TRUE)
#Reorder relationship factor variable for plotting
SN_Non_Kin_House_Symmetrized_2$Free_Time <-
reorder(as.factor(SN_Non_Kin_House_Symmetrized_2$Free_Time),
-SN_Non_Kin_House_Symmetrized_2$strain_sharing_rate,
median, na.rm = TRUE)
#Get test statistics
kruskal.test(strain_sharing_rate ~ Free_Time, data = SN_Non_Kin_House_Symmetrized_2)
nrow(SN_Non_Kin_House_Symmetrized_2)
free_time_pvals_nkh <- compare_means(strain_sharing_rate ~ Free_Time,
data = SN_Non_Kin_House_Symmetrized_2,
method = "wilcox.test",
p.adjust.method = "BH")
free_time_pvals_nkh <- free_time_pvals_nkh %>%
filter(p.signif != "ns") %>%
mutate(y.position = c(27, 29))
#Plot differences between groups for meals based on strain sharing rate
free_time_all_plot_nkh <- ggplot(SN_Non_Kin_House_Symmetrized_2, aes(Free_Time, strain_sharing_rate, color = Free_Time)) +
geom_quasirandom(size = 3, alpha = 0.5) +
xlab("Frequency of Contact (Non-Kin Dif-House") +
ylab("Strain-Sharing Rate (%)") +
scale_y_continuous( limits = c(0, 30), breaks = seq(0, 60, 10)) +
theme_pubr() +
labs_pubr() +
theme(
axis.text.x = element_text(
angle = 0,
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
geom_text(data = free_time_ssr_median_nkh,
color = 'black',
aes(label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
),"%"),
y = 30,
fontface = "bold")) +
scale_x_discrete(labels = paste0(
levels(SN_Non_Kin_House_Symmetrized_2$Meals),
"\n(N=",
table(SN_Non_Kin_House_Symmetrized_2$Meals),
")"
)) +
stat_pvalue_manual(free_time_pvals_nkh, label = "p.adj", tip.length = 0, bracket.size = 0.7, size = 5)+
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#01b0f8",
"#33d600",
"#004672",
"#ff6438",
"#537200",
"#e3006f",
"#9216ff"
))
```
Greeting Types
```{r}
#Difference by greeting types
SN_Greeting <- SN %>% filter(!is.na(a2701))
#Code greetings by epidemiological riskiness as derived from our codebook
SN_Greeting$a2703a[!is.na(SN_Greeting$a2703a)] <- 1
SN_Greeting$a2703a <- as.numeric(SN_Greeting$a2703a)
SN_Greeting$a2703b[!is.na(SN_Greeting$a2703b)] <- 2
SN_Greeting$a2703b <- as.numeric(SN_Greeting$a2703b)
SN_Greeting$a2703c[!is.na(SN_Greeting$a2703c)] <- 3
SN_Greeting$a2703c <- as.numeric(SN_Greeting$a2703c)
SN_Greeting$a2703d[!is.na(SN_Greeting$a2703d)] <- 4
SN_Greeting$a2703d <- as.numeric(SN_Greeting$a2703d)
SN_Greeting$a2703e[!is.na(SN_Greeting$a2703e)] <- 5
SN_Greeting$a2703e <- as.numeric(SN_Greeting$a2703e)
SN_Greeting$a2703f[!is.na(SN_Greeting$a2703f)] <- 6
SN_Greeting$a2703f <- as.numeric(SN_Greeting$a2703f)
SN_Greeting$a2703g[!is.na(SN_Greeting$a2703g)] <- 7
SN_Greeting$a2703g <- as.numeric(SN_Greeting$a2703g)
SN_Greeting$a2703h[!is.na(SN_Greeting$a2703h)] <- 0
SN_Greeting$a2703h <- as.numeric(SN_Greeting$a2703h)
SN_Greeting$a2703[!is.na(SN_Greeting$a2703)] <- 0
SN_Greeting$a2703 <- as.numeric(SN_Greeting$a2703)
SN_Greeting$a2704a[!is.na(SN_Greeting$a2704a)] <- 1
SN_Greeting$a2704a <- as.numeric(SN_Greeting$a2704a)
SN_Greeting$a2704b[!is.na(SN_Greeting$a2704b)] <- 2
SN_Greeting$a2704b <- as.numeric(SN_Greeting$a2704b)
SN_Greeting$a2704c[!is.na(SN_Greeting$a2704c)] <- 3
SN_Greeting$a2704c <- as.numeric(SN_Greeting$a2704c)
SN_Greeting$a2704d[!is.na(SN_Greeting$a2704d)] <- 4
SN_Greeting$a2704d <- as.numeric(SN_Greeting$a2704d)
SN_Greeting$a2704e[!is.na(SN_Greeting$a2704e)] <- 5
SN_Greeting$a2704e <- as.numeric(SN_Greeting$a2704e)
SN_Greeting$a2704f[!is.na(SN_Greeting$a2704f)] <- 6
SN_Greeting$a2704f <- as.numeric(SN_Greeting$a2704f)
SN_Greeting$a2704g[!is.na(SN_Greeting$a2704g)] <- 7
SN_Greeting$a2704g <- as.numeric(SN_Greeting$a2704g)
SN_Greeting$a2704h[!is.na(SN_Greeting$a2703h)] <- 0
SN_Greeting$a2704h <- as.numeric(SN_Greeting$a2704h)
SN_Greeting$a2704[!is.na(SN_Greeting$a2704)] <- 0
SN_Greeting$a2704 <- as.numeric(SN_Greeting$a2704)
#Get most risky greeting by nomination
SN_Greeting$max_greeting <- apply(X = SN_Greeting%>% select(a2703:a2704g),
MARGIN = 1, FUN = max, na.rm = TRUE)
#Group by pairs to get most risky greeting by pair
SN_Greeting <- SN_Greeting %>%
group_by(pair_key) %>%
mutate(max_greeting = max(max_greeting)) %>%
ungroup() %>% distinct(pair_key, .keep_all = TRUE)
readr::write_tsv(SN_Greeting,'data/export/SN_Greeting.tsv')
SN_Greeting <- SN_Greeting %>% mutate(
Riskiest_Greeting = case_when(
max_greeting == 7 ~ "Kiss on the cheek",
max_greeting == 6 ~ "Hug",
max_greeting == 5 ~ "Pat on the back",
max_greeting == 4 ~ "Handshake or hi-five",
max_greeting == 3 ~ "Verbal salute (Hello)",
max_greeting == 2 ~ "A gesture (wave, nod, etc.)",
max_greeting == 1 ~ "A smile",
max_greeting == 0 ~ "Other/Refused",
))
#Drop other/Refused
table(SN_Greeting$Riskiest_Greeting)
SN_Greeting <- SN_Greeting %>% filter(Riskiest_Greeting != "Other/Refused")
SN_Greeting$Riskiest_Greeting <- factor(SN_Greeting$Riskiest_Greeting,
levels=c("Kiss on the cheek",
"Hug",
"Pat on the back",
"Handshake or hi-five",
"Verbal salute (Hello)",
"A smile",
"A gesture (wave, nod, etc.)"))
greeting_median <- aggregate(strain_sharing_rate ~ Riskiest_Greeting,
SN_Greeting, median, na.rm = TRUE)
kruskal.test(strain_sharing_rate ~ Riskiest_Greeting,
data = SN_Greeting)
nrow(SN_Greeting)
greetings_pvals <- compare_means(strain_sharing_rate ~ Riskiest_Greeting,
data = SN_Greeting,
p.adjust.method = "BH"
)
```
```{r}
greeting_plot <- ggplot(SN_Greeting, aes(Riskiest_Greeting, strain_sharing_rate, color = Riskiest_Greeting)) +
geom_quasirandom(size = 1, alpha = 0.5) +
xlab("Greeting Type") +
scale_x_discrete(labels = paste0(
levels(as.factor(SN_Greeting$Riskiest_Greeting)),
"\n(N=",
table(SN_Greeting$Riskiest_Greeting),
")"
)) +
ylab("Strain-Sharing Rate (%)") +
scale_y_continuous( limits = c(0, 60), breaks = seq(0, 60, 10)) +
theme_pubr() +
labs_pubr() +
theme(
axis.text.x = element_text(
vjust = 1,
hjust = .5,
size = 10
),
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(size = 10),
legend.position = "none"
) +
geom_text(data = greeting_median,
color = 'black',
aes(
label = paste0(sprintf(
"%0.1f", round(strain_sharing_rate, digits = 2)
),"%"),
y = 60,
fontface = "bold"
)) +
font("ylab", face = "bold", size = 12) +
scale_color_manual(values = c("#01b0f8",
"#33d600",
"#004672",
"#ff6438",
"#537200",
"#e3006f",
"#9216ff"
)
)
```
```{r}
p1 <- ggarrange(relationships_all_plot,
labels = c("A"),
ncol = 1, nrow = 1,
label.y = 1)
p2 <- ggarrange(free_time_all_plot ,
meals_all_plot ,
labels = c("B", "C"),
align = "h",
ncol = 2,
label.y = 1)
greeting_plot <- ggarrange(greeting_plot,
labels = c("D"),
label.y = 1)
fig1 <- ggarrange(p1, p2,greeting_plot, nrow = 3,
heights = c(1.5,1,1))
svglite("Figures/figure2_full_updated.svg",
fix_text_size = FALSE,
width = 13,
height = 10)
fig1
dev.off()
```
Greeting Type non-kin dif-house
```{r}
#Difference by greeting types
SN_Greeting_Non_Kin_House <- SN_Non_Kin_House %>% filter(!is.na(a2701))
#Code greetings by epidemiological riskiness as derived from our codebook
SN_Greeting_Non_Kin_House$a2703a[!is.na(SN_Greeting_Non_Kin_House$a2703a)] <- 1
SN_Greeting_Non_Kin_House$a2703a <- as.numeric(SN_Greeting_Non_Kin_House$a2703a)
SN_Greeting_Non_Kin_House$a2703b[!is.na(SN_Greeting_Non_Kin_House$a2703b)] <- 2
SN_Greeting_Non_Kin_House$a2703b <- as.numeric(SN_Greeting_Non_Kin_House$a2703b)
SN_Greeting_Non_Kin_House$a2703c[!is.na(SN_Greeting_Non_Kin_House$a2703c)] <- 3
SN_Greeting_Non_Kin_House$a2703c <- as.numeric(SN_Greeting_Non_Kin_House$a2703c)
SN_Greeting_Non_Kin_House$a2703d[!is.na(SN_Greeting_Non_Kin_House$a2703d)] <- 4
SN_Greeting_Non_Kin_House$a2703d <- as.numeric(SN_Greeting_Non_Kin_House$a2703d)
SN_Greeting_Non_Kin_House$a2703e[!is.na(SN_Greeting_Non_Kin_House$a2703e)] <- 5
SN_Greeting_Non_Kin_House$a2703e <- as.numeric(SN_Greeting_Non_Kin_House$a2703e)
SN_Greeting_Non_Kin_House$a2703f[!is.na(SN_Greeting_Non_Kin_House$a2703f)] <- 6
SN_Greeting_Non_Kin_House$a2703f <- as.numeric(SN_Greeting_Non_Kin_House$a2703f)
SN_Greeting_Non_Kin_House$a2703g[!is.na(SN_Greeting_Non_Kin_House$a2703g)] <- 7
SN_Greeting_Non_Kin_House$a2703g <- as.numeric(SN_Greeting_Non_Kin_House$a2703g)
SN_Greeting_Non_Kin_House$a2703h[!is.na(SN_Greeting_Non_Kin_House$a2703h)] <- 0
SN_Greeting_Non_Kin_House$a2703h <- as.numeric(SN_Greeting_Non_Kin_House$a2703h)
SN_Greeting_Non_Kin_House$a2703[!is.na(SN_Greeting_Non_Kin_House$a2703)] <- 0
SN_Greeting_Non_Kin_House$a2703 <- as.numeric(SN_Greeting_Non_Kin_House$a2703)
SN_Greeting_Non_Kin_House$a2704a[!is.na(SN_Greeting_Non_Kin_House$a2704a)] <- 1
SN_Greeting_Non_Kin_House$a2704a <- as.numeric(SN_Greeting_Non_Kin_House$a2704a)
SN_Greeting_Non_Kin_House$a2704b[!is.na(SN_Greeting_Non_Kin_House$a2704b)] <- 2
SN_Greeting_Non_Kin_House$a2704b <- as.numeric(SN_Greeting_Non_Kin_House$a2704b)
SN_Greeting_Non_Kin_House$a2704c[!is.na(SN_Greeting_Non_Kin_House$a2704c)] <- 3
SN_Greeting_Non_Kin_House$a2704c <- as.numeric(SN_Greeting_Non_Kin_House$a2704c)
SN_Greeting_Non_Kin_House$a2704d[!is.na(SN_Greeting_Non_Kin_House$a2704d)] <- 4
SN_Greeting_Non_Kin_House$a2704d <- as.numeric(SN_Greeting_Non_Kin_House$a2704d)
SN_Greeting_Non_Kin_House$a2704e[!is.na(SN_Greeting_Non_Kin_House$a2704e)] <- 5
SN_Greeting_Non_Kin_House$a2704e <- as.numeric(SN_Greeting_Non_Kin_House$a2704e)
SN_Greeting_Non_Kin_House$a2704f[!is.na(SN_Greeting_Non_Kin_House$a2704f)] <- 6
SN_Greeting_Non_Kin_House$a2704f <- as.numeric(SN_Greeting_Non_Kin_House$a2704f)
SN_Greeting_Non_Kin_House$a2704g[!is.na(SN_Greeting_Non_Kin_House$a2704g)] <- 7
SN_Greeting_Non_Kin_House$a2704g <- as.numeric(SN_Greeting_Non_Kin_House$a2704g)
SN_Greeting_Non_Kin_House$a2704h[!is.na(SN_Greeting_Non_Kin_House$a2703h)] <- 0
SN_Greeting_Non_Kin_House$a2704h <- as.numeric(SN_Greeting_Non_Kin_House$a2704h)
SN_Greeting_Non_Kin_House$a2704[!is.na(SN_Greeting_Non_Kin_House$a2704)] <- 0
SN_Greeting_Non_Kin_House$a2704 <- as.numeric(SN_Greeting_Non_Kin_House$a2704)
#Get most risky greeting by nomination
SN_Greeting_Non_Kin_House$max_greeting <- apply(X = SN_Greeting_Non_Kin_House%>% select(a2703:a2704g),
MARGIN = 1, FUN = max, na.rm = TRUE)
#Group by pairs to get most risky greeting by pair
SN_Greeting_Non_Kin_House <- SN_Greeting_Non_Kin_House %>%
group_by(pair_key) %>%
mutate(max_greeting = max(max_greeting)) %>%
ungroup() %>% distinct(pair_key, .keep_all = TRUE)
SN_Greeting_Non_Kin_House <- SN_Greeting_Non_Kin_House %>% mutate(
Riskiest_Greeting = case_when(