1 Exploratory Analysis

1.1 EA: Period prevalence IMI

table1(~IMI.cow + staph.aureus.IMI.cow + nas.IMI.cow + sch.IMI.cow + nas.non.chrom.IMI.cow + staph.epidermidis.IMI.cow + staph.haemolyticus.IMI.cow + staph.hominis.IMI.cow + staph.sciuri.IMI.cow + staph.xylosus.saprophyticus.IMI.cow + staph.spp.IMI.cow + strep.IMI.cow + strep.genus.IMI.cow + strep.uberis.IMI.cow + strep.dys.IMI.cow + strep.sp.IMI.cow + strep.like.IMI.cow + aerococcus.spp.IMI.cow + aero.vir.IMI.cow + aero.spp.IMI.cow + enterococcus.spp.IMI.cow + entero.cass.IMI.cow + entero.hyr.IMI.cow + entero.mun.IMI.cow + entero.spp.IMI.cow + entero.fae.IMI.cow + entero.sac.IMI.cow + lactococcus.spp.IMI.cow + lact.garv.IMI.cow + lact.lact.IMI.cow + lact.spp.IMI.cow + micro.spp.IMI.cow + gram.neg.IMI.cow + escherichia.spp.IMI.cow + e.coli.IMI.cow + e.spp.IMI.cow + kleb.IMI.cow + pseud.IMI.cow + citr.IMI.cow + enterobacter.genus.IMI.cow + ent.clo.IMI.cow+ent.spp.IMI.cow + gram.neg.org.IMI.cow + pantoea.IMI.cow + serratia.IMI.cow + other.IMI.cow + arthro.gand.IMI.cow + arthro.spp.IMI.cow + bacillus.IMI.cow + coryne.IMI.cow + gram.pos.cocci.IMI.cow + gram.pos.rod.IMI.cow + helcoccus.IMI.cow + trueperella.IMI.cow + yeast.IMI.cow |Site , data = df_dhi_cow)
A
(N=49)
B
(N=116)
C
(N=105)
E
(N=63)
Overall
(N=333)
IMI.cow
0 26 (53.1%) 10 (8.6%) 5 (4.8%) 0 (0%) 41 (12.3%)
1 23 (46.9%) 106 (91.4%) 100 (95.2%) 63 (100%) 292 (87.7%)
staph.aureus.IMI.cow
0 44 (89.8%) 84 (72.4%) 95 (90.5%) 37 (58.7%) 260 (78.1%)
1 5 (10.2%) 32 (27.6%) 10 (9.5%) 26 (41.3%) 73 (21.9%)
nas.IMI.cow
0 35 (71.4%) 46 (39.7%) 62 (59.0%) 23 (36.5%) 166 (49.8%)
1 14 (28.6%) 70 (60.3%) 43 (41.0%) 40 (63.5%) 167 (50.2%)
sch.IMI.cow
0 37 (75.5%) 68 (58.6%) 73 (69.5%) 34 (54.0%) 212 (63.7%)
1 12 (24.5%) 48 (41.4%) 32 (30.5%) 29 (46.0%) 121 (36.3%)
nas.non.chrom.IMI.cow
0 46 (93.9%) 82 (70.7%) 92 (87.6%) 40 (63.5%) 260 (78.1%)
1 3 (6.1%) 34 (29.3%) 13 (12.4%) 23 (36.5%) 73 (21.9%)
staph.epidermidis.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 63 (100%) 333 (100%)
1 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
staph.haemolyticus.IMI.cow
0 49 (100%) 113 (97.4%) 104 (99.0%) 61 (96.8%) 327 (98.2%)
1 0 (0%) 3 (2.6%) 1 (1.0%) 2 (3.2%) 6 (1.8%)
staph.hominis.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 62 (98.4%) 332 (99.7%)
1 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 1 (0.3%)
staph.sciuri.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 59 (93.7%) 329 (98.8%)
1 0 (0%) 0 (0%) 0 (0%) 4 (6.3%) 4 (1.2%)
staph.xylosus.saprophyticus.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
staph.spp.IMI.cow
0 46 (93.9%) 84 (72.4%) 94 (89.5%) 44 (69.8%) 268 (80.5%)
1 3 (6.1%) 32 (27.6%) 11 (10.5%) 19 (30.2%) 65 (19.5%)
strep.IMI.cow
0 45 (91.8%) 81 (69.8%) 61 (58.1%) 25 (39.7%) 212 (63.7%)
1 4 (8.2%) 35 (30.2%) 44 (41.9%) 38 (60.3%) 121 (36.3%)
strep.genus.IMI.cow
0 48 (98.0%) 87 (75.0%) 96 (91.4%) 35 (55.6%) 266 (79.9%)
1 1 (2.0%) 29 (25.0%) 9 (8.6%) 28 (44.4%) 67 (20.1%)
strep.uberis.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 54 (85.7%) 324 (97.3%)
1 0 (0%) 0 (0%) 0 (0%) 9 (14.3%) 9 (2.7%)
strep.dys.IMI.cow
0 48 (98.0%) 91 (78.4%) 103 (98.1%) 48 (76.2%) 290 (87.1%)
1 1 (2.0%) 25 (21.6%) 2 (1.9%) 15 (23.8%) 43 (12.9%)
strep.sp.IMI.cow
0 49 (100%) 103 (88.8%) 97 (92.4%) 53 (84.1%) 302 (90.7%)
1 0 (0%) 13 (11.2%) 8 (7.6%) 10 (15.9%) 31 (9.3%)
strep.like.IMI.cow
0 46 (93.9%) 110 (94.8%) 66 (62.9%) 51 (81.0%) 273 (82.0%)
1 3 (6.1%) 6 (5.2%) 39 (37.1%) 12 (19.0%) 60 (18.0%)
aerococcus.spp.IMI.cow
0 47 (95.9%) 116 (100%) 102 (97.1%) 54 (85.7%) 319 (95.8%)
1 2 (4.1%) 0 (0%) 3 (2.9%) 9 (14.3%) 14 (4.2%)
aero.vir.IMI.cow
0 48 (98.0%) 116 (100%) 102 (97.1%) 56 (88.9%) 322 (96.7%)
1 1 (2.0%) 0 (0%) 3 (2.9%) 7 (11.1%) 11 (3.3%)
aero.spp.IMI.cow
0 48 (98.0%) 116 (100%) 105 (100%) 60 (95.2%) 329 (98.8%)
1 1 (2.0%) 0 (0%) 0 (0%) 3 (4.8%) 4 (1.2%)
enterococcus.spp.IMI.cow
0 49 (100%) 111 (95.7%) 72 (68.6%) 62 (98.4%) 294 (88.3%)
1 0 (0%) 5 (4.3%) 33 (31.4%) 1 (1.6%) 39 (11.7%)
entero.cass.IMI.cow
0 49 (100%) 115 (99.1%) 94 (89.5%) 63 (100%) 321 (96.4%)
1 0 (0%) 1 (0.9%) 11 (10.5%) 0 (0%) 12 (3.6%)
entero.hyr.IMI.cow
0 49 (100%) 116 (100%) 97 (92.4%) 63 (100%) 325 (97.6%)
1 0 (0%) 0 (0%) 8 (7.6%) 0 (0%) 8 (2.4%)
entero.mun.IMI.cow
0 49 (100%) 115 (99.1%) 96 (91.4%) 63 (100%) 323 (97.0%)
1 0 (0%) 1 (0.9%) 9 (8.6%) 0 (0%) 10 (3.0%)
entero.spp.IMI.cow
0 49 (100%) 113 (97.4%) 95 (90.5%) 62 (98.4%) 319 (95.8%)
1 0 (0%) 3 (2.6%) 10 (9.5%) 1 (1.6%) 14 (4.2%)
entero.fae.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
entero.sac.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
lactococcus.spp.IMI.cow
0 48 (98.0%) 116 (100%) 101 (96.2%) 63 (100%) 328 (98.5%)
1 1 (2.0%) 0 (0%) 4 (3.8%) 0 (0%) 5 (1.5%)
lact.garv.IMI.cow
0 48 (98.0%) 116 (100%) 103 (98.1%) 63 (100%) 330 (99.1%)
1 1 (2.0%) 0 (0%) 2 (1.9%) 0 (0%) 3 (0.9%)
lact.lact.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
lact.spp.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
micro.spp.IMI.cow
0 49 (100%) 115 (99.1%) 104 (99.0%) 61 (96.8%) 329 (98.8%)
1 0 (0%) 1 (0.9%) 1 (1.0%) 2 (3.2%) 4 (1.2%)
gram.neg.IMI.cow
0 49 (100%) 110 (94.8%) 83 (79.0%) 60 (95.2%) 302 (90.7%)
1 0 (0%) 6 (5.2%) 22 (21.0%) 3 (4.8%) 31 (9.3%)
escherichia.spp.IMI.cow
0 49 (100%) 112 (96.6%) 101 (96.2%) 61 (96.8%) 323 (97.0%)
1 0 (0%) 4 (3.4%) 4 (3.8%) 2 (3.2%) 10 (3.0%)
e.coli.IMI.cow
0 49 (100%) 112 (96.6%) 101 (96.2%) 61 (96.8%) 323 (97.0%)
1 0 (0%) 4 (3.4%) 4 (3.8%) 2 (3.2%) 10 (3.0%)
e.spp.IMI.cow
0 49 (100%) 115 (99.1%) 105 (100%) 62 (98.4%) 331 (99.4%)
1 0 (0%) 1 (0.9%) 0 (0%) 1 (1.6%) 2 (0.6%)
kleb.IMI.cow
0 49 (100%) 116 (100%) 102 (97.1%) 63 (100%) 330 (99.1%)
1 0 (0%) 0 (0%) 3 (2.9%) 0 (0%) 3 (0.9%)
pseud.IMI.cow
0 49 (100%) 116 (100%) 102 (97.1%) 63 (100%) 330 (99.1%)
1 0 (0%) 0 (0%) 3 (2.9%) 0 (0%) 3 (0.9%)
citr.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
enterobacter.genus.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 62 (98.4%) 331 (99.4%)
1 0 (0%) 0 (0%) 1 (1.0%) 1 (1.6%) 2 (0.6%)
ent.clo.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 62 (98.4%) 332 (99.7%)
1 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 1 (0.3%)
ent.spp.IMI.cow
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
gram.neg.org.IMI.cow
0 49 (100%) 115 (99.1%) 102 (97.1%) 63 (100%) 329 (98.8%)
1 0 (0%) 1 (0.9%) 3 (2.9%) 0 (0%) 4 (1.2%)
pantoea.IMI.cow
0 49 (100%) 116 (100%) 102 (97.1%) 63 (100%) 330 (99.1%)
1 0 (0%) 0 (0%) 3 (2.9%) 0 (0%) 3 (0.9%)
serratia.IMI.cow
0 49 (100%) 116 (100%) 102 (97.1%) 63 (100%) 330 (99.1%)
1 0 (0%) 0 (0%) 3 (2.9%) 0 (0%) 3 (0.9%)
other.IMI.cow
0 46 (93.9%) 82 (70.7%) 30 (28.6%) 51 (81.0%) 209 (62.8%)
1 3 (6.1%) 34 (29.3%) 75 (71.4%) 12 (19.0%) 124 (37.2%)
arthro.gand.IMI.cow
0 49 (100%) 115 (99.1%) 104 (99.0%) 63 (100%) 331 (99.4%)
1 0 (0%) 1 (0.9%) 1 (1.0%) 0 (0%) 2 (0.6%)
arthro.spp.IMI.cow
0 49 (100%) 115 (99.1%) 105 (100%) 63 (100%) 332 (99.7%)
1 0 (0%) 1 (0.9%) 0 (0%) 0 (0%) 1 (0.3%)
bacillus.IMI.cow
0 49 (100%) 98 (84.5%) 54 (51.4%) 53 (84.1%) 254 (76.3%)
1 0 (0%) 18 (15.5%) 51 (48.6%) 10 (15.9%) 79 (23.7%)
coryne.IMI.cow
0 48 (98.0%) 106 (91.4%) 63 (60.0%) 63 (100%) 280 (84.1%)
1 1 (2.0%) 10 (8.6%) 42 (40.0%) 0 (0%) 53 (15.9%)
gram.pos.cocci.IMI.cow
0 49 (100%) 113 (97.4%) 94 (89.5%) 63 (100%) 319 (95.8%)
1 0 (0%) 3 (2.6%) 11 (10.5%) 0 (0%) 14 (4.2%)
gram.pos.rod.IMI.cow
0 49 (100%) 115 (99.1%) 105 (100%) 63 (100%) 332 (99.7%)
1 0 (0%) 1 (0.9%) 0 (0%) 0 (0%) 1 (0.3%)
helcoccus.IMI.cow
0 49 (100%) 116 (100%) 105 (100%) 62 (98.4%) 332 (99.7%)
1 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 1 (0.3%)
trueperella.IMI.cow
0 48 (98.0%) 115 (99.1%) 105 (100%) 63 (100%) 331 (99.4%)
1 1 (2.0%) 1 (0.9%) 0 (0%) 0 (0%) 2 (0.6%)
yeast.IMI.cow
0 49 (100%) 114 (98.3%) 105 (100%) 63 (100%) 331 (99.4%)
1 0 (0%) 2 (1.7%) 0 (0%) 0 (0%) 2 (0.6%)

1.2 EA: Period prevalence persistent IMI

table1(~staph.aureus.IMI.cow.p + nas.IMI.cow.p + sch.IMI.cow.p + nas.non.chrom.IMI.cow.p + staph.epidermidis.IMI.cow.p + staph.haemolyticus.IMI.cow.p + staph.hominis.IMI.cow.p + staph.sciuri.IMI.cow.p + staph.xylosus.saprophyticus.IMI.cow.p + staph.spp.IMI.cow.p + strep.IMI.cow.p + strep.genus.IMI.cow.p + strep.uberis.IMI.cow.p + strep.dys.IMI.cow.p + strep.sp.IMI.cow.p + strep.like.IMI.cow.p + aerococcus.spp.IMI.cow.p + aero.vir.IMI.cow.p + aero.spp.IMI.cow.p + enterococcus.spp.IMI.cow.p + entero.cass.IMI.cow.p + entero.hyr.IMI.cow.p + entero.mun.IMI.cow.p + entero.spp.IMI.cow.p + entero.fae.IMI.cow.p + entero.sac.IMI.cow.p +
lactococcus.spp.IMI.cow.p + lact.garv.IMI.cow.p + lact.lact.IMI.cow.p + lact.spp.IMI.cow.p +
  micro.spp.IMI.cow.p + arthro.gand.IMI.cow.p + arthro.spp.IMI.cow.p + bacillus.IMI.cow.p + coryne.IMI.cow.p + gram.pos.cocci.IMI.cow.p + gram.pos.rod.IMI.cow.p + helcoccus.IMI.cow.p + trueperella.IMI.cow.p + yeast.IMI.cow.p |Site , data = df_dhi_cow)
A
(N=49)
B
(N=116)
C
(N=105)
E
(N=63)
Overall
(N=333)
staph.aureus.IMI.cow.p
0 44 (89.8%) 84 (72.4%) 95 (90.5%) 37 (58.7%) 260 (78.1%)
1 1 (2.0%) 3 (2.6%) 0 (0%) 6 (9.5%) 10 (3.0%)
2 4 (8.2%) 29 (25.0%) 10 (9.5%) 20 (31.7%) 63 (18.9%)
nas.IMI.cow.p
0 35 (71.4%) 46 (39.7%) 62 (59.0%) 23 (36.5%) 166 (49.8%)
1 4 (8.2%) 23 (19.8%) 28 (26.7%) 18 (28.6%) 73 (21.9%)
2 10 (20.4%) 47 (40.5%) 15 (14.3%) 22 (34.9%) 94 (28.2%)
sch.IMI.cow.p
0 37 (75.5%) 68 (58.6%) 73 (69.5%) 34 (54.0%) 212 (63.7%)
1 2 (4.1%) 10 (8.6%) 18 (17.1%) 11 (17.5%) 41 (12.3%)
2 10 (20.4%) 38 (32.8%) 14 (13.3%) 18 (28.6%) 80 (24.0%)
nas.non.chrom.IMI.cow.p
0 46 (93.9%) 82 (70.7%) 92 (87.6%) 40 (63.5%) 260 (78.1%)
1 3 (6.1%) 20 (17.2%) 12 (11.4%) 16 (25.4%) 51 (15.3%)
2 0 (0%) 14 (12.1%) 1 (1.0%) 7 (11.1%) 22 (6.6%)
staph.epidermidis.IMI.cow.p
0 49 (100%) 116 (100%) 105 (100%) 63 (100%) 333 (100%)
1 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
staph.haemolyticus.IMI.cow.p
0 49 (100%) 113 (97.4%) 104 (99.0%) 61 (96.8%) 327 (98.2%)
1 0 (0%) 3 (2.6%) 1 (1.0%) 2 (3.2%) 6 (1.8%)
staph.hominis.IMI.cow.p
0 49 (100%) 116 (100%) 105 (100%) 62 (98.4%) 332 (99.7%)
1 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 1 (0.3%)
staph.sciuri.IMI.cow.p
0 49 (100%) 116 (100%) 105 (100%) 59 (93.7%) 329 (98.8%)
1 0 (0%) 0 (0%) 0 (0%) 4 (6.3%) 4 (1.2%)
staph.xylosus.saprophyticus.IMI.cow.p
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
2 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
staph.spp.IMI.cow.p
0 46 (93.9%) 84 (72.4%) 94 (89.5%) 44 (69.8%) 268 (80.5%)
1 3 (6.1%) 18 (15.5%) 10 (9.5%) 12 (19.0%) 43 (12.9%)
2 0 (0%) 14 (12.1%) 1 (1.0%) 7 (11.1%) 22 (6.6%)
strep.IMI.cow.p
0 45 (91.8%) 81 (69.8%) 61 (58.1%) 25 (39.7%) 212 (63.7%)
1 3 (6.1%) 19 (16.4%) 40 (38.1%) 24 (38.1%) 86 (25.8%)
2 1 (2.0%) 16 (13.8%) 4 (3.8%) 14 (22.2%) 35 (10.5%)
strep.genus.IMI.cow.p
0 48 (98.0%) 87 (75.0%) 96 (91.4%) 35 (55.6%) 266 (79.9%)
1 0 (0%) 14 (12.1%) 8 (7.6%) 14 (22.2%) 36 (10.8%)
2 1 (2.0%) 15 (12.9%) 1 (1.0%) 14 (22.2%) 31 (9.3%)
strep.uberis.IMI.cow.p
0 49 (100%) 116 (100%) 105 (100%) 54 (85.7%) 324 (97.3%)
1 0 (0%) 0 (0%) 0 (0%) 5 (7.9%) 5 (1.5%)
2 0 (0%) 0 (0%) 0 (0%) 4 (6.3%) 4 (1.2%)
strep.dys.IMI.cow.p
0 48 (98.0%) 91 (78.4%) 103 (98.1%) 48 (76.2%) 290 (87.1%)
1 0 (0%) 14 (12.1%) 1 (1.0%) 6 (9.5%) 21 (6.3%)
2 1 (2.0%) 11 (9.5%) 1 (1.0%) 9 (14.3%) 22 (6.6%)
strep.sp.IMI.cow.p
0 49 (100%) 103 (88.8%) 97 (92.4%) 53 (84.1%) 302 (90.7%)
1 0 (0%) 8 (6.9%) 7 (6.7%) 7 (11.1%) 22 (6.6%)
2 0 (0%) 5 (4.3%) 1 (1.0%) 3 (4.8%) 9 (2.7%)
strep.like.IMI.cow.p
0 46 (93.9%) 110 (94.8%) 66 (62.9%) 51 (81.0%) 273 (82.0%)
1 3 (6.1%) 4 (3.4%) 35 (33.3%) 10 (15.9%) 52 (15.6%)
2 0 (0%) 2 (1.7%) 4 (3.8%) 2 (3.2%) 8 (2.4%)
aerococcus.spp.IMI.cow.p
0 47 (95.9%) 116 (100%) 102 (97.1%) 54 (85.7%) 319 (95.8%)
1 2 (4.1%) 0 (0%) 3 (2.9%) 9 (14.3%) 14 (4.2%)
2 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
aero.vir.IMI.cow.p
0 48 (98.0%) 116 (100%) 102 (97.1%) 56 (88.9%) 322 (96.7%)
1 1 (2.0%) 0 (0%) 3 (2.9%) 7 (11.1%) 11 (3.3%)
2 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
aero.spp.IMI.cow.p
0 48 (98.0%) 116 (100%) 105 (100%) 60 (95.2%) 329 (98.8%)
1 1 (2.0%) 0 (0%) 0 (0%) 3 (4.8%) 4 (1.2%)
enterococcus.spp.IMI.cow.p
0 49 (100%) 111 (95.7%) 72 (68.6%) 62 (98.4%) 294 (88.3%)
1 0 (0%) 4 (3.4%) 30 (28.6%) 1 (1.6%) 35 (10.5%)
2 0 (0%) 1 (0.9%) 3 (2.9%) 0 (0%) 4 (1.2%)
entero.cass.IMI.cow.p
0 49 (100%) 115 (99.1%) 94 (89.5%) 63 (100%) 321 (96.4%)
1 0 (0%) 1 (0.9%) 10 (9.5%) 0 (0%) 11 (3.3%)
2 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
entero.hyr.IMI.cow.p
0 49 (100%) 116 (100%) 97 (92.4%) 63 (100%) 325 (97.6%)
1 0 (0%) 0 (0%) 8 (7.6%) 0 (0%) 8 (2.4%)
entero.mun.IMI.cow.p
0 49 (100%) 115 (99.1%) 96 (91.4%) 63 (100%) 323 (97.0%)
1 0 (0%) 1 (0.9%) 9 (8.6%) 0 (0%) 10 (3.0%)
entero.spp.IMI.cow.p
0 49 (100%) 113 (97.4%) 95 (90.5%) 62 (98.4%) 319 (95.8%)
1 0 (0%) 2 (1.7%) 8 (7.6%) 1 (1.6%) 11 (3.3%)
2 0 (0%) 1 (0.9%) 2 (1.9%) 0 (0%) 3 (0.9%)
entero.fae.IMI.cow.p
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
entero.sac.IMI.cow.p
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
lactococcus.spp.IMI.cow.p
0 48 (98.0%) 116 (100%) 101 (96.2%) 63 (100%) 328 (98.5%)
1 1 (2.0%) 0 (0%) 4 (3.8%) 0 (0%) 5 (1.5%)
2 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
lact.garv.IMI.cow.p
0 48 (98.0%) 116 (100%) 103 (98.1%) 63 (100%) 330 (99.1%)
1 1 (2.0%) 0 (0%) 2 (1.9%) 0 (0%) 3 (0.9%)
lact.lact.IMI.cow.p
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
2 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
lact.spp.IMI.cow.p
0 49 (100%) 116 (100%) 104 (99.0%) 63 (100%) 332 (99.7%)
1 0 (0%) 0 (0%) 1 (1.0%) 0 (0%) 1 (0.3%)
micro.spp.IMI.cow.p
0 49 (100%) 115 (99.1%) 104 (99.0%) 61 (96.8%) 329 (98.8%)
1 0 (0%) 1 (0.9%) 1 (1.0%) 2 (3.2%) 4 (1.2%)
arthro.gand.IMI.cow.p
0 49 (100%) 115 (99.1%) 104 (99.0%) 63 (100%) 331 (99.4%)
1 0 (0%) 1 (0.9%) 1 (1.0%) 0 (0%) 2 (0.6%)
arthro.spp.IMI.cow.p
0 49 (100%) 115 (99.1%) 105 (100%) 63 (100%) 332 (99.7%)
1 0 (0%) 1 (0.9%) 0 (0%) 0 (0%) 1 (0.3%)
bacillus.IMI.cow.p
0 49 (100%) 98 (84.5%) 54 (51.4%) 53 (84.1%) 254 (76.3%)
1 0 (0%) 16 (13.8%) 35 (33.3%) 10 (15.9%) 61 (18.3%)
2 0 (0%) 2 (1.7%) 16 (15.2%) 0 (0%) 18 (5.4%)
coryne.IMI.cow.p
0 48 (98.0%) 106 (91.4%) 63 (60.0%) 63 (100%) 280 (84.1%)
1 1 (2.0%) 10 (8.6%) 30 (28.6%) 0 (0%) 41 (12.3%)
2 0 (0%) 0 (0%) 12 (11.4%) 0 (0%) 12 (3.6%)
gram.pos.cocci.IMI.cow.p
0 49 (100%) 113 (97.4%) 94 (89.5%) 63 (100%) 319 (95.8%)
1 0 (0%) 3 (2.6%) 11 (10.5%) 0 (0%) 14 (4.2%)
gram.pos.rod.IMI.cow.p
0 49 (100%) 115 (99.1%) 105 (100%) 63 (100%) 332 (99.7%)
1 0 (0%) 1 (0.9%) 0 (0%) 0 (0%) 1 (0.3%)
helcoccus.IMI.cow.p
0 49 (100%) 116 (100%) 105 (100%) 62 (98.4%) 332 (99.7%)
1 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 1 (0.3%)
trueperella.IMI.cow.p
0 48 (98.0%) 115 (99.1%) 105 (100%) 63 (100%) 331 (99.4%)
1 1 (2.0%) 1 (0.9%) 0 (0%) 0 (0%) 2 (0.6%)
yeast.IMI.cow.p
0 49 (100%) 114 (98.3%) 105 (100%) 63 (100%) 331 (99.4%)
1 0 (0%) 2 (1.7%) 0 (0%) 0 (0%) 2 (0.6%)

1.3 EA: Association between IMI and high SCC

table1(~as.factor(high_scc_cow)| staph.aureus.IMI.cow,data=df_survival)
0
(N=260)
1
(N=73)
Overall
(N=333)
as.factor(high_scc_cow)
0 144 (55.4%) 11 (15.1%) 155 (46.5%)
1 116 (44.6%) 62 (84.9%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| nas.IMI.cow,data=df_survival)
0
(N=166)
1
(N=167)
Overall
(N=333)
as.factor(high_scc_cow)
0 86 (51.8%) 69 (41.3%) 155 (46.5%)
1 80 (48.2%) 98 (58.7%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| sch.IMI.cow,data=df_survival)
0
(N=212)
1
(N=121)
Overall
(N=333)
as.factor(high_scc_cow)
0 107 (50.5%) 48 (39.7%) 155 (46.5%)
1 105 (49.5%) 73 (60.3%) 178 (53.5%)
kmsurv <- survfit(survival ~ sch.IMI.cow, data = df_survival)
summary(kmsurv, times=c(1,2,3,4,5,6))
## Call: survfit(formula = survival ~ sch.IMI.cow, data = df_survival)
## 
##                 sch.IMI.cow=0 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    212      68    0.679  0.0321        0.619        0.745
##     2    136      16    0.599  0.0339        0.536        0.670
##     3    108       4    0.577  0.0345        0.513        0.649
##     4    100       5    0.548  0.0351        0.484        0.621
##     5     93       3    0.531  0.0354        0.466        0.605
##     6     84       9    0.474  0.0363        0.408        0.551
## 
##                 sch.IMI.cow=1 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    121      32    0.736  0.0401        0.661        0.818
##     2     83      17    0.585  0.0456        0.502        0.681
##     3     63       6    0.529  0.0466        0.445        0.629
##     4     52       7    0.458  0.0475        0.374        0.561
##     5     45       3    0.427  0.0474        0.344        0.531
##     6     38       8    0.337  0.0469        0.257        0.443
table1(~as.factor(high_scc_cow)| nas.non.chrom.IMI.cow,data=df_survival)
0
(N=260)
1
(N=73)
Overall
(N=333)
as.factor(high_scc_cow)
0 126 (48.5%) 29 (39.7%) 155 (46.5%)
1 134 (51.5%) 44 (60.3%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| staph.spp.IMI.cow,data=df_survival)
0
(N=268)
1
(N=65)
Overall
(N=333)
as.factor(high_scc_cow)
0 129 (48.1%) 26 (40.0%) 155 (46.5%)
1 139 (51.9%) 39 (60.0%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| strep.IMI.cow,data=df_survival)
0
(N=212)
1
(N=121)
Overall
(N=333)
as.factor(high_scc_cow)
0 112 (52.8%) 43 (35.5%) 155 (46.5%)
1 100 (47.2%) 78 (64.5%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| strep.genus.IMI.cow,data=df_survival)
0
(N=266)
1
(N=67)
Overall
(N=333)
as.factor(high_scc_cow)
0 141 (53.0%) 14 (20.9%) 155 (46.5%)
1 125 (47.0%) 53 (79.1%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| strep.dys.IMI.cow,data=df_survival)
0
(N=290)
1
(N=43)
Overall
(N=333)
as.factor(high_scc_cow)
0 147 (50.7%) 8 (18.6%) 155 (46.5%)
1 143 (49.3%) 35 (81.4%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| strep.sp.IMI.cow,data=df_survival)
0
(N=302)
1
(N=31)
Overall
(N=333)
as.factor(high_scc_cow)
0 148 (49.0%) 7 (22.6%) 155 (46.5%)
1 154 (51.0%) 24 (77.4%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| strep.like.IMI.cow,data=df_survival)
0
(N=273)
1
(N=60)
Overall
(N=333)
as.factor(high_scc_cow)
0 123 (45.1%) 32 (53.3%) 155 (46.5%)
1 150 (54.9%) 28 (46.7%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| as.factor(enterococcus.spp.IMI.cow),data=df_survival)
0
(N=294)
1
(N=39)
Overall
(N=333)
as.factor(high_scc_cow)
0 133 (45.2%) 22 (56.4%) 155 (46.5%)
1 161 (54.8%) 17 (43.6%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| as.factor(gram.neg.IMI.cow),data=df_survival)
0
(N=302)
1
(N=31)
Overall
(N=333)
as.factor(high_scc_cow)
0 140 (46.4%) 15 (48.4%) 155 (46.5%)
1 162 (53.6%) 16 (51.6%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| as.factor(other.IMI.cow),data=df_survival)
0
(N=209)
1
(N=124)
Overall
(N=333)
as.factor(high_scc_cow)
0 79 (37.8%) 76 (61.3%) 155 (46.5%)
1 130 (62.2%) 48 (38.7%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| as.factor(coryne.IMI.cow),data=df_survival)
0
(N=280)
1
(N=53)
Overall
(N=333)
as.factor(high_scc_cow)
0 124 (44.3%) 31 (58.5%) 155 (46.5%)
1 156 (55.7%) 22 (41.5%) 178 (53.5%)
table1(~as.factor(high_scc_cow)| as.factor(bacillus.IMI.cow),data=df_survival)
0
(N=254)
1
(N=79)
Overall
(N=333)
as.factor(high_scc_cow)
0 100 (39.4%) 55 (69.6%) 155 (46.5%)
1 154 (60.6%) 24 (30.4%) 178 (53.5%)

1.4 EA: Association between IMI and milk production

table1(~milk_kg+ as.factor(high_scc_cow)|Site:postpartum_sample,data=df.per.cow_dhi)
## Warning in .table1.internal(x = x, labels = labels, groupspan = groupspan, :
## Table has 29 columns. Are you sure this is what you want?
A
B
C
E
Overall
1
(N=47)
2
(N=40)
3
(N=40)
5
(N=30)
6
(N=27)
1
(N=96)
2
(N=115)
3
(N=112)
4
(N=108)
5
(N=106)
6
(N=107)
1
(N=105)
2
(N=94)
3
(N=84)
4
(N=80)
5
(N=77)
6
(N=69)
1
(N=62)
2
(N=61)
3
(N=55)
4
(N=54)
5
(N=53)
6
(N=52)
1
(N=310)
2
(N=310)
3
(N=291)
4
(N=242)
5
(N=266)
6
(N=255)
milk_kg
Mean (SD) 22.5 (5.13) 25.5 (7.71) 24.5 (6.36) 19.2 (8.02) 12.9 (5.66) 29.2 (8.87) 30.6 (6.88) 30.7 (7.16) 30.1 (6.08) 31.5 (7.64) 30.0 (6.49) 26.1 (7.27) 29.8 (4.74) 28.9 (5.14) 27.7 (4.71) 26.2 (4.73) 24.4 (5.12) 17.6 (6.75) 18.5 (6.70) 15.9 (6.88) 16.0 (8.09) 14.5 (7.29) 13.1 (5.76) 24.8 (8.52) 27.3 (7.88) 26.5 (8.48) 26.2 (8.30) 25.2 (9.50) 23.2 (9.30)
Median [Min, Max] 22.2 [9.98, 32.7] 24.3 [4.99, 59.0] 24.0 [2.72, 36.3] 17.5 [4.08, 35.8] 12.7 [2.27, 25.4] 28.8 [2.72, 54.4] 30.4 [10.9, 46.3] 30.4 [13.6, 54.9] 29.9 [16.3, 54.4] 29.9 [2.72, 49.0] 29.9 [13.6, 54.4] 26.3 [9.98, 41.7] 29.9 [13.6, 39.9] 29.0 [10.9, 38.1] 28.1 [15.0, 38.1] 26.3 [9.98, 35.4] 24.9 [9.07, 35.4] 17.2 [1.81, 28.6] 17.7 [3.18, 30.8] 14.5 [3.63, 30.8] 13.6 [2.27, 35.4] 12.7 [1.81, 30.4] 12.2 [2.27, 27.2] 25.9 [1.81, 54.4] 27.2 [3.18, 59.0] 27.7 [2.72, 54.9] 27.7 [2.27, 54.4] 26.3 [1.81, 49.0] 25.4 [2.27, 54.4]
Missing 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 1 (1.6%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 1 (0.3%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%)
as.factor(high_scc_cow)
0 23 (48.9%) 19 (47.5%) 19 (47.5%) 11 (36.7%) 11 (40.7%) 39 (40.6%) 46 (40.0%) 44 (39.3%) 43 (39.8%) 44 (41.5%) 44 (41.1%) 72 (68.6%) 63 (67.0%) 56 (66.7%) 53 (66.3%) 51 (66.2%) 43 (62.3%) 11 (17.7%) 9 (14.8%) 7 (12.7%) 7 (13.0%) 7 (13.2%) 7 (13.5%) 145 (46.8%) 137 (44.2%) 126 (43.3%) 103 (42.6%) 113 (42.5%) 105 (41.2%)
1 24 (51.1%) 21 (52.5%) 21 (52.5%) 19 (63.3%) 16 (59.3%) 57 (59.4%) 69 (60.0%) 68 (60.7%) 65 (60.2%) 62 (58.5%) 63 (58.9%) 33 (31.4%) 31 (33.0%) 28 (33.3%) 27 (33.8%) 26 (33.8%) 26 (37.7%) 51 (82.3%) 52 (85.2%) 48 (87.3%) 47 (87.0%) 46 (86.8%) 45 (86.5%) 165 (53.2%) 173 (55.8%) 165 (56.7%) 139 (57.4%) 153 (57.5%) 150 (58.8%)

2 Confirmatory Analysis

2.1 CA: New high SCC

# ---- New subclinical mastitis ----

# Staph aureus

cox_1 <- coxph(survival~ staph.aureus.IMI.cow+cluster(Site), data=df_survival)
summary(cox_1)
## Call:
## coxph(formula = survival ~ staph.aureus.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                         coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## staph.aureus.IMI.cow1 1.1692    3.2194   0.1608    0.1244 9.401   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                       exp(coef) exp(-coef) lower .95 upper .95
## staph.aureus.IMI.cow1     3.219     0.3106     2.523     4.108
## 
## Concordance= 0.621  (se = 0.029 )
## Likelihood ratio test= 45.83  on 1 df,   p=1e-11
## Wald test            = 88.37  on 1 df,   p=<2e-16
## Score (logrank) test = 58.79  on 1 df,   p=2e-14,   Robust = 2.82  p=0.09
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_1)
##                      chisq df    p
## staph.aureus.IMI.cow 0.383  1 0.54
## GLOBAL               0.383  1 0.54
cox_1_adj <- coxph(survival~ staph.aureus.IMI.cow+calving.nas+calving.sslo+cluster(Site), data=df_survival)
summary(cox_1_adj)
## Call:
## coxph(formula = survival ~ staph.aureus.IMI.cow + calving.nas + 
##     calving.sslo, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                         coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## staph.aureus.IMI.cow1 1.2086    3.3486   0.1626    0.1213  9.967   <2e-16 ***
## calving.nas1          0.4960    1.6421   0.1612    0.2246  2.208   0.0272 *  
## calving.sslo1         0.8223    2.2756   0.1682    0.0744 11.052   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                       exp(coef) exp(-coef) lower .95 upper .95
## staph.aureus.IMI.cow1     3.349     0.2986     2.640     4.247
## calving.nas1              1.642     0.6090     1.057     2.550
## calving.sslo1             2.276     0.4394     1.967     2.633
## 
## Concordance= 0.703  (se = 0.037 )
## Likelihood ratio test= 73.35  on 3 df,   p=8e-16
## Wald test            = 15666  on 3 df,   p=<2e-16
## Score (logrank) test = 89.33  on 3 df,   p=<2e-16,   Robust = 3.82  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_1_adj)
##                      chisq df    p
## staph.aureus.IMI.cow 0.795  1 0.37
## calving.nas          0.263  1 0.61
## calving.sslo         2.270  1 0.13
## GLOBAL               3.161  3 0.37
# NAS general

cox_2 <- coxph(survival~ nas.IMI.cow+cluster(Site), data=df_survival)
summary(cox_2)
## Call:
## coxph(formula = survival ~ nas.IMI.cow, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                coef exp(coef) se(coef) robust se     z Pr(>|z|)  
## nas.IMI.cow1 0.2667    1.3057   0.1508    0.1478 1.804   0.0712 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##              exp(coef) exp(-coef) lower .95 upper .95
## nas.IMI.cow1     1.306     0.7659    0.9772     1.745
## 
## Concordance= 0.532  (se = 0.022 )
## Likelihood ratio test= 3.15  on 1 df,   p=0.08
## Wald test            = 3.25  on 1 df,   p=0.07
## Score (logrank) test = 3.15  on 1 df,   p=0.08,   Robust = 1.71  p=0.2
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_2)
##             chisq df    p
## nas.IMI.cow  1.39  1 0.24
## GLOBAL       1.39  1 0.24
cox_2_adj <- coxph(survival~ nas.IMI.cow+calving.sau+calving.sslo+cluster(Site), data=df_survival)
summary(cox_2_adj)
## Call:
## coxph(formula = survival ~ nas.IMI.cow + calving.sau + calving.sslo, 
##     data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                  coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## nas.IMI.cow1  0.29490   1.34300  0.15077   0.16425  1.795   0.0726 .  
## calving.sau1  1.23236   3.42932  0.17045   0.14159  8.704   <2e-16 ***
## calving.sslo1 0.77331   2.16693  0.16672   0.04071 18.994   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##               exp(coef) exp(-coef) lower .95 upper .95
## nas.IMI.cow1      1.343     0.7446    0.9733     1.853
## calving.sau1      3.429     0.2916    2.5983     4.526
## calving.sslo1     2.167     0.4615    2.0007     2.347
## 
## Concordance= 0.703  (se = 0.037 )
## Likelihood ratio test= 66.95  on 3 df,   p=2e-14
## Wald test            = 744.7  on 3 df,   p=<2e-16
## Score (logrank) test = 87.01  on 3 df,   p=<2e-16,   Robust = 3.97  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_2_adj)
##               chisq df     p
## nas.IMI.cow  0.6121  1 0.434
## calving.sau  0.0186  1 0.892
## calving.sslo 3.3031  1 0.069
## GLOBAL       3.9042  3 0.272
# Staph chromogenes

cox_3 <- coxph(survival~ sch.IMI.cow+cluster(Site), data=df_survival)
summary(cox_3)
## Call:
## coxph(formula = survival ~ sch.IMI.cow, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                coef exp(coef) se(coef) robust se     z Pr(>|z|)
## sch.IMI.cow1 0.2158    1.2408   0.1526    0.1764 1.223    0.221
## 
##              exp(coef) exp(-coef) lower .95 upper .95
## sch.IMI.cow1     1.241     0.8059    0.8781     1.753
## 
## Concordance= 0.512  (se = 0.025 )
## Likelihood ratio test= 1.97  on 1 df,   p=0.2
## Wald test            = 1.5  on 1 df,   p=0.2
## Score (logrank) test = 2.01  on 1 df,   p=0.2,   Robust = 1.24  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_3)
##             chisq df      p
## sch.IMI.cow  10.1  1 0.0015
## GLOBAL       10.1  1 0.0015
cox_3_adj <- coxph(survival~ sch.IMI.cow+calving.sau+calving.sslo+cluster(Site), data=df_survival)
summary(cox_3_adj)
## Call:
## coxph(formula = survival ~ sch.IMI.cow + calving.sau + calving.sslo, 
##     data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                  coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## sch.IMI.cow1  0.21054   1.23435  0.15263   0.18246  1.154    0.249    
## calving.sau1  1.22635   3.40876  0.17065   0.13465  9.108   <2e-16 ***
## calving.sslo1 0.76587   2.15087  0.16665   0.05134 14.919   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##               exp(coef) exp(-coef) lower .95 upper .95
## sch.IMI.cow1      1.234     0.8101    0.8632     1.765
## calving.sau1      3.409     0.2934    2.6181     4.438
## calving.sslo1     2.151     0.4649    1.9450     2.379
## 
## Concordance= 0.693  (se = 0.038 )
## Likelihood ratio test= 64.98  on 3 df,   p=5e-14
## Wald test            = 2868  on 3 df,   p=<2e-16
## Score (logrank) test = 84.97  on 3 df,   p=<2e-16,   Robust = 3.79  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_3_adj)
##                chisq df      p
## sch.IMI.cow   8.0589  1 0.0045
## calving.sau   0.0377  1 0.8461
## calving.sslo  3.2923  1 0.0696
## GLOBAL       11.5058  3 0.0093
# Proportional hazards assumption not met

cox_3_adj_stratified <- coxph(survival~ sch.IMI.cow*ps_sample_3_to_6 + calving.sau+calving.sslo+cluster(Site), data=df_survival)
## Warning in coxph.fit(X, Y, istrat, offset, init, control, weights = weights, :
## Loglik converged before variable 2 ; coefficient may be infinite.
summary(cox_3_adj_stratified)
## Call:
## coxph(formula = survival ~ sch.IMI.cow + ps_sample_3_to_6 + calving.sau + 
##     calving.sslo + sch.IMI.cow:ps_sample_3_to_6, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                                     coef  exp(coef)   se(coef)  robust se
## sch.IMI.cow1                  -1.428e-01  8.669e-01  1.737e-01  1.293e-01
## ps_sample_3_to_6              -2.153e+01  4.445e-10  2.156e+03  7.008e-01
## calving.sau1                   5.461e-01  1.726e+00  1.723e-01  1.525e-01
## calving.sslo1                  7.417e-01  2.100e+00  1.706e-01  9.965e-02
## sch.IMI.cow1:ps_sample_3_to_6  9.522e-01  2.591e+00  3.808e-01  2.165e-01
##                                     z Pr(>|z|)    
## sch.IMI.cow1                   -1.105 0.269187    
## ps_sample_3_to_6              -30.726  < 2e-16 ***
## calving.sau1                    3.581 0.000342 ***
## calving.sslo1                   7.443 9.83e-14 ***
## sch.IMI.cow1:ps_sample_3_to_6   4.399 1.09e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                               exp(coef) exp(-coef) lower .95 upper .95
## sch.IMI.cow1                  8.669e-01  1.154e+00 6.729e-01 1.117e+00
## ps_sample_3_to_6              4.445e-10  2.250e+09 1.125e-10 1.756e-09
## calving.sau1                  1.726e+00  5.792e-01 1.280e+00 2.328e+00
## calving.sslo1                 2.100e+00  4.763e-01 1.727e+00 2.552e+00
## sch.IMI.cow1:ps_sample_3_to_6 2.591e+00  3.859e-01 1.695e+00 3.961e+00
## 
## Concordance= 0.874  (se = 0.01 )
## Likelihood ratio test= 333.1  on 5 df,   p=<2e-16
## Wald test            = 2351  on 5 df,   p=<2e-16
## Score (logrank) test = 281.6  on 5 df,   p=<2e-16,   Robust = 4  p=0.5
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
confint(emmeans(cox_3_adj_stratified,revpairwise~sch.IMI.cow|ps_sample_3_to_6,type="response")$contrasts)
## ps_sample_3_to_6 = 0:
##  contrast                    ratio    SE  df asymp.LCL asymp.UCL
##  sch.IMI.cow1 / sch.IMI.cow0 0.867 0.112 Inf     0.673      1.12
## 
## ps_sample_3_to_6 = 1:
##  contrast                    ratio    SE  df asymp.LCL asymp.UCL
##  sch.IMI.cow1 / sch.IMI.cow0 2.247 0.400 Inf     1.584      3.19
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
# NAS non-chromogenes

cox_4 <- coxph(survival~ nas.non.chrom.IMI.cow+cluster(Site), data=df_survival)
summary(cox_4)
## Call:
## coxph(formula = survival ~ nas.non.chrom.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                           coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## nas.non.chrom.IMI.cow1 0.29314   1.34064  0.17386   0.07409 3.957  7.6e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                        exp(coef) exp(-coef) lower .95 upper .95
## nas.non.chrom.IMI.cow1     1.341     0.7459     1.159      1.55
## 
## Concordance= 0.538  (se = 0.012 )
## Likelihood ratio test= 2.7  on 1 df,   p=0.1
## Wald test            = 15.66  on 1 df,   p=8e-05
## Score (logrank) test = 2.86  on 1 df,   p=0.09,   Robust = 1.66  p=0.2
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_4)
##                       chisq df    p
## nas.non.chrom.IMI.cow  2.28  1 0.13
## GLOBAL                 2.28  1 0.13
cox_4_adj <- coxph(survival~ nas.non.chrom.IMI.cow+calving.sau+calving.sslo+cluster(Site), data=df_survival)
summary(cox_4_adj)
## Call:
## coxph(formula = survival ~ nas.non.chrom.IMI.cow + calving.sau + 
##     calving.sslo, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                           coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## nas.non.chrom.IMI.cow1 0.35978   1.43301  0.17458   0.15243  2.360   0.0183 *  
## calving.sau1           1.26138   3.53029  0.17141   0.13125  9.611   <2e-16 ***
## calving.sslo1          0.76120   2.14084  0.16672   0.04429 17.187   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                        exp(coef) exp(-coef) lower .95 upper .95
## nas.non.chrom.IMI.cow1     1.433     0.6978     1.063     1.932
## calving.sau1               3.530     0.2833     2.730     4.566
## calving.sslo1              2.141     0.4671     1.963     2.335
## 
## Concordance= 0.695  (se = 0.04 )
## Likelihood ratio test= 67.09  on 3 df,   p=2e-14
## Wald test            = 574.7  on 3 df,   p=<2e-16
## Score (logrank) test = 86.77  on 3 df,   p=<2e-16,   Robust = 3.77  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_4_adj)
##                       chisq df     p
## nas.non.chrom.IMI.cow 3.310  1 0.069
## calving.sau           0.051  1 0.821
## calving.sslo          3.054  1 0.081
## GLOBAL                6.343  3 0.096
# Staph sp.

cox_5 <- coxph(survival~ staph.spp.IMI.cow+cluster(Site), data=df_survival)
summary(cox_5)
## Call:
## coxph(formula = survival ~ staph.spp.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                      coef exp(coef) se(coef) robust se     z Pr(>|z|)   
## staph.spp.IMI.cow1 0.2769    1.3190   0.1813    0.1018 2.721  0.00652 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## staph.spp.IMI.cow1     1.319     0.7581      1.08      1.61
## 
## Concordance= 0.533  (se = 0.011 )
## Likelihood ratio test= 2.21  on 1 df,   p=0.1
## Wald test            = 7.4  on 1 df,   p=0.007
## Score (logrank) test = 2.35  on 1 df,   p=0.1,   Robust = 1.51  p=0.2
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_5)
##                   chisq df    p
## staph.spp.IMI.cow  1.98  1 0.16
## GLOBAL             1.98  1 0.16
cox_5_adj <- coxph(survival~ staph.spp.IMI.cow+calving.sau+calving.sslo+cluster(Site), data=df_survival)
summary(cox_5_adj)
## Call:
## coxph(formula = survival ~ staph.spp.IMI.cow + calving.sau + 
##     calving.sslo, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                      coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## staph.spp.IMI.cow1 0.3211    1.3787   0.1818    0.1465  2.192   0.0284 *  
## calving.sau1       1.2521    3.4975   0.1711    0.1288  9.721   <2e-16 ***
## calving.sslo1      0.7585    2.1351   0.1668    0.0410 18.499   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## staph.spp.IMI.cow1     1.379     0.7253     1.035     1.837
## calving.sau1           3.498     0.2859     2.717     4.502
## calving.sslo1          2.135     0.4684     1.970     2.314
## 
## Concordance= 0.692  (se = 0.037 )
## Likelihood ratio test= 66.04  on 3 df,   p=3e-14
## Wald test            = 537  on 3 df,   p=<2e-16
## Score (logrank) test = 85.78  on 3 df,   p=<2e-16,   Robust = 3.72  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_5_adj)
##                    chisq df     p
## staph.spp.IMI.cow 2.9827  1 0.084
## calving.sau       0.0524  1 0.819
## calving.sslo      3.0820  1 0.079
## GLOBAL            5.9580  3 0.114
# SSLO

cox_6 <- coxph(survival~ strep.IMI.cow+cluster(Site), data=df_survival)
summary(cox_6)
## Call:
## coxph(formula = survival ~ strep.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                  coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## strep.IMI.cow1 0.6004    1.8228   0.1517    0.1093 5.495 3.91e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                exp(coef) exp(-coef) lower .95 upper .95
## strep.IMI.cow1     1.823     0.5486     1.471     2.258
## 
## Concordance= 0.595  (se = 0.023 )
## Likelihood ratio test= 15.09  on 1 df,   p=1e-04
## Wald test            = 30.19  on 1 df,   p=4e-08
## Score (logrank) test = 16.14  on 1 df,   p=6e-05,   Robust = 3.11  p=0.08
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_6)
##               chisq df     p
## strep.IMI.cow  5.65  1 0.017
## GLOBAL         5.65  1 0.017
cox_6_adj <- coxph(survival~ strep.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_6_adj)
## Call:
## coxph(formula = survival ~ strep.IMI.cow + calving.sau + calving.nas, 
##     data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                   coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## strep.IMI.cow1 0.62058   1.86001  0.15361   0.03288 18.874   <2e-16 ***
## calving.sau1   1.23649   3.44351  0.17136   0.09533 12.970   <2e-16 ***
## calving.nas1   0.42234   1.52552  0.15999   0.21303  1.982   0.0474 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                exp(coef) exp(-coef) lower .95 upper .95
## strep.IMI.cow1     1.860     0.5376     1.744     1.984
## calving.sau1       3.444     0.2904     2.857     4.151
## calving.nas1       1.526     0.6555     1.005     2.316
## 
## Concordance= 0.695  (se = 0.034 )
## Likelihood ratio test= 64.95  on 3 df,   p=5e-14
## Wald test            = 36374  on 3 df,   p=<2e-16
## Score (logrank) test = 82.72  on 3 df,   p=<2e-16,   Robust = 3.19  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_6_adj)
##               chisq df     p
## strep.IMI.cow 3.565  1 0.059
## calving.sau   0.199  1 0.656
## calving.nas   0.446  1 0.504
## GLOBAL        3.869  3 0.276
# Strep species

cox_7 <- coxph(survival~ strep.genus.IMI.cow+cluster(Site), data=df_survival)
summary(cox_7)
## Call:
## coxph(formula = survival ~ strep.genus.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                        coef exp(coef) se(coef) robust se z Pr(>|z|)    
## strep.genus.IMI.cow1 0.9987    2.7148   0.1655    0.1110 9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                      exp(coef) exp(-coef) lower .95 upper .95
## strep.genus.IMI.cow1     2.715     0.3683     2.184     3.374
## 
## Concordance= 0.606  (se = 0.029 )
## Likelihood ratio test= 31.26  on 1 df,   p=2e-08
## Wald test            = 81.01  on 1 df,   p=<2e-16
## Score (logrank) test = 39.45  on 1 df,   p=3e-10,   Robust = 2.71  p=0.1
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_7)
##                     chisq df     p
## strep.genus.IMI.cow  3.55  1 0.059
## GLOBAL               3.55  1 0.059
cox_7_adj <- coxph(survival~ strep.genus.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_7_adj)
## Call:
## coxph(formula = survival ~ strep.genus.IMI.cow + calving.sau + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                         coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## strep.genus.IMI.cow1 0.80967   2.24717  0.17040   0.03037 26.665   <2e-16 ***
## calving.sau1         1.10689   3.02493  0.17558   0.07323 15.116   <2e-16 ***
## calving.nas1         0.36051   1.43406  0.15887   0.18801  1.918   0.0552 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                      exp(coef) exp(-coef) lower .95 upper .95
## strep.genus.IMI.cow1     2.247     0.4450    2.1173     2.385
## calving.sau1             3.025     0.3306    2.6205     3.492
## calving.nas1             1.434     0.6973    0.9921     2.073
## 
## Concordance= 0.703  (se = 0.042 )
## Likelihood ratio test= 69.43  on 3 df,   p=6e-15
## Wald test            = 1216  on 3 df,   p=<2e-16
## Score (logrank) test = 92.07  on 3 df,   p=<2e-16,   Robust = 2.76  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_7_adj)
##                     chisq df    p
## strep.genus.IMI.cow 2.057  1 0.15
## calving.sau         0.210  1 0.65
## calving.nas         0.185  1 0.67
## GLOBAL              2.690  3 0.44
# Strep dysgalactiae

cox_8 <- coxph(survival~ strep.dys.IMI.cow+cluster(Site), data=df_survival)
summary(cox_8)
## Call:
## coxph(formula = survival ~ strep.dys.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                      coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## strep.dys.IMI.cow1 0.9069    2.4766   0.1898    0.1904 4.763 1.91e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## strep.dys.IMI.cow1     2.477     0.4038     1.705     3.597
## 
## Concordance= 0.567  (se = 0.01 )
## Likelihood ratio test= 18.93  on 1 df,   p=1e-05
## Wald test            = 22.68  on 1 df,   p=2e-06
## Score (logrank) test = 24.43  on 1 df,   p=8e-07,   Robust = 2.91  p=0.09
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_8)
##                   chisq df    p
## strep.dys.IMI.cow 0.217  1 0.64
## GLOBAL            0.217  1 0.64
cox_8_adj <- coxph(survival~ strep.dys.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_8_adj)
## Call:
## coxph(formula = survival ~ strep.dys.IMI.cow + calving.sau + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                       coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## strep.dys.IMI.cow1 0.73096   2.07708  0.19250   0.09595  7.618 2.57e-14 ***
## calving.sau1       1.17691   3.24434  0.17289   0.11026 10.674  < 2e-16 ***
## calving.nas1       0.34139   1.40690  0.15885   0.17731  1.925   0.0542 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## strep.dys.IMI.cow1     2.077     0.4814    1.7210     2.507
## calving.sau1           3.244     0.3082    2.6138     4.027
## calving.nas1           1.407     0.7108    0.9939     1.992
## 
## Concordance= 0.691  (se = 0.041 )
## Likelihood ratio test= 61.71  on 3 df,   p=3e-13
## Wald test            = 395.2  on 3 df,   p=<2e-16
## Score (logrank) test = 82.96  on 3 df,   p=<2e-16,   Robust = 3.27  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_8_adj)
##                    chisq df    p
## strep.dys.IMI.cow 0.0873  1 0.77
## calving.sau       0.0428  1 0.84
## calving.nas       0.2098  1 0.65
## GLOBAL            0.3696  3 0.95
# Unspeciated Strep

cox_9 <- coxph(survival~ strep.sp.IMI.cow+cluster(Site), data=df_survival)
summary(cox_9)
## Call:
## coxph(formula = survival ~ strep.sp.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                     coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## strep.sp.IMI.cow1 0.7206    2.0557   0.2199    0.0704 10.24   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## strep.sp.IMI.cow1     2.056     0.4864     1.791      2.36
## 
## Concordance= 0.54  (se = 0.01 )
## Likelihood ratio test= 9.02  on 1 df,   p=0.003
## Wald test            = 104.8  on 1 df,   p=<2e-16
## Score (logrank) test = 11.21  on 1 df,   p=8e-04,   Robust = 2.34  p=0.1
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_9)
##                  chisq df    p
## strep.sp.IMI.cow 0.352  1 0.55
## GLOBAL           0.352  1 0.55
cox_9_adj <- coxph(survival~ strep.sp.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_9_adj)
## Call:
## coxph(formula = survival ~ strep.sp.IMI.cow + calving.sau + calving.nas, 
##     data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                     coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## strep.sp.IMI.cow1 0.5675    1.7638   0.2217    0.1741 3.260  0.00112 ** 
## calving.sau1      1.2179    3.3802   0.1718    0.1298 9.381  < 2e-16 ***
## calving.nas1      0.3628    1.4373   0.1589    0.1946 1.864  0.06230 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## strep.sp.IMI.cow1     1.764     0.5670    1.2539     2.481
## calving.sau1          3.380     0.2958    2.6207     4.360
## calving.nas1          1.437     0.6957    0.9815     2.105
## 
## Concordance= 0.675  (se = 0.037 )
## Likelihood ratio test= 54.94  on 3 df,   p=7e-12
## Wald test            = 247.1  on 3 df,   p=<2e-16
## Score (logrank) test = 72.99  on 3 df,   p=1e-15,   Robust = 2.76  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_9_adj)
##                   chisq df    p
## strep.sp.IMI.cow 0.3137  1 0.58
## calving.sau      0.0138  1 0.91
## calving.nas      0.1879  1 0.66
## GLOBAL           0.5330  3 0.91
# Strep-like organisms

cox_10 <- coxph(survival~ strep.like.IMI.cow+cluster(Site), data=df_survival)
summary(cox_10)
## Call:
## coxph(formula = survival ~ strep.like.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                        coef exp(coef) se(coef) robust se      z Pr(>|z|)
## strep.like.IMI.cow1 -0.1435    0.8663   0.2059    0.2054 -0.699    0.485
## 
##                     exp(coef) exp(-coef) lower .95 upper .95
## strep.like.IMI.cow1    0.8663      1.154    0.5792     1.296
## 
## Concordance= 0.508  (se = 0.015 )
## Likelihood ratio test= 0.5  on 1 df,   p=0.5
## Wald test            = 0.49  on 1 df,   p=0.5
## Score (logrank) test = 0.49  on 1 df,   p=0.5,   Robust = 0.49  p=0.5
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_10)
##                    chisq df    p
## strep.like.IMI.cow 0.515  1 0.47
## GLOBAL             0.515  1 0.47
cox_10_adj <- coxph(survival~ strep.like.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_10_adj)
## Call:
## coxph(formula = survival ~ strep.like.IMI.cow + calving.sau + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                        coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## strep.like.IMI.cow1 0.07462   1.07748  0.21001   0.20903 0.357    0.721    
## calving.sau1        1.27058   3.56292  0.17214   0.14324 8.870   <2e-16 ***
## calving.nas1        0.35983   1.43309  0.16051   0.22385 1.607    0.108    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                     exp(coef) exp(-coef) lower .95 upper .95
## strep.like.IMI.cow1     1.077     0.9281    0.7153     1.623
## calving.sau1            3.563     0.2807    2.6908     4.718
## calving.nas1            1.433     0.6978    0.9241     2.222
## 
## Concordance= 0.653  (se = 0.033 )
## Likelihood ratio test= 49.34  on 3 df,   p=1e-10
## Wald test            = 232  on 3 df,   p=<2e-16
## Score (logrank) test = 65.82  on 3 df,   p=3e-14,   Robust = 3.56  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_10_adj)
##                     chisq df    p
## strep.like.IMI.cow 0.5407  1 0.46
## calving.sau        0.0219  1 0.88
## calving.nas        0.2304  1 0.63
## GLOBAL             0.6684  3 0.88
# Enterococcus

cox_11 <- coxph(survival~ enterococcus.spp.IMI.cow+cluster(Site), data=df_survival)
summary(cox_11)
## Call:
## coxph(formula = survival ~ enterococcus.spp.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                              coef exp(coef) se(coef) robust se      z Pr(>|z|)
## enterococcus.spp.IMI.cow1 -0.2178    0.8043   0.2550    0.3109 -0.701    0.484
## 
##                           exp(coef) exp(-coef) lower .95 upper .95
## enterococcus.spp.IMI.cow1    0.8043      1.243    0.4373     1.479
## 
## Concordance= 0.509  (se = 0.011 )
## Likelihood ratio test= 0.77  on 1 df,   p=0.4
## Wald test            = 0.49  on 1 df,   p=0.5
## Score (logrank) test = 0.73  on 1 df,   p=0.4,   Robust = 0.72  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_11)
##                          chisq df    p
## enterococcus.spp.IMI.cow 0.834  1 0.36
## GLOBAL                   0.834  1 0.36
cox_11_adj <- coxph(survival~ enterococcus.spp.IMI.cow+calving.sau+calving.nas+cluster(Site), data=df_survival)
summary(cox_11_adj)
## Call:
## coxph(formula = survival ~ enterococcus.spp.IMI.cow + calving.sau + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                              coef exp(coef) se(coef) robust se     z Pr(>|z|)
## enterococcus.spp.IMI.cow1 0.02545   1.02578  0.26057   0.31903 0.080    0.936
## calving.sau1              1.26437   3.54087  0.17178   0.14458 8.745   <2e-16
## calving.nas1              0.35433   1.42523  0.16116   0.23349 1.518    0.129
##                              
## enterococcus.spp.IMI.cow1    
## calving.sau1              ***
## calving.nas1                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                           exp(coef) exp(-coef) lower .95 upper .95
## enterococcus.spp.IMI.cow1     1.026     0.9749    0.5489     1.917
## calving.sau1                  3.541     0.2824    2.6671     4.701
## calving.nas1                  1.425     0.7016    0.9019     2.252
## 
## Concordance= 0.653  (se = 0.034 )
## Likelihood ratio test= 49.22  on 3 df,   p=1e-10
## Wald test            = 270.9  on 3 df,   p=<2e-16
## Score (logrank) test = 65.66  on 3 df,   p=4e-14,   Robust = 3.99  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_11_adj)
##                           chisq df    p
## enterococcus.spp.IMI.cow 1.0637  1 0.30
## calving.sau              0.0203  1 0.89
## calving.nas              0.2101  1 0.65
## GLOBAL                   1.1460  3 0.77
# Gram negative microorganisms

cox_12 <- coxph(survival~ gram.neg.IMI.cow+cluster(Site), data=df_survival)
summary(cox_12)
## Call:
## coxph(formula = survival ~ gram.neg.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                       coef exp(coef) se(coef) robust se     z Pr(>|z|)
## gram.neg.IMI.cow1 -0.01514   0.98497  0.26209   0.25229 -0.06    0.952
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## gram.neg.IMI.cow1     0.985      1.015    0.6007     1.615
## 
## Concordance= 0.498  (se = 0.014 )
## Likelihood ratio test= 0  on 1 df,   p=1
## Wald test            = 0  on 1 df,   p=1
## Score (logrank) test = 0  on 1 df,   p=1,   Robust = 0  p=1
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_12)
##                  chisq df    p
## gram.neg.IMI.cow 0.405  1 0.52
## GLOBAL           0.405  1 0.52
cox_12_adj <- coxph(survival~ gram.neg.IMI.cow+calving.sau.sslo+calving.nas+cluster(Site), data=df_survival)
summary(cox_12_adj)
## Call:
## coxph(formula = survival ~ gram.neg.IMI.cow + calving.sau.sslo + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                     coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## gram.neg.IMI.cow1 0.1323    1.1414   0.2644    0.1948  0.679   0.4970    
## calving.sau.sslo1 1.1758    3.2408   0.1527    0.1125 10.448   <2e-16 ***
## calving.nas1      0.4199    1.5218   0.1606    0.2385  1.760   0.0784 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## gram.neg.IMI.cow1     1.141     0.8761    0.7792     1.672
## calving.sau.sslo1     3.241     0.3086    2.5993     4.041
## calving.nas1          1.522     0.6571    0.9535     2.429
## 
## Concordance= 0.691  (se = 0.035 )
## Likelihood ratio test= 60.2  on 3 df,   p=5e-13
## Wald test            = 187.5  on 3 df,   p=<2e-16
## Score (logrank) test = 69.07  on 3 df,   p=7e-15,   Robust = 3.51  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_12_adj)
##                  chisq df     p
## gram.neg.IMI.cow 0.219  1 0.640
## calving.sau.sslo 4.310  1 0.038
## calving.nas      0.493  1 0.483
## GLOBAL           4.926  3 0.177
# Others

cox_13 <- coxph(survival~ other.IMI.cow+cluster(Site), data=df_survival)
summary(cox_13)
## Call:
## coxph(formula = survival ~ other.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                   coef exp(coef) se(coef) robust se      z Pr(>|z|)  
## other.IMI.cow1 -0.6839    0.5046   0.1693    0.2998 -2.281   0.0225 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                exp(coef) exp(-coef) lower .95 upper .95
## other.IMI.cow1    0.5046      1.982    0.2804    0.9082
## 
## Concordance= 0.586  (se = 0.043 )
## Likelihood ratio test= 17.84  on 1 df,   p=2e-05
## Wald test            = 5.2  on 1 df,   p=0.02
## Score (logrank) test = 16.95  on 1 df,   p=4e-05,   Robust = 2.21  p=0.1
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_13)
##                chisq df    p
## other.IMI.cow 0.0262  1 0.87
## GLOBAL        0.0262  1 0.87
cox_13_adj <- coxph(survival~ other.IMI.cow+calving.sau.sslo+calving.nas+cluster(Site), data=df_survival)
summary(cox_13_adj)
## Call:
## coxph(formula = survival ~ other.IMI.cow + calving.sau.sslo + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                       coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## other.IMI.cow1    -0.58876   0.55501  0.17249   0.23388 -2.517   0.0118 *  
## calving.sau.sslo1  1.14702   3.14880  0.15301   0.07535 15.223   <2e-16 ***
## calving.nas1       0.29637   1.34497  0.16231   0.14136  2.097   0.0360 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## other.IMI.cow1        0.555     1.8018    0.3509    0.8778
## calving.sau.sslo1     3.149     0.3176    2.7165    3.6499
## calving.nas1          1.345     0.7435    1.0195    1.7744
## 
## Concordance= 0.717  (se = 0.035 )
## Likelihood ratio test= 72.48  on 3 df,   p=1e-15
## Wald test            = 435.4  on 3 df,   p=<2e-16
## Score (logrank) test = 79.97  on 3 df,   p=<2e-16,   Robust = 3.62  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_13_adj)
##                  chisq df     p
## other.IMI.cow    0.155  1 0.694
## calving.sau.sslo 3.957  1 0.047
## calving.nas      0.410  1 0.522
## GLOBAL           4.271  3 0.234
# Bacillus

cox_14 <- coxph(survival~ bacillus.IMI.cow+cluster(Site), data=df_survival)
summary(cox_14)
## Call:
## coxph(formula = survival ~ bacillus.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                      coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## bacillus.IMI.cow1 -0.9696    0.3792   0.2199    0.2535 -3.826  0.00013 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## bacillus.IMI.cow1    0.3792      2.637    0.2308    0.6232
## 
## Concordance= 0.591  (se = 0.038 )
## Likelihood ratio test= 24.31  on 1 df,   p=8e-07
## Wald test            = 14.64  on 1 df,   p=1e-04
## Score (logrank) test = 21  on 1 df,   p=5e-06,   Robust = 2.33  p=0.1
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_14)
##                  chisq df   p
## bacillus.IMI.cow  0.71  1 0.4
## GLOBAL            0.71  1 0.4
cox_14_adj <- coxph(survival~ bacillus.IMI.cow+calving.sau.sslo+calving.nas+cluster(Site), data=df_survival)
summary(cox_14_adj)
## Call:
## coxph(formula = survival ~ bacillus.IMI.cow + calving.sau.sslo + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                       coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## bacillus.IMI.cow1 -0.79221   0.45284  0.22241   0.21139 -3.748 0.000179 ***
## calving.sau.sslo1  1.09755   2.99683  0.15347   0.07407 14.818  < 2e-16 ***
## calving.nas1       0.32994   1.39088  0.16052   0.16713  1.974 0.048361 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## bacillus.IMI.cow1    0.4528     2.2083    0.2992    0.6853
## calving.sau.sslo1    2.9968     0.3337    2.5919    3.4650
## calving.nas1         1.3909     0.7190    1.0024    1.9300
## 
## Concordance= 0.72  (se = 0.033 )
## Likelihood ratio test= 75.07  on 3 df,   p=3e-16
## Wald test            = 289.4  on 3 df,   p=<2e-16
## Score (logrank) test = 80.63  on 3 df,   p=<2e-16,   Robust = 3.67  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_14_adj)
##                  chisq df    p
## bacillus.IMI.cow 0.417  1 0.52
## calving.sau.sslo 4.232  1 0.04
## calving.nas      0.541  1 0.46
## GLOBAL           4.778  3 0.19
# Coryne

cox_15 <- coxph(survival~ coryne.IMI.cow+cluster(Site), data=df_survival)
summary(cox_15)
## Call:
## coxph(formula = survival ~ coryne.IMI.cow, data = df_survival, 
##     cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                    coef exp(coef) se(coef) robust se      z Pr(>|z|)
## coryne.IMI.cow1 -0.4069    0.6657   0.2280    0.3529 -1.153    0.249
## 
##                 exp(coef) exp(-coef) lower .95 upper .95
## coryne.IMI.cow1    0.6657      1.502    0.3333      1.33
## 
## Concordance= 0.524  (se = 0.033 )
## Likelihood ratio test= 3.52  on 1 df,   p=0.06
## Wald test            = 1.33  on 1 df,   p=0.2
## Score (logrank) test = 3.23  on 1 df,   p=0.07,   Robust = 0.76  p=0.4
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_15)
##                chisq df    p
## coryne.IMI.cow  2.04  1 0.15
## GLOBAL          2.04  1 0.15
cox_15_adj <- coxph(survival~ coryne.IMI.cow+calving.sau.sslo+calving.nas+cluster(Site), data=df_survival)
summary(cox_15_adj)
## Call:
## coxph(formula = survival ~ coryne.IMI.cow + calving.sau.sslo + 
##     calving.nas, data = df_survival, cluster = Site)
## 
##   n= 333, number of events= 178 
## 
##                      coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## coryne.IMI.cow1   -0.3658    0.6936   0.2346    0.3115 -1.175   0.2402    
## calving.sau.sslo1  1.1782    3.2486   0.1526    0.1098 10.727   <2e-16 ***
## calving.nas1       0.3442    1.4109   0.1635    0.1742  1.976   0.0481 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                   exp(coef) exp(-coef) lower .95 upper .95
## coryne.IMI.cow1      0.6936     1.4417    0.3767     1.277
## calving.sau.sslo1    3.2486     0.3078    2.6194     4.029
## calving.nas1         1.4109     0.7088    1.0029     1.985
## 
## Concordance= 0.69  (se = 0.04 )
## Likelihood ratio test= 62.59  on 3 df,   p=2e-13
## Wald test            = 322.3  on 3 df,   p=<2e-16
## Score (logrank) test = 71.17  on 3 df,   p=2e-15,   Robust = 3.61  p=0.3
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).
cox.zph(cox_15_adj)
##                  chisq df     p
## coryne.IMI.cow   3.075  1 0.080
## calving.sau.sslo 3.992  1 0.046
## calving.nas      0.381  1 0.537
## GLOBAL           6.724  3 0.081
fp_1 <- read.csv(file = "./data/forestplot/forestplot_hr_imi_scc.csv", header=TRUE)
fp_1<-fp_1 %>% rename("Crude risk exposed"=Crude.risk.exposed,"Crude risk unexposed"=Crude.risk.unexposed,"Taxonomic group"=Taxonomic.group)

fp_1$se <- (log(fp_1$hi) - log(fp_1$est))/1.96

fp_1$` ` <- paste(rep(" ", 15), collapse = " ")

fp_1$`HR (95% CI)` <- ifelse(is.na(fp_1$se), "",
                           sprintf("%.2f (%.2f to %.2f)",
                                   fp_1$est, fp_1$low, fp_1$hi))

tm <- forest_theme(base_size = 9,xaxis_cex=0.8)

p <- forest(fp_1[,c(1:3, 8:9)],
       est = fp_1$est,
       lower = fp_1$low, 
       upper = fp_1$hi,
       ci_column = 4,
       ref_line = 1,
       arrow_lab = c("Lower hazards high SCC", "Higher hazards high SCC"),
       ticks_at = c(0.5, 1, 2,3,4),theme=tm)

plot(p)

ggsave(plot = p,"./figures_article_2/forestplot.png",width = 20, height = 12,dpi=300, units = "cm") 

2.2 CA: Milk production

# ---- Staph aureus  ----

model_1 <- lme(fixed =milk_kg~ staph.aureus.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_1,revpairwise~staph.aureus.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      23.5 3.21  3     13.3     33.7
##  1                      23.7 3.28  3     13.3     34.2
## 
## postpartum_sample = 2:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      25.6 3.21  3     15.4     35.9
##  1                      25.4 3.28  3     15.0     35.9
## 
## postpartum_sample = 3:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      24.8 3.21  3     14.5     35.0
##  1                      24.7 3.29  3     14.2     35.1
## 
## postpartum_sample = 4:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      23.7 3.21  3     13.5     33.9
##  1                      23.8 3.29  3     13.3     34.3
## 
## postpartum_sample = 5:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      23.1 3.21  3     12.9     33.3
##  1                      23.3 3.29  3     12.8     33.7
## 
## postpartum_sample = 6:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      20.9 3.21  3     10.7     31.1
##  1                      21.4 3.30  3     10.9     31.8
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.2491 0.946 328   0.263
##  p.value
##   0.7924
## 
## postpartum_sample = 2:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.2048 0.931 328  -0.220
##  p.value
##   0.8260
## 
## postpartum_sample = 3:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.0587 0.961 328  -0.061
##  p.value
##   0.9513
## 
## postpartum_sample = 4:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1328 1.005 328   0.132
##  p.value
##   0.8950
## 
## postpartum_sample = 5:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1804 0.989 328   0.182
##  p.value
##   0.8554
## 
## postpartum_sample = 6:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.4445 1.004 328   0.443
##  p.value
##   0.6582
## 
## Degrees-of-freedom method: containment
model_1 <- lme(fixed =milk_kg~ staph.aureus.IMI.cow*postpartum_sample+calving.nas+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_1 <- emmeans(model_1,revpairwise~staph.aureus.IMI.cow|postpartum_sample)

emmeans_1 
## $emmeans
## postpartum_sample = 1:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      22.8 3.12  3     12.9     32.8
##  1                      23.0 3.21  3     12.8     33.2
## 
## postpartum_sample = 2:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      25.0 3.12  3     15.0     34.9
##  1                      24.7 3.21  3     14.5     34.9
## 
## postpartum_sample = 3:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      24.1 3.13  3     14.2     34.0
##  1                      24.0 3.21  3     13.7     34.2
## 
## postpartum_sample = 4:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      23.0 3.13  3     13.1     33.0
##  1                      23.1 3.22  3     12.8     33.3
## 
## postpartum_sample = 5:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      22.4 3.13  3     12.5     32.4
##  1                      22.5 3.22  3     12.3     32.8
## 
## postpartum_sample = 6:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      20.3 3.13  3     10.3     30.2
##  1                      20.6 3.22  3     10.4     30.9
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1431 0.951 326   0.151
##  p.value
##   0.8805
## 
## postpartum_sample = 2:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.3027 0.936 326  -0.323
##  p.value
##   0.7467
## 
## postpartum_sample = 3:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.1473 0.966 326  -0.152
##  p.value
##   0.8790
## 
## postpartum_sample = 4:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.0506 1.011 326   0.050
##  p.value
##   0.9601
## 
## postpartum_sample = 5:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1062 0.995 326   0.107
##  p.value
##   0.9150
## 
## postpartum_sample = 6:
##  contrast                                      estimate    SE  df t.ratio
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.3707 1.009 326   0.367
##  p.value
##   0.7136
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_1)
## $emmeans
## postpartum_sample = 1:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      22.8 3.12  3     12.9     32.8
##  1                      23.0 3.21  3     12.8     33.2
## 
## postpartum_sample = 2:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      25.0 3.12  3     15.0     34.9
##  1                      24.7 3.21  3     14.5     34.9
## 
## postpartum_sample = 3:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      24.1 3.13  3     14.2     34.0
##  1                      24.0 3.21  3     13.7     34.2
## 
## postpartum_sample = 4:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      23.0 3.13  3     13.1     33.0
##  1                      23.1 3.22  3     12.8     33.3
## 
## postpartum_sample = 5:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      22.4 3.13  3     12.5     32.4
##  1                      22.5 3.22  3     12.3     32.8
## 
## postpartum_sample = 6:
##  staph.aureus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                      20.3 3.13  3     10.3     30.2
##  1                      20.6 3.22  3     10.4     30.9
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1431 0.951 326    -1.73
##  upper.CL
##      2.01
## 
## postpartum_sample = 2:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.3027 0.936 326    -2.14
##  upper.CL
##      1.54
## 
## postpartum_sample = 3:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0  -0.1473 0.966 326    -2.05
##  upper.CL
##      1.75
## 
## postpartum_sample = 4:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.0506 1.011 326    -1.94
##  upper.CL
##      2.04
## 
## postpartum_sample = 5:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.1062 0.995 326    -1.85
##  upper.CL
##      2.06
## 
## postpartum_sample = 6:
##  contrast                                      estimate    SE  df lower.CL
##  staph.aureus.IMI.cow1 - staph.aureus.IMI.cow0   0.3707 1.009 326    -1.61
##  upper.CL
##      2.36
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_1 <- as.data.frame(emmeans_1$emmeans)

adj_means_1
## postpartum_sample = 1:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    22.84164 3.123483  3 12.90132 32.78196
##  1                    22.98471 3.210114  3 12.76869 33.20073
## 
## postpartum_sample = 2:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    24.98845 3.124083  3 15.04623 34.93068
##  1                    24.68579 3.206451  3 14.48143 34.89015
## 
## postpartum_sample = 3:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    24.09988 3.125252  3 14.15393 34.04582
##  1                    23.95261 3.213118  3 13.72703 34.17818
## 
## postpartum_sample = 4:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    23.03822 3.131872  3 13.07121 33.00524
##  1                    23.08880 3.222302  3 12.83400 33.34360
## 
## postpartum_sample = 5:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    22.43902 3.128446  3 12.48291 32.39513
##  1                    22.54527 3.219222  3 12.30026 32.79027
## 
## postpartum_sample = 6:
##  staph.aureus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                    20.25657 3.129886  3 10.29588 30.21726
##  1                    20.62728 3.223071  3 10.37003 30.88453
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_1 <- as.data.frame(emmeans_1$contrasts) %>% select (postpartum_sample,p.value)


adj_means_1 <- adj_means_1 %>% left_join(contrasts_1, by="postpartum_sample")

adj_means_1 <- adj_means_1 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_1 <- adj_means_1 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


milk_1 <- ggplot(adj_means_1, aes(x=postpartum_sample, y=emmean, group=staph.aureus.IMI.cow, color=staph.aureus.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="Staphylococcus aureus", color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_1_adj_overall <- lme(fixed =milk_kg~ staph.aureus.IMI.cow+calving.nas+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_1_adj_overall)
##                          Estimate     2.5 %     97.5 %
## (Intercept)           24.17762289 18.107459 30.2477868
## staph.aureus.IMI.cow1  0.02637572 -1.288632  1.3413838
## calving.nas1          -0.38547607 -1.600980  0.8300278
## calving.sslo1         -1.99431519 -3.327743 -0.6608876
 # annotate("text", x=5, y=30, label= "Estimate (95%CI):",size=4)

# ---- NAS ----

model_2 <- lme(fixed =milk_kg~ nas.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_2,revpairwise~nas.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.0 3.22  3    12.72     33.2
##  1             24.2 3.23  3    13.89     34.4
## 
## postpartum_sample = 2:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.9 3.22  3    15.63     36.1
##  1             25.3 3.23  3    15.04     35.6
## 
## postpartum_sample = 3:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.3 3.23  3    15.01     35.5
##  1             24.2 3.23  3    13.94     34.5
## 
## postpartum_sample = 4:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.7 3.24  3    13.35     34.0
##  1             23.8 3.23  3    13.48     34.1
## 
## postpartum_sample = 5:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.4 3.23  3    13.09     33.7
##  1             22.9 3.23  3    12.64     33.2
## 
## postpartum_sample = 6:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             20.0 3.23  3     9.76     30.3
##  1             21.9 3.23  3    11.64     32.2
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0    1.188 0.775 328   1.532  0.1264
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -0.585 0.776 328  -0.754  0.4515
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -1.066 0.797 328  -1.337  0.1822
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0    0.116 0.853 328   0.136  0.8916
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -0.445 0.827 328  -0.538  0.5912
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0    1.887 0.841 328   2.245  0.0255
## 
## Degrees-of-freedom method: containment
model_2 <- lme(fixed =milk_kg~ nas.IMI.cow*postpartum_sample+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_2 <- emmeans(model_2,revpairwise~nas.IMI.cow|postpartum_sample)

emmeans_2
## $emmeans
## postpartum_sample = 1:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.5 3.16  3    12.40     32.5
##  1             23.4 3.17  3    13.35     33.5
## 
## postpartum_sample = 2:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.4 3.16  3    15.32     35.4
##  1             24.6 3.17  3    14.48     34.7
## 
## postpartum_sample = 3:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             24.8 3.16  3    14.71     34.8
##  1             23.5 3.17  3    13.39     33.6
## 
## postpartum_sample = 4:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.1 3.17  3    13.04     33.2
##  1             23.0 3.18  3    12.92     33.2
## 
## postpartum_sample = 5:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.9 3.17  3    12.79     32.9
##  1             22.2 3.18  3    12.09     32.3
## 
## postpartum_sample = 6:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             19.5 3.17  3     9.46     29.6
##  1             21.2 3.18  3    11.09     31.3
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0    0.987 0.779 326   1.268  0.2056
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -0.801 0.780 326  -1.026  0.3055
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -1.284 0.801 326  -1.603  0.1099
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -0.102 0.857 326  -0.119  0.9053
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0   -0.669 0.832 326  -0.804  0.4217
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df t.ratio p.value
##  nas.IMI.cow1 - nas.IMI.cow0    1.663 0.845 326   1.968  0.0499
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_2)
## $emmeans
## postpartum_sample = 1:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.5 3.16  3    12.40     32.5
##  1             23.4 3.17  3    13.35     33.5
## 
## postpartum_sample = 2:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.4 3.16  3    15.32     35.4
##  1             24.6 3.17  3    14.48     34.7
## 
## postpartum_sample = 3:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             24.8 3.16  3    14.71     34.8
##  1             23.5 3.17  3    13.39     33.6
## 
## postpartum_sample = 4:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.1 3.17  3    13.04     33.2
##  1             23.0 3.18  3    12.92     33.2
## 
## postpartum_sample = 5:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.9 3.17  3    12.79     32.9
##  1             22.2 3.18  3    12.09     32.3
## 
## postpartum_sample = 6:
##  nas.IMI.cow emmean   SE df lower.CL upper.CL
##  0             19.5 3.17  3     9.46     29.6
##  1             21.2 3.18  3    11.09     31.3
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0    0.987 0.779 326 -0.544077    2.519
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0   -0.801 0.780 326 -2.335868    0.734
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0   -1.284 0.801 326 -2.859554    0.292
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0   -0.102 0.857 326 -1.788968    1.585
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0   -0.669 0.832 326 -2.305188    0.967
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df  lower.CL upper.CL
##  nas.IMI.cow1 - nas.IMI.cow0    1.663 0.845 326  0.000863    3.326
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_2 <- as.data.frame(emmeans_2$emmeans)

adj_means_2
## postpartum_sample = 1:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           22.45393 3.157673  3 12.404810 32.50306
##  1           23.44143 3.170286  3 13.352166 33.53069
## 
## postpartum_sample = 2:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           25.37221 3.158365  3 15.320888 35.42354
##  1           24.57136 3.170636  3 14.480982 34.66174
## 
## postpartum_sample = 3:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           24.76812 3.161398  3 14.707146 34.82910
##  1           23.48424 3.172571  3 13.387706 33.58078
## 
## postpartum_sample = 4:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           23.14060 3.173148  3 13.042224 33.23897
##  1           23.03846 3.177895  3 12.924979 33.15194
## 
## postpartum_sample = 5:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           22.86545 3.166509  3 12.788207 32.94270
##  1           22.19638 3.176077  3 12.088685 32.30407
## 
## postpartum_sample = 6:
##  nas.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0           19.53763 3.167748  3  9.456442 29.61882
##  1           21.20086 3.178637  3 11.085016 31.31670
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_2 <- as.data.frame(emmeans_2$contrasts) %>% select (postpartum_sample,p.value)


adj_means_2 <- adj_means_2 %>% left_join(contrasts_2, by="postpartum_sample")

adj_means_2 <- adj_means_2 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_2 <- adj_means_2 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


milk_2 <- ggplot(adj_means_2, aes(x=postpartum_sample, y=emmean, group=nas.IMI.cow, color=nas.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="NASM",color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_2_adj_overall <- lme(fixed =milk_kg~ nas.IMI.cow+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_2_adj_overall)
##                  Estimate     2.5 %     97.5 %
## (Intercept)   24.04828039 17.948076 30.1484847
## nas.IMI.cow1   0.06725118 -1.015129  1.1496314
## calving.sau1  -0.14861744 -1.565902  1.2686667
## calving.sslo1 -1.91491994 -3.241671 -0.5881688
# ---- Staph chromogenes ----

model_3 <- lme(fixed =milk_kg~ sch.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

 emmeans(model_3,revpairwise~sch.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.2 3.21  3     13.0     33.4
##  1             24.2 3.24  3     13.9     34.5
## 
## postpartum_sample = 2:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.7 3.21  3     15.4     35.9
##  1             25.5 3.24  3     15.2     35.8
## 
## postpartum_sample = 3:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.3 3.21  3     15.1     35.6
##  1             23.7 3.24  3     13.4     34.0
## 
## postpartum_sample = 4:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.7 3.22  3     13.4     33.9
##  1             23.7 3.25  3     13.4     34.1
## 
## postpartum_sample = 5:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.6 3.22  3     13.4     33.8
##  1             22.4 3.24  3     12.0     32.7
## 
## postpartum_sample = 6:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             20.7 3.22  3     10.4     30.9
##  1             21.6 3.25  3     11.2     31.9
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   0.9706 0.804 328   1.207  0.2281
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0  -0.1638 0.800 328  -0.205  0.8379
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0  -1.6556 0.817 328  -2.026  0.0436
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   0.0422 0.867 328   0.049  0.9612
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0  -1.2375 0.846 328  -1.463  0.1444
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   0.8884 0.863 328   1.030  0.3038
## 
## Degrees-of-freedom method: containment
model_3 <- lme(fixed =milk_kg~ sch.IMI.cow*postpartum_sample+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_3 <- emmeans(model_3,revpairwise~sch.IMI.cow|postpartum_sample)

emmeans_3
## $emmeans
## postpartum_sample = 1:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.6 3.14  3     12.6     32.6
##  1             23.4 3.18  3     13.3     33.6
## 
## postpartum_sample = 2:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.1 3.14  3     15.1     35.1
##  1             24.7 3.18  3     14.6     34.8
## 
## postpartum_sample = 3:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             24.8 3.14  3     14.8     34.8
##  1             22.9 3.18  3     12.8     33.1
## 
## postpartum_sample = 4:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.1 3.15  3     13.1     33.1
##  1             23.0 3.19  3     12.8     33.1
## 
## postpartum_sample = 5:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.0 3.15  3     13.0     33.0
##  1             21.6 3.19  3     11.5     31.7
## 
## postpartum_sample = 6:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             20.1 3.15  3     10.1     30.1
##  1             20.8 3.19  3     10.7     31.0
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0    0.802 0.804 326   0.998  0.3191
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   -0.343 0.801 326  -0.428  0.6689
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   -1.842 0.818 326  -2.253  0.0249
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   -0.140 0.867 326  -0.162  0.8715
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0   -1.423 0.846 326  -1.682  0.0936
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df t.ratio p.value
##  sch.IMI.cow1 - sch.IMI.cow0    0.697 0.863 326   0.808  0.4199
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_3)
## $emmeans
## postpartum_sample = 1:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             22.6 3.14  3     12.6     32.6
##  1             23.4 3.18  3     13.3     33.6
## 
## postpartum_sample = 2:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             25.1 3.14  3     15.1     35.1
##  1             24.7 3.18  3     14.6     34.8
## 
## postpartum_sample = 3:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             24.8 3.14  3     14.8     34.8
##  1             22.9 3.18  3     12.8     33.1
## 
## postpartum_sample = 4:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.1 3.15  3     13.1     33.1
##  1             23.0 3.19  3     12.8     33.1
## 
## postpartum_sample = 5:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             23.0 3.15  3     13.0     33.0
##  1             21.6 3.19  3     11.5     31.7
## 
## postpartum_sample = 6:
##  sch.IMI.cow emmean   SE df lower.CL upper.CL
##  0             20.1 3.15  3     10.1     30.1
##  1             20.8 3.19  3     10.7     31.0
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0    0.802 0.804 326   -0.779    2.383
## 
## postpartum_sample = 2:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0   -0.343 0.801 326   -1.918    1.233
## 
## postpartum_sample = 3:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0   -1.842 0.818 326   -3.450   -0.234
## 
## postpartum_sample = 4:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0   -0.140 0.867 326   -1.847    1.566
## 
## postpartum_sample = 5:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0   -1.423 0.846 326   -3.089    0.242
## 
## postpartum_sample = 6:
##  contrast                    estimate    SE  df lower.CL upper.CL
##  sch.IMI.cow1 - sch.IMI.cow0    0.697 0.863 326   -1.001    2.396
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_3 <- as.data.frame(emmeans_3$emmeans)

adj_means_3
## postpartum_sample = 1:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           22.63204 3.141625  3 12.63398 32.63009
##  1           23.43412 3.179063  3 13.31692 33.55132
## 
## postpartum_sample = 2:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           25.06914 3.142091  3 15.06961 35.06868
##  1           24.72630 3.178837  3 14.60983 34.84278
## 
## postpartum_sample = 3:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           24.77413 3.144591  3 14.76664 34.78162
##  1           22.93216 3.180489  3 12.81043 33.05390
## 
## postpartum_sample = 4:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           23.10860 3.152611  3 13.07558 33.14161
##  1           22.96818 3.187903  3 12.82285 33.11351
## 
## postpartum_sample = 5:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           23.02810 3.148298  3 13.00881 33.04739
##  1           21.60471 3.185213  3 11.46794 31.74148
## 
## postpartum_sample = 6:
##  sch.IMI.cow   emmean       SE df lower.CL upper.CL
##  0           20.10806 3.149439  3 10.08513 30.13098
##  1           20.80530 3.188879  3 10.65686 30.95374
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_3 <- as.data.frame(emmeans_3$contrasts) %>% select (postpartum_sample,p.value)


adj_means_3 <- adj_means_3 %>% left_join(contrasts_3, by="postpartum_sample")

adj_means_3 <- adj_means_3 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_3 <- adj_means_3 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


milk_3 <- ggplot(adj_means_3, aes(x=postpartum_sample, y=emmean, group=sch.IMI.cow, color=sch.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="Staphylococcus chromogenes",color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_3_adj_overall <- lme(fixed =milk_kg~ sch.IMI.cow+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_3_adj_overall)
##                 Estimate     2.5 %     97.5 %
## (Intercept)   24.2075407 18.132064 30.2830170
## sch.IMI.cow1  -0.3057573 -1.397148  0.7856335
## calving.sau1  -0.1927147 -1.602221  1.2167920
## calving.sslo1 -1.9648252 -3.286422 -0.6432286
# ---- NAS non chromogenes -----

model_4 <- lme(fixed =milk_kg~ nas.non.chrom.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_4,revpairwise~nas.non.chrom.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       23.3 3.22  3     13.1     33.6
##  1                       24.4 3.30  3     13.9     34.9
## 
## postpartum_sample = 2:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       25.7 3.22  3     15.4     35.9
##  1                       25.4 3.29  3     14.9     35.9
## 
## postpartum_sample = 3:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       24.7 3.22  3     14.5     35.0
##  1                       24.8 3.30  3     14.3     35.3
## 
## postpartum_sample = 4:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       23.6 3.22  3     13.4     33.9
##  1                       24.2 3.30  3     13.7     34.7
## 
## postpartum_sample = 5:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       22.8 3.22  3     12.5     33.1
##  1                       24.3 3.30  3     13.8     34.8
## 
## postpartum_sample = 6:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       20.5 3.22  3     10.3     30.8
##  1                       22.7 3.31  3     12.2     33.2
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0    1.117 0.962 328   1.161
##  p.value
##   0.2465
## 
## postpartum_sample = 2:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   -0.252 0.939 328  -0.268
##  p.value
##   0.7890
## 
## postpartum_sample = 3:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0    0.113 0.956 328   0.118
##  p.value
##   0.9060
## 
## postpartum_sample = 4:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0    0.555 0.991 328   0.560
##  p.value
##   0.5759
## 
## postpartum_sample = 5:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0    1.494 0.981 328   1.523
##  p.value
##   0.1288
## 
## postpartum_sample = 6:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0    2.170 1.007 328   2.155
##  p.value
##   0.0319
## 
## Degrees-of-freedom method: containment
model_4 <- lme(fixed =milk_kg~ nas.non.chrom.IMI.cow*postpartum_sample+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_4 <- emmeans(model_4,revpairwise~nas.non.chrom.IMI.cow|postpartum_sample)

emmeans_4
## $emmeans
## postpartum_sample = 1:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       22.8 3.16  3    12.73     32.8
##  1                       23.8 3.25  3    13.47     34.2
## 
## postpartum_sample = 2:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       25.1 3.16  3    15.07     35.2
##  1                       24.8 3.25  3    14.46     35.1
## 
## postpartum_sample = 3:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       24.2 3.16  3    14.13     34.2
##  1                       24.2 3.25  3    13.88     34.6
## 
## postpartum_sample = 4:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       23.1 3.17  3    13.00     33.1
##  1                       23.5 3.25  3    13.19     33.9
## 
## postpartum_sample = 5:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       22.3 3.16  3    12.20     32.3
##  1                       23.7 3.25  3    13.31     34.0
## 
## postpartum_sample = 6:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       20.0 3.16  3     9.93     30.1
##  1                       22.1 3.26  3    11.70     32.5
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   1.0435 0.964 326   1.082
##  p.value
##   0.2800
## 
## postpartum_sample = 2:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0  -0.3287 0.942 326  -0.349
##  p.value
##   0.7273
## 
## postpartum_sample = 3:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   0.0358 0.959 326   0.037
##  p.value
##   0.9702
## 
## postpartum_sample = 4:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   0.4758 0.994 326   0.479
##  p.value
##   0.6325
## 
## postpartum_sample = 5:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   1.4090 0.984 326   1.432
##  p.value
##   0.1531
## 
## postpartum_sample = 6:
##  contrast                                        estimate    SE  df t.ratio
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   2.0768 1.009 326   2.057
##  p.value
##   0.0404
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_4)
## $emmeans
## postpartum_sample = 1:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       22.8 3.16  3    12.73     32.8
##  1                       23.8 3.25  3    13.47     34.2
## 
## postpartum_sample = 2:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       25.1 3.16  3    15.07     35.2
##  1                       24.8 3.25  3    14.46     35.1
## 
## postpartum_sample = 3:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       24.2 3.16  3    14.13     34.2
##  1                       24.2 3.25  3    13.88     34.6
## 
## postpartum_sample = 4:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       23.1 3.17  3    13.00     33.1
##  1                       23.5 3.25  3    13.19     33.9
## 
## postpartum_sample = 5:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       22.3 3.16  3    12.20     32.3
##  1                       23.7 3.25  3    13.31     34.0
## 
## postpartum_sample = 6:
##  nas.non.chrom.IMI.cow emmean   SE df lower.CL upper.CL
##  0                       20.0 3.16  3     9.93     30.1
##  1                       22.1 3.26  3    11.70     32.5
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   1.0435 0.964 326   -0.853
##  upper.CL
##      2.94
## 
## postpartum_sample = 2:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0  -0.3287 0.942 326   -2.181
##  upper.CL
##      1.52
## 
## postpartum_sample = 3:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   0.0358 0.959 326   -1.850
##  upper.CL
##      1.92
## 
## postpartum_sample = 4:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   0.4758 0.994 326   -1.480
##  upper.CL
##      2.43
## 
## postpartum_sample = 5:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   1.4090 0.984 326   -0.526
##  upper.CL
##      3.34
## 
## postpartum_sample = 6:
##  contrast                                        estimate    SE  df lower.CL
##  nas.non.chrom.IMI.cow1 - nas.non.chrom.IMI.cow0   2.0768 1.009 326    0.091
##  upper.CL
##      4.06
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_4 <- as.data.frame(emmeans_4$emmeans)

adj_means_4
## postpartum_sample = 1:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     22.77726 3.155842  3 12.733965 32.82056
##  1                     23.82080 3.251101  3 13.474348 34.16726
## 
## postpartum_sample = 2:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     25.11265 3.156518  3 15.067204 35.15810
##  1                     24.78396 3.245140  3 14.456477 35.11145
## 
## postpartum_sample = 3:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     24.17942 3.158265  3 14.128417 34.23043
##  1                     24.21522 3.248410  3 13.877328 34.55311
## 
## postpartum_sample = 4:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     23.07374 3.165021  3 13.001236 33.14625
##  1                     23.54955 3.254720  3 13.191578 33.90752
## 
## postpartum_sample = 5:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     22.25951 3.161408  3 12.198503 32.32052
##  1                     23.66853 3.253805  3 13.313474 34.02359
## 
## postpartum_sample = 6:
##  nas.non.chrom.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                     19.99689 3.162368  3  9.932821 30.06095
##  1                     22.07368 3.261114  3 11.695364 32.45200
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_4 <- as.data.frame(emmeans_4$contrasts) %>% select (postpartum_sample,p.value)


adj_means_4 <- adj_means_4 %>% left_join(contrasts_4, by="postpartum_sample")

adj_means_4 <- adj_means_4 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_4 <- adj_means_4 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

adj_means_4$nas.non.chrom.IMI.cow <- as.factor(adj_means_4$nas.non.chrom.IMI.cow)

nas_nch_title <- expression(paste("NASM non-", italic("chromogenes")))


milk_4 <- ggplot(adj_means_4, aes(x=postpartum_sample, y=emmean, group=nas.non.chrom.IMI.cow, color=nas.non.chrom.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=nas_nch_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_4_adj_overall <- lme(fixed =milk_kg~ nas.non.chrom.IMI.cow+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_4_adj_overall)
##                          Estimate      2.5 %     97.5 %
## (Intercept)            23.8798870 17.7655051 29.9942690
## nas.non.chrom.IMI.cow1  0.8247931 -0.4734207  2.1230070
## calving.sau1           -0.0255785 -1.4427343  1.3915774
## calving.sslo1          -1.8850774 -3.1973404 -0.5728145
# ---- Staph spp ---- 

model_5 <- lme(fixed =milk_kg~ staph.spp.IMI.cow*postpartum_sample+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_5,revpairwise~staph.spp.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.9 3.15  3    12.85     32.9
##  1                   23.5 3.26  3    13.17     33.9
## 
## postpartum_sample = 2:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   25.1 3.15  3    15.10     35.1
##  1                   24.7 3.25  3    14.32     35.0
## 
## postpartum_sample = 3:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.2 3.15  3    14.17     34.2
##  1                   24.1 3.25  3    13.76     34.5
## 
## postpartum_sample = 4:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.0 3.15  3    13.00     33.1
##  1                   23.6 3.26  3    13.27     34.0
## 
## postpartum_sample = 5:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.3 3.15  3    12.25     32.3
##  1                   23.7 3.26  3    13.35     34.1
## 
## postpartum_sample = 6:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   20.0 3.15  3     9.94     30.0
##  1                   22.4 3.27  3    12.02     32.8
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6702 1.009 326   0.664  0.5069
## 
## postpartum_sample = 2:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.4556 0.981 326  -0.464  0.6426
## 
## postpartum_sample = 3:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.0866 1.000 326  -0.087  0.9310
## 
## postpartum_sample = 4:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6061 1.038 326   0.584  0.5595
## 
## postpartum_sample = 5:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   1.4437 1.028 326   1.405  0.1610
## 
## postpartum_sample = 6:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   2.4508 1.053 326   2.328  0.0205
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment
model_5 <- lme(fixed =milk_kg~ staph.spp.IMI.cow*postpartum_sample+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_5 <- emmeans(model_5,revpairwise~staph.spp.IMI.cow|postpartum_sample)

emmeans_5
## $emmeans
## postpartum_sample = 1:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.9 3.15  3    12.85     32.9
##  1                   23.5 3.26  3    13.17     33.9
## 
## postpartum_sample = 2:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   25.1 3.15  3    15.10     35.1
##  1                   24.7 3.25  3    14.32     35.0
## 
## postpartum_sample = 3:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.2 3.15  3    14.17     34.2
##  1                   24.1 3.25  3    13.76     34.5
## 
## postpartum_sample = 4:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.0 3.15  3    13.00     33.1
##  1                   23.6 3.26  3    13.27     34.0
## 
## postpartum_sample = 5:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.3 3.15  3    12.25     32.3
##  1                   23.7 3.26  3    13.35     34.1
## 
## postpartum_sample = 6:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   20.0 3.15  3     9.94     30.0
##  1                   22.4 3.27  3    12.02     32.8
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6702 1.009 326   0.664  0.5069
## 
## postpartum_sample = 2:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.4556 0.981 326  -0.464  0.6426
## 
## postpartum_sample = 3:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.0866 1.000 326  -0.087  0.9310
## 
## postpartum_sample = 4:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6061 1.038 326   0.584  0.5595
## 
## postpartum_sample = 5:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   1.4437 1.028 326   1.405  0.1610
## 
## postpartum_sample = 6:
##  contrast                                estimate    SE  df t.ratio p.value
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   2.4508 1.053 326   2.328  0.0205
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_5)
## $emmeans
## postpartum_sample = 1:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.9 3.15  3    12.85     32.9
##  1                   23.5 3.26  3    13.17     33.9
## 
## postpartum_sample = 2:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   25.1 3.15  3    15.10     35.1
##  1                   24.7 3.25  3    14.32     35.0
## 
## postpartum_sample = 3:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.2 3.15  3    14.17     34.2
##  1                   24.1 3.25  3    13.76     34.5
## 
## postpartum_sample = 4:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.0 3.15  3    13.00     33.1
##  1                   23.6 3.26  3    13.27     34.0
## 
## postpartum_sample = 5:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   22.3 3.15  3    12.25     32.3
##  1                   23.7 3.26  3    13.35     34.1
## 
## postpartum_sample = 6:
##  staph.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   20.0 3.15  3     9.94     30.0
##  1                   22.4 3.27  3    12.02     32.8
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6702 1.009 326   -1.314     2.65
## 
## postpartum_sample = 2:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.4556 0.981 326   -2.385     1.47
## 
## postpartum_sample = 3:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0  -0.0866 1.000 326   -2.053     1.88
## 
## postpartum_sample = 4:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   0.6061 1.038 326   -1.435     2.65
## 
## postpartum_sample = 5:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   1.4437 1.028 326   -0.578     3.47
## 
## postpartum_sample = 6:
##  contrast                                estimate    SE  df lower.CL upper.CL
##  staph.spp.IMI.cow1 - staph.spp.IMI.cow0   2.4508 1.053 326    0.380     4.52
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_5 <- as.data.frame(emmeans_5$emmeans)

adj_means_5
## postpartum_sample = 1:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 22.85510 3.145172  3 12.845754 32.86444
##  1                 23.52531 3.255046  3 13.166303 33.88432
## 
## postpartum_sample = 2:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 25.11459 3.145831  3 15.103151 35.12603
##  1                 24.65903 3.247230  3 14.324892 34.99316
## 
## postpartum_sample = 3:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 24.19024 3.147474  3 14.173570 34.20690
##  1                 24.10364 3.251412  3 13.756198 34.45108
## 
## postpartum_sample = 4:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 23.04068 3.153831  3 13.003776 33.07757
##  1                 23.64679 3.259443  3 13.273784 34.01979
## 
## postpartum_sample = 5:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 22.27919 3.150438  3 12.253085 32.30529
##  1                 23.72287 3.258238  3 13.353705 34.09204
## 
## postpartum_sample = 6:
##  staph.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                 19.96455 3.151442  3  9.935254 29.99385
##  1                 22.41536 3.265752  3 12.022274 32.80844
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_5 <- as.data.frame(emmeans_5$contrasts) %>% select (postpartum_sample,p.value)


adj_means_5 <- adj_means_5 %>% left_join(contrasts_5, by="postpartum_sample")

adj_means_5 <- adj_means_5 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_5 <- adj_means_5 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

staph_sp_title <- expression(paste("Unspeciated ",italic("Staphylococcus ")))


milk_5 <- ggplot(adj_means_5, aes(x=postpartum_sample, y=emmean, group=staph.spp.IMI.cow, color=staph.spp.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=staph_sp_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_5_adj_overall <- lme(fixed =milk_kg~ staph.spp.IMI.cow+calving.sau+calving.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_5_adj_overall)
##                       Estimate      2.5 %     97.5 %
## (Intercept)        23.91518652 17.8207897 30.0095833
## staph.spp.IMI.cow1  0.80355204 -0.5430581  2.1501622
## calving.sau1       -0.05030637 -1.4640500  1.3634372
## calving.sslo1      -1.90948946 -3.2209941 -0.5979848
# ---- SSLO ----

model_6 <- lme(fixed =milk_kg~ strep.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_6,revpairwise~strep.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.0 3.16  3     13.9     34.0
##  1               22.8 3.19  3     12.6     32.9
## 
## postpartum_sample = 2:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               26.4 3.16  3     16.3     36.5
##  1               24.2 3.19  3     14.0     34.3
## 
## postpartum_sample = 3:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.2 3.17  3     15.1     35.2
##  1               23.9 3.20  3     13.7     34.1
## 
## postpartum_sample = 4:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.2 3.17  3     14.1     34.3
##  1               22.9 3.21  3     12.7     33.1
## 
## postpartum_sample = 5:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.3 3.17  3     13.2     33.3
##  1               22.9 3.20  3     12.7     33.1
## 
## postpartum_sample = 6:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               21.2 3.17  3     11.1     31.3
##  1               20.6 3.21  3     10.4     30.8
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.209 0.810 328  -1.493  0.1365
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -2.249 0.815 328  -2.759  0.0061
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.245 0.840 328  -1.483  0.1391
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.234 0.884 328  -1.396  0.1637
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -0.386 0.869 328  -0.445  0.6569
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -0.649 0.886 328  -0.732  0.4646
## 
## Degrees-of-freedom method: containment
model_6 <- lme(fixed =milk_kg~ strep.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_6 <- emmeans(model_6,revpairwise~strep.IMI.cow|postpartum_sample)

emmeans_6
## $emmeans
## postpartum_sample = 1:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.9 3.16  3     13.8     33.9
##  1               22.6 3.19  3     12.5     32.8
## 
## postpartum_sample = 2:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               26.3 3.16  3     16.3     36.4
##  1               24.0 3.19  3     13.8     34.2
## 
## postpartum_sample = 3:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.1 3.16  3     15.0     35.1
##  1               23.8 3.20  3     13.6     34.0
## 
## postpartum_sample = 4:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.1 3.17  3     14.0     34.2
##  1               22.8 3.20  3     12.6     33.0
## 
## postpartum_sample = 5:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.2 3.16  3     13.1     33.2
##  1               22.7 3.20  3     12.5     32.9
## 
## postpartum_sample = 6:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               21.1 3.17  3     11.1     31.2
##  1               20.4 3.21  3     10.2     30.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.263 0.817 326  -1.546  0.1232
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -2.304 0.822 326  -2.801  0.0054
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.299 0.847 326  -1.533  0.1262
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -1.289 0.892 326  -1.445  0.1494
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -0.440 0.876 326  -0.502  0.6157
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df t.ratio p.value
##  strep.IMI.cow1 - strep.IMI.cow0   -0.703 0.893 326  -0.787  0.4319
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_6)
## $emmeans
## postpartum_sample = 1:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.9 3.16  3     13.8     33.9
##  1               22.6 3.19  3     12.5     32.8
## 
## postpartum_sample = 2:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               26.3 3.16  3     16.3     36.4
##  1               24.0 3.19  3     13.8     34.2
## 
## postpartum_sample = 3:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.1 3.16  3     15.0     35.1
##  1               23.8 3.20  3     13.6     34.0
## 
## postpartum_sample = 4:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.1 3.17  3     14.0     34.2
##  1               22.8 3.20  3     12.6     33.0
## 
## postpartum_sample = 5:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.2 3.16  3     13.1     33.2
##  1               22.7 3.20  3     12.5     32.9
## 
## postpartum_sample = 6:
##  strep.IMI.cow emmean   SE df lower.CL upper.CL
##  0               21.1 3.17  3     11.1     31.2
##  1               20.4 3.21  3     10.2     30.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -1.263 0.817 326    -2.87    0.344
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -2.304 0.822 326    -3.92   -0.686
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -1.299 0.847 326    -2.97    0.368
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -1.289 0.892 326    -3.04    0.465
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -0.440 0.876 326    -2.16    1.284
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  strep.IMI.cow1 - strep.IMI.cow0   -0.703 0.893 326    -2.46    1.055
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_6 <- as.data.frame(emmeans_6$emmeans)

adj_means_6
## postpartum_sample = 1:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             23.88439 3.159264  3 13.83020 33.93858
##  1             22.62176 3.191330  3 12.46552 32.77799
## 
## postpartum_sample = 2:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             26.31171 3.158844  3 16.25886 36.36456
##  1             24.00801 3.193476  3 13.84494 34.17108
## 
## postpartum_sample = 3:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             25.08200 3.160757  3 15.02307 35.14094
##  1             23.78295 3.197742  3 13.60631 33.95959
## 
## postpartum_sample = 4:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             24.06555 3.168931  3 13.98060 34.15051
##  1             22.77702 3.203989  3 12.58050 32.97355
## 
## postpartum_sample = 5:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             23.16981 3.164363  3 13.09939 33.24022
##  1             22.72948 3.202854  3 12.53656 32.92239
## 
## postpartum_sample = 6:
##  strep.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             21.14676 3.165668  3 11.07219 31.22133
##  1             20.44382 3.206366  3 10.23973 30.64791
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_6 <- as.data.frame(emmeans_6$contrasts) %>% select (postpartum_sample,p.value)


adj_means_6 <- adj_means_6 %>% left_join(contrasts_6, by="postpartum_sample")

adj_means_6 <- adj_means_6 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_6 <- adj_means_6 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

#staph_sp_title <- expression(paste(italic("Staphylococcus "), "sp."))


#sslo_title <- expression(paste(italic("Streptococcus "), "spp. and ",italic("Streptococcus"),"-like organisms"))


milk_6 <- ggplot(adj_means_6, aes(x=postpartum_sample, y=emmean, group=strep.IMI.cow, color=strep.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="SSLO",color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_6_adj_overall <- lme(fixed =milk_kg~ strep.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_6_adj_overall)
##                  Estimate     2.5 %      97.5 %
## (Intercept)    24.1913418 18.061248 30.32143511
## strep.IMI.cow1 -1.1965434 -2.347216 -0.04587109
## calving.sau1   -0.1142546 -1.542970  1.31446042
## calving.nas1   -0.3249312 -1.538226  0.88836401
# ---- Streptococcus spp ----

model_7 <- lme(fixed =milk_kg~ strep.genus.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_7,revpairwise~strep.genus.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.8 3.15  3     13.7     33.8
##  1                     22.8 3.24  3     12.4     33.1
## 
## postpartum_sample = 2:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     26.1 3.15  3     16.1     36.2
##  1                     23.6 3.24  3     13.3     33.9
## 
## postpartum_sample = 3:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     25.0 3.16  3     15.0     35.1
##  1                     23.7 3.25  3     13.3     34.0
## 
## postpartum_sample = 4:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     24.0 3.16  3     13.9     34.1
##  1                     22.6 3.26  3     12.3     33.0
## 
## postpartum_sample = 5:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.0 3.16  3     13.0     33.1
##  1                     23.5 3.26  3     13.1     33.8
## 
## postpartum_sample = 6:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     21.1 3.16  3     11.0     31.1
##  1                     20.7 3.26  3     10.3     31.1
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -0.997 0.979 328  -1.019  0.3092
## 
## postpartum_sample = 2:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -2.514 0.980 328  -2.564  0.0108
## 
## postpartum_sample = 3:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.342 1.006 328  -1.334  0.1832
## 
## postpartum_sample = 4:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.366 1.046 328  -1.306  0.1925
## 
## postpartum_sample = 5:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0    0.422 1.035 328   0.408  0.6838
## 
## postpartum_sample = 6:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -0.386 1.044 328  -0.370  0.7119
## 
## Degrees-of-freedom method: containment
model_7 <- lme(fixed =milk_kg~ strep.genus.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_7 <- emmeans(model_7,revpairwise~strep.genus.IMI.cow|postpartum_sample)

emmeans_7
## $emmeans
## postpartum_sample = 1:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.7 3.16  3     13.6     33.7
##  1                     22.7 3.25  3     12.3     33.0
## 
## postpartum_sample = 2:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     26.1 3.16  3     16.0     36.1
##  1                     23.5 3.25  3     13.2     33.8
## 
## postpartum_sample = 3:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     25.0 3.16  3     14.9     35.0
##  1                     23.6 3.25  3     13.2     33.9
## 
## postpartum_sample = 4:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     24.0 3.17  3     13.9     34.0
##  1                     22.5 3.26  3     12.2     32.9
## 
## postpartum_sample = 5:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.0 3.16  3     12.9     33.0
##  1                     23.4 3.26  3     13.0     33.7
## 
## postpartum_sample = 6:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     21.0 3.16  3     11.0     31.1
##  1                     20.6 3.26  3     10.2     31.0
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.037 0.988 326  -1.049  0.2948
## 
## postpartum_sample = 2:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -2.554 0.989 326  -2.582  0.0103
## 
## postpartum_sample = 3:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.382 1.015 326  -1.361  0.1743
## 
## postpartum_sample = 4:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.406 1.055 326  -1.333  0.1835
## 
## postpartum_sample = 5:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0    0.382 1.044 326   0.366  0.7149
## 
## postpartum_sample = 6:
##  contrast                                    estimate    SE  df t.ratio p.value
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -0.426 1.053 326  -0.405  0.6860
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_7)
## $emmeans
## postpartum_sample = 1:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.7 3.16  3     13.6     33.7
##  1                     22.7 3.25  3     12.3     33.0
## 
## postpartum_sample = 2:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     26.1 3.16  3     16.0     36.1
##  1                     23.5 3.25  3     13.2     33.8
## 
## postpartum_sample = 3:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     25.0 3.16  3     14.9     35.0
##  1                     23.6 3.25  3     13.2     33.9
## 
## postpartum_sample = 4:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     24.0 3.17  3     13.9     34.0
##  1                     22.5 3.26  3     12.2     32.9
## 
## postpartum_sample = 5:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     23.0 3.16  3     12.9     33.0
##  1                     23.4 3.26  3     13.0     33.7
## 
## postpartum_sample = 6:
##  strep.genus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                     21.0 3.16  3     11.0     31.1
##  1                     20.6 3.26  3     10.2     31.0
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.037 0.988 326    -2.98
##  upper.CL
##     0.907
## 
## postpartum_sample = 2:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -2.554 0.989 326    -4.50
##  upper.CL
##    -0.608
## 
## postpartum_sample = 3:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.382 1.015 326    -3.38
##  upper.CL
##     0.615
## 
## postpartum_sample = 4:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -1.406 1.055 326    -3.48
##  upper.CL
##     0.669
## 
## postpartum_sample = 5:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0    0.382 1.044 326    -1.67
##  upper.CL
##     2.437
## 
## postpartum_sample = 6:
##  contrast                                    estimate    SE  df lower.CL
##  strep.genus.IMI.cow1 - strep.genus.IMI.cow0   -0.426 1.053 326    -2.50
##  upper.CL
##     1.646
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_7 <- as.data.frame(emmeans_7$emmeans)

adj_means_7
## postpartum_sample = 1:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   23.69725 3.157400  3 13.64899 33.74550
##  1                   22.66040 3.246932  3 12.32721 32.99359
## 
## postpartum_sample = 2:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   26.05944 3.157444  3 16.01104 36.10783
##  1                   23.50559 3.247803  3 13.16963 33.84155
## 
## postpartum_sample = 3:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   24.95237 3.159185  3 14.89843 35.00630
##  1                   23.57075 3.253524  3 13.21659 33.92492
## 
## postpartum_sample = 4:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   23.95306 3.165449  3 13.87919 34.02693
##  1                   22.54710 3.262135  3 12.16553 32.92867
## 
## postpartum_sample = 5:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   22.97878 3.162177  3 12.91532 33.04224
##  1                   23.36067 3.260727  3 12.98358 33.73776
## 
## postpartum_sample = 6:
##  strep.genus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                   21.03048 3.163562  3 10.96261 31.09834
##  1                   20.60435 3.262575  3 10.22138 30.98732
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_7 <- as.data.frame(emmeans_7$contrasts) %>% select (postpartum_sample,p.value)


adj_means_7 <- adj_means_7 %>% left_join(contrasts_7, by="postpartum_sample")

adj_means_7 <- adj_means_7 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_7 <- adj_means_7 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


strep_genus_title <- expression(paste(italic("Streptococcus "), "spp."))


milk_7 <- ggplot(adj_means_7, aes(x=postpartum_sample, y=emmean, group=strep.genus.IMI.cow, color=strep.genus.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=strep_genus_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_7_adj_overall <- lme(fixed =milk_kg~ strep.genus.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_7_adj_overall)
##                         Estimate     2.5 %     97.5 %
## (Intercept)          23.95765291 17.834451 30.0808549
## strep.genus.IMI.cow1 -1.10822834 -2.505762  0.2893049
## calving.sau1         -0.03882997 -1.475496  1.3978356
## calving.nas1         -0.23813552 -1.446676  0.9704050
# ----Streptococcus dysgalactiae ----

model_8 <- lme(fixed =milk_kg ~ strep.dys.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_8,revpairwise~strep.dys.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.9 3.18  3     13.8     34.0
##  1                   21.2 3.34  3     10.6     31.9
## 
## postpartum_sample = 2:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   26.0 3.18  3     15.9     36.2
##  1                   22.5 3.33  3     11.9     33.1
## 
## postpartum_sample = 3:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.9 3.18  3     14.8     35.1
##  1                   23.3 3.34  3     12.7     34.0
## 
## postpartum_sample = 4:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.7 3.19  3     13.6     33.9
##  1                   23.3 3.36  3     12.6     34.0
## 
## postpartum_sample = 5:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.1 3.18  3     12.9     33.2
##  1                   23.3 3.35  3     12.6     33.9
## 
## postpartum_sample = 6:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   21.0 3.19  3     10.8     31.1
##  1                   21.1 3.35  3     10.4     31.8
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -2.633 1.16 328  -2.264  0.0243
## 
## postpartum_sample = 2:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -3.528 1.15 328  -3.054  0.0024
## 
## postpartum_sample = 3:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -1.584 1.18 328  -1.344  0.1799
## 
## postpartum_sample = 4:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -0.427 1.23 328  -0.347  0.7286
## 
## postpartum_sample = 5:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.184 1.22 328   0.151  0.8800
## 
## postpartum_sample = 6:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.153 1.22 328   0.126  0.8999
## 
## Degrees-of-freedom method: containment
model_8 <- lme(fixed =milk_kg ~ strep.dys.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_8 <- emmeans(model_8,revpairwise~strep.dys.IMI.cow|postpartum_sample)

emmeans_8
## $emmeans
## postpartum_sample = 1:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.8 3.18  3     13.7     33.9
##  1                   21.1 3.34  3     10.5     31.7
## 
## postpartum_sample = 2:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   26.0 3.18  3     15.8     36.1
##  1                   22.4 3.34  3     11.8     33.0
## 
## postpartum_sample = 3:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.8 3.18  3     14.7     34.9
##  1                   23.2 3.34  3     12.6     33.8
## 
## postpartum_sample = 4:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.6 3.19  3     13.5     33.8
##  1                   23.2 3.36  3     12.5     33.9
## 
## postpartum_sample = 5:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.0 3.18  3     12.9     33.1
##  1                   23.1 3.36  3     12.4     33.8
## 
## postpartum_sample = 6:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   20.9 3.18  3     10.7     31.0
##  1                   21.0 3.36  3     10.3     31.7
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -2.674 1.17 326  -2.287  0.0228
## 
## postpartum_sample = 2:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -3.571 1.16 326  -3.074  0.0023
## 
## postpartum_sample = 3:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -1.625 1.19 326  -1.371  0.1713
## 
## postpartum_sample = 4:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -0.468 1.24 326  -0.378  0.7054
## 
## postpartum_sample = 5:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.143 1.22 326   0.117  0.9071
## 
## postpartum_sample = 6:
##  contrast                                estimate   SE  df t.ratio p.value
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.114 1.23 326   0.093  0.9257
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_8)
## $emmeans
## postpartum_sample = 1:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.8 3.18  3     13.7     33.9
##  1                   21.1 3.34  3     10.5     31.7
## 
## postpartum_sample = 2:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   26.0 3.18  3     15.8     36.1
##  1                   22.4 3.34  3     11.8     33.0
## 
## postpartum_sample = 3:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   24.8 3.18  3     14.7     34.9
##  1                   23.2 3.34  3     12.6     33.8
## 
## postpartum_sample = 4:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.6 3.19  3     13.5     33.8
##  1                   23.2 3.36  3     12.5     33.9
## 
## postpartum_sample = 5:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   23.0 3.18  3     12.9     33.1
##  1                   23.1 3.36  3     12.4     33.8
## 
## postpartum_sample = 6:
##  strep.dys.IMI.cow emmean   SE df lower.CL upper.CL
##  0                   20.9 3.18  3     10.7     31.0
##  1                   21.0 3.36  3     10.3     31.7
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -2.674 1.17 326    -4.97   -0.374
## 
## postpartum_sample = 2:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -3.571 1.16 326    -5.86   -1.286
## 
## postpartum_sample = 3:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -1.625 1.19 326    -3.96    0.707
## 
## postpartum_sample = 4:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0   -0.468 1.24 326    -2.90    1.963
## 
## postpartum_sample = 5:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.143 1.22 326    -2.26    2.547
## 
## postpartum_sample = 6:
##  contrast                                estimate   SE  df lower.CL upper.CL
##  strep.dys.IMI.cow1 - strep.dys.IMI.cow0    0.114 1.23 326    -2.30    2.525
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_8 <- as.data.frame(emmeans_8$emmeans)

adj_means_8
## postpartum_sample = 1:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 23.78478 3.179046  3 13.66764 33.90193
##  1                 21.11050 3.339727  3 10.48200 31.73900
## 
## postpartum_sample = 2:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 25.95271 3.179171  3 15.83517 36.07025
##  1                 22.38156 3.337909  3 11.75884 33.00428
## 
## postpartum_sample = 3:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 24.82742 3.180732  3 14.70491 34.94993
##  1                 23.20194 3.344357  3 12.55871 33.84518
## 
## postpartum_sample = 4:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 23.64398 3.186213  3 13.50403 33.78393
##  1                 23.17645 3.358895  3 12.48695 33.86595
## 
## postpartum_sample = 5:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 22.98336 3.183433  3 12.85225 33.11446
##  1                 23.12611 3.355407  3 12.44771 33.80451
## 
## postpartum_sample = 6:
##  strep.dys.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                 20.86260 3.184700  3 10.72746 30.99773
##  1                 20.97700 3.355789  3 10.29738 31.65662
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_8 <- as.data.frame(emmeans_8$contrasts) %>% select (postpartum_sample,p.value)


adj_means_8 <- adj_means_8 %>% left_join(contrasts_8, by="postpartum_sample")

adj_means_8 <- adj_means_8 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_8 <- adj_means_8 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

staph_sp_title <- expression(paste(italic("Staphylococcus "), "sp."))


milk_8 <- ggplot(adj_means_8, aes(x=postpartum_sample, y=emmean, group=strep.dys.IMI.cow, color=strep.dys.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="Streptococcus dysgalactiae",color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_8_adj_overall <- lme(fixed =milk_kg~ strep.dys.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_8_adj_overall)
##                      Estimate     2.5 %     97.5 %
## (Intercept)        23.9332044 17.755659 30.1107500
## strep.dys.IMI.cow1 -1.4750149 -3.088485  0.1384552
## calving.sau1       -0.1215295 -1.551452  1.3083932
## calving.nas1       -0.2490436 -1.455491  0.9574034
# ---- Unspeciated Strep spp.  ----

model_9 <- lme(fixed =milk_kg ~ strep.sp.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_9,revpairwise~strep.sp.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.6 3.18  3     13.5     33.7
##  1                  23.0 3.40  3     12.1     33.8
## 
## postpartum_sample = 2:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.8 3.18  3     15.7     36.0
##  1                  23.2 3.40  3     12.4     34.0
## 
## postpartum_sample = 3:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.9 3.19  3     14.7     35.0
##  1                  23.5 3.41  3     12.7     34.4
## 
## postpartum_sample = 4:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.0 3.19  3     13.9     34.2
##  1                  21.2 3.42  3     10.3     32.1
## 
## postpartum_sample = 5:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.1 3.19  3     13.0     33.2
##  1                  23.4 3.43  3     12.5     34.3
## 
## postpartum_sample = 6:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  21.0 3.19  3     10.9     31.2
##  1                  20.7 3.43  3      9.8     31.6
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.635 1.32 328  -0.482  0.6304
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.615 1.30 328  -2.004  0.0458
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -1.346 1.34 328  -1.003  0.3167
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.804 1.37 328  -2.047  0.0415
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0    0.291 1.40 328   0.208  0.8357
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.323 1.41 328  -0.229  0.8187
## 
## Degrees-of-freedom method: containment
model_9 <- lme(fixed =milk_kg ~ strep.sp.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_9 <- emmeans(model_9,revpairwise~strep.sp.IMI.cow|postpartum_sample)

emmeans_9
## $emmeans
## postpartum_sample = 1:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.5 3.19  3    13.39     33.7
##  1                  22.9 3.40  3    12.06     33.7
## 
## postpartum_sample = 2:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.8 3.19  3    15.63     35.9
##  1                  23.2 3.40  3    12.34     34.0
## 
## postpartum_sample = 3:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.8 3.19  3    14.64     34.9
##  1                  23.4 3.41  3    12.58     34.3
## 
## postpartum_sample = 4:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.0 3.19  3    13.78     34.1
##  1                  21.1 3.42  3    10.25     32.0
## 
## postpartum_sample = 5:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.0 3.19  3    12.88     33.2
##  1                  23.3 3.44  3    12.39     34.3
## 
## postpartum_sample = 6:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  21.0 3.19  3    10.81     31.1
##  1                  20.6 3.44  3     9.71     31.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.641 1.32 326  -0.485  0.6278
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.621 1.31 326  -2.005  0.0458
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -1.352 1.34 326  -1.006  0.3153
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.811 1.37 326  -2.048  0.0414
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0    0.286 1.40 326   0.204  0.8387
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df t.ratio p.value
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.329 1.41 326  -0.233  0.8156
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_9)
## $emmeans
## postpartum_sample = 1:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.5 3.19  3    13.39     33.7
##  1                  22.9 3.40  3    12.06     33.7
## 
## postpartum_sample = 2:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.8 3.19  3    15.63     35.9
##  1                  23.2 3.40  3    12.34     34.0
## 
## postpartum_sample = 3:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.8 3.19  3    14.64     34.9
##  1                  23.4 3.41  3    12.58     34.3
## 
## postpartum_sample = 4:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.0 3.19  3    13.78     34.1
##  1                  21.1 3.42  3    10.25     32.0
## 
## postpartum_sample = 5:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.0 3.19  3    12.88     33.2
##  1                  23.3 3.44  3    12.39     34.3
## 
## postpartum_sample = 6:
##  strep.sp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  21.0 3.19  3    10.81     31.1
##  1                  20.6 3.44  3     9.71     31.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.641 1.32 326    -3.24    1.957
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.621 1.31 326    -5.19   -0.049
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -1.352 1.34 326    -4.00    1.293
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -2.811 1.37 326    -5.51   -0.111
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0    0.286 1.40 326    -2.48    3.050
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  strep.sp.IMI.cow1 - strep.sp.IMI.cow0   -0.329 1.41 326    -3.10    2.444
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_9 <- as.data.frame(emmeans_9$emmeans)

adj_means_9
## postpartum_sample = 1:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                23.53571 3.187823  3 13.390631 33.68078
##  1                22.89479 3.404624  3 12.059763 33.72983
## 
## postpartum_sample = 2:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                25.77887 3.188014  3 15.633185 35.92455
##  1                23.15820 3.399783  3 12.338570 33.97782
## 
## postpartum_sample = 3:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                24.79177 3.189373  3 14.641756 34.94177
##  1                23.43943 3.413121  3 12.577353 34.30150
## 
## postpartum_sample = 4:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                23.95079 3.194610  3 13.784117 34.11747
##  1                21.13975 3.421827  3 10.249970 32.02953
## 
## postpartum_sample = 5:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                23.03544 3.191821  3 12.877638 33.19324
##  1                23.32163 3.435992  3 12.386766 34.25648
## 
## postpartum_sample = 6:
##  strep.sp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                20.97381 3.192976  3 10.812339 31.13529
##  1                20.64470 3.437415  3  9.705308 31.58408
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_9 <- as.data.frame(emmeans_9$contrasts) %>% select (postpartum_sample,p.value)


adj_means_9 <- adj_means_9 %>% left_join(contrasts_9, by="postpartum_sample")

adj_means_9 <- adj_means_9 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_9 <- adj_means_9 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


strep_sp_title <- expression(paste("Unspeciated ",italic("Streptococcus ")))



milk_9 <- ggplot(adj_means_9, aes(x=postpartum_sample, y=emmean, group=strep.sp.IMI.cow, color=strep.sp.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=strep_sp_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_9_adj_overall <- lme(fixed =milk_kg~ strep.sp.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_9_adj_overall)
##                     Estimate     2.5 %     97.5 %
## (Intercept)       23.8315558 17.638362 30.0247496
## strep.sp.IMI.cow1 -1.2385084 -3.029250  0.5522331
## calving.sau1      -0.1286207 -1.560117  1.3028755
## calving.nas1      -0.1416688 -1.340876  1.0575382
# ----Streptococcus like organisms ----

model_10 <- lme(fixed =milk_kg~ strep.like.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_10,revpairwise~strep.like.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.6 3.20  3     13.4     33.8
##  1                    23.3 3.30  3     12.8     33.8
## 
## postpartum_sample = 2:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    25.6 3.20  3     15.4     35.8
##  1                    25.4 3.31  3     14.9     36.0
## 
## postpartum_sample = 3:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    24.7 3.20  3     14.5     34.9
##  1                    24.8 3.33  3     14.2     35.4
## 
## postpartum_sample = 4:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.7 3.21  3     13.5     33.9
##  1                    23.7 3.34  3     13.1     34.4
## 
## postpartum_sample = 5:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.3 3.20  3     13.1     33.5
##  1                    22.4 3.34  3     11.8     33.0
## 
## postpartum_sample = 6:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    21.1 3.21  3     10.9     31.3
##  1                    20.8 3.35  3     10.1     31.5
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0 -0.30083 1.01 328  -0.298  0.7660
## 
## postpartum_sample = 2:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0 -0.19489 1.04 328  -0.188  0.8511
## 
## postpartum_sample = 3:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  0.09261 1.09 328   0.085  0.9326
## 
## postpartum_sample = 4:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  0.00183 1.15 328   0.002  0.9987
## 
## postpartum_sample = 5:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0 -0.86566 1.14 328  -0.760  0.4478
## 
## postpartum_sample = 6:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0 -0.27872 1.18 328  -0.236  0.8132
## 
## Degrees-of-freedom method: containment
model_10 <- lme(fixed =milk_kg~ strep.like.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_10 <- emmeans(model_10,revpairwise~strep.like.IMI.cow|postpartum_sample)

emmeans_10
## $emmeans
## postpartum_sample = 1:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.5 3.20  3    13.34     33.7
##  1                    23.2 3.31  3    12.67     33.7
## 
## postpartum_sample = 2:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    25.6 3.20  3    15.37     35.8
##  1                    25.3 3.32  3    14.78     35.9
## 
## postpartum_sample = 3:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    24.7 3.20  3    14.45     34.8
##  1                    24.7 3.34  3    14.10     35.3
## 
## postpartum_sample = 4:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.6 3.21  3    13.43     33.9
##  1                    23.6 3.35  3    12.96     34.3
## 
## postpartum_sample = 5:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.2 3.21  3    13.00     33.4
##  1                    22.3 3.35  3    11.66     33.0
## 
## postpartum_sample = 6:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    21.0 3.21  3    10.78     31.2
##  1                    20.7 3.36  3     9.99     31.4
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.3252 1.02 326  -0.320  0.7489
## 
## postpartum_sample = 2:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.2197 1.04 326  -0.211  0.8333
## 
## postpartum_sample = 3:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0   0.0696 1.10 326   0.063  0.9495
## 
## postpartum_sample = 4:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.0212 1.15 326  -0.018  0.9854
## 
## postpartum_sample = 5:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.8884 1.14 326  -0.777  0.4378
## 
## postpartum_sample = 6:
##  contrast                                  estimate   SE  df t.ratio p.value
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.3017 1.18 326  -0.255  0.7989
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_10)
## $emmeans
## postpartum_sample = 1:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.5 3.20  3    13.34     33.7
##  1                    23.2 3.31  3    12.67     33.7
## 
## postpartum_sample = 2:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    25.6 3.20  3    15.37     35.8
##  1                    25.3 3.32  3    14.78     35.9
## 
## postpartum_sample = 3:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    24.7 3.20  3    14.45     34.8
##  1                    24.7 3.34  3    14.10     35.3
## 
## postpartum_sample = 4:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.6 3.21  3    13.43     33.9
##  1                    23.6 3.35  3    12.96     34.3
## 
## postpartum_sample = 5:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    23.2 3.21  3    13.00     33.4
##  1                    22.3 3.35  3    11.66     33.0
## 
## postpartum_sample = 6:
##  strep.like.IMI.cow emmean   SE df lower.CL upper.CL
##  0                    21.0 3.21  3    10.78     31.2
##  1                    20.7 3.36  3     9.99     31.4
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.3252 1.02 326    -2.32     1.67
## 
## postpartum_sample = 2:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.2197 1.04 326    -2.27     1.83
## 
## postpartum_sample = 3:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0   0.0696 1.10 326    -2.09     2.23
## 
## postpartum_sample = 4:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.0212 1.15 326    -2.29     2.25
## 
## postpartum_sample = 5:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.8884 1.14 326    -3.14     1.36
## 
## postpartum_sample = 6:
##  contrast                                  estimate   SE  df lower.CL upper.CL
##  strep.like.IMI.cow1 - strep.like.IMI.cow0  -0.3017 1.18 326    -2.63     2.03
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_10 <- as.data.frame(emmeans_10$emmeans)

adj_means_10
## postpartum_sample = 1:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  23.53111 3.202881  3 13.338115 33.72411
##  1                  23.20590 3.311423  3 12.667471 33.74432
## 
## postpartum_sample = 2:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  25.56298 3.202835  3 15.370126 35.75583
##  1                  25.34328 3.319474  3 14.779234 35.90733
## 
## postpartum_sample = 3:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  24.65116 3.204072  3 14.454378 34.84795
##  1                  24.72076 3.335896  3 14.104447 35.33707
## 
## postpartum_sample = 4:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  23.64900 3.209689  3 13.434343 33.86367
##  1                  23.62783 3.351032  3 12.963346 34.29230
## 
## postpartum_sample = 5:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  23.20356 3.206786  3 12.998139 33.40899
##  1                  22.31520 3.348774  3 11.657905 32.97249
## 
## postpartum_sample = 6:
##  strep.like.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                  20.99198 3.207710  3 10.783615 31.20034
##  1                  20.69024 3.361775  3  9.991574 31.38891
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_10 <- as.data.frame(emmeans_10$contrasts) %>% select (postpartum_sample,p.value)


adj_means_10 <- adj_means_10 %>% left_join(contrasts_10, by="postpartum_sample")

adj_means_10 <- adj_means_10 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_10 <- adj_means_10 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


strep_like_title <- expression(paste(italic("Streptococcus"),"-like organisms"))

milk_10 <- ggplot(adj_means_10, aes(x=postpartum_sample, y=emmean, group=strep.like.IMI.cow, color=strep.like.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=strep_like_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_10_adj_overall <- lme(fixed =milk_kg~ strep.like.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_10_adj_overall)
##                       Estimate     2.5 %    97.5 %
## (Intercept)         23.7525373 17.533913 29.971162
## strep.like.IMI.cow1 -0.1848530 -1.663962  1.294256
## calving.sau1        -0.1528709 -1.593852  1.288110
## calving.nas1        -0.1249329 -1.332404  1.082539
# ---- Enterococcus genus ----


model_11 <- lme(fixed =milk_kg~ enterococcus.spp.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_11,revpairwise~enterococcus.spp.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.7 3.22  3    13.47     33.9
##  1                          22.3 3.39  3    11.53     33.1
## 
## postpartum_sample = 2:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          25.6 3.22  3    15.38     35.9
##  1                          25.3 3.40  3    14.51     36.2
## 
## postpartum_sample = 3:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          24.7 3.22  3    14.46     34.9
##  1                          24.9 3.43  3    14.01     35.9
## 
## postpartum_sample = 4:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.7 3.22  3    13.42     33.9
##  1                          23.9 3.44  3    12.97     34.9
## 
## postpartum_sample = 5:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.2 3.22  3    12.99     33.5
##  1                          22.2 3.45  3    11.22     33.2
## 
## postpartum_sample = 6:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          21.1 3.22  3    10.81     31.3
##  1                          20.6 3.46  3     9.59     31.6
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.374 1.22 328
##  t.ratio p.value
##   -1.125  0.2615
## 
## postpartum_sample = 2:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.279 1.25 328
##  t.ratio p.value
##   -0.223  0.8240
## 
## postpartum_sample = 3:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.234 1.33 328
##  t.ratio p.value
##    0.175  0.8611
## 
## postpartum_sample = 4:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.240 1.36 328
##  t.ratio p.value
##    0.176  0.8600
## 
## postpartum_sample = 5:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.042 1.38 328
##  t.ratio p.value
##   -0.757  0.4495
## 
## postpartum_sample = 6:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.456 1.41 328
##  t.ratio p.value
##   -0.323  0.7472
## 
## Degrees-of-freedom method: containment
model_11 <- lme(fixed =milk_kg~ enterococcus.spp.IMI.cow*postpartum_sample+calving.nas+calving.sau,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_11 <- emmeans(model_11,revpairwise~enterococcus.spp.IMI.cow|postpartum_sample)

emmeans_11
## $emmeans
## postpartum_sample = 1:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.6 3.22  3    13.39     33.9
##  1                          22.2 3.40  3    11.41     33.1
## 
## postpartum_sample = 2:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          25.5 3.22  3    15.30     35.8
##  1                          25.2 3.41  3    14.39     36.1
## 
## postpartum_sample = 3:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          24.6 3.22  3    14.38     34.9
##  1                          24.8 3.44  3    13.89     35.8
## 
## postpartum_sample = 4:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.6 3.23  3    13.34     33.9
##  1                          23.8 3.45  3    12.85     34.8
## 
## postpartum_sample = 5:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.2 3.22  3    12.91     33.4
##  1                          22.1 3.46  3    11.10     33.1
## 
## postpartum_sample = 6:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          21.0 3.22  3    10.72     31.2
##  1                          20.5 3.47  3     9.46     31.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.396 1.23 326
##  t.ratio p.value
##   -1.138  0.2559
## 
## postpartum_sample = 2:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.301 1.26 326
##  t.ratio p.value
##   -0.239  0.8112
## 
## postpartum_sample = 3:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.212 1.34 326
##  t.ratio p.value
##    0.158  0.8743
## 
## postpartum_sample = 4:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.220 1.37 326
##  t.ratio p.value
##    0.161  0.8724
## 
## postpartum_sample = 5:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.062 1.38 326
##  t.ratio p.value
##   -0.769  0.4422
## 
## postpartum_sample = 6:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.477 1.42 326
##  t.ratio p.value
##   -0.337  0.7367
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment
confint(emmeans_11)
## $emmeans
## postpartum_sample = 1:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.6 3.22  3    13.39     33.9
##  1                          22.2 3.40  3    11.41     33.1
## 
## postpartum_sample = 2:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          25.5 3.22  3    15.30     35.8
##  1                          25.2 3.41  3    14.39     36.1
## 
## postpartum_sample = 3:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          24.6 3.22  3    14.38     34.9
##  1                          24.8 3.44  3    13.89     35.8
## 
## postpartum_sample = 4:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.6 3.23  3    13.34     33.9
##  1                          23.8 3.45  3    12.85     34.8
## 
## postpartum_sample = 5:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          23.2 3.22  3    12.91     33.4
##  1                          22.1 3.46  3    11.10     33.1
## 
## postpartum_sample = 6:
##  enterococcus.spp.IMI.cow emmean   SE df lower.CL upper.CL
##  0                          21.0 3.22  3    10.72     31.2
##  1                          20.5 3.47  3     9.46     31.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.396 1.23 326
##  lower.CL upper.CL
##     -3.81     1.02
## 
## postpartum_sample = 2:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.301 1.26 326
##  lower.CL upper.CL
##     -2.78     2.17
## 
## postpartum_sample = 3:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.212 1.34 326
##  lower.CL upper.CL
##     -2.42     2.84
## 
## postpartum_sample = 4:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0    0.220 1.37 326
##  lower.CL upper.CL
##     -2.47     2.91
## 
## postpartum_sample = 5:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -1.062 1.38 326
##  lower.CL upper.CL
##     -3.78     1.65
## 
## postpartum_sample = 6:
##  contrast                                              estimate   SE  df
##  enterococcus.spp.IMI.cow1 - enterococcus.spp.IMI.cow0   -0.477 1.42 326
##  lower.CL upper.CL
##     -3.26     2.31
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_11 <- as.data.frame(emmeans_11$emmeans)

adj_means_11
## postpartum_sample = 1:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        23.63360 3.219617  3 13.387341 33.87986
##  1                        22.23748 3.402552  3 11.409042 33.06592
## 
## postpartum_sample = 2:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        25.54884 3.219719  3 15.302262 35.79543
##  1                        25.24797 3.413364  3 14.385127 36.11082
## 
## postpartum_sample = 3:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        24.63362 3.220840  3 14.383474 34.88377
##  1                        24.84549 3.442505  3 13.889906 35.80108
## 
## postpartum_sample = 4:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        23.60780 3.226201  3 13.340591 33.87501
##  1                        23.82751 3.450570  3 12.846256 34.80876
## 
## postpartum_sample = 5:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        23.16422 3.223425  3 12.905844 33.42260
##  1                        22.10243 3.457187  3 11.100113 33.10474
## 
## postpartum_sample = 6:
##  enterococcus.spp.IMI.cow   emmean       SE df  lower.CL upper.CL
##  0                        20.98585 3.224451  3 10.724203 31.24749
##  1                        20.50919 3.471020  3  9.462858 31.55553
## 
## Results are averaged over the levels of: calving.nas, calving.sau 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_11 <- as.data.frame(emmeans_11$contrasts) %>% select (postpartum_sample,p.value)


adj_means_11 <- adj_means_11 %>% left_join(contrasts_11, by="postpartum_sample")

adj_means_11 <- adj_means_11 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_11 <- adj_means_11 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

enterococcus_genus_title <- expression(paste(italic("Enterococcus "),"spp."))

adj_means_11$enterococcus.spp.IMI.cow <- as.factor(adj_means_11$enterococcus.spp.IMI.cow)

milk_11 <- ggplot(adj_means_11, aes(x=postpartum_sample, y=emmean, group=enterococcus.spp.IMI.cow, color=enterococcus.spp.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=enterococcus_genus_title,color="IMI") +
  theme_classic() +
  theme(plot.title=element_text(face="italic")) +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_11_adj_overall <- lme(fixed =milk_kg~ strep.like.IMI.cow+calving.sau+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_11_adj_overall)
##                       Estimate     2.5 %    97.5 %
## (Intercept)         23.7525373 17.533913 29.971162
## strep.like.IMI.cow1 -0.1848530 -1.663962  1.294256
## calving.sau1        -0.1528709 -1.593852  1.288110
## calving.nas1        -0.1249329 -1.332404  1.082539
# --- Gram negative bacteria ----

model_12 <- lme(fixed =milk_kg~ gram.neg.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_12,revpairwise~gram.neg.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.7 3.20  3     13.5     33.9
##  1                  22.0 3.42  3     11.1     32.9
## 
## postpartum_sample = 2:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.6 3.20  3     15.4     35.8
##  1                  25.4 3.42  3     14.5     36.3
## 
## postpartum_sample = 3:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.7 3.20  3     14.5     34.9
##  1                  25.2 3.43  3     14.3     36.1
## 
## postpartum_sample = 4:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.7 3.21  3     13.5     33.9
##  1                  23.6 3.44  3     12.7     34.6
## 
## postpartum_sample = 5:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.1 3.20  3     12.9     33.3
##  1                  23.7 3.45  3     12.7     34.7
## 
## postpartum_sample = 6:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.9 3.20  3     10.7     31.1
##  1                  22.5 3.48  3     11.4     33.6
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -1.683 1.32 328  -1.270  0.2049
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.241 1.33 328  -0.181  0.8562
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.510 1.36 328   0.374  0.7085
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.123 1.39 328  -0.089  0.9294
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.639 1.42 328   0.449  0.6541
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    1.638 1.49 328   1.100  0.2722
## 
## Degrees-of-freedom method: containment
model_12 <- lme(fixed =milk_kg~ gram.neg.IMI.cow*postpartum_sample+calving.nas+calving.sau.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_12 <- emmeans(model_12,revpairwise~gram.neg.IMI.cow|postpartum_sample)

emmeans_12
## $emmeans
## postpartum_sample = 1:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.3 3.09  3     13.5     33.2
##  1                  21.7 3.32  3     11.1     32.2
## 
## postpartum_sample = 2:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.2 3.09  3     15.4     35.1
##  1                  25.0 3.32  3     14.5     35.6
## 
## postpartum_sample = 3:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.3 3.10  3     14.5     34.2
##  1                  24.8 3.33  3     14.2     35.4
## 
## postpartum_sample = 4:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.4 3.10  3     13.5     33.2
##  1                  23.2 3.34  3     12.6     33.8
## 
## postpartum_sample = 5:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  22.7 3.10  3     12.8     32.6
##  1                  23.3 3.36  3     12.6     34.0
## 
## postpartum_sample = 6:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.5 3.10  3     10.6     30.4
##  1                  22.1 3.38  3     11.4     32.9
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -1.686 1.32 326  -1.277  0.2024
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.229 1.32 326  -0.174  0.8622
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.501 1.36 326   0.369  0.7120
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.148 1.39 326  -0.107  0.9148
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.615 1.42 326   0.434  0.6647
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df t.ratio p.value
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    1.635 1.48 326   1.101  0.2716
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_12)
## $emmeans
## postpartum_sample = 1:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.3 3.09  3     13.5     33.2
##  1                  21.7 3.32  3     11.1     32.2
## 
## postpartum_sample = 2:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.2 3.09  3     15.4     35.1
##  1                  25.0 3.32  3     14.5     35.6
## 
## postpartum_sample = 3:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.3 3.10  3     14.5     34.2
##  1                  24.8 3.33  3     14.2     35.4
## 
## postpartum_sample = 4:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.4 3.10  3     13.5     33.2
##  1                  23.2 3.34  3     12.6     33.8
## 
## postpartum_sample = 5:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  22.7 3.10  3     12.8     32.6
##  1                  23.3 3.36  3     12.6     34.0
## 
## postpartum_sample = 6:
##  gram.neg.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.5 3.10  3     10.6     30.4
##  1                  22.1 3.38  3     11.4     32.9
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -1.686 1.32 326    -4.28    0.911
## 
## postpartum_sample = 2:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.229 1.32 326    -2.83    2.370
## 
## postpartum_sample = 3:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.501 1.36 326    -2.17    3.171
## 
## postpartum_sample = 4:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0   -0.148 1.39 326    -2.87    2.578
## 
## postpartum_sample = 5:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    0.615 1.42 326    -2.18    3.407
## 
## postpartum_sample = 6:
##  contrast                              estimate   SE  df lower.CL upper.CL
##  gram.neg.IMI.cow1 - gram.neg.IMI.cow0    1.635 1.48 326    -1.29    4.556
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_12 <- as.data.frame(emmeans_12$emmeans)

adj_means_12
## postpartum_sample = 1:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                23.34270 3.093711  3 13.49713 33.18827
##  1                21.65706 3.318725  3 11.09540 32.21873
## 
## postpartum_sample = 2:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                25.24450 3.094065  3 15.39780 35.09119
##  1                25.01503 3.318466  3 14.45419 35.57587
## 
## postpartum_sample = 3:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                24.31372 3.095328  3 14.46300 34.16443
##  1                24.81514 3.331239  3 14.21365 35.41663
## 
## postpartum_sample = 4:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                23.36349 3.100857  3 13.49518 33.23180
##  1                23.21506 3.339960  3 12.58582 33.84431
## 
## postpartum_sample = 5:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                22.69904 3.097818  3 12.84040 32.55768
##  1                23.31451 3.355560  3 12.63562 33.99340
## 
## postpartum_sample = 6:
##  gram.neg.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                20.49715 3.098898  3 10.63508 30.35923
##  1                22.13224 3.382845  3 11.36651 32.89796
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_12 <- as.data.frame(emmeans_12$contrasts) %>% select (postpartum_sample,p.value)


adj_means_12 <- adj_means_12 %>% left_join(contrasts_12, by="postpartum_sample")

adj_means_12 <- adj_means_12 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_12 <- adj_means_12 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

milk_12 <- ggplot(adj_means_12, aes(x=postpartum_sample, y=emmean, group=gram.neg.IMI.cow, color=gram.neg.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="Gram-negative bacteria",color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_12_adj_overall <- lme(fixed =milk_kg~ gram.neg.IMI.cow+calving.sau.sslo+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_12_adj_overall)
##                       Estimate     2.5 %     97.5 %
## (Intercept)       24.374813199 18.349578 30.4000485
## gram.neg.IMI.cow1 -0.006239189 -1.838801  1.8263226
## calving.sau.sslo1 -1.725735452 -2.906934 -0.5445365
## calving.nas1      -0.432707127 -1.630841  0.7654267
# ---- Others ----

model_13 <- lme(fixed =milk_kg~ other.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_13,revpairwise~other.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.8 3.14  3     13.8     33.8
##  1               23.3 3.18  3     13.2     33.4
## 
## postpartum_sample = 2:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.3 3.14  3     15.3     35.3
##  1               26.2 3.18  3     16.1     36.4
## 
## postpartum_sample = 3:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.3 3.14  3     14.3     34.3
##  1               25.6 3.18  3     15.5     35.8
## 
## postpartum_sample = 4:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.0 3.15  3     13.0     33.1
##  1               25.0 3.19  3     14.8     35.1
## 
## postpartum_sample = 5:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               22.5 3.15  3     12.5     32.5
##  1               24.3 3.19  3     14.2     34.5
## 
## postpartum_sample = 6:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               20.6 3.15  3     10.6     30.6
##  1               21.8 3.19  3     11.7     32.0
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0   -0.500 0.851 328  -0.587  0.5574
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    0.943 0.853 328   1.106  0.2695
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.355 0.876 328   1.547  0.1228
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.941 0.915 328   2.121  0.0347
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.846 0.903 328   2.044  0.0418
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.234 0.917 328   1.346  0.1792
## 
## Degrees-of-freedom method: containment
model_13 <- lme(fixed =milk_kg~ other.IMI.cow*postpartum_sample+calving.nas+calving.sau.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_13 <- emmeans(model_13,revpairwise~other.IMI.cow|postpartum_sample)

emmeans_13
## $emmeans
## postpartum_sample = 1:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.5 3.05  3     13.8     33.2
##  1               22.9 3.09  3     13.1     32.8
## 
## postpartum_sample = 2:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.0 3.05  3     15.3     34.7
##  1               25.8 3.10  3     16.0     35.7
## 
## postpartum_sample = 3:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.0 3.05  3     14.3     33.7
##  1               25.3 3.10  3     15.4     35.1
## 
## postpartum_sample = 4:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               22.7 3.06  3     13.0     32.5
##  1               24.6 3.10  3     14.7     34.4
## 
## postpartum_sample = 5:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               22.2 3.06  3     12.5     31.9
##  1               23.9 3.10  3     14.1     33.8
## 
## postpartum_sample = 6:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               20.3 3.06  3     10.6     30.0
##  1               21.4 3.11  3     11.6     31.3
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0   -0.616 0.856 326  -0.720  0.4721
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    0.838 0.857 326   0.978  0.3287
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.271 0.881 326   1.442  0.1502
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.855 0.920 326   2.016  0.0446
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.752 0.909 326   1.928  0.0547
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df t.ratio p.value
##  other.IMI.cow1 - other.IMI.cow0    1.151 0.922 326   1.249  0.2126
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_13)
## $emmeans
## postpartum_sample = 1:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               23.5 3.05  3     13.8     33.2
##  1               22.9 3.09  3     13.1     32.8
## 
## postpartum_sample = 2:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               25.0 3.05  3     15.3     34.7
##  1               25.8 3.10  3     16.0     35.7
## 
## postpartum_sample = 3:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               24.0 3.05  3     14.3     33.7
##  1               25.3 3.10  3     15.4     35.1
## 
## postpartum_sample = 4:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               22.7 3.06  3     13.0     32.5
##  1               24.6 3.10  3     14.7     34.4
## 
## postpartum_sample = 5:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               22.2 3.06  3     12.5     31.9
##  1               23.9 3.10  3     14.1     33.8
## 
## postpartum_sample = 6:
##  other.IMI.cow emmean   SE df lower.CL upper.CL
##  0               20.3 3.06  3     10.6     30.0
##  1               21.4 3.11  3     11.6     31.3
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0   -0.616 0.856 326  -2.2996     1.07
## 
## postpartum_sample = 2:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0    0.838 0.857 326  -0.8477     2.52
## 
## postpartum_sample = 3:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0    1.271 0.881 326  -0.4627     3.00
## 
## postpartum_sample = 4:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0    1.855 0.920 326   0.0449     3.67
## 
## postpartum_sample = 5:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0    1.752 0.909 326  -0.0354     3.54
## 
## postpartum_sample = 6:
##  contrast                        estimate    SE  df lower.CL upper.CL
##  other.IMI.cow1 - other.IMI.cow0    1.151 0.922 326  -0.6621     2.96
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_13 <- as.data.frame(emmeans_13$emmeans)

adj_means_13
## postpartum_sample = 1:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             23.52108 3.051067  3 13.81122 33.23094
##  1             22.90496 3.093956  3 13.05861 32.75130
## 
## postpartum_sample = 2:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             24.99919 3.050909  3 15.28984 34.70855
##  1             25.83767 3.095156  3 15.98751 35.68784
## 
## postpartum_sample = 3:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             23.98122 3.052484  3 14.26685 33.69559
##  1             25.25208 3.099747  3 15.38730 35.11686
## 
## postpartum_sample = 4:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             22.70855 3.061907  3 12.96419 32.45291
##  1             24.56376 3.104284  3 14.68455 34.44298
## 
## postpartum_sample = 5:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             22.18873 3.056399  3 12.46190 31.91555
##  1             23.94064 3.104424  3 14.06098 33.82031
## 
## postpartum_sample = 6:
##  other.IMI.cow   emmean       SE df lower.CL upper.CL
##  0             20.29240 3.058088  3 10.56020 30.02461
##  1             21.44327 3.106973  3 11.55550 31.33105
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_13 <- as.data.frame(emmeans_13$contrasts) %>% select (postpartum_sample,p.value)


adj_means_13 <- adj_means_13 %>% left_join(contrasts_13, by="postpartum_sample")

adj_means_13 <- adj_means_13 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_13 <- adj_means_13 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))


milk_13 <- ggplot(adj_means_13, aes(x=postpartum_sample, y=emmean, group=other.IMI.cow, color=other.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title="Others",color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_13_adj_overall <- lme(fixed =milk_kg~ other.IMI.cow+calving.sau.sslo+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_13_adj_overall)
##                     Estimate      2.5 %     97.5 %
## (Intercept)       24.0502905 18.1079560 29.9926251
## other.IMI.cow1     0.8350230 -0.4155544  2.0856004
## calving.sau.sslo1 -1.6776553 -2.8585014 -0.4968093
## calving.nas1      -0.2824471 -1.4992564  0.9343622
# ---- Bacillus ----

model_14 <- lme(fixed =milk_kg~ bacillus.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_14,revpairwise~bacillus.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.4 3.13  3     13.4     33.3
##  1                  24.4 3.21  3     14.2     34.6
## 
## postpartum_sample = 2:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.2 3.13  3     15.3     35.2
##  1                  27.0 3.21  3     16.8     37.2
## 
## postpartum_sample = 3:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.3 3.13  3     14.4     34.3
##  1                  26.3 3.22  3     16.1     36.5
## 
## postpartum_sample = 4:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.2 3.14  3     13.3     33.2
##  1                  25.5 3.22  3     15.3     35.8
## 
## postpartum_sample = 5:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  22.7 3.13  3     12.7     32.6
##  1                  25.0 3.22  3     14.7     35.2
## 
## postpartum_sample = 6:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.5 3.14  3     10.5     30.5
##  1                  23.0 3.22  3     12.7     33.3
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     1.05 0.930 328   1.133  0.2580
## 
## postpartum_sample = 2:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     1.75 0.936 328   1.871  0.0622
## 
## postpartum_sample = 3:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     1.96 0.975 328   2.005  0.0458
## 
## postpartum_sample = 4:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     2.29 1.000 328   2.292  0.0226
## 
## postpartum_sample = 5:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     2.28 1.002 328   2.278  0.0234
## 
## postpartum_sample = 6:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0     2.50 1.012 328   2.474  0.0139
## 
## Degrees-of-freedom method: containment
model_14 <- lme(fixed =milk_kg~ bacillus.IMI.cow*postpartum_sample+calving.nas+calving.sau.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_14 <- emmeans(model_14,revpairwise~bacillus.IMI.cow|postpartum_sample)

emmeans_14
## $emmeans
## postpartum_sample = 1:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.1 3.04  3     13.4     32.8
##  1                  23.9 3.13  3     14.0     33.9
## 
## postpartum_sample = 2:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.0 3.05  3     15.3     34.7
##  1                  26.5 3.13  3     16.6     36.5
## 
## postpartum_sample = 3:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.1 3.05  3     14.4     33.8
##  1                  25.8 3.14  3     15.8     35.8
## 
## postpartum_sample = 4:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.0 3.05  3     13.3     32.7
##  1                  25.1 3.15  3     15.1     35.1
## 
## postpartum_sample = 5:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  22.4 3.05  3     12.7     32.1
##  1                  24.5 3.15  3     14.5     34.5
## 
## postpartum_sample = 6:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.2 3.05  3     10.5     29.9
##  1                  22.5 3.15  3     12.5     32.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    0.819 0.935 326   0.876  0.3818
## 
## postpartum_sample = 2:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    1.536 0.940 326   1.634  0.1032
## 
## postpartum_sample = 3:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    1.761 0.979 326   1.799  0.0729
## 
## postpartum_sample = 4:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.101 1.004 326   2.093  0.0371
## 
## postpartum_sample = 5:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.085 1.006 326   2.073  0.0389
## 
## postpartum_sample = 6:
##  contrast                              estimate    SE  df t.ratio p.value
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.319 1.016 326   2.284  0.0230
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_14)
## $emmeans
## postpartum_sample = 1:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.1 3.04  3     13.4     32.8
##  1                  23.9 3.13  3     14.0     33.9
## 
## postpartum_sample = 2:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  25.0 3.05  3     15.3     34.7
##  1                  26.5 3.13  3     16.6     36.5
## 
## postpartum_sample = 3:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  24.1 3.05  3     14.4     33.8
##  1                  25.8 3.14  3     15.8     35.8
## 
## postpartum_sample = 4:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  23.0 3.05  3     13.3     32.7
##  1                  25.1 3.15  3     15.1     35.1
## 
## postpartum_sample = 5:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  22.4 3.05  3     12.7     32.1
##  1                  24.5 3.15  3     14.5     34.5
## 
## postpartum_sample = 6:
##  bacillus.IMI.cow emmean   SE df lower.CL upper.CL
##  0                  20.2 3.05  3     10.5     29.9
##  1                  22.5 3.15  3     12.5     32.6
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    0.819 0.935 326   -1.020     2.66
## 
## postpartum_sample = 2:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    1.536 0.940 326   -0.313     3.38
## 
## postpartum_sample = 3:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    1.761 0.979 326   -0.165     3.69
## 
## postpartum_sample = 4:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.101 1.004 326    0.127     4.08
## 
## postpartum_sample = 5:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.085 1.006 326    0.107     4.06
## 
## postpartum_sample = 6:
##  contrast                              estimate    SE  df lower.CL upper.CL
##  bacillus.IMI.cow1 - bacillus.IMI.cow0    2.319 1.016 326    0.321     4.32
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_14 <- as.data.frame(emmeans_14$emmeans)

adj_means_14
## postpartum_sample = 1:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                23.12474 3.044840  3 13.43470 32.81478
##  1                23.94341 3.130090  3 13.98206 33.90475
## 
## postpartum_sample = 2:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                24.98658 3.045068  3 15.29581 34.67735
##  1                26.52245 3.131491  3 16.55664 36.48825
## 
## postpartum_sample = 3:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                24.08029 3.046221  3 14.38585 33.77472
##  1                25.84079 3.141734  3 15.84239 35.83919
## 
## postpartum_sample = 4:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                22.97119 3.053532  3 13.25349 32.68889
##  1                25.07219 3.145081  3 15.06314 35.08124
## 
## postpartum_sample = 5:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                22.39812 3.049381  3 12.69362 32.10261
##  1                24.48354 3.148197  3 14.46457 34.50251
## 
## postpartum_sample = 6:
##  bacillus.IMI.cow   emmean       SE df lower.CL upper.CL
##  0                20.21681 3.051139  3 10.50672 29.92689
##  1                22.53602 3.149579  3 12.51265 32.55939
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_14 <- as.data.frame(emmeans_14$contrasts) %>% select (postpartum_sample,p.value)


adj_means_14 <- adj_means_14 %>% left_join(contrasts_14, by="postpartum_sample")

adj_means_14 <- adj_means_14 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_14 <- adj_means_14 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

bacillus_title <- expression(paste(italic("Bacillus "),"spp."))


milk_14 <- ggplot(adj_means_14, aes(x=postpartum_sample, y=emmean, group=bacillus.IMI.cow, color=bacillus.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=bacillus_title,color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_14_adj_overall <- lme(fixed =milk_kg~ bacillus.IMI.cow+calving.sau.sslo+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_14_adj_overall)
##                     Estimate      2.5 %     97.5 %
## (Intercept)       23.9307560 17.9908922 29.8706198
## bacillus.IMI.cow1  1.6407200  0.3045264  2.9769136
## calving.sau.sslo1 -1.5524690 -2.7334855 -0.3714526
## calving.nas1      -0.2366099 -1.4368709  0.9636510
# ---- Coryne ----

model_15 <- lme(fixed =milk_kg~ coryne.IMI.cow*postpartum_sample,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans(model_15,revpairwise~coryne.IMI.cow|postpartum_sample)
## $emmeans
## postpartum_sample = 1:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.8 3.21  3     13.6     34.0
##  1                22.1 3.34  3     11.5     32.7
## 
## postpartum_sample = 2:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                25.6 3.21  3     15.4     35.9
##  1                25.3 3.34  3     14.7     36.0
## 
## postpartum_sample = 3:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                24.5 3.21  3     14.3     34.7
##  1                25.8 3.35  3     15.2     36.5
## 
## postpartum_sample = 4:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.5 3.22  3     13.3     33.8
##  1                24.4 3.35  3     13.7     35.0
## 
## postpartum_sample = 5:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.2 3.21  3     13.0     33.4
##  1                22.7 3.35  3     12.1     33.4
## 
## postpartum_sample = 6:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                21.0 3.21  3     10.7     31.2
##  1                21.2 3.37  3     10.5     31.9
## 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -1.725 1.10 328  -1.574  0.1165
## 
## postpartum_sample = 2:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.315 1.11 328  -0.284  0.7768
## 
## postpartum_sample = 3:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    1.320 1.12 328   1.175  0.2409
## 
## postpartum_sample = 4:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.832 1.15 328   0.722  0.4706
## 
## postpartum_sample = 5:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.468 1.15 328  -0.407  0.6839
## 
## postpartum_sample = 6:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.229 1.20 328   0.191  0.8483
## 
## Degrees-of-freedom method: containment
model_15 <- lme(fixed =milk_kg~ coryne.IMI.cow*postpartum_sample+calving.nas+calving.sau.sslo,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

emmeans_15 <- emmeans(model_15,revpairwise~coryne.IMI.cow|postpartum_sample)

emmeans_15
## $emmeans
## postpartum_sample = 1:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.5 3.10  3     13.6     33.3
##  1                21.8 3.24  3     11.4     32.1
## 
## postpartum_sample = 2:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                25.3 3.10  3     15.4     35.1
##  1                25.0 3.24  3     14.7     35.3
## 
## postpartum_sample = 3:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                24.1 3.10  3     14.3     34.0
##  1                25.5 3.25  3     15.1     35.8
## 
## postpartum_sample = 4:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.2 3.11  3     13.3     33.1
##  1                24.0 3.25  3     13.7     34.4
## 
## postpartum_sample = 5:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                22.8 3.11  3     12.9     32.7
##  1                22.4 3.25  3     12.0     32.7
## 
## postpartum_sample = 6:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                20.6 3.11  3     10.7     30.5
##  1                20.8 3.27  3     10.4     31.2
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -1.707 1.10 326  -1.556  0.1208
## 
## postpartum_sample = 2:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.277 1.11 326  -0.249  0.8033
## 
## postpartum_sample = 3:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    1.342 1.13 326   1.192  0.2343
## 
## postpartum_sample = 4:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.847 1.15 326   0.734  0.4636
## 
## postpartum_sample = 5:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.465 1.15 326  -0.403  0.6869
## 
## postpartum_sample = 6:
##  contrast                          estimate   SE  df t.ratio p.value
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.242 1.20 326   0.202  0.8403
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment
confint(emmeans_15)
## $emmeans
## postpartum_sample = 1:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.5 3.10  3     13.6     33.3
##  1                21.8 3.24  3     11.4     32.1
## 
## postpartum_sample = 2:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                25.3 3.10  3     15.4     35.1
##  1                25.0 3.24  3     14.7     35.3
## 
## postpartum_sample = 3:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                24.1 3.10  3     14.3     34.0
##  1                25.5 3.25  3     15.1     35.8
## 
## postpartum_sample = 4:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                23.2 3.11  3     13.3     33.1
##  1                24.0 3.25  3     13.7     34.4
## 
## postpartum_sample = 5:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                22.8 3.11  3     12.9     32.7
##  1                22.4 3.25  3     12.0     32.7
## 
## postpartum_sample = 6:
##  coryne.IMI.cow emmean   SE df lower.CL upper.CL
##  0                20.6 3.11  3     10.7     30.5
##  1                20.8 3.27  3     10.4     31.2
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95 
## 
## $contrasts
## postpartum_sample = 1:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0   -1.707 1.10 326   -3.866    0.452
## 
## postpartum_sample = 2:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.277 1.11 326   -2.462    1.909
## 
## postpartum_sample = 3:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0    1.342 1.13 326   -0.873    3.557
## 
## postpartum_sample = 4:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.847 1.15 326   -1.425    3.119
## 
## postpartum_sample = 5:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0   -0.465 1.15 326   -2.730    1.801
## 
## postpartum_sample = 6:
##  contrast                          estimate   SE  df lower.CL upper.CL
##  coryne.IMI.cow1 - coryne.IMI.cow0    0.242 1.20 326   -2.119    2.603
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
adj_means_15 <- as.data.frame(emmeans_15$emmeans)

adj_means_15
## postpartum_sample = 1:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              23.46034 3.101375  3 13.59038 33.33030
##  1              21.75314 3.240671  3 11.43988 32.06640
## 
## postpartum_sample = 2:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              25.26237 3.101661  3 15.39150 35.13324
##  1              24.98545 3.244353  3 14.66047 35.31043
## 
## postpartum_sample = 3:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              24.12634 3.103244  3 14.25043 34.00224
##  1              25.46800 3.247048  3 15.13444 35.80155
## 
## postpartum_sample = 4:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              23.16975 3.109571  3 13.27371 33.06579
##  1              24.01721 3.252772  3 13.66544 34.36898
## 
## postpartum_sample = 5:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              22.82729 3.106291  3 12.94168 32.71289
##  1              22.36266 3.253276  3 12.00929 32.71604
## 
## postpartum_sample = 6:
##  coryne.IMI.cow   emmean       SE df lower.CL upper.CL
##  0              20.58823 3.107245  3 10.69960 30.47687
##  1              20.83034 3.269769  3 10.42447 31.23620
## 
## Results are averaged over the levels of: calving.nas, calving.sau.sslo 
## Degrees-of-freedom method: containment 
## Confidence level used: 0.95
contrasts_15 <- as.data.frame(emmeans_15$contrasts) %>% select (postpartum_sample,p.value)


adj_means_15 <- adj_means_15 %>% left_join(contrasts_15, by="postpartum_sample")

adj_means_15 <- adj_means_15 %>% group_by(postpartum_sample) %>% mutate(max_milk = max(upper.CL)) %>% ungroup()

adj_means_15 <- adj_means_15 %>% mutate(p.value = ifelse(max_milk == upper.CL, p.value, 1))

bacillus_title <- expression(paste(italic("Bacillus "),"spp."))


coryne_title <- expression(paste(italic("Corynebacterium "),"spp."))


milk_15 <- ggplot(adj_means_15, aes(x=postpartum_sample, y=emmean, group=coryne.IMI.cow, color=coryne.IMI.cow)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=emmean-SE, ymax=emmean+SE), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,32) +
  labs(x="Monthly postpartum test",y="Milk production (Kg/day)",title=coryne_title,color="IMI") +
  theme_classic() +
  scale_color_viridis(discrete = TRUE) +
  scale_color_manual(labels = c("No", "Yes"),
                     values = c("#009999", "#990000")) +
  geom_text(aes(label = ifelse(p.value < 0.05, "*", "")),
            vjust = -3, size = 5, color = "black")
## Scale for colour is already present.
## Adding another scale for colour, which will replace the existing scale.
model_15_adj_overall <- lme(fixed =milk_kg~ coryne.IMI.cow+calving.sau.sslo+calving.nas,random=~1|Site/CowId,correlation= corAR1(form = ~ps_sample),data=df.per.cow_dhi,na.action = na.omit)

Confint(model_15_adj_overall)
##                     Estimate     2.5 %     97.5 %
## (Intercept)       24.4096492 18.359195 30.4601035
## coryne.IMI.cow1   -0.2210869 -1.817931  1.3757573
## calving.sau.sslo1 -1.7228949 -2.904027 -0.5417626
## calving.nas1      -0.4562007 -1.666231  0.7538293
library(ggpubr)

staph_figure <- ggarrange( milk_1 + rremove("ylab") + rremove("xlab"), 
    milk_2 + rremove("ylab") + rremove("xlab"), 
                                milk_3 + rremove("ylab") + rremove("xlab"),
                                milk_4 + rremove("ylab") + rremove("xlab"), 
                                milk_5 + rremove("ylab") + rremove("xlab"), 
                                ncol = 2, nrow = 3, common.legend = TRUE,align = "h",labels = c("A", "B", "C","D","E","F","G")) 


staph_figure_annotated <- annotate_figure(staph_figure,
                          bottom = text_grob("Monthly postpartum test", color = "black",
                                             hjust = 0.75, x = 0.5),
                          left = text_grob("Milk production (Kg/day)", color = "black", rot = 90))


ggsave(plot = last_plot(),"./figures_article_2/staph_spp.png",width = 20, height = 30, units = "cm") 


sslo_figure <- ggarrange(milk_6 + rremove("ylab") + rremove("xlab"), 
                         milk_7 + rremove("ylab") + rremove("xlab"), 
                         milk_8 + rremove("ylab") + rremove("xlab"), 
                         milk_9 + rremove("ylab") + rremove("xlab"),
                         milk_10 + rremove("ylab") + rremove("xlab"), 
                         milk_11 + rremove("ylab") + rremove("xlab"), 
                          ncol = 2, nrow = 3, common.legend = TRUE,align = "h",labels = c("A", "B", "C","D","E","F","G","H","I","J","K","L"))


sslo_figure_annotated <- annotate_figure(sslo_figure,
                                          bottom = text_grob("Monthly postpartum test", color = "black",
                                                             hjust = 0.75, x = 0.5),
                                          left = text_grob("Milk production (Kg/day)", color = "black", rot = 90))


sslo_figure_annotated

ggsave(plot = last_plot(),"./figures_article_2/sslo.png",width = 20, height = 30, units = "cm") 



gn_others_figure <- ggarrange(milk_12 + rremove("ylab") + rremove("xlab"), 
                         milk_13 + rremove("ylab") + rremove("xlab"), 
                         milk_14 + rremove("ylab") + rremove("xlab"),
                         milk_15 + rremove("ylab") + rremove("xlab"),
                         ncol = 2, nrow = 2, common.legend = TRUE,align = "h",labels = c("A", "B", "C","D","E","F","G","H","I","J","K","L"))


gn_others_figure_annotated <- annotate_figure(gn_others_figure,
                                         bottom = text_grob("Postpartum test", color = "black",
                                                            hjust = 0.75, x = 0.5),
                                         left = text_grob("Milk production (Kg/day)", color = "black", rot = 90))

gn_others_figure_annotated

ggsave(plot = last_plot(),"./figures_article_2/others_gn.png",width = 20, height = 20, units = "cm") 

2.3 CA: Number of DHIA tests with high SCC

# ---- Staph aureus ----




#theme_set(theme(axis.text.x=element_blank(),theme_classic))


nhscc_1 <- glmer.nb(n_high_scc~as.factor(staph.aureus.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.9256)  ( log )
## Formula: n_high_scc ~ as.factor(staph.aureus.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.4   1033.5   -502.2   1004.4      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0820 -0.6943 -0.4837  0.5208  6.4994 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2725   0.522   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                        -0.07368    0.27420  -0.269    0.788    
## as.factor(staph.aureus.IMI.cow.p)1  0.52493    0.31970   1.642    0.101    
## as.factor(staph.aureus.IMI.cow.p)2  0.99756    0.14466   6.896 5.35e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1
## a.(..IMI..)1 -0.070             
## a.(..IMI..)2 -0.149  0.183
emmeans(nhscc_1,revpairwise~staph.aureus.IMI.cow.p,type="response")
## $emmeans
##  staph.aureus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                         0.929 0.255 Inf     0.543      1.59
##  1                         1.570 0.638 Inf     0.708      3.48
##  2                         2.519 0.731 Inf     1.426      4.45
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                          ratio    SE  df null z.ratio
##  staph.aureus.IMI.cow.p1 / staph.aureus.IMI.cow.p0  1.69 0.540 Inf    1   1.642
##  staph.aureus.IMI.cow.p2 / staph.aureus.IMI.cow.p0  2.71 0.392 Inf    1   6.896
##  staph.aureus.IMI.cow.p2 / staph.aureus.IMI.cow.p1  1.60 0.523 Inf    1   1.451
##  p.value
##   0.2280
##   <.0001
##   0.3151
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_1_adj <- glmer.nb(n_high_scc~as.factor(staph.aureus.IMI.cow.p)+calving.nas+calving.sslo+(1|Site),data=df_dhi_cow, 
                 family=poisson(link="log")) 

summary(nhscc_1_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.3133)  ( log )
## Formula: n_high_scc ~ as.factor(staph.aureus.IMI.cow.p) + calving.nas +  
##     calving.sslo + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1000.1   1026.7   -493.0    986.1      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1731 -0.7209 -0.5672  0.4965  6.1943 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1612   0.4015  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                         -0.3397     0.2294  -1.481   0.1387    
## as.factor(staph.aureus.IMI.cow.p)1   0.4196     0.3172   1.323   0.1860    
## as.factor(staph.aureus.IMI.cow.p)2   1.1263     0.1456   7.737 1.02e-14 ***
## calving.nas1                         0.2987     0.1466   2.037   0.0416 *  
## calving.sslo1                        0.6322     0.1507   4.194 2.74e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.n1
## a.(..IMI..)1 -0.099                                   
## a.(..IMI..)2 -0.265  0.204                            
## calving.ns1  -0.277  0.181        0.268               
## calvng.ssl1  -0.228 -0.134        0.182        0.226
exp(Confint(nhscc_1_adj))
##                                     Estimate     2.5 %   97.5 %
## (Intercept)                        0.7120179 0.4542028 1.116174
## as.factor(staph.aureus.IMI.cow.p)1 1.5213525 0.8169388 2.833154
## as.factor(staph.aureus.IMI.cow.p)2 3.0841480 2.3185792 4.102499
## calving.nas1                       1.3480740 1.0113662 1.796880
## calving.sslo1                      1.8816910 1.4004044 2.528385
emmeans_p1 <- emmeans(nhscc_1_adj,revpairwise~staph.aureus.IMI.cow.p,type="response")
performance::icc(nhscc_1_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.166
##   Unadjusted ICC: 0.131
emmeans_p1
## $emmeans
##  staph.aureus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                          1.13 0.251 Inf     0.735      1.75
##  1                          1.73 0.639 Inf     0.835      3.56
##  2                          3.50 0.872 Inf     2.145      5.70
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                          ratio    SE  df null z.ratio
##  staph.aureus.IMI.cow.p1 / staph.aureus.IMI.cow.p0  1.52 0.483 Inf    1   1.323
##  staph.aureus.IMI.cow.p2 / staph.aureus.IMI.cow.p0  3.08 0.449 Inf    1   7.737
##  staph.aureus.IMI.cow.p2 / staph.aureus.IMI.cow.p1  2.03 0.651 Inf    1   2.202
##  p.value
##   0.3825
##   <.0001
##   0.0708
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p1 <- as.data.frame(emmeans_p1$emmeans)

adj_means_p1
##  staph.aureus.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                      1.134023 0.2508948 Inf 0.7350205  1.749623
##  1                      1.725249 0.6388647 Inf 0.8349287  3.564956
##  2                      3.497495 0.8720493 Inf 2.1454703  5.701535
## 
## Results are averaged over the levels of: calving.nas, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_1 <- ggplot(adj_means_p1, aes(x=staph.aureus.IMI.cow.p, y=response, fill=staph.aureus.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.05)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="Staphylococcus aureus", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(plot.title=element_text(face="italic"),axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                     values = c("#009999", "#FF9933","#990000"))



# Add IRR and 95% CI


contrasts_p1 <- as.data.frame(exp(Confint(nhscc_1_adj)))

contrasts_p1$comparison <- row.names(contrasts_p1)

contrasts_p1 <- contrasts_p1 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p1 <- contrasts_p1 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p1_with_extra <- rbind(contrasts_p1, extra_observation) %>% arrange(comparison)


contrasts_p1_with_extra$staph.aureus.IMI.cow <- as.factor(row_number(contrasts_p1_with_extra))




nhscc_plot_1_b <- ggplot(contrasts_p1_with_extra, aes(x=staph.aureus.IMI.cow,y=Estimate,colour=staph.aureus.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# ---- nas ----

nhscc_2 <- glmer.nb(n_high_scc~as.factor(nas.IMI.cow.p)+(1|Site),data=df_dhi_cow) 

summary(nhscc_2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.1879)  ( log )
## Formula: n_high_scc ~ as.factor(nas.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1057.1   1076.1   -523.6   1047.1      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9287 -0.6468 -0.3387  0.3807  5.3829 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.4266   0.6531  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                            Estimate Std. Error z value Pr(>|z|)
## (Intercept)                0.218621   0.342840   0.638    0.524
## as.factor(nas.IMI.cow.p)1 -0.004837   0.184691  -0.026    0.979
## as.factor(nas.IMI.cow.p)2 -0.029165   0.169170  -0.172    0.863
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1
## a.(.IMI..)1 -0.158            
## a.(.IMI..)2 -0.173  0.373
emmeans(nhscc_2,revpairwise~nas.IMI.cow.p,type="response")
## $emmeans
##  nas.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                 1.24 0.427 Inf     0.636      2.44
##  1                 1.24 0.449 Inf     0.608      2.52
##  2                 1.21 0.429 Inf     0.603      2.42
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                        ratio    SE  df null z.ratio p.value
##  nas.IMI.cow.p1 / nas.IMI.cow.p0 0.995 0.184 Inf    1  -0.026  0.9996
##  nas.IMI.cow.p2 / nas.IMI.cow.p0 0.971 0.164 Inf    1  -0.172  0.9837
##  nas.IMI.cow.p2 / nas.IMI.cow.p1 0.976 0.194 Inf    1  -0.123  0.9918
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_2_adj <- glmer.nb(n_high_scc~as.factor(nas.IMI.cow.p)+calving.sau+calving.sslo+(1|Site),data=df_dhi_cow, 
                 family=poisson(link="log")) 

summary(nhscc_2_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.8672)  ( log )
## Formula: n_high_scc ~ as.factor(nas.IMI.cow.p) + calving.sau + calving.sslo +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.9   1041.6   -500.5   1000.9      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0816 -0.6823 -0.5750  0.4641  6.1270 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1725   0.4153  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                -0.2927     0.2422  -1.209 0.226837    
## as.factor(nas.IMI.cow.p)1   0.2259     0.1719   1.314 0.188834    
## as.factor(nas.IMI.cow.p)2   0.2729     0.1613   1.692 0.090614 .  
## calving.sau1                0.9870     0.1544   6.391 1.65e-10 ***
## calving.sslo1               0.5907     0.1553   3.804 0.000142 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1 a.(.IMI..)2 calvng.s1
## a.(.IMI..)1 -0.279                                  
## a.(.IMI..)2 -0.315  0.420                           
## calving.sa1 -0.242  0.142       0.227               
## calvng.ssl1 -0.247  0.192       0.221       0.155
exp(Confint(nhscc_2_adj))
##                            Estimate     2.5 %   97.5 %
## (Intercept)               0.7462164 0.4641763 1.199628
## as.factor(nas.IMI.cow.p)1 1.2534936 0.8948844 1.755809
## as.factor(nas.IMI.cow.p)2 1.3137751 0.9577304 1.802182
## calving.sau1              2.6831372 1.9823986 3.631573
## calving.sslo1             1.8052135 1.3315695 2.447334
emmeans_p2 <- emmeans(nhscc_2_adj,revpairwise~nas.IMI.cow.p,type="response")

performance::icc(nhscc_2_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.168
##   Unadjusted ICC: 0.139
emmeans_p2
## $emmeans
##  nas.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                 1.64 0.382 Inf      1.04      2.59
##  1                 2.06 0.542 Inf      1.23      3.45
##  2                 2.16 0.557 Inf      1.30      3.58
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                        ratio    SE  df null z.ratio p.value
##  nas.IMI.cow.p1 / nas.IMI.cow.p0  1.25 0.216 Inf    1   1.314  0.3873
##  nas.IMI.cow.p2 / nas.IMI.cow.p0  1.31 0.212 Inf    1   1.692  0.2082
##  nas.IMI.cow.p2 / nas.IMI.cow.p1  1.05 0.188 Inf    1   0.261  0.9630
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p2 <- as.data.frame(emmeans_p2$emmeans)

adj_means_p2
##  nas.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0             1.642292 0.3822744 Inf  1.040680  2.591693
##  1             2.058603 0.5423335 Inf  1.228360  3.450003
##  2             2.157603 0.5574379 Inf  1.300335  3.580039
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_2 <- ggplot(adj_means_p2, aes(x=nas.IMI.cow.p, y=response, fill=nas.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.05)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="NASM", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9933","#990000"))



# Add IRR and 95% CI


contrasts_p2 <- as.data.frame(exp(Confint(nhscc_2_adj)))

contrasts_p2$comparison <- row.names(contrasts_p2)

contrasts_p2 <- contrasts_p2 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p2 <- contrasts_p2 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p2_with_extra <- rbind(contrasts_p2, extra_observation) %>% arrange(comparison)


contrasts_p2_with_extra$nas.IMI.cow <- as.factor(row_number(contrasts_p2_with_extra))




nhscc_plot_2_b <- ggplot(contrasts_p2_with_extra, aes(x=nas.IMI.cow,y=Estimate,colour=nas.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))


# ---- staph chromogenes ----

nhscc_3 <- glmer.nb(n_high_scc~ sch.IMI.cow.p+(1|Site),data=df_dhi_cow)

summary(nhscc_3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.1915)  ( log )
## Formula: n_high_scc ~ sch.IMI.cow.p + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1056.9   1075.9   -523.4   1046.9      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9365 -0.6580 -0.3422  0.4194  5.1473 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.4264   0.653   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)     0.21767    0.33970   0.641    0.522
## sch.IMI.cow.p1  0.05770    0.22044   0.262    0.794
## sch.IMI.cow.p2 -0.06562    0.16914  -0.388    0.698
## 
## Correlation of Fixed Effects:
##             (Intr) s.IMI..1
## sch.IMI.c.1 -0.103         
## sch.IMI.c.2 -0.130  0.219
emmeans(nhscc_3,revpairwise~sch.IMI.cow.p,type="response")
## $emmeans
##  sch.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                 1.24 0.422 Inf     0.639      2.42
##  1                 1.32 0.508 Inf     0.619      2.80
##  2                 1.16 0.418 Inf     0.576      2.35
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                        ratio    SE  df null z.ratio p.value
##  sch.IMI.cow.p1 / sch.IMI.cow.p0 1.059 0.234 Inf    1   0.262  0.9629
##  sch.IMI.cow.p2 / sch.IMI.cow.p0 0.936 0.158 Inf    1  -0.388  0.9204
##  sch.IMI.cow.p2 / sch.IMI.cow.p1 0.884 0.218 Inf    1  -0.500  0.8715
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_3_adj <- glmer.nb(n_high_scc~ sch.IMI.cow.p+calving.sau+calving.sslo+(1|Site),data=df_dhi_cow)
summary(nhscc_3_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.8634)  ( log )
## Formula: n_high_scc ~ sch.IMI.cow.p + calving.sau + calving.sslo + (1 |  
##     Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1016.7   1043.3   -501.3   1002.7      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0807 -0.6798 -0.5298  0.4310  5.9787 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1935   0.4399  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)     -0.2160     0.2459  -0.878 0.379858    
## sch.IMI.cow.p1   0.2034     0.2009   1.013 0.311232    
## sch.IMI.cow.p2   0.1537     0.1578   0.974 0.330002    
## calving.sau1     0.9509     0.1523   6.245 4.23e-10 ***
## calving.sslo1    0.5606     0.1538   3.646 0.000266 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) s.IMI..1 s.IMI..2 calvng.s1
## sch.IMI.c.1 -0.170                            
## sch.IMI.c.2 -0.225  0.250                     
## calving.sa1 -0.201  0.063    0.161            
## calvng.ssl1 -0.210  0.137    0.178    0.133
exp(Confint(nhscc_3_adj))
##                 Estimate     2.5 %   97.5 %
## (Intercept)    0.8057629 0.4975894 1.304799
## sch.IMI.cow.p1 1.2256049 0.8267006 1.816991
## sch.IMI.cow.p2 1.1661357 0.8559456 1.588737
## calving.sau1   2.5880955 1.9203365 3.488054
## calving.sslo1  1.7518036 1.2960075 2.367900
emmeans_p3 <- emmeans(nhscc_3_adj,revpairwise~sch.IMI.cow.p,type="response")

performance::icc(nhscc_3_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.185
##   Unadjusted ICC: 0.156
emmeans_p3
## $emmeans
##  sch.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                 1.72 0.414 Inf      1.07      2.75
##  1                 2.10 0.624 Inf      1.18      3.76
##  2                 2.00 0.544 Inf      1.17      3.41
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                        ratio    SE  df null z.ratio p.value
##  sch.IMI.cow.p1 / sch.IMI.cow.p0 1.226 0.246 Inf    1   1.013  0.5688
##  sch.IMI.cow.p2 / sch.IMI.cow.p0 1.166 0.184 Inf    1   0.974  0.5933
##  sch.IMI.cow.p2 / sch.IMI.cow.p1 0.951 0.212 Inf    1  -0.224  0.9728
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p3 <- as.data.frame(emmeans_p3$emmeans)

adj_means_p3
##  sch.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0             1.715695 0.4141869 Inf  1.068935  2.753779
##  1             2.102765 0.6239178 Inf  1.175515  3.761433
##  2             2.000733 0.5437399 Inf  1.174513  3.408164
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_3 <- ggplot(adj_means_p3, aes(x=sch.IMI.cow.p, y=response, fill=sch.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.05)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="Staphylococcus chromogenes", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(plot.title=element_text(face="italic"),axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9933","#990000"))


# Add IRR and 95% CI


contrasts_p3 <- as.data.frame(exp(Confint(nhscc_3_adj)))

contrasts_p3$comparison <- row.names(contrasts_p3)

contrasts_p3 <- contrasts_p3 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p3 <- contrasts_p3 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p3_with_extra <- rbind(contrasts_p3, extra_observation) %>% arrange(comparison)


contrasts_p3_with_extra$sch.IMI.cow <- as.factor(row_number(contrasts_p3_with_extra))


nhscc_plot_3_b <- ggplot(contrasts_p3_with_extra, aes(x=sch.IMI.cow,y=Estimate,colour=sch.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))


# ---- nas non chromogenes -----

nhscc_4 <- glmer.nb(n_high_scc~as.factor(nas.non.chrom.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.1899)  ( log )
## Formula: n_high_scc ~ as.factor(nas.non.chrom.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1056.7   1075.7   -523.3   1046.7      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9446 -0.6655 -0.3770  0.3568  5.3736 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.4199   0.648   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                     Estimate Std. Error z value Pr(>|z|)
## (Intercept)                           0.2104     0.3352   0.628    0.530
## as.factor(nas.non.chrom.IMI.cow.p)1  -0.0680     0.1960  -0.347    0.729
## as.factor(nas.non.chrom.IMI.cow.p)2   0.1382     0.2645   0.522    0.601
## 
## Correlation of Fixed Effects:
##               (Intr) a.(...IMI..)1
## a.(...IMI..)1 -0.090              
## a.(...IMI..)2 -0.064  0.173
emmeans(nhscc_4,revpairwise~nas.non.chrom.IMI.cow.p,type="response")
## $emmeans
##  nas.non.chrom.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                           1.23 0.414 Inf     0.640      2.38
##  1                           1.15 0.430 Inf     0.555      2.39
##  2                           1.42 0.586 Inf     0.630      3.19
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                            ratio    SE  df null
##  nas.non.chrom.IMI.cow.p1 / nas.non.chrom.IMI.cow.p0 0.934 0.183 Inf    1
##  nas.non.chrom.IMI.cow.p2 / nas.non.chrom.IMI.cow.p0 1.148 0.304 Inf    1
##  nas.non.chrom.IMI.cow.p2 / nas.non.chrom.IMI.cow.p1 1.229 0.370 Inf    1
##  z.ratio p.value
##   -0.347  0.9358
##    0.522  0.8604
##    0.686  0.7718
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_4_adj <- glmer.nb(n_high_scc~as.factor(nas.non.chrom.IMI.cow.p)+calving.sau+calving.sslo+(1|Site),data=df_dhi_cow)

summary(nhscc_4_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.9317)  ( log )
## Formula: n_high_scc ~ as.factor(nas.non.chrom.IMI.cow.p) + calving.sau +  
##     calving.sslo + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.2   1040.9   -500.1   1000.2      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0779 -0.6893 -0.6201  0.4502  6.7601 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1706   0.413   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                          -0.2143     0.2298  -0.933 0.351061    
## as.factor(nas.non.chrom.IMI.cow.p)1   0.1533     0.1773   0.865 0.387126    
## as.factor(nas.non.chrom.IMI.cow.p)2   0.4690     0.2408   1.948 0.051416 .  
## calving.sau1                          0.9924     0.1533   6.473  9.6e-11 ***
## calving.sslo1                         0.5460     0.1505   3.629 0.000285 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##               (Intr) a.(...IMI..)1 a.(...IMI..)2 calvng.s1
## a.(...IMI..)1 -0.167                                      
## a.(...IMI..)2 -0.136  0.205                               
## calving.sa1   -0.216  0.158         0.209                 
## calvng.ssl1   -0.193  0.095         0.073         0.130
exp(Confint(nhscc_4_adj))
##                                      Estimate     2.5 %   97.5 %
## (Intercept)                         0.8071326 0.5144762 1.266265
## as.factor(nas.non.chrom.IMI.cow.p)1 1.1656933 0.8235430 1.649994
## as.factor(nas.non.chrom.IMI.cow.p)2 1.5983862 0.9971218 2.562213
## calving.sau1                        2.6976781 1.9975281 3.643236
## calving.sslo1                       1.7263792 1.2854517 2.318551
emmeans_p4 <- emmeans(nhscc_4_adj,revpairwise~nas.non.chrom.IMI.cow.p,type="response")

performance::icc(nhscc_4_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.168
##   Unadjusted ICC: 0.139
emmeans_p4
## $emmeans
##  nas.non.chrom.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                           1.74 0.396 Inf      1.12      2.72
##  1                           2.03 0.560 Inf      1.18      3.49
##  2                           2.78 0.902 Inf      1.48      5.25
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                            ratio    SE  df null
##  nas.non.chrom.IMI.cow.p1 / nas.non.chrom.IMI.cow.p0  1.17 0.207 Inf    1
##  nas.non.chrom.IMI.cow.p2 / nas.non.chrom.IMI.cow.p0  1.60 0.385 Inf    1
##  nas.non.chrom.IMI.cow.p2 / nas.non.chrom.IMI.cow.p1  1.37 0.368 Inf    1
##  z.ratio p.value
##    0.865  0.6625
##    1.948  0.1254
##    1.177  0.4667
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p4 <- as.data.frame(emmeans_p4$emmeans)

adj_means_p4
##  nas.non.chrom.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                       1.741839 0.3955068 Inf  1.116174  2.718218
##  1                       2.030450 0.5604649 Inf  1.182049  3.487783
##  2                       2.784132 0.9015661 Inf  1.475880  5.252046
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
adj_means_p4$nas.non.chrom.IMI.cow.p <- as.factor(adj_means_p4$nas.non.chrom.IMI.cow.p)

nhscc_plot_4 <- ggplot(adj_means_p4, aes(x=nas.non.chrom.IMI.cow.p, y=response, fill=nas.non.chrom.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.05)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=nas_nch_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))


# Add IRR and 95% CI


contrasts_p4 <- as.data.frame(exp(Confint(nhscc_4_adj)))

contrasts_p4$comparison <- row.names(contrasts_p4)

contrasts_p4 <- contrasts_p4 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p4 <- contrasts_p4 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p4_with_extra <- rbind(contrasts_p4, extra_observation) %>% arrange(comparison)


contrasts_p4_with_extra$nas.non.chromogenes.IMI <- as.factor(row_number(contrasts_p4_with_extra))


nhscc_plot_4_b <- ggplot(contrasts_p4_with_extra, aes(x=nas.non.chromogenes.IMI,y=Estimate,colour=nas.non.chromogenes.IMI)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))








# ---- Staph sp ----

nhscc_5 <- glmer.nb(n_high_scc~as.factor(staph.spp.IMI.cow.p)+(1|Site),data=df_dhi_cow)

summary(nhscc_5)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.1931)  ( log )
## Formula: n_high_scc ~ as.factor(staph.spp.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1056.5   1075.5   -523.2   1046.5      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9451 -0.6567 -0.3788  0.3544  5.3635 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.4202   0.6482  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                 Estimate Std. Error z value Pr(>|z|)
## (Intercept)                       0.2139     0.3349   0.639    0.523
## as.factor(staph.spp.IMI.cow.p)1  -0.1138     0.2120  -0.537    0.592
## as.factor(staph.spp.IMI.cow.p)2   0.1331     0.2632   0.506    0.613
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1
## a.(..IMI..)1 -0.078             
## a.(..IMI..)2 -0.060  0.149
emmeans(nhscc_5,revpairwise~staph.spp.IMI.cow.p,type="response")
## $emmeans
##  staph.spp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                       1.24 0.415 Inf     0.642      2.39
##  1                       1.11 0.422 Inf     0.523      2.34
##  2                       1.41 0.585 Inf     0.629      3.18
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                    ratio    SE  df null z.ratio
##  staph.spp.IMI.cow.p1 / staph.spp.IMI.cow.p0 0.892 0.189 Inf    1  -0.537
##  staph.spp.IMI.cow.p2 / staph.spp.IMI.cow.p0 1.142 0.301 Inf    1   0.506
##  staph.spp.IMI.cow.p2 / staph.spp.IMI.cow.p1 1.280 0.400 Inf    1   0.790
##  p.value
##   0.8533
##   0.8686
##   0.7091
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_5_adj <- glmer.nb(n_high_scc~as.factor(staph.spp.IMI.cow.p)+calving.sau+calving.sslo+(1|Site),data=df_dhi_cow)

summary(nhscc_5_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.9167)  ( log )
## Formula: 
## n_high_scc ~ as.factor(staph.spp.IMI.cow.p) + calving.sau + calving.sslo +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.9   1041.6   -500.5   1000.9      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0538 -0.6911 -0.6238  0.5508  6.6742 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1825   0.4272  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     -0.18625    0.23501  -0.793 0.428038    
## as.factor(staph.spp.IMI.cow.p)1  0.02929    0.19132   0.153 0.878319    
## as.factor(staph.spp.IMI.cow.p)2  0.43307    0.23947   1.808 0.070537 .  
## calving.sau1                     0.97479    0.15254   6.390 1.66e-10 ***
## calving.sslo1                    0.53515    0.15025   3.562 0.000368 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 calvng.s1
## a.(..IMI..)1 -0.128                                    
## a.(..IMI..)2 -0.121  0.168                             
## calving.sa1  -0.200  0.106        0.196                
## calvng.ssl1  -0.179  0.037        0.060        0.120
exp(Confint(nhscc_5_adj))
##                                  Estimate     2.5 %   97.5 %
## (Intercept)                     0.8300619 0.5236884 1.315673
## as.factor(staph.spp.IMI.cow.p)1 1.0297248 0.7077284 1.498220
## as.factor(staph.spp.IMI.cow.p)2 1.5419837 0.9643663 2.465571
## calving.sau1                    2.6506048 1.9656300 3.574277
## calving.sslo1                   1.7077068 1.2720978 2.292483
emmeans_p5 <- emmeans(nhscc_5_adj,revpairwise~staph.spp.IMI.cow.p,type="response")

performance::icc(nhscc_5_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.177
##   Unadjusted ICC: 0.149
emmeans_p5
## $emmeans
##  staph.spp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                       1.77 0.413 Inf      1.12      2.79
##  1                       1.82 0.526 Inf      1.03      3.21
##  2                       2.72 0.893 Inf      1.43      5.18
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                    ratio    SE  df null z.ratio
##  staph.spp.IMI.cow.p1 / staph.spp.IMI.cow.p0  1.03 0.197 Inf    1   0.153
##  staph.spp.IMI.cow.p2 / staph.spp.IMI.cow.p0  1.54 0.369 Inf    1   1.808
##  staph.spp.IMI.cow.p2 / staph.spp.IMI.cow.p1  1.50 0.420 Inf    1   1.441
##  p.value
##   0.9872
##   0.1668
##   0.3200
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p5 <- as.data.frame(emmeans_p5$emmeans)

adj_means_p5
##  staph.spp.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                   1.765996 0.4126282 Inf  1.117133  2.791737
##  1                   1.818490 0.5264436 Inf  1.031080  3.207225
##  2                   2.723137 0.8932602 Inf  1.431703  5.179476
## 
## Results are averaged over the levels of: calving.sau, calving.sslo 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_5 <- ggplot(adj_means_p5, aes(x=staph.spp.IMI.cow.p, y=response, fill=staph.spp.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.05)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=staph_sp_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))





# Add IRR and 95% CI


contrasts_p5 <- as.data.frame(exp(Confint(nhscc_5_adj)))

contrasts_p5$comparison <- row.names(contrasts_p5)

contrasts_p5 <- contrasts_p5 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p5 <- contrasts_p5 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p5_with_extra <- rbind(contrasts_p5, extra_observation) %>% arrange(comparison)


contrasts_p5_with_extra$staph.sp.IMI.cow <- as.factor(row_number(contrasts_p5_with_extra))


nhscc_plot_5_b <- ggplot(contrasts_p5_with_extra, aes(x=staph.sp.IMI.cow,y=Estimate,colour=staph.sp.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))



# ---- sslo ----

nhscc_6 <- glmer.nb(n_high_scc~as.factor(strep.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_6)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.3409)  ( log )
## Formula: n_high_scc ~ as.factor(strep.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1043.9   1063.0   -517.0   1033.9      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0262 -0.6844 -0.5306  0.4239  6.1268 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.3353   0.579   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  0.04808    0.30493   0.158 0.874710    
## as.factor(strep.IMI.cow.p)1  0.24825    0.16976   1.462 0.143644    
## as.factor(strep.IMI.cow.p)2  0.73632    0.20440   3.602 0.000315 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1
## a.(.IMI..)1 -0.169            
## a.(.IMI..)2 -0.125  0.283
emmeans(nhscc_6,revpairwise~strep.IMI.cow.p,type="response")
## $emmeans
##  strep.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                   1.05 0.320 Inf     0.577      1.91
##  1                   1.34 0.434 Inf     0.714      2.53
##  2                   2.19 0.756 Inf     1.114      4.31
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                            ratio    SE  df null z.ratio p.value
##  strep.IMI.cow.p1 / strep.IMI.cow.p0  1.28 0.218 Inf    1   1.462  0.3092
##  strep.IMI.cow.p2 / strep.IMI.cow.p0  2.09 0.427 Inf    1   3.602  0.0009
##  strep.IMI.cow.p2 / strep.IMI.cow.p1  1.63 0.368 Inf    1   2.162  0.0778
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_6_adj <- glmer.nb(n_high_scc~as.factor(strep.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_6_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.1099)  ( log )
## Formula: n_high_scc ~ as.factor(strep.IMI.cow.p) + calving.sau + calving.nas +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1007.1   1033.7   -496.5    993.1      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1755 -0.7012 -0.5475  0.4410  5.8608 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1494   0.3866  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -0.2706     0.2244  -1.206   0.2279    
## as.factor(strep.IMI.cow.p)1   0.1768     0.1590   1.112   0.2662    
## as.factor(strep.IMI.cow.p)2   0.8848     0.1859   4.759 1.94e-06 ***
## calving.sau1                  1.0090     0.1517   6.652 2.89e-11 ***
## calving.nas1                  0.2416     0.1463   1.652   0.0985 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1 a.(.IMI..)2 clvng.s1
## a.(.IMI..)1 -0.236                                 
## a.(.IMI..)2 -0.227  0.304                          
## calving.sa1 -0.220 -0.057       0.156              
## calving.ns1 -0.277  0.162       0.179       0.235
exp(Confint(nhscc_6_adj))
##                              Estimate     2.5 %   97.5 %
## (Intercept)                 0.7629539 0.4914839 1.184370
## as.factor(strep.IMI.cow.p)1 1.1934012 0.8738071 1.629886
## as.factor(strep.IMI.cow.p)2 2.4224277 1.6826927 3.487361
## calving.sau1                2.7429386 2.0375202 3.692583
## calving.nas1                1.2733438 0.9559540 1.696111
emmeans_p6 <- emmeans(nhscc_6_adj,revpairwise~strep.IMI.cow.p,type="response")

performance::icc(nhscc_6_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.153
##   Unadjusted ICC: 0.123
emmeans_p6
## $emmeans
##  strep.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                   1.43 0.311 Inf      0.93      2.19
##  1                   1.70 0.411 Inf      1.06      2.73
##  2                   3.45 0.929 Inf      2.04      5.85
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                            ratio    SE  df null z.ratio p.value
##  strep.IMI.cow.p1 / strep.IMI.cow.p0  1.19 0.190 Inf    1   1.112  0.5068
##  strep.IMI.cow.p2 / strep.IMI.cow.p0  2.42 0.450 Inf    1   4.759  <.0001
##  strep.IMI.cow.p2 / strep.IMI.cow.p1  2.03 0.415 Inf    1   3.460  0.0016
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p6 <- as.data.frame(emmeans_p6$emmeans)

adj_means_p6
##  strep.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0               1.425868 0.3107043 Inf 0.9302459  2.185549
##  1               1.701632 0.4109926 Inf 1.0599278  2.731839
##  2               3.454061 0.9293949 Inf 2.0384245  5.852824
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_6 <- ggplot(adj_means_p6, aes(x=strep.IMI.cow.p, y=response, fill=strep.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="SSLO", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))



# Add IRR and 95% CI


contrasts_p6 <- as.data.frame(exp(Confint(nhscc_6_adj)))

contrasts_p6$comparison <- row.names(contrasts_p6)

contrasts_p6 <- contrasts_p6 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p6 <- contrasts_p6 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p6_with_extra <- rbind(contrasts_p6, extra_observation) %>% arrange(comparison)


contrasts_p6_with_extra$strep.IMI.cow <- as.factor(row_number(contrasts_p6_with_extra))


nhscc_plot_6_b <- ggplot(contrasts_p6_with_extra, aes(x=strep.IMI.cow,y=Estimate,colour=strep.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))







# ---- Strep genus ----

nhscc_7 <- glmer.nb(n_high_scc~as.factor(strep.genus.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_7)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.3406)  ( log )
## Formula: n_high_scc ~ as.factor(strep.genus.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1045.9   1064.9   -517.9   1035.9      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0187 -0.6900 -0.5313  0.3946  5.6287 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2977   0.5456  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                   Estimate Std. Error z value Pr(>|z|)   
## (Intercept)                        0.08445    0.28583   0.295  0.76764   
## as.factor(strep.genus.IMI.cow.p)1  0.40528    0.20928   1.937  0.05280 . 
## as.factor(strep.genus.IMI.cow.p)2  0.66899    0.21287   3.143  0.00167 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1
## a.(..IMI..)1 -0.104             
## a.(..IMI..)2 -0.101  0.234
emmeans(nhscc_7,revpairwise~strep.genus.IMI.cow.p,type="response")
## $emmeans
##  strep.genus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                         1.09 0.311 Inf     0.621      1.91
##  1                         1.63 0.549 Inf     0.844      3.15
##  2                         2.12 0.719 Inf     1.094      4.13
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                        ratio    SE  df null z.ratio
##  strep.genus.IMI.cow.p1 / strep.genus.IMI.cow.p0  1.50 0.314 Inf    1   1.937
##  strep.genus.IMI.cow.p2 / strep.genus.IMI.cow.p0  1.95 0.416 Inf    1   3.143
##  strep.genus.IMI.cow.p2 / strep.genus.IMI.cow.p1  1.30 0.340 Inf    1   1.009
##  p.value
##   0.1285
##   0.0048
##   0.5709
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_7_adj <- glmer.nb(n_high_scc~as.factor(strep.genus.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_7_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.0238)  ( log )
## Formula: n_high_scc ~ as.factor(strep.genus.IMI.cow.p) + calving.sau +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1012.7   1039.3   -499.3    998.7      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1505 -0.7235 -0.4997  0.4101  5.4245 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1384   0.372   
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                        -0.2197     0.2136  -1.029    0.304    
## as.factor(strep.genus.IMI.cow.p)1   0.2451     0.1970   1.244    0.213    
## as.factor(strep.genus.IMI.cow.p)2   0.8019     0.1952   4.108 4.00e-05 ***
## calving.sau1                        0.9724     0.1539   6.317 2.66e-10 ***
## calving.nas1                        0.2400     0.1472   1.630    0.103    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.138                                   
## a.(..IMI..)2 -0.192  0.249                            
## calving.sa1  -0.223 -0.130        0.133               
## calving.ns1  -0.275  0.146        0.186        0.227
exp(Confint(nhscc_7_adj))
##                                    Estimate     2.5 %   97.5 %
## (Intercept)                       0.8027626 0.5282110 1.220020
## as.factor(strep.genus.IMI.cow.p)1 1.2777709 0.8685330 1.879835
## as.factor(strep.genus.IMI.cow.p)2 2.2298341 1.5208605 3.269307
## calving.sau1                      2.6441797 1.9555850 3.575240
## calving.nas1                      1.2712131 0.9526318 1.696335
emmeans_p7 <- emmeans(nhscc_7_adj,revpairwise~strep.genus.IMI.cow.p,type="response")

performance::icc(nhscc_7_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.142
##   Unadjusted ICC: 0.116
emmeans_p7
## $emmeans
##  strep.genus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                         1.47 0.307 Inf     0.978      2.22
##  1                         1.88 0.501 Inf     1.115      3.17
##  2                         3.28 0.899 Inf     1.919      5.61
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                        ratio    SE  df null z.ratio
##  strep.genus.IMI.cow.p1 / strep.genus.IMI.cow.p0  1.28 0.252 Inf    1   1.244
##  strep.genus.IMI.cow.p2 / strep.genus.IMI.cow.p0  2.23 0.435 Inf    1   4.108
##  strep.genus.IMI.cow.p2 / strep.genus.IMI.cow.p1  1.75 0.420 Inf    1   2.316
##  p.value
##   0.4270
##   0.0001
##   0.0536
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p7 <- as.data.frame(emmeans_p7$emmeans)

adj_means_p7
##  strep.genus.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                     1.471776 0.3072060 Inf 0.9776182  2.215717
##  1                     1.880593 0.5011662 Inf 1.1154624  3.170550
##  2                     3.281817 0.8987324 Inf 1.9187174  5.613292
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
adj_means_p7$strep.genus.IMI.cow.p <- as.factor(adj_means_p7$strep.genus.IMI.cow.p)

nhscc_plot_7 <- ggplot(adj_means_p7, aes(x=strep.genus.IMI.cow.p, y=response, fill=strep.genus.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=strep_genus_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))


# Add IRR and 95% CI


contrasts_p7 <- as.data.frame(exp(Confint(nhscc_7_adj)))

contrasts_p7$comparison <- row.names(contrasts_p7)

contrasts_p7 <- contrasts_p7 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p7 <- contrasts_p7 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p7_with_extra <- rbind(contrasts_p7, extra_observation) %>% arrange(comparison)


contrasts_p7_with_extra$strep.genus.IMI.cow <- as.factor(row_number(contrasts_p7_with_extra))


nhscc_plot_7_b <- ggplot(contrasts_p7_with_extra, aes(x=strep.genus.IMI.cow,y=Estimate,colour=strep.genus.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))






# ---- Strep dys -----

nhscc_8 <- glmer.nb(n_high_scc~as.factor(strep.dys.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_8)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.0014)  ( log )
## Formula: 
## n_high_scc ~ as.factor(strep.dys.IMI.cow.p) + calving.sau + calving.nas +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.5   1041.2   -500.3   1000.5      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1090 -0.6891 -0.4627  0.4176  5.3431 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1626   0.4032  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      -0.1946     0.2262  -0.860  0.38952    
## as.factor(strep.dys.IMI.cow.p)1   0.2566     0.2370   1.083  0.27894    
## as.factor(strep.dys.IMI.cow.p)2   0.8397     0.2194   3.827  0.00013 ***
## calving.sau1                      0.9944     0.1536   6.473 9.61e-11 ***
## calving.nas1                      0.2516     0.1477   1.703  0.08855 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.083                                   
## a.(..IMI..)2 -0.160  0.148                            
## calving.sa1  -0.227 -0.055        0.174               
## calving.ns1  -0.255  0.087        0.212        0.258
emmeans(nhscc_8,revpairwise~strep.dys.IMI.cow.p,type="response")
## $emmeans
##  strep.dys.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                       1.53 0.340 Inf     0.994      2.37
##  1                       1.98 0.619 Inf     1.076      3.66
##  2                       3.55 1.089 Inf     1.949      6.48
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                    ratio    SE  df null z.ratio
##  strep.dys.IMI.cow.p1 / strep.dys.IMI.cow.p0  1.29 0.306 Inf    1   1.083
##  strep.dys.IMI.cow.p2 / strep.dys.IMI.cow.p0  2.32 0.508 Inf    1   3.827
##  strep.dys.IMI.cow.p2 / strep.dys.IMI.cow.p1  1.79 0.534 Inf    1   1.956
##  p.value
##   0.5248
##   0.0004
##   0.1234
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_8_adj <- glmer.nb(n_high_scc~as.factor(strep.dys.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_8_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.0014)  ( log )
## Formula: 
## n_high_scc ~ as.factor(strep.dys.IMI.cow.p) + calving.sau + calving.nas +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1014.5   1041.2   -500.3   1000.5      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1090 -0.6891 -0.4627  0.4176  5.3431 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1626   0.4032  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      -0.1946     0.2262  -0.860  0.38952    
## as.factor(strep.dys.IMI.cow.p)1   0.2566     0.2370   1.083  0.27894    
## as.factor(strep.dys.IMI.cow.p)2   0.8397     0.2194   3.827  0.00013 ***
## calving.sau1                      0.9944     0.1536   6.473 9.61e-11 ***
## calving.nas1                      0.2516     0.1477   1.703  0.08855 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.083                                   
## a.(..IMI..)2 -0.160  0.148                            
## calving.sa1  -0.227 -0.055        0.174               
## calving.ns1  -0.255  0.087        0.212        0.258
exp(Confint(nhscc_8_adj))
##                                  Estimate     2.5 %   97.5 %
## (Intercept)                     0.8231469 0.5283906 1.282329
## as.factor(strep.dys.IMI.cow.p)1 1.2925700 0.8122575 2.056906
## as.factor(strep.dys.IMI.cow.p)2 2.3155924 1.5063371 3.559607
## calving.sau1                    2.7030657 2.0002996 3.652735
## calving.nas1                    1.2860300 0.9627723 1.717824
emmeans_p8 <- emmeans(nhscc_8_adj,revpairwise~strep.dys.IMI.cow.p,type="response")

performance::icc(nhscc_8_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.163
##   Unadjusted ICC: 0.135
emmeans_p8
## $emmeans
##  strep.dys.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                       1.53 0.340 Inf     0.994      2.37
##  1                       1.98 0.619 Inf     1.076      3.66
##  2                       3.55 1.089 Inf     1.949      6.48
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                    ratio    SE  df null z.ratio
##  strep.dys.IMI.cow.p1 / strep.dys.IMI.cow.p0  1.29 0.306 Inf    1   1.083
##  strep.dys.IMI.cow.p2 / strep.dys.IMI.cow.p0  2.32 0.508 Inf    1   3.827
##  strep.dys.IMI.cow.p2 / strep.dys.IMI.cow.p1  1.79 0.534 Inf    1   1.956
##  p.value
##   0.5248
##   0.0004
##   0.1234
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p8 <- as.data.frame(emmeans_p8$emmeans)

adj_means_p8
##  strep.dys.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                   1.534727 0.3398207 Inf 0.9943913  2.368673
##  1                   1.983742 0.6188508 Inf 1.0763247  3.656177
##  2                   3.553803 1.0893428 Inf 1.9488370  6.480538
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_8 <- ggplot(adj_means_p8, aes(x=strep.dys.IMI.cow.p, y=response, fill=strep.dys.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="Streptococcus dysgalactiae", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(plot.title=element_text(face="italic"),axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))




contrasts_p8 <- as.data.frame(exp(Confint(nhscc_8_adj)))

contrasts_p8$comparison <- row.names(contrasts_p8)

contrasts_p8 <- contrasts_p8 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p8 <- contrasts_p8 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p8_with_extra <- rbind(contrasts_p8, extra_observation) %>% arrange(comparison)


contrasts_p8_with_extra$strep.dys.IMI.cow <- as.factor(row_number(contrasts_p8_with_extra))


nhscc_plot_8_b <- ggplot(contrasts_p8_with_extra, aes(x=strep.dys.IMI.cow,y=Estimate,colour=strep.dys.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))








# ---- Strep sp ---- 


nhscc_9 <- glmer.nb(n_high_scc~as.factor(strep.sp.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)
summary(nhscc_9)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7424)  ( log )
## Formula: 
## n_high_scc ~ as.factor(strep.sp.IMI.cow.p) + calving.sau + calving.nas +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1025.4   1052.1   -505.7   1011.4      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0817 -0.6890 -0.4988  0.3713  5.6669 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2259   0.4753  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                    -0.09497    0.25780  -0.368    0.713    
## as.factor(strep.sp.IMI.cow.p)1  0.29442    0.23997   1.227    0.220    
## as.factor(strep.sp.IMI.cow.p)2  0.54983    0.34174   1.609    0.108    
## calving.sau1                    0.92939    0.15586   5.963 2.48e-09 ***
## calving.nas1                    0.13986    0.14992   0.933    0.351    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.092                                   
## a.(..IMI..)2 -0.042  0.065                            
## calving.sa1  -0.187  0.027        0.060               
## calving.ns1  -0.205  0.125       -0.053        0.227
emmeans(nhscc_9,revpairwise~strep.sp.IMI.cow.p,type="response")
## $emmeans
##  strep.sp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                      1.55 0.397 Inf      0.94      2.56
##  1                      2.08 0.713 Inf      1.07      4.07
##  2                      2.69 1.126 Inf      1.18      6.11
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                  ratio    SE  df null z.ratio p.value
##  strep.sp.IMI.cow.p1 / strep.sp.IMI.cow.p0  1.34 0.322 Inf    1   1.227  0.4372
##  strep.sp.IMI.cow.p2 / strep.sp.IMI.cow.p0  1.73 0.592 Inf    1   1.609  0.2418
##  strep.sp.IMI.cow.p2 / strep.sp.IMI.cow.p1  1.29 0.522 Inf    1   0.631  0.8029
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_9_adj <- glmer.nb(n_high_scc~as.factor(strep.sp.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_9_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7424)  ( log )
## Formula: 
## n_high_scc ~ as.factor(strep.sp.IMI.cow.p) + calving.sau + calving.nas +  
##     (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1025.4   1052.1   -505.7   1011.4      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0817 -0.6890 -0.4988  0.3713  5.6669 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2259   0.4753  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                    -0.09497    0.25780  -0.368    0.713    
## as.factor(strep.sp.IMI.cow.p)1  0.29442    0.23997   1.227    0.220    
## as.factor(strep.sp.IMI.cow.p)2  0.54983    0.34174   1.609    0.108    
## calving.sau1                    0.92939    0.15586   5.963 2.48e-09 ***
## calving.nas1                    0.13986    0.14992   0.933    0.351    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.092                                   
## a.(..IMI..)2 -0.042  0.065                            
## calving.sa1  -0.187  0.027        0.060               
## calving.ns1  -0.205  0.125       -0.053        0.227
exp(Confint(nhscc_9_adj))
##                                 Estimate     2.5 %   97.5 %
## (Intercept)                    0.9094011 0.5486801 1.507272
## as.factor(strep.sp.IMI.cow.p)1 1.3423495 0.8386923 2.148466
## as.factor(strep.sp.IMI.cow.p)2 1.7329525 0.8869519 3.385893
## calving.sau1                   2.5329572 1.8661945 3.437944
## calving.nas1                   1.1501112 0.8572821 1.542964
emmeans_p9 <- emmeans(nhscc_9_adj,revpairwise~strep.sp.IMI.cow.p,type="response")

performance::icc(nhscc_9_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.206
##   Unadjusted ICC: 0.182
emmeans_p9
## $emmeans
##  strep.sp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                      1.55 0.397 Inf      0.94      2.56
##  1                      2.08 0.713 Inf      1.07      4.07
##  2                      2.69 1.126 Inf      1.18      6.11
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                  ratio    SE  df null z.ratio p.value
##  strep.sp.IMI.cow.p1 / strep.sp.IMI.cow.p0  1.34 0.322 Inf    1   1.227  0.4372
##  strep.sp.IMI.cow.p2 / strep.sp.IMI.cow.p0  1.73 0.592 Inf    1   1.609  0.2418
##  strep.sp.IMI.cow.p2 / strep.sp.IMI.cow.p1  1.29 0.522 Inf    1   0.631  0.8029
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p9 <- as.data.frame(emmeans_p9$emmeans)

adj_means_p9
##  strep.sp.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                  1.552170 0.3968246 Inf 0.9404222  2.561862
##  1                  2.083555 0.7127592 Inf 1.0656608  4.073716
##  2                  2.689837 1.1257382 Inf 1.1843633  6.108955
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_9 <- ggplot(adj_means_p9, aes(x=strep.sp.IMI.cow.p, y=response, fill=strep.sp.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=strep_sp_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))





# IRR and 95% CI

contrasts_p9 <- as.data.frame(exp(Confint(nhscc_9_adj)))

contrasts_p9$comparison <- row.names(contrasts_p9)

contrasts_p9 <- contrasts_p9 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p9 <- contrasts_p9 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p9_with_extra <- rbind(contrasts_p9, extra_observation) %>% arrange(comparison)


contrasts_p9_with_extra$strep.sp.IMI.cow <- as.factor(row_number(contrasts_p9_with_extra))


nhscc_plot_9_b <- ggplot(contrasts_p9_with_extra, aes(x=strep.sp.IMI.cow,y=Estimate,colour=strep.sp.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))



# ---- Strep like -----

nhscc_10 <- glmer.nb(n_high_scc~as.factor(strep.like.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)
summary(nhscc_10)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.6721)  ( log )
## Formula: n_high_scc ~ as.factor(strep.like.IMI.cow.p) + calving.sau +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1028.2   1054.9   -507.1   1014.2      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0498 -0.6862 -0.5328  0.4150  5.5629 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.25     0.5     
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      -0.05781    0.27094  -0.213    0.831    
## as.factor(strep.like.IMI.cow.p)1 -0.05842    0.21844  -0.267    0.789    
## as.factor(strep.like.IMI.cow.p)2  0.41573    0.43166   0.963    0.336    
## calving.sau1                      0.92630    0.15795   5.865  4.5e-09 ***
## calving.nas1                      0.13653    0.14990   0.911    0.362    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.128                                   
## a.(..IMI..)2 -0.070  0.108                            
## calving.sa1  -0.185  0.042        0.091               
## calving.ns1  -0.195  0.029        0.036        0.233
emmeans(nhscc_10,revpairwise~strep.like.IMI.cow.p,type="response")
## $emmeans
##  strep.like.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                        1.61 0.431 Inf     0.948      2.72
##  1                        1.51 0.496 Inf     0.797      2.88
##  2                        2.43 1.219 Inf     0.912      6.49
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                      ratio    SE  df null z.ratio
##  strep.like.IMI.cow.p1 / strep.like.IMI.cow.p0 0.943 0.206 Inf    1  -0.267
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p0 1.515 0.654 Inf    1   0.963
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p1 1.607 0.743 Inf    1   1.026
##  p.value
##   0.9613
##   0.6003
##   0.5605
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_10_adj <- glmer.nb(n_high_scc~as.factor(strep.like.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_10_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.6721)  ( log )
## Formula: n_high_scc ~ as.factor(strep.like.IMI.cow.p) + calving.sau +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1028.2   1054.9   -507.1   1014.2      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0498 -0.6862 -0.5328  0.4150  5.5629 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.25     0.5     
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                      -0.05781    0.27094  -0.213    0.831    
## as.factor(strep.like.IMI.cow.p)1 -0.05842    0.21844  -0.267    0.789    
## as.factor(strep.like.IMI.cow.p)2  0.41573    0.43166   0.963    0.336    
## calving.sau1                      0.92630    0.15795   5.865  4.5e-09 ***
## calving.nas1                      0.13653    0.14990   0.911    0.362    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.128                                   
## a.(..IMI..)2 -0.070  0.108                            
## calving.sa1  -0.185  0.042        0.091               
## calving.ns1  -0.195  0.029        0.036        0.233
exp(Confint(nhscc_10_adj))
##                                   Estimate     2.5 %   97.5 %
## (Intercept)                      0.9438254 0.5549652 1.605157
## as.factor(strep.like.IMI.cow.p)1 0.9432519 0.6147397 1.447318
## as.factor(strep.like.IMI.cow.p)2 1.5154751 0.6503038 3.531680
## calving.sau1                     2.5251417 1.8528436 3.441381
## calving.nas1                     1.1462949 0.8544859 1.537757
emmeans(nhscc_10_adj,revpairwise~strep.like.IMI.cow.p,type="response")
## $emmeans
##  strep.like.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                        1.61 0.431 Inf     0.948      2.72
##  1                        1.51 0.496 Inf     0.797      2.88
##  2                        2.43 1.219 Inf     0.912      6.49
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                      ratio    SE  df null z.ratio
##  strep.like.IMI.cow.p1 / strep.like.IMI.cow.p0 0.943 0.206 Inf    1  -0.267
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p0 1.515 0.654 Inf    1   0.963
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p1 1.607 0.743 Inf    1   1.026
##  p.value
##   0.9613
##   0.6003
##   0.5605
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
emmeans_p10 <- emmeans(nhscc_10_adj,revpairwise~strep.like.IMI.cow.p,type="response")

performance::icc(nhscc_10_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.221
##   Unadjusted ICC: 0.199
emmeans_p10
## $emmeans
##  strep.like.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                        1.61 0.431 Inf     0.948      2.72
##  1                        1.51 0.496 Inf     0.797      2.88
##  2                        2.43 1.219 Inf     0.912      6.49
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                      ratio    SE  df null z.ratio
##  strep.like.IMI.cow.p1 / strep.like.IMI.cow.p0 0.943 0.206 Inf    1  -0.267
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p0 1.515 0.654 Inf    1   0.963
##  strep.like.IMI.cow.p2 / strep.like.IMI.cow.p1 1.607 0.743 Inf    1   1.026
##  p.value
##   0.9613
##   0.6003
##   0.5605
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p10 <- as.data.frame(emmeans_p10$emmeans)

adj_means_p10
##  strep.like.IMI.cow.p response        SE  df asymp.LCL asymp.UCL
##  0                    1.605768 0.4314697 Inf 0.9483418  2.718946
##  1                    1.514643 0.4959215 Inf 0.7972815  2.877459
##  2                    2.433501 1.2185379 Inf 0.9120224  6.493181
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_10 <- ggplot(adj_means_p10, aes(x=strep.like.IMI.cow.p, y=response, fill=strep.like.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=strep_like_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))




# IRR and 95% CI

contrasts_p10 <- as.data.frame(exp(Confint(nhscc_10_adj)))

contrasts_p10$comparison <- row.names(contrasts_p10)

contrasts_p10 <- contrasts_p10 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p10 <- contrasts_p10 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p10_with_extra <- rbind(contrasts_p10, extra_observation) %>% arrange(comparison)


contrasts_p10_with_extra$strep.like.IMI.cow <- contrasts_p10_with_extra$comparison


nhscc_plot_10_b <- ggplot(contrasts_p10_with_extra, aes(x=strep.like.IMI.cow,y=Estimate,colour=strep.like.IMI.cow)) + 
  geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
              position=position_dodge(0.05)) +
ylim(0,5) +
coord_flip() +
    scale_x_discrete(limits = rev) +
    geom_hline(yintercept=1, linetype="dashed", size=0.5) +
    theme_classic() +
   theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
    scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))





# ---- Enterococcus -----

nhscc_11 <- glmer.nb(n_high_scc~as.factor(enterococcus.spp.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow)
summary(nhscc_11)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7715)  ( log )
## Formula: n_high_scc ~ as.factor(enterococcus.spp.IMI.cow.p) + calving.sau +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1022.6   1049.3   -504.3   1008.6      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0663 -0.6879 -0.5381  0.3775  6.2141 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2878   0.5365  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                        Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            -0.09739    0.28684  -0.340   0.7342    
## as.factor(enterococcus.spp.IMI.cow.p)1  0.20441    0.27569   0.741   0.4584    
## as.factor(enterococcus.spp.IMI.cow.p)2  1.31320    0.51812   2.535   0.0113 *  
## calving.sau1                            0.92967    0.15491   6.001 1.96e-09 ***
## calving.nas1                            0.12893    0.14871   0.867   0.3859    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.105                                   
## a.(..IMI..)2 -0.051  0.129                            
## calving.sa1  -0.166  0.004        0.044               
## calving.ns1  -0.182  0.062       -0.002        0.228
emmeans(nhscc_11,revpairwise~enterococcus.spp.IMI.cow.p,type="response")
## $emmeans
##  enterococcus.spp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                              1.54 0.439 Inf     0.881      2.69
##  1                              1.89 0.716 Inf     0.899      3.97
##  2                              5.73 3.329 Inf     1.833     17.89
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                                  ratio    SE  df null
##  enterococcus.spp.IMI.cow.p1 / enterococcus.spp.IMI.cow.p0  1.23 0.338 Inf    1
##  enterococcus.spp.IMI.cow.p2 / enterococcus.spp.IMI.cow.p0  3.72 1.926 Inf    1
##  enterococcus.spp.IMI.cow.p2 / enterococcus.spp.IMI.cow.p1  3.03 1.681 Inf    1
##  z.ratio p.value
##    0.741  0.7388
##    2.535  0.0303
##    2.000  0.1123
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_11_adj <- glmer.nb(n_high_scc~as.factor(enterococcus.spp.IMI.cow.p)+calving.sau+calving.nas+(1|Site),data=df_dhi_cow) 

summary(nhscc_11_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7715)  ( log )
## Formula: n_high_scc ~ as.factor(enterococcus.spp.IMI.cow.p) + calving.sau +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1022.6   1049.3   -504.3   1008.6      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0663 -0.6879 -0.5381  0.3775  6.2141 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.2878   0.5365  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                        Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                            -0.09739    0.28684  -0.340   0.7342    
## as.factor(enterococcus.spp.IMI.cow.p)1  0.20441    0.27569   0.741   0.4584    
## as.factor(enterococcus.spp.IMI.cow.p)2  1.31320    0.51812   2.535   0.0113 *  
## calving.sau1                            0.92967    0.15491   6.001 1.96e-09 ***
## calving.nas1                            0.12893    0.14871   0.867   0.3859    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##              (Intr) a.(..IMI..)1 a.(..IMI..)2 clvng.s1
## a.(..IMI..)1 -0.105                                   
## a.(..IMI..)2 -0.051  0.129                            
## calving.sa1  -0.166  0.004        0.044               
## calving.ns1  -0.182  0.062       -0.002        0.228
exp(Confint(nhscc_11_adj))
##                                         Estimate     2.5 %    97.5 %
## (Intercept)                            0.9072028 0.5170648  1.591709
## as.factor(enterococcus.spp.IMI.cow.p)1 1.2268053 0.7146753  2.105923
## as.factor(enterococcus.spp.IMI.cow.p)2 3.7180497 1.3467669 10.264504
## calving.sau1                           2.5336638 1.8702195  3.432459
## calving.nas1                           1.1376116 0.8499899  1.522560
emmeans_p11 <- emmeans(nhscc_11_adj,revpairwise~enterococcus.spp.IMI.cow.p,type="response")

performance::icc(nhscc_11_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.250
##   Unadjusted ICC: 0.222
emmeans_p11
## $emmeans
##  enterococcus.spp.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                              1.54 0.439 Inf     0.881      2.69
##  1                              1.89 0.716 Inf     0.899      3.97
##  2                              5.73 3.329 Inf     1.833     17.89
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                                  ratio    SE  df null
##  enterococcus.spp.IMI.cow.p1 / enterococcus.spp.IMI.cow.p0  1.23 0.338 Inf    1
##  enterococcus.spp.IMI.cow.p2 / enterococcus.spp.IMI.cow.p0  3.72 1.926 Inf    1
##  enterococcus.spp.IMI.cow.p2 / enterococcus.spp.IMI.cow.p1  3.03 1.681 Inf    1
##  z.ratio p.value
##    0.741  0.7388
##    2.535  0.0303
##    2.000  0.1123
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p11 <- as.data.frame(emmeans_p11$emmeans)

adj_means_p11
##  enterococcus.spp.IMI.cow.p response       SE  df asymp.LCL asymp.UCL
##  0                          1.540196 0.439083 Inf 0.8808725  2.693015
##  1                          1.889520 0.715580 Inf 0.8994828  3.969266
##  2                          5.726524 3.328765 Inf 1.8327237 17.893082
## 
## Results are averaged over the levels of: calving.sau, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
adj_means_p11$upper_SE <- adj_means_p11$response + adj_means_p11$SE

adj_means_p11$upper_SE <- ifelse(adj_means_p11$upper_SE>6,6,adj_means_p11$upper_SE)

adj_means_p11$enterococcus.spp.IMI.cow.p <- as.factor(adj_means_p11$enterococcus.spp.IMI.cow.p)

nhscc_plot_11 <- ggplot(adj_means_p11, aes(x=enterococcus.spp.IMI.cow.p, y=response, fill=enterococcus.spp.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=upper_SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=enterococcus_genus_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))



# IRR and 95% CI

contrasts_p11 <- as.data.frame(exp(Confint(nhscc_11_adj)))

contrasts_p11$comparison <- row.names(contrasts_p11)

contrasts_p11 <- contrasts_p11 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p11 <- contrasts_p11 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p11_with_extra <- rbind(contrasts_p11, extra_observation) %>% arrange(comparison)

contrasts_p11_with_extra$upper <- ifelse(contrasts_p11_with_extra$upper>5,5,contrasts_p11_with_extra$upper)


contrasts_p11_with_extra$entero.IMI.cow <- contrasts_p11_with_extra$comparison

nhscc_plot_11_b <- ggplot(contrasts_p11_with_extra, aes(x=entero.IMI.cow,y=Estimate,colour=entero.IMI.cow)) + 
  geom_point() +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,5) +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  geom_hline(yintercept=1, linetype="dashed", size=0.5) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))



# ---- gram-negative ----

nhscc_12 <- glmer.nb(n_high_scc~as.factor(gram.neg.IMI.cow)+calving.sau.sslo+calving.nas+(1|Site),data=df_dhi_cow)
summary(nhscc_12)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7309)  ( log )
## Formula: n_high_scc ~ as.factor(gram.neg.IMI.cow) + calving.sau.sslo +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1018.9   1041.8   -503.5   1006.9      327 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1164 -0.6677 -0.5929  0.4443  6.2294 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.201    0.4483  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -0.25356    0.25192  -1.007    0.314    
## as.factor(gram.neg.IMI.cow)1  0.00776    0.25805   0.030    0.976    
## calving.sau.sslo1             0.95342    0.14538   6.558 5.44e-11 ***
## calving.nas1                  0.19609    0.15166   1.293    0.196    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(..I clv..1
## a.(..IMI.)1 -0.075              
## clvng.s.ss1 -0.301  0.009       
## calving.ns1 -0.236 -0.053  0.281
emmeans(nhscc_12,revpairwise~gram.neg.IMI.cow,type="response")
## $emmeans
##  gram.neg.IMI.cow response    SE  df asymp.LCL asymp.UCL
##  0                    1.38 0.331 Inf     0.862      2.21
##  1                    1.39 0.466 Inf     0.720      2.68
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                              ratio   SE  df null z.ratio p.value
##  gram.neg.IMI.cow1 / gram.neg.IMI.cow0  1.01 0.26 Inf    1   0.030  0.9760
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Tests are performed on the log scale
nhscc_12_adj <- glmer.nb(n_high_scc~as.factor(gram.neg.IMI.cow)+calving.sau.sslo+calving.nas+(1|Site),data=df_dhi_cow, 
                 family=poisson(link="log")) 
summary(nhscc_12_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7309)  ( log )
## Formula: n_high_scc ~ as.factor(gram.neg.IMI.cow) + calving.sau.sslo +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1018.9   1041.8   -503.5   1006.9      327 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1164 -0.6677 -0.5929  0.4443  6.2294 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.201    0.4483  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -0.25356    0.25192  -1.007    0.314    
## as.factor(gram.neg.IMI.cow)1  0.00776    0.25805   0.030    0.976    
## calving.sau.sslo1             0.95342    0.14538   6.558 5.44e-11 ***
## calving.nas1                  0.19609    0.15166   1.293    0.196    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(..I clv..1
## a.(..IMI.)1 -0.075              
## clvng.s.ss1 -0.301  0.009       
## calving.ns1 -0.236 -0.053  0.281
exp(Confint(nhscc_12_adj))
##                               Estimate     2.5 %   97.5 %
## (Intercept)                  0.7760319 0.4736407 1.271482
## as.factor(gram.neg.IMI.cow)1 1.0077903 0.6077429 1.671169
## calving.sau.sslo1            2.5945692 1.9512878 3.449921
## calving.nas1                 1.2166347 0.9037964 1.637758
emmeans_p12 <- emmeans(nhscc_12_adj,revpairwise~gram.neg.IMI.cow,type="response")

performance::icc(nhscc_12_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.188
##   Unadjusted ICC: 0.157
emmeans_p12
## $emmeans
##  gram.neg.IMI.cow response    SE  df asymp.LCL asymp.UCL
##  0                    1.38 0.331 Inf     0.862      2.21
##  1                    1.39 0.466 Inf     0.720      2.68
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                              ratio   SE  df null z.ratio p.value
##  gram.neg.IMI.cow1 / gram.neg.IMI.cow0  1.01 0.26 Inf    1   0.030  0.9760
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Tests are performed on the log scale
adj_means_p12 <- as.data.frame(emmeans_p12$emmeans)

adj_means_p12
##  gram.neg.IMI.cow response        SE  df asymp.LCL asymp.UCL
##  0                1.378772 0.3307150 Inf 0.8616310  2.206294
##  1                1.389512 0.4663321 Inf 0.7197672  2.682458
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_12 <- ggplot(adj_means_p12, aes(x=gram.neg.IMI.cow, y=response, fill=gram.neg.IMI.cow)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="Gram-negative bacteria", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))


# IRR and 95% CI

contrasts_p12 <- as.data.frame(exp(Confint(nhscc_12_adj)))

contrasts_p12$comparison <- row.names(contrasts_p12)

contrasts_p12 <- contrasts_p12 %>% filter(grepl("IMI.cow",comparison))

contrasts_p12 <- contrasts_p12 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p12_with_extra <- rbind(contrasts_p12, extra_observation) %>% arrange(comparison)


contrasts_p12_with_extra$gram.neg.IMI.cow <- contrasts_p12_with_extra$comparison


nhscc_plot_12_b <- ggplot(contrasts_p12_with_extra, aes(x=gram.neg.IMI.cow,y=Estimate,colour=gram.neg.IMI.cow)) + 
  geom_point() +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,5) +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  geom_hline(yintercept=1, linetype="dashed", size=0.5) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))




# ---- Other ----

nhscc_13 <- glmer.nb(n_high_scc~as.factor(other.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_13)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.3153)  ( log )
## Formula: n_high_scc ~ as.factor(other.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1043.2   1062.3   -516.6   1033.2      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9678 -0.7029 -0.3853  0.5195  4.4717 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.3325   0.5767  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)   
## (Intercept)                   0.3684     0.3021   1.220  0.22263   
## as.factor(other.IMI.cow.p)1  -0.4131     0.1737  -2.378  0.01739 * 
## as.factor(other.IMI.cow.p)2  -1.2776     0.4038  -3.164  0.00156 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1
## a.(.IMI..)1 -0.142            
## a.(.IMI..)2 -0.082  0.205
emmeans(nhscc_13,revpairwise~other.IMI.cow.p,type="response")
## $emmeans
##  other.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                  1.445 0.437 Inf     0.800      2.61
##  1                  0.956 0.312 Inf     0.504      1.81
##  2                  0.403 0.195 Inf     0.156      1.04
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                            ratio    SE  df null z.ratio p.value
##  other.IMI.cow.p1 / other.IMI.cow.p0 0.662 0.115 Inf    1  -2.378  0.0458
##  other.IMI.cow.p2 / other.IMI.cow.p0 0.279 0.113 Inf    1  -3.164  0.0044
##  other.IMI.cow.p2 / other.IMI.cow.p1 0.421 0.171 Inf    1  -2.132  0.0835
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_13_adj <- glmer.nb(n_high_scc~as.factor(other.IMI.cow.p)+calving.sau.sslo+calving.nas+(1|Site),data=df_dhi_cow)

summary(nhscc_13_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.9341)  ( log )
## Formula: n_high_scc ~ as.factor(other.IMI.cow.p) + calving.sau.sslo +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1006.1   1032.7   -496.0    992.1      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1616 -0.7222 -0.4305  0.4000  4.8598 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.139    0.3728  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -0.0626     0.2230  -0.281  0.77890    
## as.factor(other.IMI.cow.p)1  -0.4231     0.1644  -2.573  0.01008 *  
## as.factor(other.IMI.cow.p)2  -1.2479     0.3939  -3.168  0.00153 ** 
## calving.sau.sslo1             0.9399     0.1438   6.537 6.26e-11 ***
## calving.nas1                  0.1213     0.1495   0.811  0.41710    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1 a.(.IMI..)2 clv..1
## a.(.IMI..)1 -0.209                               
## a.(.IMI..)2 -0.115  0.200                        
## clvng.s.ss1 -0.333 -0.008      -0.001            
## calving.ns1 -0.298  0.139       0.053       0.287
exp(Confint(nhscc_13_adj))
##                              Estimate     2.5 %    97.5 %
## (Intercept)                 0.9393227 0.6067868 1.4540973
## as.factor(other.IMI.cow.p)1 0.6550376 0.4745873 0.9040996
## as.factor(other.IMI.cow.p)2 0.2871143 0.1326718 0.6213428
## calving.sau.sslo1           2.5596347 1.9310914 3.3927602
## calving.nas1                1.1289835 0.8422240 1.5133787
emmeans_p13 <- emmeans(nhscc_13_adj,revpairwise~other.IMI.cow.p,type="response")

performance::icc(nhscc_13_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.141
##   Unadjusted ICC: 0.102
emmeans_p13
## $emmeans
##  other.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                  1.597 0.331 Inf     1.064      2.40
##  1                  1.046 0.252 Inf     0.653      1.68
##  2                  0.458 0.195 Inf     0.199      1.06
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                            ratio    SE  df null z.ratio p.value
##  other.IMI.cow.p1 / other.IMI.cow.p0 0.655 0.108 Inf    1  -2.573  0.0272
##  other.IMI.cow.p2 / other.IMI.cow.p0 0.287 0.113 Inf    1  -3.168  0.0044
##  other.IMI.cow.p2 / other.IMI.cow.p1 0.438 0.173 Inf    1  -2.087  0.0925
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p13 <- as.data.frame(emmeans_p13$emmeans)

adj_means_p13
##  other.IMI.cow.p  response       SE  df asymp.LCL asymp.UCL
##  0               1.5967892 0.330891 Inf 1.0637983  2.396823
##  1               1.0459569 0.251676 Inf 0.6526788  1.676209
##  2               0.4584611 0.195011 Inf 0.1991753  1.055284
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_13 <- ggplot(adj_means_p13, aes(x=other.IMI.cow.p, y=response, fill=other.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title="Others", fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))



# IRR and 95% CI

contrasts_p13 <- as.data.frame(exp(Confint(nhscc_13_adj)))

contrasts_p13$comparison <- row.names(contrasts_p13)

contrasts_p13 <- contrasts_p13 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p13 <- contrasts_p13 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p13_with_extra <- rbind(contrasts_p13, extra_observation) %>% arrange(comparison)


contrasts_p13_with_extra$other.IMI.cow <- contrasts_p13_with_extra$comparison


nhscc_plot_13_b <- ggplot(contrasts_p13_with_extra, aes(x=other.IMI.cow,y=Estimate,colour=other.IMI.cow)) + 
  geom_point() +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,5) +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  geom_hline(yintercept=1, linetype="dashed", size=0.5) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))


# ---- Bacillus sp. ----

nhscc_14 <- glmer.nb(n_high_scc~as.factor(bacillus.IMI.cow.p)+(1|Site),data=df_dhi_cow)
summary(nhscc_14)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.3252)  ( log )
## Formula: n_high_scc ~ as.factor(bacillus.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1040.0   1059.1   -515.0   1030.0      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9704 -0.7225 -0.3602  0.5213  5.1848 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.3631   0.6026  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                Estimate Std. Error z value Pr(>|z|)   
## (Intercept)                      0.3201     0.3124   1.024  0.30561   
## as.factor(bacillus.IMI.cow.p)1  -0.4844     0.2023  -2.395  0.01663 * 
## as.factor(bacillus.IMI.cow.p)2  -2.1423     0.7512  -2.852  0.00435 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1
## a.(.IMI..)1 -0.090            
## a.(.IMI..)2 -0.031  0.073
emmeans(nhscc_14,revpairwise~bacillus.IMI.cow.p,type="response")
## $emmeans
##  bacillus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                     1.377 0.430 Inf    0.7466     2.541
##  1                     0.848 0.303 Inf    0.4218     1.707
##  2                     0.162 0.130 Inf    0.0334     0.782
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                  ratio     SE  df null z.ratio
##  bacillus.IMI.cow.p1 / bacillus.IMI.cow.p0 0.616 0.1246 Inf    1  -2.395
##  bacillus.IMI.cow.p2 / bacillus.IMI.cow.p0 0.117 0.0882 Inf    1  -2.852
##  bacillus.IMI.cow.p2 / bacillus.IMI.cow.p1 0.191 0.1455 Inf    1  -2.171
##  p.value
##   0.0439
##   0.0121
##   0.0762
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_14_adj <- glmer.nb(n_high_scc~as.factor(bacillus.IMI.cow.p)+calving.sau.sslo+calving.nas+(1|Site),data=df_dhi_cow)
summary(nhscc_14_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.8753)  ( log )
## Formula: n_high_scc ~ as.factor(bacillus.IMI.cow.p) + calving.sau.sslo +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1006.9   1033.6   -496.5    992.9      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1470 -0.6874 -0.3663  0.4135  5.3495 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1713   0.4139  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     -0.1088     0.2384  -0.456   0.6483    
## as.factor(bacillus.IMI.cow.p)1  -0.4397     0.1925  -2.284   0.0224 *  
## as.factor(bacillus.IMI.cow.p)2  -1.8771     0.7436  -2.524   0.0116 *  
## calving.sau.sslo1                0.8958     0.1443   6.209 5.32e-10 ***
## calving.nas1                     0.1298     0.1504   0.863   0.3882    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1 a.(.IMI..)2 clv..1
## a.(.IMI..)1 -0.146                               
## a.(.IMI..)2 -0.062  0.075                        
## clvng.s.ss1 -0.324  0.036       0.058            
## calving.ns1 -0.269  0.120       0.037       0.291
exp(Confint(nhscc_14_adj))
##                                 Estimate      2.5 %    97.5 %
## (Intercept)                    0.8969462 0.56211600 1.4312214
## as.factor(bacillus.IMI.cow.p)1 0.6442200 0.44174124 0.9395080
## as.factor(bacillus.IMI.cow.p)2 0.1530353 0.03563394 0.6572333
## calving.sau.sslo1              2.4493509 1.84607725 3.2497663
## calving.nas1                   1.1385588 0.84789000 1.5288729
emmeans_p14 <- emmeans(nhscc_14_adj,revpairwise~bacillus.IMI.cow.p,type="response")

performance::icc(nhscc_14_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.167
##   Unadjusted ICC: 0.117
emmeans_p14
## $emmeans
##  bacillus.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                     1.498 0.335 Inf    0.9660      2.32
##  1                     0.965 0.270 Inf    0.5577      1.67
##  2                     0.229 0.176 Inf    0.0508      1.03
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                                  ratio    SE  df null z.ratio p.value
##  bacillus.IMI.cow.p1 / bacillus.IMI.cow.p0 0.644 0.124 Inf    1  -2.284  0.0580
##  bacillus.IMI.cow.p2 / bacillus.IMI.cow.p0 0.153 0.114 Inf    1  -2.524  0.0311
##  bacillus.IMI.cow.p2 / bacillus.IMI.cow.p1 0.238 0.179 Inf    1  -1.906  0.1368
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p14 <- as.data.frame(emmeans_p14$emmeans)

adj_means_p14
##  bacillus.IMI.cow.p  response        SE  df asymp.LCL asymp.UCL
##  0                  1.4978544 0.3351745 Inf 0.9660430  2.322431
##  1                  0.9649478 0.2699302 Inf 0.5576895  1.669611
##  2                  0.2292246 0.1762916 Inf 0.0507716  1.034907
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_14 <- ggplot(adj_means_p14, aes(x=bacillus.IMI.cow.p, y=response, fill=bacillus.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="n tests with high SCC",title=bacillus_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))




# IRR and 95% CI

contrasts_p14 <- as.data.frame(exp(Confint(nhscc_14_adj)))

contrasts_p14$comparison <- row.names(contrasts_p14)

contrasts_p14 <- contrasts_p14 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p14 <- contrasts_p14 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p14_with_extra <- rbind(contrasts_p14, extra_observation) %>% arrange(comparison)


contrasts_p14_with_extra$bacillus.IMI.cow <- contrasts_p14_with_extra$comparison


nhscc_plot_14_b <- ggplot(contrasts_p14_with_extra, aes(x=bacillus.IMI.cow,y=Estimate,colour=bacillus.IMI.cow)) + 
  geom_point() +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,5) +
  coord_flip() +
  scale_x_discrete(limits = rev) +
  geom_hline(yintercept=1, linetype="dashed", size=0.5) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))



# ---- Coryne ----

nhscc_15 <- glmer.nb(n_high_scc~as.factor(coryne.IMI.cow.p)+(1|Site),data=df_dhi_cow, 
                 family=poisson(link="log")) 
summary(nhscc_15)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.19)  ( log )
## Formula: n_high_scc ~ as.factor(coryne.IMI.cow.p) + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1056.3   1075.3   -523.2   1046.3      328 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -0.9277 -0.6569 -0.3310  0.5641  5.1652 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.4047   0.6362  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                              Estimate Std. Error z value Pr(>|z|)
## (Intercept)                   0.22488    0.32908   0.683    0.494
## as.factor(coryne.IMI.cow.p)1 -0.03028    0.25078  -0.121    0.904
## as.factor(coryne.IMI.cow.p)2 -0.48656    0.54832  -0.887    0.375
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1
## a.(.IMI..)1 -0.085            
## a.(.IMI..)2 -0.048  0.137
emmeans(nhscc_15,revpairwise~coryne.IMI.cow.p,type="response")
## $emmeans
##  coryne.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                    1.25 0.412 Inf     0.657      2.39
##  1                    1.21 0.482 Inf     0.558      2.64
##  2                    0.77 0.482 Inf     0.226      2.62
## 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                              ratio    SE  df null z.ratio p.value
##  coryne.IMI.cow.p1 / coryne.IMI.cow.p0 0.970 0.243 Inf    1  -0.121  0.9920
##  coryne.IMI.cow.p2 / coryne.IMI.cow.p0 0.615 0.337 Inf    1  -0.887  0.6483
##  coryne.IMI.cow.p2 / coryne.IMI.cow.p1 0.634 0.362 Inf    1  -0.799  0.7035
## 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
nhscc_15_adj <- glmer.nb(n_high_scc~as.factor(coryne.IMI.cow.p)+calving.sau.sslo+calving.nas+(1|Site),data=df_dhi_cow, 
                 family=poisson(link="log")) 
summary(nhscc_15_adj)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(1.7459)  ( log )
## Formula: n_high_scc ~ as.factor(coryne.IMI.cow.p) + calving.sau.sslo +  
##     calving.nas + (1 | Site)
##    Data: df_dhi_cow
## 
##      AIC      BIC   logLik deviance df.resid 
##   1019.5   1046.1   -502.7   1005.5      326 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.1191 -0.6705 -0.4961  0.4369  6.0043 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Site   (Intercept) 0.1819   0.4265  
## Number of obs: 333, groups:  Site, 4
## 
## Fixed effects:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -0.23399    0.24360  -0.961    0.337    
## as.factor(coryne.IMI.cow.p)1 -0.03402    0.23910  -0.142    0.887    
## as.factor(coryne.IMI.cow.p)2 -0.61883    0.53583  -1.155    0.248    
## calving.sau.sslo1             0.96085    0.14558   6.600 4.11e-11 ***
## calving.nas1                  0.18642    0.15173   1.229    0.219    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) a.(.IMI..)1 a.(.IMI..)2 clv..1
## a.(.IMI..)1 -0.125                               
## a.(.IMI..)2 -0.060  0.142                        
## clvng.s.ss1 -0.307 -0.010      -0.049            
## calving.ns1 -0.260  0.087       0.048       0.279
exp(Confint(nhscc_15_adj))
##                               Estimate     2.5 %   97.5 %
## (Intercept)                  0.7913691 0.4909348 1.275658
## as.factor(coryne.IMI.cow.p)1 0.9665524 0.6049281 1.544355
## as.factor(coryne.IMI.cow.p)2 0.5385758 0.1884283 1.539386
## calving.sau.sslo1            2.6139173 1.9650501 3.477043
## calving.nas1                 1.2049248 0.8949683 1.622229
emmeans_p15 <- emmeans(nhscc_15_adj,revpairwise~coryne.IMI.cow.p,type="response")

performance::icc(nhscc_15_adj)
## Warning: mu of 1.2 is too close to zero, estimate of random effect variances may
##   be unreliable.
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.173
##   Unadjusted ICC: 0.142
emmeans_p15
## $emmeans
##  coryne.IMI.cow.p response    SE  df asymp.LCL asymp.UCL
##  0                   1.404 0.323 Inf     0.894      2.21
##  1                   1.357 0.426 Inf     0.734      2.51
##  2                   0.756 0.431 Inf     0.248      2.31
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
##  contrast                              ratio    SE  df null z.ratio p.value
##  coryne.IMI.cow.p1 / coryne.IMI.cow.p0 0.967 0.231 Inf    1  -0.142  0.9889
##  coryne.IMI.cow.p2 / coryne.IMI.cow.p0 0.539 0.289 Inf    1  -1.155  0.4803
##  coryne.IMI.cow.p2 / coryne.IMI.cow.p1 0.557 0.309 Inf    1  -1.054  0.5427
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## P value adjustment: tukey method for comparing a family of 3 estimates 
## Tests are performed on the log scale
adj_means_p15 <- as.data.frame(emmeans_p15$emmeans)

adj_means_p15
##  coryne.IMI.cow.p  response        SE  df asymp.LCL asymp.UCL
##  0                1.4044459 0.3232936 Inf 0.8944671  2.205188
##  1                1.3574705 0.4258833 Inf 0.7339735  2.510617
##  2                0.7564005 0.4309324 Inf 0.2476338  2.310435
## 
## Results are averaged over the levels of: calving.sau.sslo, calving.nas 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale
nhscc_plot_15 <- ggplot(adj_means_p15, aes(x=coryne.IMI.cow.p, y=response, fill=coryne.IMI.cow.p)) + 
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin=response-SE, ymax=response+SE), width=.2,
                position=position_dodge(0.06)) +
  coord_flip() +
  ylim(0,6) +
  labs(y="Number of DHIA tests with high SCC (SE)",title=coryne_title, fill="IMI type") +
  scale_x_discrete(limits = rev) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_fill_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                    values = c("#009999", "#FF9944","#990000"))




# IRR and 95% CI

contrasts_p15 <- as.data.frame(exp(Confint(nhscc_15_adj)))

contrasts_p15$comparison <- row.names(contrasts_p15)

contrasts_p15 <- contrasts_p15 %>% filter(grepl("IMI.cow.p",comparison))

contrasts_p15 <- contrasts_p15 %>% rename("lower"=c(2),"upper"=c(3))

# Create a new data frame with the extra observation
extra_observation <- data.frame(comparison = "0",
                                Estimate = 1,
                                upper = 1,
                                lower = 1)

# Combine the extra observation with the original data frame
contrasts_p15_with_extra <- rbind(contrasts_p15, extra_observation) %>% arrange(comparison)


contrasts_p15_with_extra$coryne.IMI.cow <- contrasts_p15_with_extra$comparison


nhscc_plot_15_b <- ggplot(contrasts_p15_with_extra, aes(x=coryne.IMI.cow,y=Estimate,colour=coryne.IMI.cow)) + 
  geom_point() +
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,
                position=position_dodge(0.05)) +
  ylim(0,5) +
  coord_flip() +
  labs(y="IRR (95%CI)") +
  scale_x_discrete(limits = rev) +
  geom_hline(yintercept=1, linetype="dashed", size=0.5) +
  theme_classic() +
  theme(axis.text.y=element_blank(),axis.title.y = element_blank()) +
  scale_colour_manual(labels = c("No IMI", "Transient IMI", "Persistent IMI"),
                      values = c("#009999", "#FF9933","#990000"))




nhscc_figure_2 <- ggarrange( nhscc_plot_1 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_1_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_2 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_2_b + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_3 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_3_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_4 + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_4_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_5 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_5_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_6 + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_6_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_7 + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_7_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_8 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_8_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_9 + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_9_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_10 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_10_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_11 + rremove("ylab") + rremove("xlab"), 
                           nhscc_plot_11_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_12 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_12_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_13 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_13_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_14 + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_14_b + rremove("ylab") + rremove("xlab"),
                           nhscc_plot_15  + rremove("ylab"),
                           nhscc_plot_15_b + rremove("ylab"),
                           ncol = 2, nrow = 15, common.legend = TRUE,align = "h") 


nhscc_figure_annotated_2 <- annotate_figure(nhscc_figure_2,
                                          left = text_grob("Taxonomic group", color = "black", rot = 90))


ggsave(plot = last_plot(),"./figures_article_2/nhscc_figure_nb.png",width = 20, height = 40, units = "cm")