library(knitr)
library(readr)
library(lme4)
library(emmeans)
library(geepack)
library(table1)
library(ggplot2)
library(tidyverse)
library(janitor)
library(psych)
library(performance)

cow <- readRDS("~/Dropbox/R backup/2021/SDCT.Milkyield/sdct.my.cow.rds")
qtr <- readRDS("~/Dropbox/R backup/2021/SDCT.Milkyield/sdct.my.qtr.rds")
dhi <- readRDS("~/Dropbox/R backup/2021/SDCT.Milkyield/sdct.my.dhi.rds")



Acknowledgements

Sam Rowe*1,2 Fidele Kabera,3,4 Simon Dufour,3,4 Sandra Godden,2 Jean-Philippe Roy4,5 Daryl Nydam6

1Sydney School of Veterinary Science, The University of Sydney, Camden, New South Wales 2570, Australia 2Department of Veterinary Population Medicine, University of Minnesota, St. Paul 55108 3Département de pathologie et microbiologie, Faculté de médecine vétérinaire, Université de Montréal, Saint-Hyacinthe, Québec, Canada J2S 2M2 4Mastitis Network, Saint-Hyacinthe, Québec, Canada J2S 2M2 5Département de sciences cliniques, Faculté de médecine vétérinaire, Université de Montréal, Saint-Hyacinthe, Québec, Canada J2S 2M2 6Department of Public and Ecosystem Health, College of Veterinary Medicine, Cornell University, Ithaca, NY 14853, USA;

Inspect Data

qtr <- qtr %>% ungroup()
dhi <- dhi %>% ungroup()
cow <- cow %>% ungroup()



#summarytools::dfSummary(Baseline, style='grid')
print(summarytools::dfSummary(qtr,valid.col=FALSE, graph.magnif=0.8, style="grid"), method = "render")

Data Frame Summary

qtr

Dimensions: 5097 x 23
Duplicates: 0
No Variable Stats / Values Freqs (% of Valid) Graph Missing
1 FARMID [numeric]
Mean (sd) : 5.6 (3.8)
min ≤ med ≤ max:
1 ≤ 4 ≤ 16
IQR (CV) : 4 (0.7)
16 distinct values 0 (0.0%)
2 CowID [numeric]
Mean (sd) : 4264.5 (3257.6)
min ≤ med ≤ max:
2 ≤ 4058 ≤ 23096
IQR (CV) : 4472 (0.8)
1265 distinct values 0 (0.0%)
3 QTR [character]
1. 1
2. 2
3. 3
4. 4
5. LF
6. LR
7. RF
8. RR
233(4.6%)
234(4.6%)
228(4.5%)
229(4.5%)
1052(20.6%)
1025(20.1%)
1059(20.8%)
1037(20.3%)
0 (0.0%)
4 ab [numeric]
Min : 0
Mean : 0.6
Max : 1
0:1808(35.5%)
1:3289(64.5%)
0 (0.0%)
5 Parity [factor]
1. 1
2. 2
3. 3
2236(43.9%)
1472(28.9%)
1389(27.3%)
0 (0.0%)
6 CalvDat [POSIXct, POSIXt]
min : 2015-08-14
med : 2018-08-22
max : 2018-10-24
range : 3y 2m 10d
266 distinct values 0 (0.0%)
7 DOSCC [numeric]
Mean (sd) : 4.4 (1.2)
min ≤ med ≤ max:
1.6 ≤ 4.3 ≤ 8.6
IQR (CV) : 1.6 (0.3)
429 distinct values 4 (0.1%)
8 DOMY [numeric]
Mean (sd) : 26.5 (8.6)
min ≤ med ≤ max:
1.8 ≤ 27.2 ≤ 49.4
IQR (CV) : 11.3 (0.3)
238 distinct values 0 (0.0%)
9 Enrolldat [POSIXct, POSIXt]
min : 2015-07-09
med : 2018-06-25
max : 2018-08-06
range : 3y 0m 28d
136 distinct values 1 (0.0%)
10 Tx [factor]
1. C_SDCT
2. A_SDCT
3. BDCT
1849(36.3%)
1355(26.6%)
1893(37.1%)
0 (0.0%)
11 DOCult1 [character]
1. No Growth
2. Staphylococcus chromogene
3. Staphylococcus sp.
4. Corynebacterium sp.
5. Staphylococcus xylosus/sa
6. Micrococcus sp.
7. Staphylococcus haemolytic
8. Bacillus sp.
9. Staphylococcus epidermidi
10. Staphylococcus aureus
[ 49 others ]
3501(74.7%)
325(6.9%)
187(4.0%)
162(3.5%)
59(1.3%)
51(1.1%)
46(1.0%)
35(0.7%)
29(0.6%)
25(0.5%)
268(5.7%)
409 (8.0%)
12 DOCult2 [character]
1. Corynebacterium sp.
2. Staphylococcus sp.
3. Streptococcus sp.
4. Staphylococcus epidermidi
5. Micrococcus sp.
6. Staphylococcus chromogene
7. Gram positive rod
8. Streptococcus uberis
9. Aerococcus viridans
10. Bacillus sp.
[ 19 others ]
15(17.6%)
13(15.3%)
9(10.6%)
7(8.2%)
4(4.7%)
4(4.7%)
3(3.5%)
3(3.5%)
2(2.4%)
2(2.4%)
23(27.1%)
5012 (98.3%)
13 PCCult1 [character]
1. No Growth
2. Staphylococcus sp.
3. Staphylococcus chromogene
4. Bacillus sp.
5. Staphylococcus sciuri
6. Corynebacterium sp.
7. Gram positive rod
8. Staphylococcus haemolytic
9. Staphylococcus xylosus/sa
10. Streptococcus sp.
[ 55 others ]
3977(78.0%)
197(3.9%)
188(3.7%)
126(2.5%)
124(2.4%)
117(2.3%)
36(0.7%)
30(0.6%)
27(0.5%)
21(0.4%)
254(5.0%)
0 (0.0%)
14 PCCult2 [character]
1. Staphylococcus sp.
2. Corynebacterium sp.
3. Staphylococcus sciuri
4. Staphylococcus chromogene
5. Gram positive rod
6. Staphylococcus haemolytic
7. Bacillus sp.
8. Aerococcus viridans
9. Stenotrophomonas maltophi
10. Staphylococcus xylosus/sa
[ 22 others ]
23(19.3%)
14(11.8%)
12(10.1%)
10(8.4%)
8(6.7%)
8(6.7%)
5(4.2%)
4(3.4%)
4(3.4%)
3(2.5%)
28(23.5%)
4978 (97.7%)
15 source [character]
1. CA
2. US
924(18.1%)
4173(81.9%)
0 (0.0%)
16 cure [numeric]
Min : 0
Mean : 0.9
Max : 1
0:142(12.0%)
1:1045(88.0%)
3910 (76.7%)
17 newimi [numeric]
Min : 0
Mean : 0.2
Max : 1
0:3673(79.2%)
1:965(20.8%)
459 (9.0%)
18 imipc [numeric]
Min : 0
Mean : 0.2
Max : 1
0:3977(78.0%)
1:1120(22.0%)
0 (0.0%)
19 my3 [factor]
1. Low production
2. Mid production
3. High production
1699(33.3%)
1699(33.3%)
1699(33.3%)
0 (0.0%)
20 my3h [factor]
1. Low production
2. Mid production
3. High production
1705(33.5%)
1698(33.3%)
1694(33.2%)
0 (0.0%)
21 imido [numeric]
Min : 0
Mean : 0.3
Max : 1
0:3501(74.7%)
1:1187(25.3%)
409 (8.0%)
22 alg [numeric]
Min : 0
Mean : 0.3
Max : 1
0:3742(73.4%)
1:1355(26.6%)
0 (0.0%)
23 cult [numeric]
Min : 0
Mean : 0.4
Max : 1
0:3248(63.7%)
1:1849(36.3%)
0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.1)
2022-09-26

print(summarytools::dfSummary(dhi,valid.col=FALSE, graph.magnif=0.8, style="grid"), method = "render")

Data Frame Summary

dhi

Dimensions: 4553 x 19
Duplicates: 0
No Variable Stats / Values Freqs (% of Valid) Graph Missing
1 CowID [numeric]
Mean (sd) : 4442.5 (3271.4)
min ≤ med ≤ max:
2 ≤ 4206 ≤ 23096
IQR (CV) : 4458 (0.7)
1283 distinct values 0 (0.0%)
2 FARMID [numeric]
Mean (sd) : 5.1 (3.2)
min ≤ med ≤ max:
1 ≤ 4 ≤ 16
IQR (CV) : 2 (0.6)
16 distinct values 0 (0.0%)
3 Parity [factor]
1. 1
2. 2
3. 3
2016(44.3%)
1312(28.8%)
1225(26.9%)
0 (0.0%)
4 CalvDat [Date]
min : 2015-08-14
med : 2018-08-25
max : 2018-10-24
range : 3y 2m 10d
242 distinct values 0 (0.0%)
5 DOSCC [numeric]
Mean (sd) : 4.4 (1.2)
min ≤ med ≤ max:
1.6 ≤ 4.3 ≤ 8.6
IQR (CV) : 1.6 (0.3)
424 distinct values 0 (0.0%)
6 DOMY [numeric]
Mean (sd) : 27 (8.6)
min ≤ med ≤ max:
1.8 ≤ 27.7 ≤ 49.4
IQR (CV) : 10.9 (0.3)
215 distinct values 0 (0.0%)
7 Enrolldat [Date]
min : 2015-07-09
med : 2018-06-29
max : 2018-08-06
range : 3y 0m 28d
115 distinct values 0 (0.0%)
8 Tx [factor]
1. C_SDCT
2. A_SDCT
3. BDCT
1629(35.8%)
1292(28.4%)
1632(35.8%)
0 (0.0%)
9 TestNo [factor]
1. 1
2. 2
3. 3
4. 4
1362(29.9%)
1313(28.8%)
1196(26.3%)
682(15.0%)
0 (0.0%)
10 DIM [numeric]
Mean (sd) : 60.6 (33.9)
min ≤ med ≤ max:
1 ≤ 60 ≤ 120
IQR (CV) : 60 (0.6)
120 distinct values 0 (0.0%)
11 MY [numeric]
Mean (sd) : 48.5 (10.4)
min ≤ med ≤ max:
0.9 ≤ 48.5 ≤ 93.4
IQR (CV) : 13.2 (0.2)
399 distinct values 2 (0.0%)
12 SCC [numeric]
Mean (sd) : 181.4 (654.4)
min ≤ med ≤ max:
0 ≤ 38 ≤ 9999
IQR (CV) : 89 (3.6)
665 distinct values 0 (0.0%)
13 TestDIMcat20 [factor]
1. 10
2. 30
3. 50
4. 70
5. 90
6. 110
740(16.3%)
772(17.0%)
767(16.8%)
808(17.7%)
676(14.8%)
790(17.4%)
0 (0.0%)
14 my3 [factor]
1. Low production
2. Mid production
3. High production
1520(33.4%)
1516(33.3%)
1517(33.3%)
0 (0.0%)
15 my3h [factor]
1. Low production
2. Mid production
3. High production
1524(33.5%)
1520(33.4%)
1509(33.1%)
0 (0.0%)
16 scc [numeric]
Mean (sd) : 3.9 (1.4)
min ≤ med ≤ max:
0.9 ≤ 3.6 ≤ 9.2
IQR (CV) : 1.8 (0.4)
665 distinct values 0 (0.0%)
17 source [character]
1. CA
2. US
564(12.4%)
3989(87.6%)
0 (0.0%)
18 alg [numeric]
Min : 0
Mean : 0.3
Max : 1
0:3261(71.6%)
1:1292(28.4%)
0 (0.0%)
19 cult [numeric]
Min : 0
Mean : 0.4
Max : 1
0:2924(64.2%)
1:1629(35.8%)
0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.1)
2022-09-26

print(summarytools::dfSummary(cow,valid.col=FALSE, graph.magnif=0.8, style="grid"), method = "render")

Data Frame Summary

cow

Dimensions: 1485 x 19
Duplicates: 0
No Variable Stats / Values Freqs (% of Valid) Graph Missing
1 CowID [numeric]
Mean (sd) : 4286.9 (3232.5)
min ≤ med ≤ max:
2 ≤ 4116 ≤ 23096
IQR (CV) : 4609 (0.8)
1388 distinct values 0 (0.0%)
2 FARMID [numeric]
Mean (sd) : 5.6 (3.8)
min ≤ med ≤ max:
1 ≤ 4 ≤ 16
IQR (CV) : 4 (0.7)
16 distinct values 0 (0.0%)
3 Parity [factor]
1. 1
2. 2
3. 3
626(42.2%)
434(29.2%)
425(28.6%)
0 (0.0%)
4 Enrolldat [POSIXct, POSIXt]
min : 2015-07-09
med : 2018-06-25
max : 2018-08-06
range : 3y 0m 28d
143 distinct values 0 (0.0%)
5 Tx [factor]
1. C_SDCT
2. A_SDCT
3. BDCT
536(36.1%)
394(26.5%)
555(37.4%)
0 (0.0%)
6 CalvDat [Date]
min : 2015-08-14
med : 2018-08-22
max : 2018-10-24
range : 3y 2m 10d
287 distinct values 0 (0.0%)
7 DOMY [numeric]
Mean (sd) : 26.7 (8.5)
min ≤ med ≤ max:
1.8 ≤ 27.7 ≤ 49.4
IQR (CV) : 11 (0.3)
248 distinct values 0 (0.0%)
8 DOSCC [numeric]
Mean (sd) : 4.4 (1.2)
min ≤ med ≤ max:
1.6 ≤ 4.3 ≤ 8.6
IQR (CV) : 1.6 (0.3)
454 distinct values 1 (0.1%)
9 CM [numeric]
Min : 0
Mean : 0.1
Max : 1
0:1282(86.3%)
1:203(13.7%)
0 (0.0%)
10 CMTAR [numeric]
Mean (sd) : 106.5 (31.6)
min ≤ med ≤ max:
0 ≤ 120 ≤ 120
IQR (CV) : 0 (0.3)
109 distinct values 0 (0.0%)
11 CMDP [numeric]
Min : 0
Mean : 0
Max : 1
0:1479(99.6%)
1:6(0.4%)
0 (0.0%)
12 CullDP [numeric] 1 distinct value
0:1485(100.0%)
0 (0.0%)
13 Cull2TAR [numeric]
Mean (sd) : 113.1 (23.8)
min ≤ med ≤ max:
1 ≤ 120 ≤ 120
IQR (CV) : 0 (0.2)
80 distinct values 0 (0.0%)
14 Cull2 [numeric]
Min : 0
Mean : 0.1
Max : 1
0:1337(90.0%)
1:148(10.0%)
0 (0.0%)
15 my3 [factor]
1. Low production
2. Mid production
3. High production
495(33.3%)
495(33.3%)
495(33.3%)
0 (0.0%)
16 my3h [factor]
1. Low production
2. Mid production
3. High production
503(33.9%)
494(33.3%)
488(32.9%)
0 (0.0%)
17 source [character]
1. CA
2. US
274(18.5%)
1211(81.5%)
0 (0.0%)
18 alg [numeric]
Min : 0
Mean : 0.3
Max : 1
0:1091(73.5%)
1:394(26.5%)
0 (0.0%)
19 cult [numeric]
Min : 0
Mean : 0.4
Max : 1
0:949(63.9%)
1:536(36.1%)
0 (0.0%)

Generated by summarytools 1.0.1 (R version 4.2.1)
2022-09-26

Report milk culture results at dry-off

do <- table(c(qtr$DOCult1,qtr$DOCult2)) %>% as.data.frame
do$atrisk <- nrow(qtr[!is.na(qtr$imido),])
do$percent <- round((do$Freq / do$atrisk) * 100,2)
do %>% select(Var1,Freq,percent)
##                                    Var1 Freq percent
## 1              Acinetobacter guillouiae    1    0.02
## 2                     Acinetobacter sp.    5    0.11
## 3                       Actinomyces sp.    2    0.04
## 4                        Aerococcus sp.   14    0.30
## 5                   Aerococcus viridans   13    0.28
## 6                          Bacillus sp.   37    0.79
## 7                    Brevibacillus agri    1    0.02
## 8                     Brevundimonas sp.    1    0.02
## 9                  Chryseobacterium sp.    1    0.02
## 10                  Corynebacterium sp.  177    3.78
## 11               Enterobacter aerogenes    1    0.02
## 12                 Enterobacter cloacae    9    0.19
## 13                   Enterobacter kobei    1    0.02
## 14                Enterococcus faecalis    1    0.02
## 15                 Enterococcus faecium    1    0.02
## 16                   Enterococcus hirae    2    0.04
## 17                     Enterococcus sp.    6    0.13
## 18                     Escherichia coli    7    0.15
## 19                      Escherichia sp.    3    0.06
## 20               Gram negative organism    6    0.13
## 21                 Gram positive coccus   11    0.23
## 22               Gram positive organism   14    0.30
## 23                    Gram positive rod   21    0.45
## 24                Klebsiella pneumoniae    4    0.09
## 25                          Kocuria sp.    5    0.11
## 26                      Kocuria species    3    0.06
## 27                    Lactobacillus sp.    3    0.06
## 28                 Lactococcus garvieae    8    0.17
## 29                   Lactococcus lactis    5    0.11
## 30                      Lactococcus sp.   11    0.23
## 31                   Micrococcus luteus    3    0.06
## 32                      Micrococcus sp.   55    1.17
## 33                        Neisseria sp.    4    0.09
## 34                            No Growth 3501   74.68
## 35                    Paenibacillus sp.    3    0.06
## 36               Pseudomonas aeruginosa    3    0.06
## 37                      Pseudomonas sp.   11    0.23
## 38                           Rothia sp.    6    0.13
## 39                Staphylococcus aureus   26    0.55
## 40           Staphylococcus auricularis    1    0.02
## 41               Staphylococcus capitis    6    0.13
## 42           Staphylococcus chromogenes  329    7.02
## 43                Staphylococcus cohnii    3    0.06
## 44           Staphylococcus epidermidis   36    0.77
## 45               Staphylococcus equorum   11    0.23
## 46            Staphylococcus gallinarum    1    0.02
## 47          Staphylococcus haemolyticus   47    1.00
## 48               Staphylococcus hominis    5    0.11
## 49                Staphylococcus sciuri   19    0.41
## 50              Staphylococcus simulans   11    0.23
## 51                   Staphylococcus sp.  200    4.27
## 52              Staphylococcus succinus    1    0.02
## 53 Staphylococcus xylosus/saprophyticus   60    1.28
## 54                 Stenotrophomonas sp.    1    0.02
## 55                  Streptococcus mitis    6    0.13
## 56                 Streptococcus oralis    2    0.04
## 57                    Streptococcus sp.   33    0.70
## 58                 Streptococcus uberis    8    0.17
## 59                     Streptomyces sp.    2    0.04
## 60                 Trueperella pyogenes    2    0.04
## 61                                Yeast    3    0.06

Report milk culture results at calving

pc <- table(c(qtr$PCCult1,qtr$PCCult2)) %>% as.data.frame
pc$atrisk <- nrow(qtr[!is.na(qtr$imipc),])
pc$percent <- round((pc$Freq / pc$atrisk) * 100,2)
pc %>% select(Var1,Freq,percent)
##                                    Var1 Freq percent
## 1                     Acinetobacter sp.   19    0.37
## 2                        Aerococcus sp.   10    0.20
## 3                   Aerococcus viridans   19    0.37
## 4                      Arthrobacter sp.    2    0.04
## 5                          Bacillus sp.  131    2.57
## 6               Brachybacterium faecium    1    0.02
## 7                   Brachybacterium sp.    2    0.04
## 8                     Brevundimonas sp.    2    0.04
## 9                  Chryseobacterium sp.    1    0.02
## 10                  Corynebacterium sp.  131    2.57
## 11                 Enterobacter cloacae    5    0.10
## 12                Enterococcus faecalis    4    0.08
## 13                   Enterococcus hirae    1    0.02
## 14         Enterococcus saccharolyticus    3    0.06
## 15                     Enterococcus sp.    3    0.06
## 16            Enterococcus thailandicus    1    0.02
## 17                     Escherichia coli    9    0.18
## 18                      Escherichia sp.    2    0.04
## 19               Gram negative organism   19    0.37
## 20                 Gram positive coccus   18    0.35
## 21               Gram positive organism   15    0.29
## 22                    Gram positive rod   44    0.86
## 23                   Klebsiella oxytoca    3    0.06
## 24                Klebsiella pneumoniae    1    0.02
## 25                   Kocuria carniphila    1    0.02
## 26                    Kocuria palustris    1    0.02
## 27                          Kocuria sp.    1    0.02
## 28                    Lactobacillus sp.    1    0.02
## 29                 Lactococcus garvieae    6    0.12
## 30                   Lactococcus lactis    1    0.02
## 31                      Lactococcus sp.    6    0.12
## 32                   Micrococcus luteus    6    0.12
## 33                      Micrococcus sp.    8    0.16
## 34                        Moraxella sp.    2    0.04
## 35                 Neisseria flavescens    2    0.04
## 36                        Neisseria sp.    4    0.08
## 37                            No Growth 3977   78.03
## 38                         Oligella sp.    1    0.02
## 39                    Paenibacillus sp.    2    0.04
## 40                          Pantoea sp.    6    0.12
## 41                       Prototheca sp.    8    0.16
## 42               Pseudomonas aeruginosa    1    0.02
## 43                      Pseudomonas sp.    4    0.08
## 44                           Rothia sp.   20    0.39
## 45                         Serratia sp.    1    0.02
## 46                Staphylococcus aureus   18    0.35
## 47               Staphylococcus capitis    3    0.06
## 48           Staphylococcus chromogenes  198    3.88
## 49                Staphylococcus cohnii    1    0.02
## 50           Staphylococcus epidermidis    4    0.08
## 51               Staphylococcus equorum    1    0.02
## 52            Staphylococcus gallinarum    1    0.02
## 53          Staphylococcus haemolyticus   38    0.75
## 54               Staphylococcus hominis    4    0.08
## 55                Staphylococcus sciuri  136    2.67
## 56              Staphylococcus simulans    5    0.10
## 57                   Staphylococcus sp.  220    4.32
## 58 Staphylococcus xylosus/saprophyticus   30    0.59
## 59         Stenotrophomonas maltophilia    5    0.10
## 60           Streptococcus dysgalactiae    4    0.08
## 61             Streptococcus gallinarum    1    0.02
## 62                  Streptococcus mitis    7    0.14
## 63                    Streptococcus sp.   21    0.41
## 64                 Streptococcus uberis    3    0.06
## 65                 Trueperella pyogenes    2    0.04
## 66                    Weissella confusa    1    0.02
## 67                                Yeast    8    0.16
table1(~ my3 + factor(ab) + factor(imido) + DOMY + DOSCC + factor(Parity) + factor(FARMID) + source | Tx, data=qtr, na.is.category = F)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
Overall
(N=5097)
my3
Low production 582 (31.5%) 411 (30.3%) 706 (37.3%) 1699 (33.3%)
Mid production 636 (34.4%) 442 (32.6%) 621 (32.8%) 1699 (33.3%)
High production 631 (34.1%) 502 (37.0%) 566 (29.9%) 1699 (33.3%)
factor(ab)
0 1049 (56.7%) 759 (56.0%) 0 (0%) 1808 (35.5%)
1 800 (43.3%) 596 (44.0%) 1893 (100%) 3289 (64.5%)
factor(imido)
0 1274 (74.6%) 938 (75.6%) 1289 (74.1%) 3501 (74.7%)
1 434 (25.4%) 303 (24.4%) 450 (25.9%) 1187 (25.3%)
Missing 141 (7.6%) 114 (8.4%) 154 (8.1%) 409 (8.0%)
DOMY
Mean (SD) 26.8 (8.56) 27.1 (8.45) 25.8 (8.61) 26.5 (8.57)
Median [Min, Max] 27.7 [1.81, 49.4] 28.6 [2.72, 49.4] 26.4 [4.54, 49.4] 27.2 [1.81, 49.4]
DOSCC
Mean (SD) 4.36 (1.22) 4.43 (1.20) 4.48 (1.18) 4.42 (1.20)
Median [Min, Max] 4.20 [1.61, 8.59] 4.38 [1.61, 7.59] 4.40 [1.61, 8.35] 4.33 [1.61, 8.59]
Missing 0 (0%) 0 (0%) 4 (0.2%) 4 (0.1%)
factor(Parity)
1 872 (47.2%) 599 (44.2%) 765 (40.4%) 2236 (43.9%)
2 472 (25.5%) 428 (31.6%) 572 (30.2%) 1472 (28.9%)
3 505 (27.3%) 328 (24.2%) 556 (29.4%) 1389 (27.3%)
factor(FARMID)
1 100 (5.41%) 83 (6.13%) 112 (5.92%) 295 (5.79%)
2 92 (4.98%) 124 (9.15%) 108 (5.71%) 324 (6.36%)
3 291 (15.7%) 259 (19.1%) 285 (15.1%) 835 (16.4%)
4 456 (24.7%) 424 (31.3%) 390 (20.6%) 1270 (24.9%)
5 310 (16.8%) 278 (20.5%) 299 (15.8%) 887 (17.4%)
6 61 (3.30%) 55 (4.06%) 52 (2.75%) 168 (3.30%)
7 116 (6.27%) 132 (9.74%) 146 (7.71%) 394 (7.73%)
8 32 (1.73%) 0 (0%) 36 (1.90%) 68 (1.33%)
9 32 (1.73%) 0 (0%) 44 (2.32%) 76 (1.49%)
10 32 (1.73%) 0 (0%) 49 (2.59%) 81 (1.59%)
11 43 (2.33%) 0 (0%) 35 (1.85%) 78 (1.53%)
12 28 (1.51%) 0 (0%) 40 (2.11%) 68 (1.33%)
13 40 (2.16%) 0 (0%) 54 (2.85%) 94 (1.84%)
14 112 (6.06%) 0 (0%) 136 (7.18%) 248 (4.87%)
15 20 (1.08%) 0 (0%) 35 (1.85%) 55 (1.08%)
16 84 (4.54%) 0 (0%) 72 (3.80%) 156 (3.06%)
source
CA 423 (22.9%) 0 (0%) 501 (26.5%) 924 (18.1%)
US 1426 (77.1%) 1355 (100%) 1392 (73.5%) 4173 (81.9%)
table1(~ DOMY + my3 + source + factor(Parity) + factor(FARMID) + DOSCC | Tx, data=cow, na.is.category = F)
C_SDCT
(N=536)
A_SDCT
(N=394)
BDCT
(N=555)
Overall
(N=1485)
DOMY
Mean (SD) 26.9 (8.60) 27.3 (8.38) 26.0 (8.52) 26.7 (8.52)
Median [Min, Max] 27.7 [1.81, 49.4] 29.0 [2.72, 49.4] 27.0 [4.54, 49.4] 27.7 [1.81, 49.4]
my3
Low production 171 (31.9%) 121 (30.7%) 203 (36.6%) 495 (33.3%)
Mid production 182 (34.0%) 127 (32.2%) 186 (33.5%) 495 (33.3%)
High production 183 (34.1%) 146 (37.1%) 166 (29.9%) 495 (33.3%)
source
CA 126 (23.5%) 0 (0%) 148 (26.7%) 274 (18.5%)
US 410 (76.5%) 394 (100%) 407 (73.3%) 1211 (81.5%)
factor(Parity)
1 241 (45.0%) 167 (42.4%) 218 (39.3%) 626 (42.2%)
2 141 (26.3%) 123 (31.2%) 170 (30.6%) 434 (29.2%)
3 154 (28.7%) 104 (26.4%) 167 (30.1%) 425 (28.6%)
factor(FARMID)
1 27 (5.04%) 21 (5.33%) 28 (5.05%) 76 (5.12%)
2 24 (4.48%) 31 (7.87%) 27 (4.86%) 82 (5.52%)
3 87 (16.2%) 83 (21.1%) 93 (16.8%) 263 (17.7%)
4 136 (25.4%) 130 (33.0%) 122 (22.0%) 388 (26.1%)
5 84 (15.7%) 77 (19.5%) 81 (14.6%) 242 (16.3%)
6 16 (2.99%) 15 (3.81%) 16 (2.88%) 47 (3.16%)
7 36 (6.72%) 37 (9.39%) 40 (7.21%) 113 (7.61%)
8 9 (1.68%) 0 (0%) 9 (1.62%) 18 (1.21%)
9 12 (2.24%) 0 (0%) 13 (2.34%) 25 (1.68%)
10 9 (1.68%) 0 (0%) 14 (2.52%) 23 (1.55%)
11 11 (2.05%) 0 (0%) 11 (1.98%) 22 (1.48%)
12 8 (1.49%) 0 (0%) 11 (1.98%) 19 (1.28%)
13 14 (2.61%) 0 (0%) 15 (2.70%) 29 (1.95%)
14 34 (6.34%) 0 (0%) 42 (7.57%) 76 (5.12%)
15 6 (1.12%) 0 (0%) 10 (1.80%) 16 (1.08%)
16 23 (4.29%) 0 (0%) 23 (4.14%) 46 (3.10%)
DOSCC
Mean (SD) 4.39 (1.23) 4.45 (1.21) 4.46 (1.18) 4.43 (1.21)
Median [Min, Max] 4.26 [1.61, 8.59] 4.39 [1.61, 8.25] 4.38 [1.61, 8.35] 4.34 [1.61, 8.59]
Missing 0 (0%) 0 (0%) 1 (0.2%) 1 (0.1%)
table1(~ my3 + factor(ab) + factor(imido) +  factor(FARMID) | source, data=qtr, na.is.category = F)
CA
(N=924)
US
(N=4173)
Overall
(N=5097)
my3
Low production 441 (47.7%) 1258 (30.1%) 1699 (33.3%)
Mid production 322 (34.8%) 1377 (33.0%) 1699 (33.3%)
High production 161 (17.4%) 1538 (36.9%) 1699 (33.3%)
factor(ab)
0 243 (26.3%) 1565 (37.5%) 1808 (35.5%)
1 681 (73.7%) 2608 (62.5%) 3289 (64.5%)
factor(imido)
0 642 (71.8%) 2859 (75.4%) 3501 (74.7%)
1 252 (28.2%) 935 (24.6%) 1187 (25.3%)
Missing 30 (3.2%) 379 (9.1%) 409 (8.0%)
factor(FARMID)
1 0 (0%) 295 (7.07%) 295 (5.79%)
2 0 (0%) 324 (7.76%) 324 (6.36%)
3 0 (0%) 835 (20.0%) 835 (16.4%)
4 0 (0%) 1270 (30.4%) 1270 (24.9%)
5 0 (0%) 887 (21.3%) 887 (17.4%)
6 0 (0%) 168 (4.03%) 168 (3.30%)
7 0 (0%) 394 (9.44%) 394 (7.73%)
8 68 (7.36%) 0 (0%) 68 (1.33%)
9 76 (8.23%) 0 (0%) 76 (1.49%)
10 81 (8.77%) 0 (0%) 81 (1.59%)
11 78 (8.44%) 0 (0%) 78 (1.53%)
12 68 (7.36%) 0 (0%) 68 (1.33%)
13 94 (10.2%) 0 (0%) 94 (1.84%)
14 248 (26.8%) 0 (0%) 248 (4.87%)
15 55 (5.95%) 0 (0%) 55 (1.08%)
16 156 (16.9%) 0 (0%) 156 (3.06%)
table1(~ my3 + DOMY + DOSCC + factor(Parity) + factor(FARMID) + Tx | source, data=cow, na.is.category = F)
CA
(N=274)
US
(N=1211)
Overall
(N=1485)
my3
Low production 134 (48.9%) 361 (29.8%) 495 (33.3%)
Mid production 96 (35.0%) 399 (32.9%) 495 (33.3%)
High production 44 (16.1%) 451 (37.2%) 495 (33.3%)
DOMY
Mean (SD) 23.7 (6.82) 27.4 (8.73) 26.7 (8.52)
Median [Min, Max] 23.8 [6.40, 43.1] 29.0 [1.81, 49.4] 27.7 [1.81, 49.4]
DOSCC
Mean (SD) 4.43 (1.16) 4.43 (1.22) 4.43 (1.21)
Median [Min, Max] 4.37 [1.61, 7.69] 4.33 [1.61, 8.59] 4.34 [1.61, 8.59]
Missing 1 (0.4%) 0 (0%) 1 (0.1%)
factor(Parity)
1 116 (42.3%) 510 (42.1%) 626 (42.2%)
2 70 (25.5%) 364 (30.1%) 434 (29.2%)
3 88 (32.1%) 337 (27.8%) 425 (28.6%)
factor(FARMID)
1 0 (0%) 76 (6.28%) 76 (5.12%)
2 0 (0%) 82 (6.77%) 82 (5.52%)
3 0 (0%) 263 (21.7%) 263 (17.7%)
4 0 (0%) 388 (32.0%) 388 (26.1%)
5 0 (0%) 242 (20.0%) 242 (16.3%)
6 0 (0%) 47 (3.88%) 47 (3.16%)
7 0 (0%) 113 (9.33%) 113 (7.61%)
8 18 (6.57%) 0 (0%) 18 (1.21%)
9 25 (9.12%) 0 (0%) 25 (1.68%)
10 23 (8.39%) 0 (0%) 23 (1.55%)
11 22 (8.03%) 0 (0%) 22 (1.48%)
12 19 (6.93%) 0 (0%) 19 (1.28%)
13 29 (10.6%) 0 (0%) 29 (1.95%)
14 76 (27.7%) 0 (0%) 76 (5.12%)
15 16 (5.84%) 0 (0%) 16 (1.08%)
16 46 (16.8%) 0 (0%) 46 (3.10%)
Tx
C_SDCT 126 (46.0%) 410 (33.9%) 536 (36.1%)
A_SDCT 0 (0%) 394 (32.5%) 394 (26.5%)
BDCT 148 (54.0%) 407 (33.6%) 555 (37.4%)
table1(~ factor(ab) + factor(imido) + DOMY + DOSCC + factor(Parity) | my3*Tx, data=qtr, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=582)
A_SDCT
(N=411)
BDCT
(N=706)
C_SDCT
(N=636)
A_SDCT
(N=442)
BDCT
(N=621)
C_SDCT
(N=631)
A_SDCT
(N=502)
BDCT
(N=566)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
factor(ab)
0 345 (59.3%) 162 (39.4%) 0 (0%) 371 (58.3%) 271 (61.3%) 0 (0%) 333 (52.8%) 326 (64.9%) 0 (0%) 1049 (56.7%) 759 (56.0%) 0 (0%)
1 237 (40.7%) 249 (60.6%) 706 (100%) 265 (41.7%) 171 (38.7%) 621 (100%) 298 (47.2%) 176 (35.1%) 566 (100%) 800 (43.3%) 596 (44.0%) 1893 (100%)
factor(imido)
0 436 (79.6%) 309 (82.0%) 496 (75.5%) 439 (74.4%) 304 (74.7%) 438 (76.2%) 399 (70.0%) 325 (71.1%) 355 (70.0%) 1274 (74.6%) 938 (75.6%) 1289 (74.1%)
1 112 (20.4%) 68 (18.0%) 161 (24.5%) 151 (25.6%) 103 (25.3%) 137 (23.8%) 171 (30.0%) 132 (28.9%) 152 (30.0%) 434 (25.4%) 303 (24.4%) 450 (25.9%)
Missing 34 (5.8%) 34 (8.3%) 49 (6.9%) 46 (7.2%) 35 (7.9%) 46 (7.4%) 61 (9.7%) 45 (9.0%) 59 (10.4%) 141 (7.6%) 114 (8.4%) 154 (8.1%)
DOMY
Mean (SD) 16.7 (5.14) 16.8 (5.77) 16.9 (5.01) 27.3 (2.11) 27.7 (2.06) 27.2 (2.00) 35.5 (3.91) 35.0 (3.44) 35.5 (4.01) 26.8 (8.56) 27.1 (8.45) 25.8 (8.61)
Median [Min, Max] 17.6 [1.81, 23.6] 18.6 [2.72, 23.6] 18.1 [4.54, 23.6] 27.4 [23.6, 30.4] 27.7 [23.6, 30.4] 27.2 [23.6, 30.4] 34.5 [30.4, 49.4] 34.5 [30.4, 49.4] 34.5 [30.4, 49.4] 27.7 [1.81, 49.4] 28.6 [2.72, 49.4] 26.4 [4.54, 49.4]
DOSCC
Mean (SD) 5.03 (1.30) 4.93 (1.20) 5.01 (1.20) 4.19 (1.06) 4.21 (1.12) 4.24 (1.08) 3.92 (1.03) 4.22 (1.15) 4.08 (1.02) 4.36 (1.22) 4.43 (1.20) 4.48 (1.18)
Median [Min, Max] 5.02 [1.95, 8.17] 4.91 [1.61, 7.35] 4.88 [2.60, 8.35] 4.11 [1.95, 7.89] 4.25 [1.79, 7.56] 4.19 [1.61, 7.38] 3.85 [1.61, 8.59] 4.13 [2.30, 7.59] 3.99 [1.61, 6.61] 4.20 [1.61, 8.59] 4.38 [1.61, 7.59] 4.40 [1.61, 8.35]
Missing 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 4 (0.6%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 4 (0.2%)
factor(Parity)
1 223 (38.3%) 132 (32.1%) 259 (36.7%) 291 (45.8%) 202 (45.7%) 259 (41.7%) 358 (56.7%) 265 (52.8%) 247 (43.6%) 872 (47.2%) 599 (44.2%) 765 (40.4%)
2 155 (26.6%) 158 (38.4%) 226 (32.0%) 168 (26.4%) 133 (30.1%) 176 (28.3%) 149 (23.6%) 137 (27.3%) 170 (30.0%) 472 (25.5%) 428 (31.6%) 572 (30.2%)
3 204 (35.1%) 121 (29.4%) 221 (31.3%) 177 (27.8%) 107 (24.2%) 186 (30.0%) 124 (19.7%) 100 (19.9%) 149 (26.3%) 505 (27.3%) 328 (24.2%) 556 (29.4%)
table1(~ factor(ab) | my3*Tx, data=qtr, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=582)
A_SDCT
(N=411)
BDCT
(N=706)
C_SDCT
(N=636)
A_SDCT
(N=442)
BDCT
(N=621)
C_SDCT
(N=631)
A_SDCT
(N=502)
BDCT
(N=566)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
factor(ab)
0 345 (59.3%) 162 (39.4%) 0 (0%) 371 (58.3%) 271 (61.3%) 0 (0%) 333 (52.8%) 326 (64.9%) 0 (0%) 1049 (56.7%) 759 (56.0%) 0 (0%)
1 237 (40.7%) 249 (60.6%) 706 (100%) 265 (41.7%) 171 (38.7%) 621 (100%) 298 (47.2%) 176 (35.1%) 566 (100%) 800 (43.3%) 596 (44.0%) 1893 (100%)
table1(~ factor(imipc) + factor(cure) + factor(newimi) | my3*Tx, data=qtr, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=582)
A_SDCT
(N=411)
BDCT
(N=706)
C_SDCT
(N=636)
A_SDCT
(N=442)
BDCT
(N=621)
C_SDCT
(N=631)
A_SDCT
(N=502)
BDCT
(N=566)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
factor(imipc)
0 440 (75.6%) 326 (79.3%) 568 (80.5%) 504 (79.2%) 340 (76.9%) 497 (80.0%) 481 (76.2%) 386 (76.9%) 435 (76.9%) 1425 (77.1%) 1052 (77.6%) 1500 (79.2%)
1 142 (24.4%) 85 (20.7%) 138 (19.5%) 132 (20.8%) 102 (23.1%) 124 (20.0%) 150 (23.8%) 116 (23.1%) 131 (23.1%) 424 (22.9%) 303 (22.4%) 393 (20.8%)
factor(cure)
0 13 (11.6%) 8 (11.8%) 22 (13.7%) 20 (13.2%) 13 (12.6%) 13 (9.49%) 16 (9.36%) 15 (11.4%) 22 (14.5%) 49 (11.3%) 36 (11.9%) 57 (12.7%)
1 99 (88.4%) 60 (88.2%) 139 (86.3%) 131 (86.8%) 90 (87.4%) 124 (90.5%) 155 (90.6%) 117 (88.6%) 130 (85.5%) 385 (88.7%) 267 (88.1%) 393 (87.3%)
Missing 470 (80.8%) 343 (83.5%) 545 (77.2%) 485 (76.3%) 339 (76.7%) 484 (77.9%) 460 (72.9%) 370 (73.7%) 414 (73.1%) 1415 (76.5%) 1052 (77.6%) 1443 (76.2%)
factor(newimi)
0 416 (76.2%) 295 (78.7%) 530 (81.2%) 472 (81.2%) 318 (79.3%) 463 (81.4%) 435 (76.9%) 351 (78.3%) 393 (78.8%) 1323 (78.1%) 964 (78.8%) 1386 (80.5%)
1 130 (23.8%) 80 (21.3%) 123 (18.8%) 109 (18.8%) 83 (20.7%) 106 (18.6%) 131 (23.1%) 97 (21.7%) 106 (21.2%) 370 (21.9%) 260 (21.2%) 335 (19.5%)
Missing 36 (6.2%) 36 (8.8%) 53 (7.5%) 55 (8.6%) 41 (9.3%) 52 (8.4%) 65 (10.3%) 54 (10.8%) 67 (11.8%) 156 (8.4%) 131 (9.7%) 172 (9.1%)
table1(~ factor(CM) + factor(CMDP) + factor(Parity) | Tx, data=cow, na.is.category = F)
C_SDCT
(N=536)
A_SDCT
(N=394)
BDCT
(N=555)
Overall
(N=1485)
factor(CM)
0 472 (88.1%) 346 (87.8%) 464 (83.6%) 1282 (86.3%)
1 64 (11.9%) 48 (12.2%) 91 (16.4%) 203 (13.7%)
factor(CMDP)
0 534 (99.6%) 391 (99.2%) 554 (99.8%) 1479 (99.6%)
1 2 (0.373%) 3 (0.761%) 1 (0.180%) 6 (0.404%)
factor(Parity)
1 241 (45.0%) 167 (42.4%) 218 (39.3%) 626 (42.2%)
2 141 (26.3%) 123 (31.2%) 170 (30.6%) 434 (29.2%)
3 154 (28.7%) 104 (26.4%) 167 (30.1%) 425 (28.6%)
table1(~ factor(CM) + factor(CMDP) | my3*Tx, data=cow, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=171)
A_SDCT
(N=121)
BDCT
(N=203)
C_SDCT
(N=182)
A_SDCT
(N=127)
BDCT
(N=186)
C_SDCT
(N=183)
A_SDCT
(N=146)
BDCT
(N=166)
C_SDCT
(N=536)
A_SDCT
(N=394)
BDCT
(N=555)
factor(CM)
0 154 (90.1%) 102 (84.3%) 166 (81.8%) 157 (86.3%) 113 (89.0%) 161 (86.6%) 161 (88.0%) 131 (89.7%) 137 (82.5%) 472 (88.1%) 346 (87.8%) 464 (83.6%)
1 17 (9.94%) 19 (15.7%) 37 (18.2%) 25 (13.7%) 14 (11.0%) 25 (13.4%) 22 (12.0%) 15 (10.3%) 29 (17.5%) 64 (11.9%) 48 (12.2%) 91 (16.4%)
factor(CMDP)
0 170 (99.4%) 121 (100%) 203 (100%) 182 (100%) 127 (100%) 186 (100%) 182 (99.5%) 143 (97.9%) 165 (99.4%) 534 (99.6%) 391 (99.2%) 554 (99.8%)
1 1 (0.585%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 1 (0.546%) 3 (2.05%) 1 (0.602%) 2 (0.373%) 3 (0.761%) 1 (0.180%)
cow %>% tabyl(source,my3) %>% adorn_totals("row") %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 1) %>%
  adorn_ns %>%
  adorn_title
##                    my3                               
##  source Low production Mid production High production
##      CA    48.9% (134)    35.0%  (96)     16.1%  (44)
##      US    29.8% (361)    32.9% (399)     37.2% (451)
##   Total    33.3% (495)    33.3% (495)     33.3% (495)
cow %>% tabyl(FARMID,my3) %>% adorn_totals("row") %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 1) %>%
  adorn_ns %>%
  adorn_title
##                    my3                               
##  FARMID Low production Mid production High production
##       1    56.6%  (43)    31.6%  (24)     11.8%   (9)
##       2    31.7%  (26)    28.0%  (23)     40.2%  (33)
##       3    20.5%  (54)    41.4% (109)     38.0% (100)
##       4    18.3%  (71)    29.1% (113)     52.6% (204)
##       5    60.7% (147)    30.2%  (73)      9.1%  (22)
##       6     6.4%   (3)    38.3%  (18)     55.3%  (26)
##       7    15.0%  (17)    34.5%  (39)     50.4%  (57)
##       8    72.2%  (13)    16.7%   (3)     11.1%   (2)
##       9    76.0%  (19)    24.0%   (6)      0.0%   (0)
##      10    17.4%   (4)    60.9%  (14)     21.7%   (5)
##      11    50.0%  (11)    31.8%   (7)     18.2%   (4)
##      12    78.9%  (15)    15.8%   (3)      5.3%   (1)
##      13    69.0%  (20)    27.6%   (8)      3.4%   (1)
##      14    36.8%  (28)    34.2%  (26)     28.9%  (22)
##      15    31.2%   (5)    43.8%   (7)     25.0%   (4)
##      16    41.3%  (19)    47.8%  (22)     10.9%   (5)
##   Total    33.3% (495)    33.3% (495)     33.3% (495)

1st lactation cows are over-represented in the Algorithm cows, probably due to

Extract crude frequencies

cm.freq.tx <- cow %>% group_by(Tx) %>% summarise(
  n.cm.0 = sum(CM==0),
  n.cm.1 = sum(CM==1),
  p.cm.1 = n.cm.1/(n.cm.1+n.cm.0),
  cm = paste0(n.cm.1, "/" ,n.cm.0 + n.cm.1, " (",100*round(p.cm.1,4),"%)")
)
cm.freq.tx$my3 <- "All production levels"
cm.freq.my3 <- cow %>% group_by(my3,Tx) %>% summarise(
  n.cm.0 = sum(CM==0),
  n.cm.1 = sum(CM==1),
  p.cm.1 = n.cm.1/(n.cm.1+n.cm.0),
  cm = paste0(n.cm.1, "/" ,n.cm.0 + n.cm.1, " (",100*round(p.cm.1,4),"%)")
)

cm.freq <- rbind(cm.freq.tx,cm.freq.my3) %>% select(my3,Tx,cm)
cm.freq$risk <- cm.freq$cm
cm.freq$outcome <- "Clinical mastitis"
cm.freq$cm <- NULL

#Cull
Cull2.freq.tx <- cow %>% group_by(Tx) %>% summarise(
  n.Cull2.0 = sum(Cull2==0),
  n.Cull2.1 = sum(Cull2==1),
  p.Cull2.1 = n.Cull2.1/(n.Cull2.1+n.Cull2.0),
  Cull2 = paste0(n.Cull2.1, "/" ,n.Cull2.0 + n.Cull2.1, " (",100*round(p.Cull2.1,4),"%)")
)
Cull2.freq.tx$my3 <- "All production levels"
Cull2.freq.my3 <- cow %>% group_by(my3,Tx) %>% summarise(
  n.Cull2.0 = sum(Cull2==0),
  n.Cull2.1 = sum(Cull2==1),
  p.Cull2.1 = n.Cull2.1/(n.Cull2.1+n.Cull2.0),
  Cull2 = paste0(n.Cull2.1, "/" ,n.Cull2.0 + n.Cull2.1, " (",100*round(p.Cull2.1,4),"%)")
)

Cull2.freq <- rbind(Cull2.freq.tx,Cull2.freq.my3) %>% select(my3,Tx,Cull2)
Cull2.freq$risk <- Cull2.freq$Cull2
Cull2.freq$outcome <- "Cull"
Cull2.freq$Cull2 <- NULL


imi.freq.tx <- qtr %>% group_by(Tx) %>% summarise(
  n.imipc.0 = sum(imipc==0),
  n.imipc.1 = sum(imipc==1),
  p.imipc.1 = n.imipc.1/(n.imipc.1+n.imipc.0),
  imipc = paste0(n.imipc.1, "/" ,n.imipc.0 + n.imipc.1, " (",100*round(p.imipc.1,4),"%)"),
  n.newimi.0 = sum(newimi==0,na.rm=T),
  n.newimi.1 = sum(newimi==1,na.rm=T),
  p.newimi.1 = n.newimi.1/(n.newimi.1+n.newimi.0),
  newimi = paste0(n.newimi.1, "/" ,n.newimi.0 + n.newimi.1, " (",100*round(p.newimi.1,4),"%)"),
  n.cure.0 = sum(cure==0,na.rm=T),
  n.cure.1 = sum(cure==1,na.rm=T),
  p.cure.1 = n.cure.1/(n.cure.1+n.cure.0),
  imicure = paste0(n.cure.1, "/" ,n.cure.0 + n.cure.1, " (",100*round(p.cure.1,4),"%)"),
)

imi.freq.tx$my3 <- "All production levels"

imi.freq.my3 <- qtr %>% group_by(my3,Tx) %>% summarise(
  n.imipc.0 = sum(imipc==0),
  n.imipc.1 = sum(imipc==1),
  p.imipc.1 = n.imipc.1/(n.imipc.1+n.imipc.0),
  imipc = paste0(n.imipc.1, "/" ,n.imipc.0 + n.imipc.1, " (",100*round(p.imipc.1,4),"%)"),
  n.newimi.0 = sum(newimi==0,na.rm=T),
  n.newimi.1 = sum(newimi==1,na.rm=T),
  p.newimi.1 = n.newimi.1/(n.newimi.1+n.newimi.0),
  newimi = paste0(n.newimi.1, "/" ,n.newimi.0 + n.newimi.1, " (",100*round(p.newimi.1,4),"%)"),
  n.cure.0 = sum(cure==0,na.rm=T),
  n.cure.1 = sum(cure==1,na.rm=T),
  p.cure.1 = n.cure.1/(n.cure.1+n.cure.0),
  imicure = paste0(n.cure.1, "/" ,n.cure.0 + n.cure.1, " (",100*round(p.cure.1,4),"%)"),
)

colnames(imi.freq.tx)
##  [1] "Tx"         "n.imipc.0"  "n.imipc.1"  "p.imipc.1"  "imipc"     
##  [6] "n.newimi.0" "n.newimi.1" "p.newimi.1" "newimi"     "n.cure.0"  
## [11] "n.cure.1"   "p.cure.1"   "imicure"    "my3"
colnames(imi.freq.my3)
##  [1] "my3"        "Tx"         "n.imipc.0"  "n.imipc.1"  "p.imipc.1" 
##  [6] "imipc"      "n.newimi.0" "n.newimi.1" "p.newimi.1" "newimi"    
## [11] "n.cure.0"   "n.cure.1"   "p.cure.1"   "imicure"
imi.freq <- rbind(imi.freq.tx,imi.freq.my3) %>% select(my3,Tx,imipc,newimi,imicure) %>% pivot_longer(
  cols = imipc:imicure,
  names_to = "outcome",
  values_to = "risk"
)

freq.tab <- rbind(imi.freq,cm.freq,Cull2.freq) %>% arrange(outcome,my3)

write.csv(freq.tab,"freq.table.csv")

Analysis

Post calving clinical mastitis

library(survival)
library(survminer)

KM <- survfit(Surv(CMTAR, CM) ~ 1, data = cow)
cmtable <- summary(KM, times = c(30,60,90,120*(1:10)))
data.frame(
  days = cmtable[["time"]],
  incidence = (1 - cmtable[["surv"]])*100
)
##   days incidence
## 1   30  3.759954
## 2   60  7.614181
## 3   90 10.809019
## 4  120 14.249354
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Comparison of US and Canadian clinical mastitis incidence

KM <- survfit(Surv(CMTAR, CM) ~ source, data = cow)

ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow)
#summary(KM)
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Clinical mastitis - all production groups combined

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow)
km.all <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Low production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="Low production"))
km.low <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "Low production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Mid production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="Mid production"))
km.mid <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "Mid production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

High production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="High production"))
km.high <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "High production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

legend.cm <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "bottom",
           title = "High production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Export images

splots <- list(km.low,
               km.mid,
               km.high)
tiff("KM.tiff", units="in", width=13*.7, height=4*.7, res=300)
arrange_ggsurvplots(splots,ncol=3,nrow=1)
dev.off()
## quartz_off_screen 
##                 2
arrange_ggsurvplots(splots,ncol=3,nrow=1)

tiff("legend.tiff", units="in", width=6, height=3, res=300)
legend.cm
dev.off()
## quartz_off_screen 
##                 2

Cox regression

All production groups pooled together

library(broom)
cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")

SR <- coxph(Surv(CMTAR, CM) ~ Tx + my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(SR)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(CMTAR, CM)
##                Df  Chisq Pr(>Chisq)  
## Tx              2 4.3132    0.11572  
## my3             2 0.3167    0.85355  
## factor(Parity)  2 7.2864    0.02617 *
## DOSCC           1 1.1321    0.28732  
## factor(source)  1 1.1883    0.27567  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(SR)
## Call:
## coxph(formula = Surv(CMTAR, CM) ~ Tx + my3 + factor(Parity) + 
##     DOSCC + factor(source), data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 203 
##    (1 observation deleted due to missingness)
## 
##                        coef exp(coef) se(coef) robust se      z Pr(>|z|)   
## TxC_SDCT           -0.31922   0.72672  0.16346   0.17060 -1.871  0.06132 . 
## TxA_SDCT           -0.22858   0.79567  0.18654   0.14708 -1.554  0.12016   
## my3Mid production  -0.02463   0.97567  0.17890   0.23770 -0.104  0.91746   
## my3High production  0.04946   1.05070  0.18300   0.30945  0.160  0.87302   
## factor(Parity)2     0.31451   1.36959  0.17436   0.11654  2.699  0.00696 **
## factor(Parity)3     0.28821   1.33403  0.17961   0.23617  1.220  0.22235   
## DOSCC               0.10164   1.10698  0.06410   0.09552  1.064  0.28732   
## factor(source)US   -0.25687   0.77347  0.17972   0.23564 -1.090  0.27567   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT              0.7267     1.3760    0.5202     1.015
## TxA_SDCT              0.7957     1.2568    0.5964     1.062
## my3Mid production     0.9757     1.0249    0.6123     1.555
## my3High production    1.0507     0.9517    0.5729     1.927
## factor(Parity)2       1.3696     0.7301    1.0899     1.721
## factor(Parity)3       1.3340     0.7496    0.8397     2.119
## DOSCC                 1.1070     0.9034    0.9180     1.335
## factor(source)US      0.7735     1.2929    0.4874     1.227
## 
## Concordance= 0.581  (se = 0.031 )
## Likelihood ratio test= 16.62  on 8 df,   p=0.03
## Wald test            = 52.13  on 8 df,   p=2e-08
## Score (logrank) test = 16.81  on 8 df,   p=0.03,   Robust = 10.38  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).
cm.main <- tidy(SR,conf.int = T,exp=T)[1:2,] %>% select(term,estimate,conf.low,conf.high)
cm.main <- cm.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

cm.main$production <- "All production levels"
cm.main$contrast <- "none"
cm.main$outcome <- "Clinical mastitis"

Model diagnostics

SR <- cox.zph(SR)
SR
##                chisq df    p
## Tx             3.283  2 0.19
## my3            0.799  2 0.67
## factor(Parity) 2.112  2 0.35
## DOSCC          0.959  1 0.33
## factor(source) 1.524  1 0.22
## GLOBAL         9.887  8 0.27
ggcoxzph(SR)

Interaction model

CPH <- coxph(Surv(CMTAR, CM) ~ Tx*my3 + Parity + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(CMTAR, CM)
##                Df   Chisq Pr(>Chisq)  
## Tx              2  9.0584    0.01079 *
## my3             2  0.0731    0.96412  
## Parity          2  6.3562    0.04166 *
## DOSCC           1  1.2109    0.27116  
## factor(source)  1  1.5412    0.21444  
## Tx:my3          4 11.8755    0.01830 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(CPH)
## Call:
## coxph(formula = Surv(CMTAR, CM) ~ Tx + my3 + Parity + DOSCC + 
##     factor(source) + Tx:my3, data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 203 
##    (1 observation deleted due to missingness)
## 
##                                 coef exp(coef) se(coef) robust se      z
## TxC_SDCT                    -0.65608   0.51888  0.29329   0.43924 -1.494
## TxA_SDCT                    -0.01085   0.98921  0.29136   0.26769 -0.041
## my3Mid production           -0.19858   0.81989  0.26454   0.32092 -0.619
## my3High production           0.08911   1.09320  0.25892   0.40401  0.221
## Parity2                      0.30233   1.35301  0.17451   0.12005  2.518
## Parity3                      0.27770   1.32009  0.17976   0.23911  1.161
## DOSCC                        0.10641   1.11227  0.06430   0.09670  1.100
## factor(source)US            -0.29858   0.74187  0.18109   0.24051 -1.241
## TxC_SDCT:my3Mid production   0.67550   1.96501  0.40779   0.44977  1.502
## TxA_SDCT:my3Mid production  -0.13496   0.87375  0.43753   0.38804 -0.348
## TxC_SDCT:my3High production  0.32126   1.37886  0.40764   0.51713  0.621
## TxA_SDCT:my3High production -0.47544   0.62161  0.42788   0.39897 -1.192
##                             Pr(>|z|)  
## TxC_SDCT                      0.1353  
## TxA_SDCT                      0.9677  
## my3Mid production             0.5361  
## my3High production            0.8254  
## Parity2                       0.0118 *
## Parity3                       0.2455  
## DOSCC                         0.2712  
## factor(source)US              0.2144  
## TxC_SDCT:my3Mid production    0.1331  
## TxA_SDCT:my3Mid production    0.7280  
## TxC_SDCT:my3High production   0.5344  
## TxA_SDCT:my3High production   0.2334  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                             exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT                       0.5189     1.9272    0.2194     1.227
## TxA_SDCT                       0.9892     1.0109    0.5854     1.672
## my3Mid production              0.8199     1.2197    0.4371     1.538
## my3High production             1.0932     0.9147    0.4952     2.413
## Parity2                        1.3530     0.7391    1.0693     1.712
## Parity3                        1.3201     0.7575    0.8262     2.109
## DOSCC                          1.1123     0.8991    0.9202     1.344
## factor(source)US               0.7419     1.3479    0.4630     1.189
## TxC_SDCT:my3Mid production     1.9650     0.5089    0.8138     4.745
## TxA_SDCT:my3Mid production     0.8737     1.1445    0.4084     1.869
## TxC_SDCT:my3High production    1.3789     0.7252    0.5004     3.799
## TxA_SDCT:my3High production    0.6216     1.6087    0.2844     1.359
## 
## Concordance= 0.591  (se = 0.034 )
## Likelihood ratio test= 21.68  on 12 df,   p=0.04
## Wald test            = 83.14  on 12 df,   p=1e-12
## Score (logrank) test = 21.85  on 12 df,   p=0.04,   Robust = 14.02  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).
emmeans(CPH,pairwise ~ Tx | my3,adjust = "none",type = "response") %>% confint
## $emmeans
## my3 = Low production:
##  Tx     response    SE  df asymp.LCL asymp.UCL
##  BDCT      1.675 0.658 Inf     0.775      3.62
##  C_SDCT    0.869 0.571 Inf     0.240      3.15
##  A_SDCT    1.657 0.946 Inf     0.541      5.07
## 
## my3 = Mid production:
##  Tx     response    SE  df asymp.LCL asymp.UCL
##  BDCT      1.373 0.787 Inf     0.446      4.22
##  C_SDCT    1.400 0.718 Inf     0.512      3.82
##  A_SDCT    1.187 0.467 Inf     0.549      2.57
## 
## my3 = High production:
##  Tx     response    SE  df asymp.LCL asymp.UCL
##  BDCT      1.831 1.009 Inf     0.621      5.39
##  C_SDCT    1.310 0.913 Inf     0.334      5.13
##  A_SDCT    1.126 0.491 Inf     0.479      2.65
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale 
## 
## $contrasts
## my3 = Low production:
##  contrast        ratio    SE  df asymp.LCL asymp.UCL
##  BDCT / C_SDCT   1.927 0.847 Inf     0.815      4.56
##  BDCT / A_SDCT   1.011 0.271 Inf     0.598      1.71
##  C_SDCT / A_SDCT 0.525 0.185 Inf     0.263      1.05
## 
## my3 = Mid production:
##  contrast        ratio    SE  df asymp.LCL asymp.UCL
##  BDCT / C_SDCT   0.981 0.193 Inf     0.667      1.44
##  BDCT / A_SDCT   1.157 0.282 Inf     0.718      1.86
##  C_SDCT / A_SDCT 1.180 0.277 Inf     0.744      1.87
## 
## my3 = High production:
##  contrast        ratio    SE  df asymp.LCL asymp.UCL
##  BDCT / C_SDCT   1.398 0.430 Inf     0.764      2.56
##  BDCT / A_SDCT   1.626 0.428 Inf     0.971      2.72
##  C_SDCT / A_SDCT 1.164 0.486 Inf     0.513      2.64
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the log scale

Model diagnostics

SR <- cox.zph(CPH)
SR
##                 chisq df    p
## Tx              3.243  2 0.20
## my3             0.790  2 0.67
## Parity          2.088  2 0.35
## DOSCC           0.981  1 0.32
## factor(source)  1.546  1 0.21
## Tx:my3          0.103  4 1.00
## GLOBAL         17.871 12 0.12
ggcoxzph(SR)

cow$Tx <- fct_relevel(cow$Tx,"C_SDCT","A_SDCT","BDCT")
CPH <- coxph(Surv(CMTAR, CM) ~ Tx*my3 + strata(Parity) + DOSCC + factor(source)  + cluster(FARMID), data=cow)

out <- emmeans(CPH,pairwise ~ Tx | my3,adjust = "none",type = "response") %>% confint

CPH2 <- coxph(Surv(CMTAR, CM) ~ cult*my3 + alg*my3 + strata(Parity) + DOSCC + factor(source)  + cluster(FARMID), data=cow)
int.p <- car::Anova(CPH2) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "Clinical mastitis"
int.p.cm <- int.p

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["ratio"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT / A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Clinical mastitis"
tab.cm <- tab
tab.cm
##        production      contrast            estimate           outcome
## 1  Low production C_SDCT / BDCT 0.52 (0.22 to 1.23) Clinical mastitis
## 2  Low production A_SDCT / BDCT 0.99 (0.59 to 1.66) Clinical mastitis
## 3  Mid production C_SDCT / BDCT  1.02 (0.69 to 1.5) Clinical mastitis
## 4  Mid production A_SDCT / BDCT 0.87 (0.54 to 1.39) Clinical mastitis
## 5 High production C_SDCT / BDCT 0.72 (0.39 to 1.31) Clinical mastitis
## 6 High production A_SDCT / BDCT 0.62 (0.37 to 1.03) Clinical mastitis

Culling 1-120 DIM

cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")

KM <- survfit(Surv(Cull2TAR, Cull2) ~ 1, data = cow)
cmtable <- summary(KM, times = c(30,60,90,120*(1:10)))
data.frame(
  days = cmtable[["time"]],
  incidence = (1 - cmtable[["surv"]])*100
)
##   days incidence
## 1   30  3.771044
## 2   60  6.127946
## 3   90  7.946128
## 4  120  9.966330
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
summary(KM)
## Call: survfit(formula = Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
## 
##                 Tx=BDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     3    555       1    0.998 0.00180        0.995        1.000
##     4    554       1    0.996 0.00254        0.991        1.000
##     5    553       1    0.995 0.00311        0.989        1.000
##     6    552       2    0.991 0.00401        0.983        0.999
##     7    550       2    0.987 0.00474        0.978        0.997
##     9    548       1    0.986 0.00506        0.976        0.996
##    11    547       2    0.982 0.00565        0.971        0.993
##    13    545       2    0.978 0.00617        0.966        0.991
##    15    543       1    0.977 0.00642        0.964        0.989
##    19    542       2    0.973 0.00688        0.960        0.987
##    21    540       2    0.969 0.00731        0.955        0.984
##    22    538       2    0.966 0.00772        0.951        0.981
##    23    536       2    0.962 0.00810        0.946        0.978
##    27    534       2    0.959 0.00846        0.942        0.975
##    28    532       2    0.955 0.00880        0.938        0.972
##    29    530       1    0.953 0.00897        0.936        0.971
##    30    529       1    0.951 0.00913        0.934        0.969
##    36    528       2    0.948 0.00945        0.929        0.966
##    38    526       1    0.946 0.00960        0.927        0.965
##    39    525       1    0.944 0.00975        0.925        0.963
##    41    524       2    0.941 0.01004        0.921        0.960
##    49    522       2    0.937 0.01032        0.917        0.957
##    54    520       1    0.935 0.01045        0.915        0.956
##    61    519       1    0.933 0.01059        0.913        0.954
##    64    518       1    0.932 0.01072        0.911        0.953
##    65    517       1    0.930 0.01085        0.909        0.951
##    72    516       1    0.928 0.01098        0.907        0.950
##    75    515       1    0.926 0.01110        0.905        0.948
##    78    514       1    0.924 0.01123        0.903        0.947
##    79    513       1    0.923 0.01135        0.901        0.945
##    83    512       1    0.921 0.01147        0.899        0.943
##    93    511       2    0.917 0.01170        0.894        0.940
##    99    509       2    0.914 0.01193        0.890        0.937
##   101    507       1    0.912 0.01204        0.888        0.936
##   112    506       1    0.910 0.01215        0.886        0.934
##   115    505       1    0.908 0.01226        0.884        0.932
##   116    504       1    0.906 0.01237        0.882        0.931
##   117    503       1    0.905 0.01248        0.880        0.929
##   118    502       1    0.903 0.01258        0.878        0.928
## 
##                 Tx=C_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    536       1    0.998 0.00186        0.994        1.000
##     2    535       2    0.994 0.00322        0.988        1.000
##     3    533       2    0.991 0.00415        0.983        0.999
##     4    531       2    0.987 0.00490        0.977        0.997
##     5    529       1    0.985 0.00524        0.975        0.995
##     7    528       1    0.983 0.00555        0.972        0.994
##     9    527       1    0.981 0.00584        0.970        0.993
##    11    526       1    0.979 0.00612        0.968        0.992
##    13    525       1    0.978 0.00639        0.965        0.990
##    21    524       1    0.976 0.00664        0.963        0.989
##    23    523       1    0.974 0.00689        0.960        0.987
##    24    522       1    0.972 0.00712        0.958        0.986
##    33    521       1    0.970 0.00735        0.956        0.985
##    34    520       1    0.968 0.00757        0.954        0.983
##    36    519       1    0.966 0.00778        0.951        0.982
##    37    518       2    0.963 0.00819        0.947        0.979
##    38    516       1    0.961 0.00838        0.945        0.977
##    42    515       1    0.959 0.00857        0.942        0.976
##    43    514       1    0.957 0.00875        0.940        0.974
##    44    513       2    0.953 0.00911        0.936        0.971
##    45    511       1    0.951 0.00928        0.933        0.970
##    49    510       1    0.950 0.00945        0.931        0.968
##    57    509       1    0.948 0.00961        0.929        0.967
##    58    508       1    0.946 0.00977        0.927        0.965
##    63    507       1    0.944 0.00993        0.925        0.964
##    69    506       1    0.942 0.01008        0.923        0.962
##    70    505       1    0.940 0.01023        0.920        0.961
##    74    504       1    0.938 0.01038        0.918        0.959
##    75    503       1    0.937 0.01053        0.916        0.957
##    76    502       1    0.935 0.01067        0.914        0.956
##    79    501       1    0.933 0.01081        0.912        0.954
##    84    500       1    0.931 0.01095        0.910        0.953
##    89    499       1    0.929 0.01109        0.908        0.951
##    91    498       2    0.925 0.01135        0.903        0.948
##    96    496       2    0.922 0.01161        0.899        0.945
##    99    494       1    0.920 0.01173        0.897        0.943
##   111    493       2    0.916 0.01198        0.893        0.940
##   114    491       1    0.914 0.01210        0.891        0.938
##   115    490       1    0.912 0.01222        0.889        0.937
##   116    489       2    0.909 0.01245        0.885        0.933
##   117    487       2    0.905 0.01267        0.880        0.930
##   119    485       1    0.903 0.01278        0.878        0.928
## 
##                 Tx=A_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    394       2    0.995 0.00358        0.988        1.000
##     2    392       1    0.992 0.00438        0.984        1.000
##     3    391       1    0.990 0.00505        0.980        1.000
##     4    390       1    0.987 0.00564        0.976        0.998
##     6    389       1    0.985 0.00617        0.973        0.997
##     9    388       1    0.982 0.00666        0.969        0.995
##    13    387       3    0.975 0.00792        0.959        0.990
##    18    384       1    0.972 0.00830        0.956        0.988
##    19    383       1    0.970 0.00866        0.953        0.987
##    21    382       1    0.967 0.00900        0.950        0.985
##    24    381       1    0.964 0.00933        0.946        0.983
##    33    380       1    0.962 0.00964        0.943        0.981
##    36    379       1    0.959 0.00994        0.940        0.979
##    41    378       1    0.957 0.01024        0.937        0.977
##    42    377       1    0.954 0.01052        0.934        0.975
##    44    376       1    0.952 0.01079        0.931        0.973
##    45    375       1    0.949 0.01106        0.928        0.971
##    51    374       1    0.947 0.01132        0.925        0.969
##    52    373       1    0.944 0.01157        0.922        0.967
##    54    372       1    0.942 0.01181        0.919        0.965
##    55    371       1    0.939 0.01205        0.916        0.963
##    58    370       1    0.937 0.01228        0.913        0.961
##    60    369       1    0.934 0.01251        0.910        0.959
##    62    368       1    0.931 0.01273        0.907        0.957
##    74    367       1    0.929 0.01294        0.904        0.955
##    77    366       2    0.924 0.01336        0.898        0.950
##    81    364       1    0.921 0.01356        0.895        0.948
##    85    363       1    0.919 0.01376        0.892        0.946
##    86    362       1    0.916 0.01396        0.889        0.944
##    87    361       1    0.914 0.01415        0.886        0.942
##    88    360       1    0.911 0.01433        0.884        0.940
##    90    359       1    0.909 0.01452        0.881        0.938
##    97    358       1    0.906 0.01470        0.878        0.935
##    99    357       1    0.904 0.01487        0.875        0.933
##   102    356       1    0.901 0.01505        0.872        0.931
##   105    355       1    0.898 0.01522        0.869        0.929
##   111    354       1    0.896 0.01538        0.866        0.927
##   116    353       1    0.893 0.01555        0.863        0.924
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Culling - all production groups combined

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
km.all <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Low production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="Low production"))
km.low <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "Low production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Mid production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="Mid production"))
km.mid <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "Mid production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

High production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="High production"))
km.high <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),legend.labs = c("Blanket DCT","Culture SDCT","Algorithm SDCT"),
           legend = "none",
           title = "High production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Export images

splots <- list(km.low,
               km.mid,
               km.high)
tiff("KM2.tiff", units="in", width=13*.7, height=4*.7, res=300)
arrange_ggsurvplots(splots,ncol=3,nrow=1)
dev.off()
## quartz_off_screen 
##                 2
arrange_ggsurvplots(splots,ncol=3,nrow=1)

Cox regression

All production groups pooled together

cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")
SR <- coxph(Surv(Cull2TAR, Cull2) ~ Tx + my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(SR)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df   Chisq Pr(>Chisq)    
## Tx              2  0.2417   0.886185    
## my3             2  3.6772   0.159039    
## factor(Parity)  2 28.1800    7.6e-07 ***
## DOSCC           1 10.0458   0.001527 ** 
## factor(source)  1  5.3009   0.021314 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(SR)
## Call:
## coxph(formula = Surv(Cull2TAR, Cull2) ~ Tx + my3 + factor(Parity) + 
##     DOSCC + factor(source), data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 148 
##    (1 observation deleted due to missingness)
## 
##                        coef exp(coef) se(coef) robust se      z Pr(>|z|)    
## TxC_SDCT            0.01656   1.01669  0.19486   0.17473  0.095  0.92452    
## TxA_SDCT            0.05923   1.06101  0.21274   0.21555  0.275  0.78350    
## my3Mid production  -0.29969   0.74105  0.20648   0.23807 -1.259  0.20810    
## my3High production -0.36079   0.69712  0.21546   0.18815 -1.918  0.05517 .  
## factor(Parity)2     0.25084   1.28510  0.23463   0.18764  1.337  0.18129    
## factor(Parity)3     0.96662   2.62904  0.20835   0.20476  4.721 2.35e-06 ***
## DOSCC               0.12146   1.12915  0.07417   0.03832  3.170  0.00153 ** 
## factor(source)US    0.35858   1.43129  0.24290   0.15574  2.302  0.02131 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT              1.0167     0.9836    0.7219     1.432
## TxA_SDCT              1.0610     0.9425    0.6954     1.619
## my3Mid production     0.7410     1.3494    0.4647     1.182
## my3High production    0.6971     1.4345    0.4821     1.008
## factor(Parity)2       1.2851     0.7781    0.8896     1.856
## factor(Parity)3       2.6290     0.3804    1.7600     3.927
## DOSCC                 1.1291     0.8856    1.0474     1.217
## factor(source)US      1.4313     0.6987    1.0548     1.942
## 
## Concordance= 0.648  (se = 0.022 )
## Likelihood ratio test= 45.04  on 8 df,   p=4e-07
## Wald test            = 454.2  on 8 df,   p=<2e-16
## Score (logrank) test = 48.55  on 8 df,   p=8e-08,   Robust = 9.11  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).
cull.main <- tidy(SR,conf.int = T,exp=T)[1:2,] %>% select(term,estimate,conf.low,conf.high)
cull.main <- cull.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

cull.main$production <- "All production levels"
cull.main$contrast <- "none"
cull.main$outcome <- "Culling"

Model diagnostics

SR <- cox.zph(SR)
SR
##                chisq df    p
## Tx             1.403  2 0.50
## my3            2.390  2 0.30
## factor(Parity) 1.159  2 0.56
## DOSCC          0.772  1 0.38
## factor(source) 1.175  1 0.28
## GLOBAL         5.768  8 0.67
ggcoxzph(SR)

Interaction model

CPH <- coxph(Surv(Cull2TAR, Cull2) ~ Tx*my3 + Parity + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df   Chisq Pr(>Chisq)    
## Tx              2  3.0951   0.212771    
## my3             2 83.0898  < 2.2e-16 ***
## Parity          2 26.0526  2.202e-06 ***
## DOSCC           1 10.8066   0.001011 ** 
## factor(source)  1  4.9243   0.026481 *  
## Tx:my3          4  3.2706   0.513606    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(CPH)
## Call:
## coxph(formula = Surv(Cull2TAR, Cull2) ~ Tx + my3 + Parity + DOSCC + 
##     factor(source) + Tx:my3, data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 148 
##    (1 observation deleted due to missingness)
## 
##                                 coef exp(coef) se(coef) robust se      z
## TxC_SDCT                     0.02307   1.02334  0.29911   0.36641  0.063
## TxA_SDCT                     0.24488   1.27747  0.31050   0.22565  1.085
## my3Mid production           -0.13088   0.87732  0.32430   0.45901 -0.285
## my3High production          -0.33376   0.71622  0.35523   0.39258 -0.850
## Parity2                      0.24447   1.27694  0.23497   0.18706  1.307
## Parity3                      0.96071   2.61354  0.20869   0.21027  4.569
## DOSCC                        0.12487   1.13300  0.07423   0.03799  3.287
## factor(source)US             0.34701   1.41483  0.24425   0.15638  2.219
## TxC_SDCT:my3Mid production  -0.10820   0.89745  0.45934   0.62570 -0.173
## TxA_SDCT:my3Mid production  -0.46963   0.62524  0.50106   0.42943 -1.094
## TxC_SDCT:my3High production  0.08828   1.09229  0.48296   0.63324  0.139
## TxA_SDCT:my3High production -0.19083   0.82627  0.50445   0.63638 -0.300
##                             Pr(>|z|)    
## TxC_SDCT                     0.94980    
## TxA_SDCT                     0.27783    
## my3Mid production            0.77554    
## my3High production           0.39522    
## Parity2                      0.19126    
## Parity3                      4.9e-06 ***
## DOSCC                        0.00101 ** 
## factor(source)US             0.02648 *  
## TxC_SDCT:my3Mid production   0.86271    
## TxA_SDCT:my3Mid production   0.27413    
## TxC_SDCT:my3High production  0.88913    
## TxA_SDCT:my3High production  0.76428    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                             exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT                       1.0233     0.9772    0.4990     2.098
## TxA_SDCT                       1.2775     0.7828    0.8209     1.988
## my3Mid production              0.8773     1.1398    0.3568     2.157
## my3High production             0.7162     1.3962    0.3318     1.546
## Parity2                        1.2769     0.7831    0.8850     1.842
## Parity3                        2.6135     0.3826    1.7308     3.946
## DOSCC                          1.1330     0.8826    1.0517     1.221
## factor(source)US               1.4148     0.7068    1.0414     1.922
## TxC_SDCT:my3Mid production     0.8974     1.1143    0.2633     3.059
## TxA_SDCT:my3Mid production     0.6252     1.5994    0.2695     1.451
## TxC_SDCT:my3High production    1.0923     0.9155    0.3157     3.779
## TxA_SDCT:my3High production    0.8263     1.2103    0.2374     2.876
## 
## Concordance= 0.647  (se = 0.022 )
## Likelihood ratio test= 46.09  on 12 df,   p=7e-06
## Wald test            = 787.2  on 12 df,   p=<2e-16
## Score (logrank) test = 50.09  on 12 df,   p=1e-06,   Robust = 13.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).

Model diagnostics

SR <- cox.zph(CPH)
SR
##                chisq df    p
## Tx             1.414  2 0.49
## my3            2.378  2 0.30
## Parity         1.155  2 0.56
## DOSCC          0.754  1 0.39
## factor(source) 1.190  1 0.28
## Tx:my3         4.889  4 0.30
## GLOBAL         8.684 12 0.73
ggcoxzph(SR)

cow$Tx <- fct_relevel(cow$Tx,"C_SDCT","A_SDCT","BDCT")
CPH <- coxph(Surv(Cull2TAR, Cull2) ~ Tx*my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df   Chisq Pr(>Chisq)    
## Tx              2  3.0951   0.212771    
## my3             2 83.0898  < 2.2e-16 ***
## factor(Parity)  2 26.0526  2.202e-06 ***
## DOSCC           1 10.8066   0.001011 ** 
## factor(source)  1  4.9243   0.026481 *  
## Tx:my3          4  3.2706   0.513606    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
out <- emmeans(CPH,pairwise ~ Tx | my3,adjust = "none",type = "response") %>% confint

CPH2 <- coxph(Surv(Cull2TAR, Cull2) ~ cult*my3 + alg*my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
int.p <- car::Anova(CPH2) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "Culling"
int.p.cull <- int.p

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["ratio"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT / A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Culling"
tab.cull <- tab
tab.cull
##        production      contrast            estimate outcome
## 1  Low production C_SDCT / BDCT   1.02 (0.5 to 2.1) Culling
## 2  Low production A_SDCT / BDCT 1.28 (0.82 to 1.99) Culling
## 3  Mid production C_SDCT / BDCT 0.92 (0.49 to 1.73) Culling
## 4  Mid production A_SDCT / BDCT  0.8 (0.44 to 1.46) Culling
## 5 High production C_SDCT / BDCT 1.12 (0.45 to 2.79) Culling
## 6 High production A_SDCT / BDCT 1.06 (0.29 to 3.79) Culling

IMI post calving

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(imipc ~ Tx + my3 + Parity + factor(source) + DOSCC, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

imipc.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
imipc.main <- imipc.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

imipc.main$production <- "All production levels"
imipc.main$contrast <- "none"
imipc.main$outcome <- "IMI at calving"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(imipc ~ Tx*my3 + Parity + factor(source) + DOSCC, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

glm <- glm(imipc ~ alg*my3 + cult*my3 + Parity + factor(source) + DOSCC, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"))

int.p <- car::Anova(glm) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "imipc"
int.p.imipc <- int.p

summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ Tx * my3 + Parity + factor(source) + 
##     DOSCC, family = binomial(link = "identity"), data = qtr %>% 
##     filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                              Estimate   Std.err  Wald Pr(>|W|)   
## (Intercept)                  0.058980  0.059952 0.968  0.32522   
## TxA_SDCT                    -0.052961  0.026035 4.138  0.04193 * 
## TxBDCT                      -0.049438  0.037557 1.733  0.18807   
## my3Mid production           -0.019865  0.026733 0.552  0.45742   
## my3High production           0.012972  0.030122 0.185  0.66672   
## Parity2                      0.008496  0.022265 0.146  0.70279   
## Parity3                      0.018443  0.017062 1.168  0.27973   
## factor(source)US             0.056618  0.044750 1.601  0.20579   
## DOSCC                        0.027901  0.009242 9.113  0.00254 **
## TxA_SDCT:my3Mid production   0.062130  0.040739 2.326  0.12724   
## TxBDCT:my3Mid production     0.045203  0.036210 1.558  0.21190   
## TxA_SDCT:my3High production  0.034456  0.041533 0.688  0.40677   
## TxBDCT:my3High production    0.036414  0.033993 1.147  0.28408   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1  0.1438
## Number of clusters:   16  Maximum cluster size: 1270
emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.220 0.0283 Inf    0.1643     0.275
##  A_SDCT Low production   0.167 0.0306 Inf    0.1068     0.227
##  BDCT   Low production   0.170 0.0365 Inf    0.0988     0.242
##  C_SDCT Mid production   0.200 0.0255 Inf    0.1499     0.250
##  A_SDCT Mid production   0.209 0.0249 Inf    0.1602     0.258
##  BDCT   Mid production   0.196 0.0327 Inf    0.1315     0.260
##  C_SDCT High production  0.233 0.0212 Inf    0.1911     0.274
##  A_SDCT High production  0.214 0.0193 Inf    0.1763     0.252
##  BDCT   High production  0.220 0.0292 Inf    0.1625     0.277
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100

tab$outcome <- "IMI at calving"
tab.imipc <- tab
tab.imipc
##        production      contrast              estimate        outcome
## 1  Low production C_SDCT - BDCT  0.05 (-0.02 to 0.12) IMI at calving
## 2  Low production A_SDCT - BDCT     0 (-0.04 to 0.03) IMI at calving
## 3  Mid production C_SDCT - BDCT     0 (-0.05 to 0.05) IMI at calving
## 4  Mid production A_SDCT - BDCT  0.01 (-0.04 to 0.07) IMI at calving
## 5 High production C_SDCT - BDCT  0.01 (-0.02 to 0.05) IMI at calving
## 6 High production A_SDCT - BDCT -0.01 (-0.06 to 0.05) IMI at calving
tab.imipc
##        production      contrast              estimate        outcome
## 1  Low production C_SDCT - BDCT  0.05 (-0.02 to 0.12) IMI at calving
## 2  Low production A_SDCT - BDCT     0 (-0.04 to 0.03) IMI at calving
## 3  Mid production C_SDCT - BDCT     0 (-0.05 to 0.05) IMI at calving
## 4  Mid production A_SDCT - BDCT  0.01 (-0.04 to 0.07) IMI at calving
## 5 High production C_SDCT - BDCT  0.01 (-0.02 to 0.05) IMI at calving
## 6 High production A_SDCT - BDCT -0.01 (-0.06 to 0.05) IMI at calving

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(imipc ~ Tx*my3 + Parity + DOSCC + factor(source)  + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(imipc ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: imipc ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##     5106     5198    -2539     5078     5079 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -0.906 -0.598 -0.436 -0.239  4.829 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.392    0.626   
## Number of obs: 5093, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -1.9070     0.2908   -6.56  5.5e-11 ***
## TxA_SDCT                     -0.3099     0.1638   -1.89   0.0585 .  
## TxBDCT                       -0.2718     0.1395   -1.95   0.0513 .  
## my3Mid production            -0.2117     0.1473   -1.44   0.1505    
## my3High production           -0.1097     0.1514   -0.72   0.4686    
## Parity2                       0.1638     0.0868    1.89   0.0593 .  
## Parity3                       0.2391     0.0895    2.67   0.0075 ** 
## DOSCC                         0.0810     0.0335    2.42   0.0156 *  
## factor(source)US             -0.1493     0.3382   -0.44   0.6590    
## TxA_SDCT:my3Mid production    0.4109     0.2234    1.84   0.0658 .  
## TxBDCT:my3Mid production      0.2618     0.2006    1.31   0.1919    
## TxA_SDCT:my3High production   0.2094     0.2190    0.96   0.3390    
## TxBDCT:my3High production     0.2636     0.1991    1.32   0.1854    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: imipc
##                Chisq Df Pr(>Chisq)  
## Tx              1.86  2      0.395  
## my3             0.30  2      0.862  
## Parity          7.70  2      0.021 *
## DOSCC           5.84  1      0.016 *
## factor(source)  0.19  1      0.659  
## Tx:my3          4.45  4      0.348  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: imipc ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 5288
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0328 -0.6519 -0.4101 -0.0958  2.4775 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.0078   0.0883  
##  Residual             0.1620   0.4025  
## Number of obs: 5093, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  0.13198    0.04381    3.01
## TxA_SDCT                    -0.05075    0.02669   -1.90
## TxBDCT                      -0.04369    0.02260   -1.93
## my3Mid production           -0.03508    0.02405   -1.46
## my3High production          -0.01799    0.02515   -0.72
## Parity2                      0.02529    0.01412    1.79
## Parity3                      0.03795    0.01471    2.58
## DOSCC                        0.01320    0.00555    2.38
## factor(source)US            -0.00558    0.04782   -0.12
## TxA_SDCT:my3Mid production   0.06646    0.03636    1.83
## TxBDCT:my3Mid production     0.04117    0.03217    1.28
## TxA_SDCT:my3High production  0.03316    0.03585    0.93
## TxBDCT:my3High production    0.04203    0.03258    1.29
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: imipc
##                Chisq Df Pr(>Chisq)  
## Tx              1.91  2      0.385  
## my3             0.35  2      0.840  
## Parity          7.19  2      0.028 *
## DOSCC           5.65  1      0.017 *
## factor(source)  0.01  1      0.907  
## Tx:my3          4.33  4      0.363  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.184 0.0289 Inf    0.1340     0.247
##  A_SDCT Low production  0.142 0.0258 Inf    0.0985     0.200
##  BDCT   Low production  0.147 0.0239 Inf    0.1057     0.200
##  C_SDCT Mid production  0.154 0.0253 Inf    0.1110     0.211
##  A_SDCT Mid production  0.168 0.0287 Inf    0.1189     0.232
##  BDCT   Mid production  0.153 0.0251 Inf    0.1101     0.209
##  C_SDCT High production 0.168 0.0273 Inf    0.1211     0.229
##  A_SDCT High production 0.155 0.0266 Inf    0.1092     0.214
##  BDCT   High production 0.167 0.0275 Inf    0.1199     0.228
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT      0.04 (0 to 0.09)
## 2  Low production A_SDCT - BDCT -0.01 (-0.06 to 0.04)
## 3  Mid production C_SDCT - BDCT     0 (-0.04 to 0.05)
## 4  Mid production A_SDCT - BDCT  0.02 (-0.03 to 0.07)
## 5 High production C_SDCT - BDCT     0 (-0.04 to 0.05)
## 6 High production A_SDCT - BDCT -0.02 (-0.06 to 0.03)

IMI cure

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(cure ~ Tx + my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

imicure.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
imicure.main <- imicure.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

imicure.main$production <- "All production levels"
imicure.main$contrast <- "none"
imicure.main$outcome <- "IMI cure"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")

gee <- geeglm(cure ~ Tx*my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = cure ~ Tx * my3 + Parity + DOSCC + factor(source), 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(cure) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                             Estimate  Std.err   Wald Pr(>|W|)    
## (Intercept)                  1.00675  0.03928 657.00   <2e-16 ***
## TxA_SDCT                     0.01459  0.02525   0.33    0.563    
## TxBDCT                      -0.03046  0.02715   1.26    0.262    
## my3Mid production           -0.02144  0.02231   0.92    0.337    
## my3High production           0.02009  0.02314   0.75    0.385    
## Parity2                     -0.03668  0.01403   6.83    0.009 ** 
## Parity3                     -0.04331  0.03168   1.87    0.172    
## DOSCC                       -0.01202  0.00617   3.80    0.051 .  
## factor(source)US            -0.05330  0.03086   2.98    0.084 .  
## TxA_SDCT:my3Mid production   0.02096  0.03195   0.43    0.512    
## TxBDCT:my3Mid production     0.05894  0.02763   4.55    0.033 *  
## TxA_SDCT:my3High production -0.03436  0.04932   0.49    0.486    
## TxBDCT:my3High production   -0.02447  0.03060   0.64    0.424    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.287
## Number of clusters:   16  Maximum cluster size: 432
glm <- glm(cure ~ alg*my3 + cult*my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"))
int.p <- car::Anova(glm) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "imicure"
int.p.cure <- int.p


emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.898 0.0264 Inf     0.846     0.950
##  A_SDCT Low production   0.913 0.0324 Inf     0.849     0.976
##  BDCT   Low production   0.868 0.0389 Inf     0.791     0.944
##  C_SDCT Mid production   0.877 0.0238 Inf     0.830     0.923
##  A_SDCT Mid production   0.912 0.0143 Inf     0.884     0.940
##  BDCT   Mid production   0.905 0.0172 Inf     0.871     0.939
##  C_SDCT High production  0.918 0.0131 Inf     0.893     0.944
##  A_SDCT High production  0.898 0.0258 Inf     0.848     0.949
##  BDCT   High production  0.863 0.0163 Inf     0.831     0.895
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100

tab$outcome <- "IMI cure"
tab.imicure <- tab
tab.imicure
##        production      contrast              estimate  outcome
## 1  Low production C_SDCT - BDCT  0.03 (-0.02 to 0.08) IMI cure
## 2  Low production A_SDCT - BDCT  0.05 (-0.02 to 0.11) IMI cure
## 3  Mid production C_SDCT - BDCT -0.03 (-0.09 to 0.03) IMI cure
## 4  Mid production A_SDCT - BDCT  0.01 (-0.02 to 0.03) IMI cure
## 5 High production C_SDCT - BDCT   0.05 (0.02 to 0.09) IMI cure
## 6 High production A_SDCT - BDCT  0.04 (-0.02 to 0.09) IMI cure

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(cure ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(cure ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: cure ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##      875      946     -424      847     1171 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -3.961  0.260  0.333  0.401  0.582 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.328    0.573   
## Number of obs: 1185, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                   2.8963     0.6433    4.50  6.7e-06 ***
## TxA_SDCT                      0.1204     0.4931    0.24    0.807    
## TxBDCT                       -0.1422     0.3834   -0.37    0.711    
## my3Mid production            -0.1459     0.4015   -0.36    0.716    
## my3High production            0.3537     0.4223    0.84    0.402    
## Parity2                      -0.3767     0.2213   -1.70    0.089 .  
## Parity3                      -0.2253     0.2349   -0.96    0.337    
## DOSCC                        -0.0637     0.0912   -0.70    0.485    
## factor(source)US             -0.1004     0.4187   -0.24    0.810    
## TxA_SDCT:my3Mid production   -0.0131     0.6254   -0.02    0.983    
## TxBDCT:my3Mid production      0.5154     0.5397    0.96    0.340    
## TxA_SDCT:my3High production  -0.2580     0.6272   -0.41    0.681    
## TxBDCT:my3High production    -0.3065     0.5244   -0.58    0.559    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## optimizer (Nelder_Mead) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 0.00282032 (tol = 0.002, component 1)
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: cure
##                Chisq Df Pr(>Chisq)
## Tx              0.39  2       0.82
## my3             0.65  2       0.72
## Parity          2.93  2       0.23
## DOSCC           0.49  1       0.48
## factor(source)  0.06  1       0.81
## Tx:my3          2.70  4       0.61
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: cure ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 748
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -2.949  0.201  0.326  0.446  0.688 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.00297  0.0545  
##  Residual             0.10365  0.3219  
## Number of obs: 1185, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  0.95152    0.06273   15.17
## TxA_SDCT                     0.01292    0.05078    0.25
## TxBDCT                      -0.01566    0.04005   -0.39
## my3Mid production           -0.01576    0.04185   -0.38
## my3High production           0.03373    0.04168    0.81
## Parity2                     -0.03796    0.02299   -1.65
## Parity3                     -0.02184    0.02410   -0.91
## DOSCC                       -0.00613    0.00936   -0.65
## factor(source)US            -0.00602    0.03895   -0.15
## TxA_SDCT:my3Mid production  -0.00220    0.06520   -0.03
## TxBDCT:my3Mid production     0.05305    0.05537    0.96
## TxA_SDCT:my3High production -0.02312    0.06308   -0.37
## TxBDCT:my3High production   -0.02941    0.05416   -0.54
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: cure
##                Chisq Df Pr(>Chisq)
## Tx              0.32  2       0.85
## my3             0.70  2       0.70
## Parity          2.78  2       0.25
## DOSCC           0.43  1       0.51
## factor(source)  0.02  1       0.88
## Tx:my3          2.74  4       0.60
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.913 0.0287 Inf     0.838     0.955
##  A_SDCT Low production  0.922 0.0320 Inf     0.832     0.966
##  BDCT   Low production  0.901 0.0278 Inf     0.832     0.944
##  C_SDCT Mid production  0.901 0.0286 Inf     0.829     0.945
##  A_SDCT Mid production  0.910 0.0309 Inf     0.828     0.955
##  BDCT   Mid production  0.930 0.0234 Inf     0.868     0.964
##  C_SDCT High production 0.937 0.0206 Inf     0.883     0.968
##  A_SDCT High production 0.929 0.0245 Inf     0.863     0.964
##  BDCT   High production 0.905 0.0281 Inf     0.834     0.948
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["lower.CL"]],
  ucl = out[["contrasts"]][["upper.CL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT  0.02 (-0.06 to 0.09)
## 2  Low production A_SDCT - BDCT  0.03 (-0.07 to 0.12)
## 3  Mid production C_SDCT - BDCT -0.04 (-0.11 to 0.04)
## 4  Mid production A_SDCT - BDCT -0.03 (-0.11 to 0.06)
## 5 High production C_SDCT - BDCT  0.05 (-0.03 to 0.12)
## 6 High production A_SDCT - BDCT  0.03 (-0.04 to 0.11)

NewIMI

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")

gee <- geeglm(newimi ~ Tx + my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

iminew.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
iminew.main <- iminew.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

iminew.main$production <- "All production levels"
iminew.main$contrast <- "none"
iminew.main$outcome <- "New IMI risk"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")

gee <- geeglm(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) , data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ Tx * my3 + Parity + DOSCC + factor(source), 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(newimi) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                             Estimate  Std.err Wald Pr(>|W|)   
## (Intercept)                  0.05905  0.06482 0.83   0.3623   
## TxA_SDCT                    -0.04170  0.03348 1.55   0.2130   
## TxBDCT                      -0.05209  0.04118 1.60   0.2059   
## my3Mid production           -0.03708  0.02879 1.66   0.1978   
## my3High production           0.01131  0.03407 0.11   0.7399   
## Parity2                      0.00815  0.02343 0.12   0.7279   
## Parity3                      0.01569  0.01766 0.79   0.3742   
## DOSCC                        0.02704  0.00925 8.54   0.0035 **
## factor(source)US             0.05721  0.04858 1.39   0.2389   
## TxA_SDCT:my3Mid production   0.05020  0.04223 1.41   0.2345   
## TxBDCT:my3Mid production     0.05598  0.03817 2.15   0.1425   
## TxA_SDCT:my3High production  0.01119  0.04701 0.06   0.8119   
## TxBDCT:my3High production    0.02649  0.03685 0.52   0.4722   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1    0.17
## Number of clusters:   16  Maximum cluster size: 1223
glm <- glm(newimi ~ alg*my3 + cult*my3 + Parity + DOSCC + factor(source) , data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"))
int.p <- car::Anova(glm) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "newimi"
int.p.newimi <- int.p


emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.216 0.0308 Inf    0.1555     0.276
##  A_SDCT Low production   0.174 0.0327 Inf    0.1100     0.238
##  BDCT   Low production   0.164 0.0393 Inf    0.0866     0.241
##  C_SDCT Mid production   0.179 0.0271 Inf    0.1256     0.232
##  A_SDCT Mid production   0.187 0.0260 Inf    0.1363     0.238
##  BDCT   Mid production   0.183 0.0351 Inf    0.1139     0.251
##  C_SDCT High production  0.227 0.0223 Inf    0.1835     0.271
##  A_SDCT High production  0.197 0.0189 Inf    0.1595     0.234
##  BDCT   High production  0.202 0.0330 Inf    0.1368     0.266
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100
#mean

tab$outcome <- "New IMI risk"
tab.newimi <- tab
tab.newimi
##        production      contrast             estimate      outcome
## 1  Low production C_SDCT - BDCT 0.05 (-0.03 to 0.13) New IMI risk
## 2  Low production A_SDCT - BDCT 0.01 (-0.02 to 0.04) New IMI risk
## 3  Mid production C_SDCT - BDCT    0 (-0.05 to 0.04) New IMI risk
## 4  Mid production A_SDCT - BDCT    0 (-0.04 to 0.05) New IMI risk
## 5 High production C_SDCT - BDCT 0.03 (-0.02 to 0.07) New IMI risk
## 6 High production A_SDCT - BDCT    0 (-0.07 to 0.06) New IMI risk

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: newimi ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##     4481     4572    -2227     4453     4620 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -0.915 -0.591 -0.401 -0.233  4.996 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.423    0.65    
## Number of obs: 4634, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -1.8922     0.3033   -6.24  4.4e-10 ***
## TxA_SDCT                     -0.2704     0.1710   -1.58    0.114    
## TxBDCT                       -0.3038     0.1466   -2.07    0.038 *  
## my3Mid production            -0.3388     0.1573   -2.15    0.031 *  
## my3High production           -0.1451     0.1611   -0.90    0.368    
## Parity2                       0.1988     0.0929    2.14    0.032 *  
## Parity3                       0.2634     0.0958    2.75    0.006 ** 
## DOSCC                         0.0696     0.0353    1.97    0.048 *  
## factor(source)US             -0.1715     0.3522   -0.49    0.626    
## TxA_SDCT:my3Mid production    0.3685     0.2383    1.55    0.122    
## TxBDCT:my3Mid production      0.3504     0.2141    1.64    0.102    
## TxA_SDCT:my3High production   0.1042     0.2321    0.45    0.654    
## TxBDCT:my3High production     0.2214     0.2130    1.04    0.299    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: newimi
##                Chisq Df Pr(>Chisq)  
## Tx              2.37  2      0.305  
## my3             1.38  2      0.501  
## Parity          8.58  2      0.014 *
## DOSCC           3.90  1      0.048 *
## factor(source)  0.24  1      0.626  
## Tx:my3          3.93  4      0.415  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: newimi ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 4607
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -1.051 -0.659 -0.367 -0.106  2.528 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.00792  0.089   
##  Residual             0.15472  0.393   
## Number of obs: 4634, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  0.13930    0.04413    3.16
## TxA_SDCT                    -0.04298    0.02719   -1.58
## TxBDCT                      -0.04737    0.02289   -2.07
## my3Mid production           -0.05305    0.02446   -2.17
## my3High production          -0.02283    0.02576   -0.89
## Parity2                      0.02945    0.01444    2.04
## Parity3                      0.04016    0.01505    2.67
## DOSCC                        0.01076    0.00560    1.92
## factor(source)US            -0.00675    0.04816   -0.14
## TxA_SDCT:my3Mid production   0.05684    0.03716    1.53
## TxBDCT:my3Mid production     0.05315    0.03273    1.62
## TxA_SDCT:my3High production  0.01513    0.03677    0.41
## TxBDCT:my3High production    0.03397    0.03342    1.02
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: newimi
##                Chisq Df Pr(>Chisq)  
## Tx              2.39  2      0.302  
## my3             1.52  2      0.469  
## Parity          8.07  2      0.018 *
## DOSCC           3.68  1      0.055 .
## factor(source)  0.02  1      0.889  
## Tx:my3          3.90  4      0.420  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.180 0.0296 Inf    0.1294     0.246
##  A_SDCT Low production  0.144 0.0271 Inf    0.0983     0.205
##  BDCT   Low production  0.140 0.0240 Inf    0.0989     0.194
##  C_SDCT Mid production  0.136 0.0239 Inf    0.0950     0.190
##  A_SDCT Mid production  0.147 0.0274 Inf    0.1014     0.209
##  BDCT   Mid production  0.141 0.0246 Inf    0.0994     0.196
##  C_SDCT High production 0.160 0.0276 Inf    0.1129     0.222
##  A_SDCT High production 0.139 0.0257 Inf    0.0956     0.197
##  BDCT   High production 0.149 0.0265 Inf    0.1042     0.209
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT      0.05 (0 to 0.09)
## 2  Low production A_SDCT - BDCT     0 (-0.05 to 0.06)
## 3  Mid production C_SDCT - BDCT -0.01 (-0.05 to 0.04)
## 4  Mid production A_SDCT - BDCT  0.01 (-0.04 to 0.06)
## 5 High production C_SDCT - BDCT  0.01 (-0.03 to 0.06)
## 6 High production A_SDCT - BDCT -0.01 (-0.07 to 0.04)

DHI Dataset

Somatic cell count (1-120 DIM)

All production groups pooled together

dhi$Tx <- fct_relevel(dhi$Tx,"BDCT","C_SDCT","A_SDCT")
dhi <- dhi %>% arrange(FARMID,CowID)
mm0 <- geeglm(scc ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID), 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID), data = dhi %>% filter(!is.na(DOSCC)), id = CowID)
## 
##  Coefficients:
##                    Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)          3.3045  0.1784 343.27  < 2e-16 ***
## TxC_SDCT             0.0278  0.0639   0.19   0.6631    
## TxA_SDCT             0.0273  0.0684   0.16   0.6903    
## my3Mid production    0.0421  0.0720   0.34   0.5589    
## my3High production   0.0755  0.0763   0.98   0.3219    
## TestDIMcat2030      -0.5416  0.0646  70.37  < 2e-16 ***
## TestDIMcat2050      -0.5565  0.0589  89.18  < 2e-16 ***
## TestDIMcat2070      -0.4182  0.0606  47.60  5.2e-12 ***
## TestDIMcat2090      -0.2862  0.0648  19.51  1.0e-05 ***
## TestDIMcat20110     -0.1244  0.0611   4.15   0.0417 *  
## DOSCC                0.1759  0.0268  43.02  5.4e-11 ***
## Parity2              0.1333  0.0625   4.55   0.0329 *  
## Parity3              0.3022  0.0735  16.90  3.9e-05 ***
## factor(FARMID)2      0.0545  0.1709   0.10   0.7497    
## factor(FARMID)3     -0.0429  0.1346   0.10   0.7499    
## factor(FARMID)4      0.0336  0.1316   0.07   0.7981    
## factor(FARMID)5      0.0314  0.1357   0.05   0.8171    
## factor(FARMID)6     -0.5076  0.1803   7.92   0.0049 ** 
## factor(FARMID)7     -0.3009  0.1492   4.07   0.0437 *  
## factor(FARMID)8     -0.0673  0.2795   0.06   0.8097    
## factor(FARMID)9     -0.0525  0.2980   0.03   0.8602    
## factor(FARMID)10    -0.2642  0.2269   1.36   0.2443    
## factor(FARMID)11    -0.4061  0.2410   2.84   0.0919 .  
## factor(FARMID)12    -0.4632  0.2170   4.56   0.0328 *  
## factor(FARMID)13     0.1282  0.3030   0.18   0.6722    
## factor(FARMID)14     0.0986  0.2545   0.15   0.6983    
## factor(FARMID)15     0.0221  0.3689   0.00   0.9521    
## factor(FARMID)16    -0.0613  0.2328   0.07   0.7923    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0566
## Number of clusters:   1364  Maximum cluster size: 4
ggplot(data.frame(eta=predict(mm0,type="link"),pearson=residuals(mm0,type="pearson")),
       aes(x=eta,y=pearson)) + geom_point() + theme_bw()

qqnorm(residuals(mm0))

scc.main <- tidy(mm0,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
scc.main <- scc.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

scc.main$production <- "All production levels"
scc.main$contrast <- "none"
scc.main$outcome <- "Log SCC (1-120 DIM)"
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(Tx,TestDIMcat20,SCC,LCL,UCL))
emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )
emm
## # A tibble: 18 × 5
##    Tx     TestDIMcat20   SCC   LCL   UCL
##    <fct>  <chr>        <dbl> <dbl> <dbl>
##  1 BDCT   10            63.6  55.3  73.1
##  2 C_SDCT 10            65.4  56.8  75.3
##  3 A_SDCT 10            65.4  55.5  76.9
##  4 BDCT   30            37.0  32.1  42.6
##  5 C_SDCT 30            38.0  33.0  43.8
##  6 A_SDCT 30            38.0  32.4  44.7
##  7 BDCT   50            36.5  31.8  41.8
##  8 C_SDCT 50            37.5  32.6  43.2
##  9 A_SDCT 50            37.5  31.9  44.0
## 10 BDCT   70            41.9  36.3  48.3
## 11 C_SDCT 70            43.0  37.5  49.5
## 12 A_SDCT 70            43.0  36.6  50.5
## 13 BDCT   90            47.8  41.5  55.0
## 14 C_SDCT 90            49.1  42.4  56.8
## 15 A_SDCT 90            49.1  41.7  57.8
## 16 BDCT   110           56.2  48.8  64.6
## 17 C_SDCT 110           57.7  50.1  66.6
## 18 A_SDCT 110           57.7  49.1  67.9
curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=Tx, colour=Tx) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$LCL, ymax=emm$UCL,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

    
curve

Interaction model (Tx * milk yield)

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(scc ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID), 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)

summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID), data = dhi %>% filter(!is.na(DOSCC)), id = CowID)
## 
##  Coefficients:
##                             Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)                   3.2661  0.1929 286.67  < 2e-16 ***
## TxA_SDCT                      0.1569  0.1267   1.53   0.2155    
## TxBDCT                        0.0084  0.1101   0.01   0.9392    
## my3Mid production             0.0828  0.1141   0.53   0.4683    
## my3High production            0.2013  0.1197   2.83   0.0928 .  
## TestDIMcat2030               -0.5401  0.0645  70.06  < 2e-16 ***
## TestDIMcat2050               -0.5556  0.0589  88.90  < 2e-16 ***
## TestDIMcat2070               -0.4169  0.0606  47.35  5.9e-12 ***
## TestDIMcat2090               -0.2846  0.0648  19.30  1.1e-05 ***
## TestDIMcat20110              -0.1225  0.0611   4.02   0.0449 *  
## DOSCC                         0.1781  0.0269  43.89  3.5e-11 ***
## Parity2                       0.1315  0.0627   4.40   0.0359 *  
## Parity3                       0.3010  0.0732  16.94  3.9e-05 ***
## factor(FARMID)2               0.0517  0.1726   0.09   0.7647    
## factor(FARMID)3              -0.0427  0.1354   0.10   0.7525    
## factor(FARMID)4               0.0285  0.1327   0.05   0.8298    
## factor(FARMID)5               0.0224  0.1361   0.03   0.8691    
## factor(FARMID)6              -0.5184  0.1810   8.20   0.0042 ** 
## factor(FARMID)7              -0.3022  0.1498   4.07   0.0437 *  
## factor(FARMID)8              -0.0442  0.2805   0.02   0.8747    
## factor(FARMID)9              -0.0294  0.3004   0.01   0.9220    
## factor(FARMID)10             -0.2859  0.2294   1.55   0.2128    
## factor(FARMID)11             -0.3844  0.2408   2.55   0.1104    
## factor(FARMID)12             -0.4512  0.2202   4.20   0.0405 *  
## factor(FARMID)13              0.1514  0.3046   0.25   0.6193    
## factor(FARMID)14              0.1086  0.2497   0.19   0.6637    
## factor(FARMID)15              0.0274  0.3643   0.01   0.9400    
## factor(FARMID)16             -0.0574  0.2329   0.06   0.8053    
## TxA_SDCT:my3Mid production   -0.1910  0.1708   1.25   0.2634    
## TxBDCT:my3Mid production      0.0425  0.1533   0.08   0.7816    
## TxA_SDCT:my3High production  -0.2570  0.1686   2.32   0.1273    
## TxBDCT:my3High production    -0.1480  0.1573   0.89   0.3466    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0562
## Number of clusters:   1364  Maximum cluster size: 4
lmer <- lmer(scc ~ alg*my3 + cult*my3 + TestDIMcat20 + DOSCC + Parity + (1|FARMID/CowID), 
              data=dhi %>% filter(!is.na(DOSCC)))

int.p <- car::Anova(lmer) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "scc"
int.p.scc <- int.p


out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint


tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Log SCC (1-120 DIM)"
tab.scc <- tab
tab.scc
##        production      contrast              estimate             outcome
## 1  Low production C_SDCT - BDCT -0.01 (-0.22 to 0.21) Log SCC (1-120 DIM)
## 2  Low production A_SDCT - BDCT  0.15 (-0.09 to 0.39) Log SCC (1-120 DIM)
## 3  Mid production C_SDCT - BDCT -0.05 (-0.26 to 0.16) Log SCC (1-120 DIM)
## 4  Mid production A_SDCT - BDCT -0.09 (-0.31 to 0.14) Log SCC (1-120 DIM)
## 5 High production C_SDCT - BDCT  0.14 (-0.08 to 0.36) Log SCC (1-120 DIM)
## 6 High production A_SDCT - BDCT  0.04 (-0.19 to 0.26) Log SCC (1-120 DIM)
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(my3,Tx,TestDIMcat20,SCC,LCL,UCL))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )


curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=Tx, colour=Tx) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=LCL, ymax=UCL,group=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",
        legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1") + facet_grid( ~ my3)

tiff("curve1.tiff", units="in", width=10, height=6, res=300)
curve
dev.off()
## quartz_off_screen 
##                 2
curve

Interaction model using linear mixed model

lmm <- lmer(scc ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + (1|FARMID/CowID), 
              data=dhi)

summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: scc ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + (1 | FARMID/CowID)
##    Data: dhi
## 
## REML criterion at convergence: 14784
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -5.136 -0.523 -0.110  0.436  4.116 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  CowID:FARMID (Intercept) 0.701    0.837   
##  FARMID       (Intercept) 0.014    0.118   
##  Residual                 1.053    1.026   
## Number of obs: 4553, groups:  CowID:FARMID, 1364; FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  3.16592    0.15439   20.51
## TxA_SDCT                     0.17014    0.12451    1.37
## TxBDCT                       0.00547    0.11058    0.05
## my3Mid production            0.06995    0.11663    0.60
## my3High production           0.19372    0.11878    1.63
## TestDIMcat2030              -0.56693    0.05762   -9.84
## TestDIMcat2050              -0.57328    0.05519  -10.39
## TestDIMcat2070              -0.41919    0.05476   -7.65
## TestDIMcat2090              -0.29961    0.05865   -5.11
## TestDIMcat20110             -0.11768    0.05479   -2.15
## DOSCC                        0.18835    0.02625    7.18
## Parity2                      0.10068    0.06828    1.47
## Parity3                      0.29597    0.07124    4.15
## TxA_SDCT:my3Mid production  -0.14597    0.17226   -0.85
## TxBDCT:my3Mid production     0.02985    0.15757    0.19
## TxA_SDCT:my3High production -0.25644    0.17125   -1.50
## TxBDCT:my3High production   -0.13304    0.16038   -0.83
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: scc
##               Chisq Df Pr(>Chisq)    
## Tx             0.75  2    0.68885    
## my3            0.98  2    0.61214    
## TestDIMcat20 183.45  5    < 2e-16 ***
## DOSCC         51.50  1    7.2e-13 ***
## Parity        17.41  2    0.00017 ***
## Tx:my3         2.99  4    0.55912    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT -0.01 (-0.22 to 0.21)
## 2  Low production A_SDCT - BDCT   0.16 (-0.07 to 0.4)
## 3  Mid production C_SDCT - BDCT -0.04 (-0.26 to 0.18)
## 4  Mid production A_SDCT - BDCT -0.01 (-0.25 to 0.22)
## 5 High production C_SDCT - BDCT   0.13 (-0.1 to 0.36)
## 6 High production A_SDCT - BDCT   0.04 (-0.2 to 0.28)

Milk Yield (1-120 DIM)

All production groups pooled together

dhi$Tx <- fct_relevel(dhi$Tx,"BDCT","C_SDCT","A_SDCT")
dhi <- dhi %>% arrange(FARMID,CowID)
mm0 <- geeglm(MY ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID) + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID) + DOMY, data = dhi %>% filter(!is.na(DOSCC)), 
##     id = CowID)
## 
##  Coefficients:
##                    Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)         31.8031  1.5941 398.04  < 2e-16 ***
## TxC_SDCT            -0.1808  0.4107   0.19  0.65965    
## TxA_SDCT            -0.9814  0.4546   4.66  0.03084 *  
## my3Mid production   -0.5712  0.6640   0.74  0.38971    
## my3High production   0.5849  0.9623   0.37  0.54328    
## TestDIMcat2030      10.6606  0.4583 541.14  < 2e-16 ***
## TestDIMcat2050      12.3200  0.3998 949.77  < 2e-16 ***
## TestDIMcat2070      11.5855  0.4275 734.54  < 2e-16 ***
## TestDIMcat2090      10.1087  0.4414 524.56  < 2e-16 ***
## TestDIMcat20110      7.9665  0.4369 332.43  < 2e-16 ***
## DOSCC                0.1355  0.1756   0.59  0.44052    
## Parity2              2.8994  0.4220  47.21  6.4e-12 ***
## Parity3              3.3581  0.4942  46.16  1.1e-11 ***
## factor(FARMID)2     -1.6117  1.0459   2.37  0.12330    
## factor(FARMID)3     -4.9980  0.8516  34.44  4.4e-09 ***
## factor(FARMID)4      4.8781  0.8744  31.12  2.4e-08 ***
## factor(FARMID)5      0.9036  0.8896   1.03  0.30974    
## factor(FARMID)6      1.8089  1.0391   3.03  0.08172 .  
## factor(FARMID)7      1.6084  0.9903   2.64  0.10434    
## factor(FARMID)8     -1.8911  1.6306   1.35  0.24615    
## factor(FARMID)9     -7.7980  1.6977  21.10  4.4e-06 ***
## factor(FARMID)10    -1.6553  1.2098   1.87  0.17125    
## factor(FARMID)11    -3.2094  1.7474   3.37  0.06625 .  
## factor(FARMID)12    -4.1596  1.7436   5.69  0.01705 *  
## factor(FARMID)13    -4.8266  1.2447  15.04  0.00011 ***
## factor(FARMID)14    -2.0698  2.4659   0.70  0.40127    
## factor(FARMID)15    -4.9931  1.6537   9.12  0.00253 ** 
## factor(FARMID)16    -2.3555  1.2312   3.66  0.05574 .  
## DOMY                 0.1937  0.0484  15.99  6.4e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     71.3    2.37
## Number of clusters:   1363  Maximum cluster size: 4
ggplot(data.frame(eta=predict(mm0,type="link"),pearson=residuals(mm0,type="pearson")),
       aes(x=eta,y=pearson)) + geom_point() + theme_bw()

qqnorm(residuals(mm0))

my.main <- tidy(mm0,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
my.main <- my.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

my.main$production <- "All production levels"
my.main$contrast <- "none"
my.main$outcome <- "Milk Yield (1-120 DIM)"
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm <- emm %>% subset(select=c(Tx,TestDIMcat20,estimate,conf.low,conf.high))
emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(30,55))) + aes(x=as.numeric(TestDIMcat20), y=estimate, group=Tx, colour=Tx) +
  labs(y = "Milk Yield)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$conf.low, ymax=emm$conf.high,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

    
curve

Interaction model (Tx * milk yield)

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID) + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)


summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID) + DOMY, data = dhi %>% filter(!is.na(DOSCC)), 
##     id = CowID)
## 
##  Coefficients:
##                             Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)                  31.9459  1.6143 391.62  < 2e-16 ***
## TxA_SDCT                     -1.6111  0.8959   3.23   0.0721 .  
## TxBDCT                        0.0852  0.6959   0.01   0.9026    
## my3Mid production            -0.7481  0.8719   0.74   0.3909    
## my3High production            0.0409  1.1758   0.00   0.9722    
## TestDIMcat2030               10.6529  0.4586 539.63  < 2e-16 ***
## TestDIMcat2050               12.3127  0.3997 948.81  < 2e-16 ***
## TestDIMcat2070               11.5762  0.4274 733.67  < 2e-16 ***
## TestDIMcat2090               10.0970  0.4416 522.79  < 2e-16 ***
## TestDIMcat20110               7.9542  0.4368 331.58  < 2e-16 ***
## DOSCC                         0.1279  0.1756   0.53   0.4664    
## Parity2                       2.8962  0.4200  47.55  5.4e-12 ***
## Parity3                       3.3503  0.4930  46.18  1.1e-11 ***
## factor(FARMID)2              -1.6389  1.0480   2.45   0.1178    
## factor(FARMID)3              -5.0154  0.8553  34.38  4.5e-09 ***
## factor(FARMID)4               4.8965  0.8796  30.99  2.6e-08 ***
## factor(FARMID)5               0.9276  0.8942   1.08   0.2996    
## factor(FARMID)6               1.8861  1.0472   3.24   0.0717 .  
## factor(FARMID)7               1.6251  0.9947   2.67   0.1023    
## factor(FARMID)8              -2.0086  1.6474   1.49   0.2228    
## factor(FARMID)9              -7.8925  1.7156  21.16  4.2e-06 ***
## factor(FARMID)10             -1.4838  1.2213   1.48   0.2244    
## factor(FARMID)11             -3.3325  1.7722   3.54   0.0601 .  
## factor(FARMID)12             -4.1916  1.7713   5.60   0.0180 *  
## factor(FARMID)13             -4.9395  1.2652  15.24  9.5e-05 ***
## factor(FARMID)14             -2.1162  2.4571   0.74   0.3891    
## factor(FARMID)15             -5.0086  1.6358   9.37   0.0022 ** 
## factor(FARMID)16             -2.3548  1.2507   3.54   0.0597 .  
## DOMY                          0.1925  0.0485  15.74  7.3e-05 ***
## TxA_SDCT:my3Mid production    1.3455  1.1085   1.47   0.2248    
## TxBDCT:my3Mid production     -0.5500  0.9506   0.33   0.5629    
## TxA_SDCT:my3High production   1.0020  1.2024   0.69   0.4047    
## TxBDCT:my3High production     0.8429  1.0448   0.65   0.4198    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     71.1    2.36
## Number of clusters:   1363  Maximum cluster size: 4
lmer <- lmer(scc ~ my3*cult + my3*alg+ TestDIMcat20 + DOSCC + Parity + DOMY + (1|FARMID/CowID), 
              data=dhi %>% filter(!is.na(DOSCC)))

int.p <- car::Anova(lmer) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "my"
int.p.my <- int.p


out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Milk Yield"

tab.my <- tab
tab.my
##        production      contrast              estimate    outcome
## 1  Low production C_SDCT - BDCT -0.09 (-1.45 to 1.28) Milk Yield
## 2  Low production A_SDCT - BDCT     -1.7 (-3.39 to 0) Milk Yield
## 3  Mid production C_SDCT - BDCT  0.46 (-0.82 to 1.75) Milk Yield
## 4  Mid production A_SDCT - BDCT    0.2 (-1.11 to 1.5) Milk Yield
## 5 High production C_SDCT - BDCT -0.93 (-2.45 to 0.59) Milk Yield
## 6 High production A_SDCT - BDCT -1.54 (-3.11 to 0.04) Milk Yield
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm <- emm %>% subset(select=c(my3,Tx,TestDIMcat20,estimate,conf.low,conf.high))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(30,55))) + aes(x=as.numeric(TestDIMcat20), y=estimate, group=Tx, colour=Tx) +
  labs(y = "Milk Yield (kg/d)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$conf.low, ymax=emm$conf.high,colour=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1") + facet_grid( ~ my3)

tiff("curve2.tiff", units="in", width=10, height=6, res=300)
curve
dev.off()    
## quartz_off_screen 
##                 2
curve

Interaction model using linear mixed model

lmm <- lmer(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + (1|FARMID/CowID), 
              data=dhi)

summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + (1 | FARMID/CowID)
##    Data: dhi
## 
## REML criterion at convergence: 31700
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -6.498 -0.468  0.021  0.518  4.182 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  CowID:FARMID (Intercept) 29.14    5.40    
##  FARMID       (Intercept)  9.33    3.06    
##  Residual                 43.72    6.61    
## Number of obs: 4551, groups:  CowID:FARMID, 1363; FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  34.0021     1.2497   27.21
## TxA_SDCT                     -1.3972     0.8145   -1.72
## TxBDCT                        0.0731     0.7140    0.10
## my3Mid production             1.2098     0.7631    1.59
## my3High production            3.5547     0.7856    4.52
## TestDIMcat2030               11.1954     0.3718   30.11
## TestDIMcat2050               12.2545     0.3558   34.44
## TestDIMcat2070               11.8824     0.3531   33.65
## TestDIMcat2090               10.2685     0.3782   27.15
## TestDIMcat20110               7.9111     0.3533   22.39
## DOSCC                        -0.0128     0.1723   -0.07
## Parity2                       2.9730     0.4420    6.73
## Parity3                       3.1969     0.4621    6.92
## TxA_SDCT:my3Mid production    1.1928     1.1170    1.07
## TxBDCT:my3Mid production     -0.5866     1.0179   -0.58
## TxA_SDCT:my3High production   0.8122     1.1118    0.73
## TxBDCT:my3High production     0.8237     1.0375    0.79
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: MY
##                Chisq Df Pr(>Chisq)    
## Tx              3.81  2       0.15    
## my3            69.16  2    9.6e-16 ***
## TestDIMcat20 1659.54  5    < 2e-16 ***
## DOSCC           0.01  1       0.94    
## Parity         64.68  2    9.0e-15 ***
## Tx:my3          4.33  4       0.36    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
int.p <- car::Anova(lmer) %>% slice_tail(n=2) %>% select(`Pr(>Chisq)`)
int.p <- tibble::rownames_to_column(int.p, "interaction")
int.p$outcome <- "scc"
int.p.scc <- int.p

out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

icc(lmm)
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.468
##   Unadjusted ICC: 0.367
tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT -0.07 (-1.47 to 1.33)
## 2  Low production A_SDCT - BDCT -1.47 (-3.01 to 0.07)
## 3  Mid production C_SDCT - BDCT  0.51 (-0.91 to 1.94)
## 4  Mid production A_SDCT - BDCT  0.31 (-1.21 to 1.83)
## 5 High production C_SDCT - BDCT  -0.9 (-2.37 to 0.58)
## 6 High production A_SDCT - BDCT -1.48 (-3.03 to 0.07)

Performing analysis without including farmID as fixed or random effect (milk yield)

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)


summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     DOMY, data = dhi %>% filter(!is.na(DOSCC)), id = CowID)
## 
##  Coefficients:
##                             Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)                  28.4835  1.5742 327.40  < 2e-16 ***
## TxA_SDCT                     -0.1063  0.9654   0.01    0.912    
## TxBDCT                        0.4428  0.7748   0.33    0.568    
## my3Mid production             0.4428  0.9665   0.21    0.647    
## my3High production            2.6525  1.2674   4.38    0.036 *  
## TestDIMcat2030               10.4483  0.4781 477.52  < 2e-16 ***
## TestDIMcat2050               12.0588  0.4017 901.00  < 2e-16 ***
## TestDIMcat2070               11.3746  0.4376 675.70  < 2e-16 ***
## TestDIMcat2090                9.6402  0.4643 431.00  < 2e-16 ***
## TestDIMcat20110               7.7699  0.4426 308.18  < 2e-16 ***
## DOSCC                         0.7748  0.1895  16.72  4.3e-05 ***
## Parity2                       2.1652  0.4678  21.43  3.7e-06 ***
## Parity3                       2.5068  0.5445  21.19  4.2e-06 ***
## DOMY                          0.2104  0.0533  15.60  7.8e-05 ***
## TxA_SDCT:my3Mid production    0.3068  1.2690   0.06    0.809    
## TxBDCT:my3Mid production     -0.9791  1.0915   0.80    0.370    
## TxA_SDCT:my3High production  -0.4263  1.2905   0.11    0.741    
## TxBDCT:my3High production    -0.2135  1.1542   0.03    0.853    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     83.2    2.66
## Number of clusters:   1363  Maximum cluster size: 4
out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Milk Yield"

tab
##        production      contrast              estimate    outcome
## 1  Low production C_SDCT - BDCT -0.44 (-1.96 to 1.08) Milk Yield
## 2  Low production A_SDCT - BDCT   -0.55 (-2.4 to 1.3) Milk Yield
## 3  Mid production C_SDCT - BDCT  0.54 (-0.97 to 2.05) Milk Yield
## 4  Mid production A_SDCT - BDCT   0.74 (-0.83 to 2.3) Milk Yield
## 5 High production C_SDCT - BDCT -0.23 (-1.91 to 1.45) Milk Yield
## 6 High production A_SDCT - BDCT    -0.76 (-2.52 to 1) Milk Yield

Stratifying to only include USA cows

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC) & source=="US")
              ,id=CowID)

summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     DOMY, data = dhi %>% filter(!is.na(DOSCC) & source == "US"), 
##     id = CowID)
## 
##  Coefficients:
##                              Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)                 29.422307  1.684163 305.20  < 2e-16 ***
## TxA_SDCT                    -1.534068  1.022797   2.25  0.13365    
## TxBDCT                       0.417250  0.912267   0.21  0.64740    
## my3Mid production           -0.687125  1.088984   0.40  0.52806    
## my3High production           1.001085  1.395710   0.51  0.47321    
## TestDIMcat2030              11.199074  0.512109 478.23  < 2e-16 ***
## TestDIMcat2050              12.847842  0.426208 908.69  < 2e-16 ***
## TestDIMcat2070              12.020908  0.464903 668.57  < 2e-16 ***
## TestDIMcat2090              10.476140  0.500353 438.38  < 2e-16 ***
## TestDIMcat20110              8.388605  0.475187 311.64  < 2e-16 ***
## DOSCC                        0.729068  0.204831  12.67  0.00037 ***
## Parity2                      1.686000  0.502318  11.27  0.00079 ***
## Parity3                      2.405592  0.587740  16.75  4.3e-05 ***
## DOMY                         0.228885  0.055732  16.87  4.0e-05 ***
## TxA_SDCT:my3Mid production   1.148374  1.336622   0.74  0.39025    
## TxBDCT:my3Mid production    -1.264223  1.247883   1.03  0.31102    
## TxA_SDCT:my3High production  0.790430  1.342647   0.35  0.55606    
## TxBDCT:my3High production   -0.000547  1.261011   0.00  0.99965    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     84.9     2.9
## Number of clusters:   1177  Maximum cluster size: 4
out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Milk Yield"

tab
##        production      contrast              estimate    outcome
## 1  Low production C_SDCT - BDCT -0.42 (-2.21 to 1.37) Milk Yield
## 2  Low production A_SDCT - BDCT -1.95 (-3.91 to 0.01) Milk Yield
## 3  Mid production C_SDCT - BDCT  0.85 (-0.82 to 2.52) Milk Yield
## 4  Mid production A_SDCT - BDCT   0.46 (-1.2 to 2.13) Milk Yield
## 5 High production C_SDCT - BDCT  -0.42 (-2.13 to 1.3) Milk Yield
## 6 High production A_SDCT - BDCT -1.16 (-2.93 to 0.61) Milk Yield
fulltab <- rbind(tab.cm,
                 tab.cull,
     tab.imipc,
     tab.imicure,
     tab.newimi,
     tab.scc,
     tab.my)



full.main <- rbind(cm.main,
                   cull.main,
                   imipc.main,
                   imicure.main,
                   iminew.main,
                   scc.main,
                   my.main)

int.p <- rbind(int.p.cm,
               int.p.cull,
               int.p.imipc,
               int.p.cure,
               int.p.newimi,
               int.p.scc,
               int.p.my)

write.csv(int.p,"interaction.csv")

full.main$contrast <- full.main$term
full.main$term <- NULL

tableout <- rbind(fulltab,full.main) %>% arrange(outcome,production)

write.csv(tableout,'tableout.csv')

Milk yield at DO —

IMI at 1-13 DIM

Need to choose covariates

ggplot(qtr, aes(x=DOMY, y=imipc)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

IMI in early lactation

gee <- geeglm(imipc ~ my3, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)         0.21483  0.03640 34.83  3.6e-09 ***
## my3Mid production  -0.00362  0.01539  0.06     0.81    
## my3High production  0.01883  0.02272  0.69     0.41    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.162
## Number of clusters:   16  Maximum cluster size: 1270
gee <- geeglm(imipc ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ my3 + factor(Parity) + DOSCC + Tx, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)   
## (Intercept)         0.07067  0.04971  2.02   0.1551   
## my3Mid production   0.01972  0.01479  1.78   0.1824   
## my3High production  0.04586  0.02069  4.91   0.0267 * 
## factor(Parity)2     0.01015  0.02261  0.20   0.6537   
## factor(Parity)3     0.01602  0.01536  1.09   0.2969   
## DOSCC               0.02942  0.00912 10.42   0.0012 **
## TxA_SDCT           -0.00632  0.01388  0.21   0.6488   
## TxBDCT             -0.02268  0.02224  1.04   0.3080   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.162
## Number of clusters:   16  Maximum cluster size: 1270
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term               estimate std.error statistic p.value conf.low conf.high
##   <chr>                 <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.0707    0.0497      2.02  0.155   -0.0268     0.168 
## 2 my3Mid production   0.0197    0.0148      1.78  0.182   -0.00927    0.0487
## 3 my3High production  0.0459    0.0207      4.91  0.0267   0.00531    0.0864
## 4 factor(Parity)2     0.0101    0.0226      0.201 0.654   -0.0342     0.0545
## 5 factor(Parity)3     0.0160    0.0154      1.09  0.297   -0.0141     0.0461
## 6 DOSCC               0.0294    0.00912    10.4   0.00125  0.0116     0.0473
## 7 TxA_SDCT           -0.00632   0.0139      0.207 0.649   -0.0335     0.0209
## 8 TxBDCT             -0.0227    0.0222      1.04  0.308   -0.0663     0.0209

IMI cure

ggplot(qtr, aes(x=DOMY, y=cure)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

#Univariable
gee <- geeglm(cure ~ my3, data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = cure ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)         0.87390 0.03263 717.21   <2e-16 ***
## my3Mid production   0.00785 0.02043   0.15     0.70    
## my3High production  0.00962 0.02778   0.12     0.73    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.301
## Number of clusters:   16  Maximum cluster size: 432
#Multivariable
gee <- geeglm(cure ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = cure ~ my3 + factor(Parity) + DOSCC + Tx, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                     Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)         0.952458  0.057031 278.92   <2e-16 ***
## my3Mid production   0.002327  0.018367   0.02    0.899    
## my3High production -0.001926  0.024822   0.01    0.938    
## factor(Parity)2    -0.034728  0.015091   5.30    0.021 *  
## factor(Parity)3    -0.025484  0.026793   0.90    0.342    
## DOSCC              -0.010907  0.006862   2.53    0.112    
## TxA_SDCT           -0.000967  0.013113   0.01    0.941    
## TxBDCT             -0.010846  0.017682   0.38    0.540    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.337
## Number of clusters:   16  Maximum cluster size: 432
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term                estimate std.error statistic p.value conf.low conf.high
##   <chr>                  <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.952      0.0570  279.       0        0.841    1.06   
## 2 my3Mid production   0.00233    0.0184    0.0160   0.899   -0.0337   0.0383 
## 3 my3High production -0.00193    0.0248    0.00602  0.938   -0.0506   0.0467 
## 4 factor(Parity)2    -0.0347     0.0151    5.30     0.0214  -0.0643  -0.00515
## 5 factor(Parity)3    -0.0255     0.0268    0.905    0.342   -0.0780   0.0270 
## 6 DOSCC              -0.0109     0.00686   2.53     0.112   -0.0244   0.00254
## 7 TxA_SDCT           -0.000967   0.0131    0.00544  0.941   -0.0267   0.0247 
## 8 TxBDCT             -0.0108     0.0177    0.376    0.540   -0.0455   0.0238

NewIMI

ggplot(qtr, aes(x=DOMY, y=newimi)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

gee <- geeglm(newimi ~ my3, data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)         0.21156  0.03874 29.83  4.7e-08 ***
## my3Mid production  -0.01893  0.01647  1.32     0.25    
## my3High production  0.00919  0.02160  0.18     0.67    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.198
## Number of clusters:   16  Maximum cluster size: 1223
gee <- geeglm(newimi ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ my3 + factor(Parity) + DOSCC + Tx, 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(newimi) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)   
## (Intercept)         0.07371  0.05216  2.00   0.1576   
## my3Mid production   0.00362  0.01594  0.05   0.8203   
## my3High production  0.03427  0.02064  2.76   0.0969 . 
## factor(Parity)2     0.01107  0.02390  0.21   0.6431   
## factor(Parity)3     0.01335  0.01603  0.69   0.4049   
## DOSCC               0.02839  0.00897 10.02   0.0016 **
## TxA_SDCT           -0.00585  0.01637  0.13   0.7209   
## TxBDCT             -0.02493  0.02298  1.18   0.2779   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.198
## Number of clusters:   16  Maximum cluster size: 1223
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term               estimate std.error statistic p.value conf.low conf.high
##   <chr>                 <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.0737    0.0522     2.00   0.158   -0.0285     0.176 
## 2 my3Mid production   0.00362   0.0159     0.0516 0.820   -0.0276     0.0349
## 3 my3High production  0.0343    0.0206     2.76   0.0969  -0.00619    0.0747
## 4 factor(Parity)2     0.0111    0.0239     0.215  0.643   -0.0358     0.0579
## 5 factor(Parity)3     0.0134    0.0160     0.694  0.405   -0.0181     0.0448
## 6 DOSCC               0.0284    0.00897   10.0    0.00155  0.0108     0.0460
## 7 TxA_SDCT           -0.00585   0.0164     0.128  0.721   -0.0379     0.0262
## 8 TxBDCT             -0.0249    0.0230     1.18   0.278   -0.0700     0.0201

Post calving SCC

desc <- dhi %>% group_by(my3,TestDIMcat20) %>% summarise(
  atrisk = sum(!is.na(scc)),
  scm.n = sum(scc > log(200)),
  scm.risk = scm.n/atrisk 
)

ggplot(desc, aes(x=TestDIMcat20 %>% as.character() %>% as.numeric, y=scm.risk, color = my3)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", size=1.5)+
  xlab("Days in milk")+
  ylab("Risk of subclinical mastitis")+
  theme_bw()

mm0 <- geeglm(scc ~ my3 + TestDIMcat20 + factor(FARMID), data=dhi, id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(FARMID), data = dhi, 
##     id = CowID)
## 
##  Coefficients:
##                     Estimate   Std.err    Wald Pr(>|W|)    
## (Intercept)         4.302244  0.132182 1059.36  < 2e-16 ***
## my3Mid production  -0.107239  0.071262    2.26   0.1324    
## my3High production -0.100626  0.075204    1.79   0.1809    
## TestDIMcat2030     -0.517743  0.065647   62.20  3.1e-15 ***
## TestDIMcat2050     -0.559186  0.059315   88.88  < 2e-16 ***
## TestDIMcat2070     -0.410397  0.061308   44.81  2.2e-11 ***
## TestDIMcat2090     -0.279092  0.066168   17.79  2.5e-05 ***
## TestDIMcat20110    -0.122015  0.061394    3.95   0.0469 *  
## factor(FARMID)2    -0.000324  0.175405    0.00   0.9985    
## factor(FARMID)3    -0.064586  0.138362    0.22   0.6407    
## factor(FARMID)4     0.077785  0.135469    0.33   0.5658    
## factor(FARMID)5     0.132248  0.139731    0.90   0.3439    
## factor(FARMID)6    -0.491592  0.181716    7.32   0.0068 ** 
## factor(FARMID)7    -0.347942  0.155238    5.02   0.0250 *  
## factor(FARMID)8     0.024863  0.292672    0.01   0.9323    
## factor(FARMID)9    -0.125526  0.296649    0.18   0.6722    
## factor(FARMID)10   -0.315094  0.259063    1.48   0.2239    
## factor(FARMID)11   -0.332164  0.249733    1.77   0.1835    
## factor(FARMID)12   -0.600634  0.235406    6.51   0.0107 *  
## factor(FARMID)13    0.101746  0.317137    0.10   0.7483    
## factor(FARMID)14    0.092777  0.264044    0.12   0.7253    
## factor(FARMID)15   -0.022538  0.393192    0.00   0.9543    
## factor(FARMID)16   -0.166800  0.238637    0.49   0.4846    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.78  0.0595
## Number of clusters:   1364  Maximum cluster size: 4
mm0 <- geeglm(scc ~ my3 + TestDIMcat20 + factor(Parity) + DOSCC + Tx + factor(FARMID), data=dhi, id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(Parity) + 
##     DOSCC + Tx + factor(FARMID), data = dhi, id = CowID)
## 
##  Coefficients:
##                     Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)         3.332302  0.180892 339.35  < 2e-16 ***
## my3Mid production   0.042088  0.072003   0.34   0.5589    
## my3High production  0.075529  0.076253   0.98   0.3219    
## TestDIMcat2030     -0.541606  0.064565  70.37  < 2e-16 ***
## TestDIMcat2050     -0.556534  0.058934  89.18  < 2e-16 ***
## TestDIMcat2070     -0.418189  0.060615  47.60  5.2e-12 ***
## TestDIMcat2090     -0.286162  0.064782  19.51  1.0e-05 ***
## TestDIMcat20110    -0.124401  0.061089   4.15   0.0417 *  
## factor(Parity)2     0.133325  0.062498   4.55   0.0329 *  
## factor(Parity)3     0.302242  0.073518  16.90  3.9e-05 ***
## DOSCC               0.175860  0.026812  43.02  5.4e-11 ***
## TxA_SDCT           -0.000571  0.068141   0.00   0.9933    
## TxBDCT             -0.027835  0.063889   0.19   0.6631    
## factor(FARMID)2     0.054528  0.170947   0.10   0.7497    
## factor(FARMID)3    -0.042897  0.134559   0.10   0.7499    
## factor(FARMID)4     0.033644  0.131551   0.07   0.7981    
## factor(FARMID)5     0.031388  0.135680   0.05   0.8171    
## factor(FARMID)6    -0.507588  0.180322   7.92   0.0049 ** 
## factor(FARMID)7    -0.300935  0.149200   4.07   0.0437 *  
## factor(FARMID)8    -0.067311  0.279497   0.06   0.8097    
## factor(FARMID)9    -0.052484  0.298032   0.03   0.8602    
## factor(FARMID)10   -0.264201  0.226903   1.36   0.2443    
## factor(FARMID)11   -0.406134  0.240999   2.84   0.0919 .  
## factor(FARMID)12   -0.463233  0.217017   4.56   0.0328 *  
## factor(FARMID)13    0.128225  0.303021   0.18   0.6722    
## factor(FARMID)14    0.098630  0.254484   0.15   0.6983    
## factor(FARMID)15    0.022149  0.368868   0.00   0.9521    
## factor(FARMID)16   -0.061304  0.232800   0.07   0.7923    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0566
## Number of clusters:   1364  Maximum cluster size: 4
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(Parity) + 
##     DOSCC + Tx + factor(FARMID), data = dhi, id = CowID)
## 
##  Coefficients:
##                     Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)         3.332302  0.180892 339.35  < 2e-16 ***
## my3Mid production   0.042088  0.072003   0.34   0.5589    
## my3High production  0.075529  0.076253   0.98   0.3219    
## TestDIMcat2030     -0.541606  0.064565  70.37  < 2e-16 ***
## TestDIMcat2050     -0.556534  0.058934  89.18  < 2e-16 ***
## TestDIMcat2070     -0.418189  0.060615  47.60  5.2e-12 ***
## TestDIMcat2090     -0.286162  0.064782  19.51  1.0e-05 ***
## TestDIMcat20110    -0.124401  0.061089   4.15   0.0417 *  
## factor(Parity)2     0.133325  0.062498   4.55   0.0329 *  
## factor(Parity)3     0.302242  0.073518  16.90  3.9e-05 ***
## DOSCC               0.175860  0.026812  43.02  5.4e-11 ***
## TxA_SDCT           -0.000571  0.068141   0.00   0.9933    
## TxBDCT             -0.027835  0.063889   0.19   0.6631    
## factor(FARMID)2     0.054528  0.170947   0.10   0.7497    
## factor(FARMID)3    -0.042897  0.134559   0.10   0.7499    
## factor(FARMID)4     0.033644  0.131551   0.07   0.7981    
## factor(FARMID)5     0.031388  0.135680   0.05   0.8171    
## factor(FARMID)6    -0.507588  0.180322   7.92   0.0049 ** 
## factor(FARMID)7    -0.300935  0.149200   4.07   0.0437 *  
## factor(FARMID)8    -0.067311  0.279497   0.06   0.8097    
## factor(FARMID)9    -0.052484  0.298032   0.03   0.8602    
## factor(FARMID)10   -0.264201  0.226903   1.36   0.2443    
## factor(FARMID)11   -0.406134  0.240999   2.84   0.0919 .  
## factor(FARMID)12   -0.463233  0.217017   4.56   0.0328 *  
## factor(FARMID)13    0.128225  0.303021   0.18   0.6722    
## factor(FARMID)14    0.098630  0.254484   0.15   0.6983    
## factor(FARMID)15    0.022149  0.368868   0.00   0.9521    
## factor(FARMID)16   -0.061304  0.232800   0.07   0.7923    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0566
## Number of clusters:   1364  Maximum cluster size: 4
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~ my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(my3,TestDIMcat20,SCC,LCL,UCL))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=my3, colour=my3) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$LCL, ymax=emm$UCL,colour=my3,fill=my3), linetype=0, alpha=0.1) + geom_line(aes(colour=my3,linetype=my3),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

curve

Repeat analysis with different definition for my3 (i.e within herd)

Compare agreement between my3 types

cohen.kappa(x=cbind(cow$my3,cow$my3h))
## Call: cohen.kappa1(x = x, w = w, n.obs = n.obs, alpha = alpha, levels = levels)
## 
## Cohen Kappa and Weighted Kappa correlation coefficients and confidence boundaries 
##                  lower estimate upper
## unweighted kappa  0.46     0.50  0.53
## weighted kappa    0.72     0.74  0.76
## 
##  Number of subjects = 1485
cow %>% tabyl(my3,my3h)
##              my3 Low production Mid production High production
##   Low production            369            121               5
##   Mid production            134            248             113
##  High production              0            125             370

Create new my3 variable

qtr$my3 <- qtr$my3h
cow$my3 <- cow$my3h
dhi$my3 <- dhi$my3h
table1(~ Tx + factor(imido) + DOMY + DOSCC + factor(Parity) + factor(FARMID) + source | my3, data=qtr, na.is.category = F)
Low production
(N=1705)
Mid production
(N=1698)
High production
(N=1694)
Overall
(N=5097)
Tx
C_SDCT 561 (32.9%) 610 (35.9%) 678 (40.0%) 1849 (36.3%)
A_SDCT 439 (25.7%) 499 (29.4%) 417 (24.6%) 1355 (26.6%)
BDCT 705 (41.3%) 589 (34.7%) 599 (35.4%) 1893 (37.1%)
factor(imido)
0 1169 (75.1%) 1194 (75.3%) 1138 (73.6%) 3501 (74.7%)
1 387 (24.9%) 392 (24.7%) 408 (26.4%) 1187 (25.3%)
Missing 149 (8.7%) 112 (6.6%) 148 (8.7%) 409 (8.0%)
DOMY
Mean (SD) 18.4 (6.60) 27.0 (4.89) 34.2 (5.20) 26.5 (8.57)
Median [Min, Max] 19.5 [1.81, 29.0] 28.6 [13.6, 34.0] 34.5 [21.6, 49.4] 27.2 [1.81, 49.4]
DOSCC
Mean (SD) 4.93 (1.24) 4.29 (1.11) 4.05 (1.09) 4.42 (1.20)
Median [Min, Max] 4.87 [1.61, 8.35] 4.19 [1.61, 7.69] 3.91 [1.61, 8.59] 4.33 [1.61, 8.59]
Missing 0 (0%) 0 (0%) 4 (0.2%) 4 (0.1%)
factor(Parity)
1 574 (33.7%) 793 (46.7%) 869 (51.3%) 2236 (43.9%)
2 580 (34.0%) 445 (26.2%) 447 (26.4%) 1472 (28.9%)
3 551 (32.3%) 460 (27.1%) 378 (22.3%) 1389 (27.3%)
factor(FARMID)
1 99 (5.81%) 98 (5.77%) 98 (5.79%) 295 (5.79%)
2 108 (6.33%) 108 (6.36%) 108 (6.38%) 324 (6.36%)
3 279 (16.4%) 278 (16.4%) 278 (16.4%) 835 (16.4%)
4 424 (24.9%) 423 (24.9%) 423 (25.0%) 1270 (24.9%)
5 296 (17.4%) 296 (17.4%) 295 (17.4%) 887 (17.4%)
6 56 (3.28%) 56 (3.30%) 56 (3.31%) 168 (3.30%)
7 132 (7.74%) 131 (7.71%) 131 (7.73%) 394 (7.73%)
8 23 (1.35%) 23 (1.35%) 22 (1.30%) 68 (1.33%)
9 26 (1.52%) 25 (1.47%) 25 (1.48%) 76 (1.49%)
10 27 (1.58%) 27 (1.59%) 27 (1.59%) 81 (1.59%)
11 26 (1.52%) 26 (1.53%) 26 (1.53%) 78 (1.53%)
12 23 (1.35%) 23 (1.35%) 22 (1.30%) 68 (1.33%)
13 32 (1.88%) 31 (1.83%) 31 (1.83%) 94 (1.84%)
14 83 (4.87%) 83 (4.89%) 82 (4.84%) 248 (4.87%)
15 19 (1.11%) 18 (1.06%) 18 (1.06%) 55 (1.08%)
16 52 (3.05%) 52 (3.06%) 52 (3.07%) 156 (3.06%)
source
CA 311 (18.2%) 308 (18.1%) 305 (18.0%) 924 (18.1%)
US 1394 (81.8%) 1390 (81.9%) 1389 (82.0%) 4173 (81.9%)
table1(~ factor(imido) + DOMY + DOSCC + factor(Parity) | my3*Tx, data=qtr, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=561)
A_SDCT
(N=439)
BDCT
(N=705)
C_SDCT
(N=610)
A_SDCT
(N=499)
BDCT
(N=589)
C_SDCT
(N=678)
A_SDCT
(N=417)
BDCT
(N=599)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
factor(imido)
0 392 (75.4%) 301 (77.2%) 476 (73.7%) 411 (73.1%) 361 (76.0%) 422 (76.9%) 471 (75.2%) 276 (73.4%) 391 (71.9%) 1274 (74.6%) 938 (75.6%) 1289 (74.1%)
1 128 (24.6%) 89 (22.8%) 170 (26.3%) 151 (26.9%) 114 (24.0%) 127 (23.1%) 155 (24.8%) 100 (26.6%) 153 (28.1%) 434 (25.4%) 303 (24.4%) 450 (25.9%)
Missing 41 (7.3%) 49 (11.2%) 59 (8.4%) 48 (7.9%) 24 (4.8%) 40 (6.8%) 52 (7.7%) 41 (9.8%) 55 (9.2%) 141 (7.6%) 114 (8.4%) 154 (8.1%)
DOMY
Mean (SD) 18.3 (6.54) 18.8 (7.04) 18.2 (6.35) 26.6 (5.19) 27.8 (4.67) 26.6 (4.66) 34.0 (5.36) 35.0 (4.36) 34.0 (5.50) 26.8 (8.56) 27.1 (8.45) 25.8 (8.61)
Median [Min, Max] 19.1 [1.81, 27.7] 21.3 [2.72, 29.0] 18.6 [4.54, 28.1] 28.6 [13.6, 34.0] 29.5 [13.6, 34.0] 27.6 [14.5, 34.0] 34.0 [21.6, 49.4] 35.4 [26.3, 49.4] 34.0 [22.4, 49.4] 27.7 [1.81, 49.4] 28.6 [2.72, 49.4] 26.4 [4.54, 49.4]
DOSCC
Mean (SD) 4.98 (1.29) 4.82 (1.21) 4.96 (1.21) 4.21 (1.08) 4.38 (1.16) 4.29 (1.09) 4.00 (1.10) 4.09 (1.11) 4.09 (1.05) 4.36 (1.22) 4.43 (1.20) 4.48 (1.18)
Median [Min, Max] 4.95 [1.95, 8.17] 4.81 [1.61, 7.38] 4.87 [2.48, 8.35] 4.13 [1.61, 7.69] 4.30 [2.20, 7.56] 4.26 [1.61, 7.16] 3.89 [1.79, 8.59] 3.97 [2.30, 7.59] 4.06 [1.61, 7.38] 4.20 [1.61, 8.59] 4.38 [1.61, 7.59] 4.40 [1.61, 8.35]
Missing 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 0 (0%) 4 (0.7%) 0 (0%) 0 (0%) 4 (0.2%)
factor(Parity)
1 185 (33.0%) 128 (29.2%) 261 (37.0%) 323 (53.0%) 234 (46.9%) 236 (40.1%) 364 (53.7%) 237 (56.8%) 268 (44.7%) 872 (47.2%) 599 (44.2%) 765 (40.4%)
2 190 (33.9%) 174 (39.6%) 216 (30.6%) 125 (20.5%) 146 (29.3%) 174 (29.5%) 157 (23.2%) 108 (25.9%) 182 (30.4%) 472 (25.5%) 428 (31.6%) 572 (30.2%)
3 186 (33.2%) 137 (31.2%) 228 (32.3%) 162 (26.6%) 119 (23.8%) 179 (30.4%) 157 (23.2%) 72 (17.3%) 149 (24.9%) 505 (27.3%) 328 (24.2%) 556 (29.4%)
table1(~ factor(imipc) + factor(cure) + factor(newimi) | my3*Tx, data=qtr, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=561)
A_SDCT
(N=439)
BDCT
(N=705)
C_SDCT
(N=610)
A_SDCT
(N=499)
BDCT
(N=589)
C_SDCT
(N=678)
A_SDCT
(N=417)
BDCT
(N=599)
C_SDCT
(N=1849)
A_SDCT
(N=1355)
BDCT
(N=1893)
factor(imipc)
0 426 (75.9%) 342 (77.9%) 553 (78.4%) 470 (77.0%) 389 (78.0%) 472 (80.1%) 529 (78.0%) 321 (77.0%) 475 (79.3%) 1425 (77.1%) 1052 (77.6%) 1500 (79.2%)
1 135 (24.1%) 97 (22.1%) 152 (21.6%) 140 (23.0%) 110 (22.0%) 117 (19.9%) 149 (22.0%) 96 (23.0%) 124 (20.7%) 424 (22.9%) 303 (22.4%) 393 (20.8%)
factor(cure)
0 15 (11.7%) 12 (13.5%) 24 (14.1%) 19 (12.6%) 10 (8.77%) 12 (9.45%) 15 (9.68%) 14 (14.0%) 21 (13.7%) 49 (11.3%) 36 (11.9%) 57 (12.7%)
1 113 (88.3%) 77 (86.5%) 146 (85.9%) 132 (87.4%) 104 (91.2%) 115 (90.6%) 140 (90.3%) 86 (86.0%) 132 (86.3%) 385 (88.7%) 267 (88.1%) 393 (87.3%)
Missing 433 (77.2%) 350 (79.7%) 535 (75.9%) 459 (75.2%) 385 (77.2%) 462 (78.4%) 523 (77.1%) 317 (76.0%) 446 (74.5%) 1415 (76.5%) 1052 (77.6%) 1443 (76.2%)
factor(newimi)
0 399 (77.0%) 302 (78.2%) 510 (79.6%) 433 (78.3%) 373 (79.2%) 440 (80.7%) 491 (78.9%) 289 (78.7%) 436 (81.5%) 1323 (78.1%) 964 (78.8%) 1386 (80.5%)
1 119 (23.0%) 84 (21.8%) 131 (20.4%) 120 (21.7%) 98 (20.8%) 105 (19.3%) 131 (21.1%) 78 (21.3%) 99 (18.5%) 370 (21.9%) 260 (21.2%) 335 (19.5%)
Missing 43 (7.7%) 53 (12.1%) 64 (9.1%) 57 (9.3%) 28 (5.6%) 44 (7.5%) 56 (8.3%) 50 (12.0%) 64 (10.7%) 156 (8.4%) 131 (9.7%) 172 (9.1%)
table1(~ factor(CM) + factor(CMDP) | my3*Tx, data=cow, na.is.category = F)
Low production
Mid production
High production
Overall
C_SDCT
(N=169)
A_SDCT
(N=128)
BDCT
(N=206)
C_SDCT
(N=172)
A_SDCT
(N=147)
BDCT
(N=175)
C_SDCT
(N=195)
A_SDCT
(N=119)
BDCT
(N=174)
C_SDCT
(N=536)
A_SDCT
(N=394)
BDCT
(N=555)
factor(CM)
0 153 (90.5%) 109 (85.2%) 173 (84.0%) 149 (86.6%) 132 (89.8%) 148 (84.6%) 170 (87.2%) 105 (88.2%) 143 (82.2%) 472 (88.1%) 346 (87.8%) 464 (83.6%)
1 16 (9.47%) 19 (14.8%) 33 (16.0%) 23 (13.4%) 15 (10.2%) 27 (15.4%) 25 (12.8%) 14 (11.8%) 31 (17.8%) 64 (11.9%) 48 (12.2%) 91 (16.4%)
factor(CMDP)
0 169 (100%) 128 (100%) 206 (100%) 171 (99.4%) 147 (100%) 175 (100%) 194 (99.5%) 116 (97.5%) 173 (99.4%) 534 (99.6%) 391 (99.2%) 554 (99.8%)
1 0 (0%) 0 (0%) 0 (0%) 1 (0.581%) 0 (0%) 0 (0%) 1 (0.513%) 3 (2.52%) 1 (0.575%) 2 (0.373%) 3 (0.761%) 1 (0.180%)
cow %>% tabyl(source,my3) %>% adorn_totals("row") %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 1) %>%
  adorn_ns %>%
  adorn_title
##                    my3                               
##  source Low production Mid production High production
##      CA    35.0%  (96)    32.8%  (90)     32.1%  (88)
##      US    33.6% (407)    33.4% (404)     33.0% (400)
##   Total    33.9% (503)    33.3% (494)     32.9% (488)
cow %>% tabyl(FARMID,my3) %>% adorn_totals("row") %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 1) %>%
  adorn_ns %>%
  adorn_title
##                    my3                               
##  FARMID Low production Mid production High production
##       1    34.2%  (26)    32.9%  (25)     32.9%  (25)
##       2    34.1%  (28)    32.9%  (27)     32.9%  (27)
##       3    33.5%  (88)    33.5%  (88)     33.1%  (87)
##       4    33.5% (130)    33.2% (129)     33.2% (129)
##       5    33.5%  (81)    33.5%  (81)     33.1%  (80)
##       6    34.0%  (16)    34.0%  (16)     31.9%  (15)
##       7    33.6%  (38)    33.6%  (38)     32.7%  (37)
##       8    33.3%   (6)    33.3%   (6)     33.3%   (6)
##       9    36.0%   (9)    32.0%   (8)     32.0%   (8)
##      10    34.8%   (8)    34.8%   (8)     30.4%   (7)
##      11    36.4%   (8)    31.8%   (7)     31.8%   (7)
##      12    36.8%   (7)    31.6%   (6)     31.6%   (6)
##      13    34.5%  (10)    34.5%  (10)     31.0%   (9)
##      14    34.2%  (26)    32.9%  (25)     32.9%  (25)
##      15    37.5%   (6)    31.2%   (5)     31.2%   (5)
##      16    34.8%  (16)    32.6%  (15)     32.6%  (15)
##   Total    33.9% (503)    33.3% (494)     32.9% (488)

Extract crude frequencies

cm.freq.tx <- cow %>% group_by(Tx) %>% summarise(
  n.cm.0 = sum(CM==0),
  n.cm.1 = sum(CM==1),
  p.cm.1 = n.cm.1/(n.cm.1+n.cm.0),
  cm = paste0(n.cm.1, "/" ,n.cm.0 + n.cm.1, " (",100*round(p.cm.1,4),"%)")
)
cm.freq.tx$my3 <- "All production levels"
cm.freq.my3 <- cow %>% group_by(my3,Tx) %>% summarise(
  n.cm.0 = sum(CM==0),
  n.cm.1 = sum(CM==1),
  p.cm.1 = n.cm.1/(n.cm.1+n.cm.0),
  cm = paste0(n.cm.1, "/" ,n.cm.0 + n.cm.1, " (",100*round(p.cm.1,4),"%)")
)

cm.freq <- rbind(cm.freq.tx,cm.freq.my3) %>% select(my3,Tx,cm)
cm.freq$risk <- cm.freq$cm
cm.freq$outcome <- "Clinical mastitis"
cm.freq$cm <- NULL

#Cull
Cull2.freq.tx <- cow %>% group_by(Tx) %>% summarise(
  n.Cull2.0 = sum(Cull2==0),
  n.Cull2.1 = sum(Cull2==1),
  p.Cull2.1 = n.Cull2.1/(n.Cull2.1+n.Cull2.0),
  Cull2 = paste0(n.Cull2.1, "/" ,n.Cull2.0 + n.Cull2.1, " (",100*round(p.Cull2.1,4),"%)")
)
Cull2.freq.tx$my3 <- "All production levels"
Cull2.freq.my3 <- cow %>% group_by(my3,Tx) %>% summarise(
  n.Cull2.0 = sum(Cull2==0),
  n.Cull2.1 = sum(Cull2==1),
  p.Cull2.1 = n.Cull2.1/(n.Cull2.1+n.Cull2.0),
  Cull2 = paste0(n.Cull2.1, "/" ,n.Cull2.0 + n.Cull2.1, " (",100*round(p.Cull2.1,4),"%)")
)

Cull2.freq <- rbind(Cull2.freq.tx,Cull2.freq.my3) %>% select(my3,Tx,Cull2)
Cull2.freq$risk <- Cull2.freq$Cull2
Cull2.freq$outcome <- "Cull"
Cull2.freq$Cull2 <- NULL


imi.freq.tx <- qtr %>% group_by(Tx) %>% summarise(
  n.imipc.0 = sum(imipc==0),
  n.imipc.1 = sum(imipc==1),
  p.imipc.1 = n.imipc.1/(n.imipc.1+n.imipc.0),
  imipc = paste0(n.imipc.1, "/" ,n.imipc.0 + n.imipc.1, " (",100*round(p.imipc.1,4),"%)"),
  n.newimi.0 = sum(newimi==0,na.rm=T),
  n.newimi.1 = sum(newimi==1,na.rm=T),
  p.newimi.1 = n.newimi.1/(n.newimi.1+n.newimi.0),
  newimi = paste0(n.newimi.1, "/" ,n.newimi.0 + n.newimi.1, " (",100*round(p.newimi.1,4),"%)"),
  n.cure.0 = sum(cure==0,na.rm=T),
  n.cure.1 = sum(cure==1,na.rm=T),
  p.cure.1 = n.cure.1/(n.cure.1+n.cure.0),
  imicure = paste0(n.cure.1, "/" ,n.cure.0 + n.cure.1, " (",100*round(p.cure.1,4),"%)"),
)

imi.freq.tx$my3 <- "All production levels"

imi.freq.my3 <- qtr %>% group_by(my3,Tx) %>% summarise(
  n.imipc.0 = sum(imipc==0),
  n.imipc.1 = sum(imipc==1),
  p.imipc.1 = n.imipc.1/(n.imipc.1+n.imipc.0),
  imipc = paste0(n.imipc.1, "/" ,n.imipc.0 + n.imipc.1, " (",100*round(p.imipc.1,4),"%)"),
  n.newimi.0 = sum(newimi==0,na.rm=T),
  n.newimi.1 = sum(newimi==1,na.rm=T),
  p.newimi.1 = n.newimi.1/(n.newimi.1+n.newimi.0),
  newimi = paste0(n.newimi.1, "/" ,n.newimi.0 + n.newimi.1, " (",100*round(p.newimi.1,4),"%)"),
  n.cure.0 = sum(cure==0,na.rm=T),
  n.cure.1 = sum(cure==1,na.rm=T),
  p.cure.1 = n.cure.1/(n.cure.1+n.cure.0),
  imicure = paste0(n.cure.1, "/" ,n.cure.0 + n.cure.1, " (",100*round(p.cure.1,4),"%)"),
)

colnames(imi.freq.tx)
##  [1] "Tx"         "n.imipc.0"  "n.imipc.1"  "p.imipc.1"  "imipc"     
##  [6] "n.newimi.0" "n.newimi.1" "p.newimi.1" "newimi"     "n.cure.0"  
## [11] "n.cure.1"   "p.cure.1"   "imicure"    "my3"
colnames(imi.freq.my3)
##  [1] "my3"        "Tx"         "n.imipc.0"  "n.imipc.1"  "p.imipc.1" 
##  [6] "imipc"      "n.newimi.0" "n.newimi.1" "p.newimi.1" "newimi"    
## [11] "n.cure.0"   "n.cure.1"   "p.cure.1"   "imicure"
imi.freq <- rbind(imi.freq.tx,imi.freq.my3) %>% select(my3,Tx,imipc,newimi,imicure) %>% pivot_longer(
  cols = imipc:imicure,
  names_to = "outcome",
  values_to = "risk"
)

freq.tab <- rbind(imi.freq,cm.freq,Cull2.freq) %>% arrange(outcome,my3)

write.csv(freq.tab,"freq.table2.csv")

Analysis

Post calving clinical mastitis

library(survival)
library(survminer)

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow)
summary(KM)
## Call: survfit(formula = Surv(CMTAR, CM) ~ Tx, data = cow)
## 
##                 Tx=C_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     2    535       2    0.996 0.00264        0.991        1.000
##     3    531       1    0.994 0.00323        0.988        1.000
##     4    528       2    0.991 0.00418        0.982        0.999
##     9    522       1    0.989 0.00458        0.980        0.998
##    11    520       1    0.987 0.00495        0.977        0.997
##    15    517       1    0.985 0.00529        0.975        0.995
##    16    516       1    0.983 0.00562        0.972        0.994
##    18    515       1    0.981 0.00592        0.970        0.993
##    21    514       1    0.979 0.00621        0.967        0.991
##    24    511       1    0.977 0.00649        0.965        0.990
##    25    509       1    0.975 0.00675        0.962        0.989
##    27    508       1    0.973 0.00701        0.960        0.987
##    31    507       1    0.972 0.00725        0.957        0.986
##    32    506       1    0.970 0.00749        0.955        0.984
##    33    505       1    0.968 0.00771        0.953        0.983
##    35    502       1    0.966 0.00794        0.950        0.981
##    38    500       1    0.964 0.00815        0.948        0.980
##    40    498       2    0.960 0.00857        0.943        0.977
##    42    496       1    0.958 0.00877        0.941        0.975
##    43    494       1    0.956 0.00896        0.939        0.974
##    47    491       1    0.954 0.00915        0.936        0.972
##    50    489       1    0.952 0.00934        0.934        0.971
##    52    488       1    0.950 0.00952        0.932        0.969
##    53    487       1    0.948 0.00970        0.929        0.967
##    58    485       1    0.946 0.00987        0.927        0.966
##    59    483       1    0.944 0.01005        0.925        0.964
##    60    482       1    0.942 0.01021        0.923        0.963
##    66    480       1    0.940 0.01038        0.920        0.961
##    70    479       1    0.938 0.01054        0.918        0.959
##    71    477       3    0.933 0.01101        0.911        0.954
##    72    474       1    0.931 0.01116        0.909        0.953
##    73    473       1    0.929 0.01131        0.907        0.951
##    76    470       1    0.927 0.01146        0.904        0.949
##    77    468       1    0.925 0.01160        0.902        0.948
##    78    467       1    0.923 0.01175        0.900        0.946
##    80    465       1    0.921 0.01189        0.898        0.944
##    83    464       1    0.919 0.01203        0.895        0.943
##    84    463       1    0.917 0.01216        0.893        0.941
##    86    461       1    0.915 0.01230        0.891        0.939
##    88    460       1    0.913 0.01243        0.889        0.937
##    94    459       1    0.911 0.01256        0.886        0.936
##    95    458       1    0.909 0.01269        0.884        0.934
##    97    456       2    0.905 0.01295        0.880        0.931
##   100    454       2    0.901 0.01319        0.875        0.927
##   101    452       1    0.899 0.01331        0.873        0.925
##   103    451       1    0.897 0.01343        0.871        0.924
##   105    450       1    0.895 0.01355        0.869        0.922
##   106    449       1    0.893 0.01366        0.866        0.920
##   108    448       1    0.891 0.01378        0.864        0.918
##   110    447       1    0.889 0.01389        0.862        0.917
##   112    445       1    0.887 0.01400        0.860        0.915
##   115    444       2    0.883 0.01422        0.855        0.911
##   117    439       3    0.877 0.01454        0.849        0.906
##   120    434       1    0.875 0.01465        0.847        0.904
## 
##                 Tx=A_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    394       2    0.995 0.00358        0.988        1.000
##     2    390       1    0.992 0.00439        0.984        1.000
##     3    388       2    0.987 0.00566        0.976        0.998
##     6    384       1    0.985 0.00620        0.973        0.997
##    15    379       2    0.979 0.00718        0.966        0.994
##    19    377       1    0.977 0.00761        0.962        0.992
##    24    374       2    0.972 0.00842        0.955        0.988
##    29    371       1    0.969 0.00880        0.952        0.986
##    31    370       1    0.966 0.00915        0.949        0.985
##    35    368       2    0.961 0.00983        0.942        0.981
##    37    366       1    0.959 0.01015        0.939        0.979
##    42    364       1    0.956 0.01046        0.936        0.977
##    43    362       2    0.951 0.01104        0.929        0.973
##    45    359       1    0.948 0.01133        0.926        0.970
##    46    358       2    0.943 0.01187        0.920        0.966
##    50    356       1    0.940 0.01212        0.917        0.964
##    51    355       2    0.935 0.01262        0.910        0.960
##    53    351       2    0.929 0.01310        0.904        0.955
##    54    349       1    0.927 0.01333        0.901        0.953
##    56    346       1    0.924 0.01356        0.898        0.951
##    57    345       1    0.921 0.01378        0.895        0.949
##    58    344       1    0.919 0.01400        0.892        0.947
##    60    342       1    0.916 0.01421        0.889        0.944
##    61    341       1    0.913 0.01442        0.886        0.942
##    62    340       1    0.911 0.01463        0.882        0.940
##    71    339       2    0.905 0.01503        0.876        0.935
##    75    337       2    0.900 0.01541        0.870        0.931
##    76    335       2    0.895 0.01578        0.864        0.926
##    81    333       1    0.892 0.01596        0.861        0.924
##    90    329       1    0.889 0.01614        0.858        0.921
##    99    327       2    0.884 0.01649        0.852        0.917
##   107    323       1    0.881 0.01667        0.849        0.914
##   111    322       1    0.878 0.01684        0.846        0.912
##   112    321       1    0.876 0.01701        0.843        0.909
##   119    319       1    0.873 0.01717        0.840        0.907
## 
##                 Tx=BDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     0    555       1    0.998 0.00180        0.995        1.000
##     1    554       2    0.995 0.00311        0.989        1.000
##     2    552       1    0.993 0.00359        0.986        1.000
##     3    551       3    0.987 0.00474        0.978        0.997
##     4    547       1    0.986 0.00506        0.976        0.996
##     5    545       2    0.982 0.00565        0.971        0.993
##     6    542       1    0.980 0.00592        0.969        0.992
##     7    539       1    0.978 0.00619        0.966        0.991
##     9    536       1    0.977 0.00644        0.964        0.989
##    10    534       1    0.975 0.00668        0.962        0.988
##    11    533       2    0.971 0.00714        0.957        0.985
##    14    529       1    0.969 0.00736        0.955        0.984
##    16    528       1    0.967 0.00757        0.953        0.982
##    17    527       1    0.966 0.00777        0.950        0.981
##    18    526       1    0.964 0.00797        0.948        0.979
##    19    525       2    0.960 0.00835        0.944        0.977
##    20    523       3    0.955 0.00889        0.937        0.972
##    22    518       2    0.951 0.00923        0.933        0.969
##    26    514       1    0.949 0.00940        0.931        0.968
##    27    513       1    0.947 0.00956        0.929        0.966
##    31    509       1    0.945 0.00972        0.926        0.965
##    33    508       1    0.943 0.00988        0.924        0.963
##    34    507       1    0.942 0.01003        0.922        0.961
##    35    506       1    0.940 0.01018        0.920        0.960
##    36    505       1    0.938 0.01033        0.918        0.958
##    37    503       1    0.936 0.01048        0.916        0.957
##    38    502       2    0.932 0.01076        0.911        0.954
##    40    499       1    0.930 0.01090        0.909        0.952
##    42    496       1    0.928 0.01104        0.907        0.950
##    47    495       1    0.927 0.01118        0.905        0.949
##    50    493       1    0.925 0.01131        0.903        0.947
##    52    492       1    0.923 0.01144        0.901        0.946
##    53    491       2    0.919 0.01170        0.896        0.942
##    57    489       1    0.917 0.01183        0.894        0.941
##    58    488       2    0.913 0.01207        0.890        0.937
##    59    486       1    0.912 0.01219        0.888        0.936
##    61    485       1    0.910 0.01231        0.886        0.934
##    63    483       1    0.908 0.01243        0.884        0.932
##    64    482       1    0.906 0.01255        0.882        0.931
##    66    481       1    0.904 0.01266        0.880        0.929
##    68    480       2    0.900 0.01288        0.875        0.926
##    70    478       1    0.898 0.01299        0.873        0.924
##    73    476       1    0.896 0.01310        0.871        0.923
##    74    475       2    0.893 0.01332        0.867        0.919
##    76    473       1    0.891 0.01342        0.865        0.918
##    78    472       2    0.887 0.01363        0.861        0.914
##    79    469       1    0.885 0.01373        0.859        0.912
##    81    468       1    0.883 0.01383        0.857        0.911
##    82    467       1    0.881 0.01393        0.855        0.909
##    83    466       1    0.879 0.01403        0.852        0.907
##    84    465       2    0.876 0.01422        0.848        0.904
##    87    463       1    0.874 0.01431        0.846        0.902
##    91    462       2    0.870 0.01450        0.842        0.899
##    92    460       2    0.866 0.01468        0.838        0.896
##    95    457       2    0.862 0.01486        0.834        0.892
##    96    455       1    0.861 0.01495        0.832        0.890
##    97    454       1    0.859 0.01503        0.830        0.889
##   101    453       1    0.857 0.01512        0.828        0.887
##   102    452       1    0.855 0.01521        0.826        0.885
##   103    451       2    0.851 0.01537        0.821        0.882
##   104    449       1    0.849 0.01545        0.819        0.880
##   107    448       1    0.847 0.01554        0.817        0.878
##   109    447       1    0.845 0.01562        0.815        0.877
##   110    446       1    0.844 0.01570        0.813        0.875
##   111    445       1    0.842 0.01577        0.811        0.873
##   112    444       1    0.840 0.01585        0.809        0.871
##   113    443       2    0.836 0.01601        0.805        0.868
##   114    441       1    0.834 0.01608        0.803        0.866
##   115    440       2    0.830 0.01623        0.799        0.863
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Clinical mastitis - all production groups combined

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow)
km.all <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Low production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="Low production"))
km.low <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "Low production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Mid production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="Mid production"))
km.mid <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "Mid production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

High production cows

KM <- survfit(Surv(CMTAR, CM) ~ Tx, data = cow %>% filter(my3=="High production"))
km.high <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.20),
           legend = "bottom",
           title = "High production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)
#png("plot.png", width=500*10, height=720*10, res=100*15)
splots <- list(km.all,
               km.low,
               km.mid,
               km.high)

arrange_ggsurvplots(splots,ncol=2,nrow=2)

#dev.off()

Cox regression

All production groups pooled together

library(broom)
cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")
SR <- coxph(Surv(CMTAR, CM) ~ Tx + my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(SR)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(CMTAR, CM)
##                Df Chisq Pr(>Chisq)  
## Tx              2  4.72      0.094 .
## my3             2  2.60      0.273  
## factor(Parity)  2  8.42      0.015 *
## DOSCC           1  1.27      0.259  
## factor(source)  1  0.83      0.363  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(SR)
## Call:
## coxph(formula = Surv(CMTAR, CM) ~ Tx + my3 + factor(Parity) + 
##     DOSCC + factor(source), data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 203 
##    (1 observation deleted due to missingness)
## 
##                       coef exp(coef) se(coef) robust se     z Pr(>|z|)   
## TxC_SDCT           -0.3250    0.7225   0.1636    0.1667 -1.95   0.0512 . 
## TxA_SDCT           -0.2295    0.7950   0.1868    0.1452 -1.58   0.1140   
## my3Mid production   0.0664    1.0687   0.1777    0.2351  0.28   0.7776   
## my3High production  0.2005    1.2221   0.1783    0.2221  0.90   0.3666   
## factor(Parity)2     0.3213    1.3790   0.1745    0.1110  2.89   0.0038 **
## factor(Parity)3     0.2936    1.3412   0.1795    0.2302  1.28   0.2021   
## DOSCC               0.1171    1.1242   0.0627    0.1037  1.13   0.2591   
## factor(source)US   -0.2463    0.7817   0.1763    0.2708 -0.91   0.3631   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT               0.723      1.384     0.521      1.00
## TxA_SDCT               0.795      1.258     0.598      1.06
## my3Mid production      1.069      0.936     0.674      1.69
## my3High production     1.222      0.818     0.791      1.89
## factor(Parity)2        1.379      0.725     1.109      1.71
## factor(Parity)3        1.341      0.746     0.854      2.11
## DOSCC                  1.124      0.890     0.917      1.38
## factor(source)US       0.782      1.279     0.460      1.33
## 
## Concordance= 0.583  (se = 0.027 )
## Likelihood ratio test= 17.8  on 8 df,   p=0.02
## Wald test            = 82.6  on 8 df,   p=1e-14
## Score (logrank) test = 18  on 8 df,   p=0.02,   Robust = 10.9  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).
cm.main <- tidy(SR,conf.int = T,exp=T)[1:2,] %>% select(term,estimate,conf.low,conf.high)
cm.main <- cm.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

cm.main$production <- "All production levels"
cm.main$contrast <- "none"
cm.main$outcome <- "Clinical mastitis"

Model diagnostics

SR <- cox.zph(SR)
SR
##                chisq df    p
## Tx             3.288  2 0.19
## my3            0.369  2 0.83
## factor(Parity) 2.111  2 0.35
## DOSCC          0.940  1 0.33
## factor(source) 1.504  1 0.22
## GLOBAL         8.886  8 0.35
ggcoxzph(SR)

Interaction model

CPH <- coxph(Surv(CMTAR, CM) ~ Tx*my3 + Parity + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(CMTAR, CM)
##                Df Chisq Pr(>Chisq)    
## Tx              2 30.51    2.4e-07 ***
## my3             2  8.02      0.018 *  
## Parity          2  7.49      0.024 *  
## DOSCC           1  1.27      0.259    
## factor(source)  1  0.81      0.367    
## Tx:my3          4  9.92      0.042 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(CPH)
## Call:
## coxph(formula = Surv(CMTAR, CM) ~ Tx + my3 + Parity + DOSCC + 
##     factor(source) + Tx:my3, data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 203 
##    (1 observation deleted due to missingness)
## 
##                                 coef exp(coef) se(coef) robust se     z
## TxC_SDCT                    -0.56604   0.56777  0.30468   0.50495 -1.12
## TxA_SDCT                    -0.00437   0.99564  0.29268   0.33655 -0.01
## my3Mid production            0.03085   1.03133  0.26315   0.43687  0.07
## my3High production           0.21381   1.23838  0.25637   0.38327  0.56
## Parity2                      0.32306   1.38135  0.17498   0.11811  2.74
## Parity3                      0.28850   1.33443  0.17974   0.23082  1.25
## DOSCC                        0.12092   1.12854  0.06300   0.10724  1.13
## factor(source)US            -0.24794   0.78041  0.17650   0.27466 -0.90
## TxC_SDCT:my3Mid production   0.41435   1.51339  0.41674   0.64595  0.64
## TxA_SDCT:my3Mid production  -0.33488   0.71543  0.43281   0.43862 -0.76
## TxC_SDCT:my3High production  0.26960   1.30944  0.40700   0.57677  0.47
## TxA_SDCT:my3High production -0.35729   0.69957  0.43314   0.41758 -0.86
##                             Pr(>|z|)   
## TxC_SDCT                      0.2623   
## TxA_SDCT                      0.9896   
## my3Mid production             0.9437   
## my3High production            0.5769   
## Parity2                       0.0062 **
## Parity3                       0.2113   
## DOSCC                         0.2595   
## factor(source)US              0.3667   
## TxC_SDCT:my3Mid production    0.5212   
## TxA_SDCT:my3Mid production    0.4452   
## TxC_SDCT:my3High production   0.6402   
## TxA_SDCT:my3High production   0.3922   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                             exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT                        0.568      1.761     0.211      1.53
## TxA_SDCT                        0.996      1.004     0.515      1.93
## my3Mid production               1.031      0.970     0.438      2.43
## my3High production              1.238      0.808     0.584      2.62
## Parity2                         1.381      0.724     1.096      1.74
## Parity3                         1.334      0.749     0.849      2.10
## DOSCC                           1.129      0.886     0.915      1.39
## factor(source)US                0.780      1.281     0.456      1.34
## TxC_SDCT:my3Mid production      1.513      0.661     0.427      5.37
## TxA_SDCT:my3Mid production      0.715      1.398     0.303      1.69
## TxC_SDCT:my3High production     1.309      0.764     0.423      4.06
## TxA_SDCT:my3High production     0.700      1.429     0.309      1.59
## 
## Concordance= 0.588  (se = 0.032 )
## Likelihood ratio test= 20.7  on 12 df,   p=0.06
## Wald test            = 251  on 12 df,   p=<2e-16
## Score (logrank) test = 20.7  on 12 df,   p=0.06,   Robust = 15.3  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).

Model diagnostics

SR <- cox.zph(CPH)
SR
##                 chisq df    p
## Tx              3.242  2 0.20
## my3             0.370  2 0.83
## Parity          2.117  2 0.35
## DOSCC           0.953  1 0.33
## factor(source)  1.507  1 0.22
## Tx:my3          4.929  4 0.29
## GLOBAL         17.342 12 0.14
ggcoxzph(SR)

cow$Tx <- fct_relevel(cow$Tx,"C_SDCT","A_SDCT","BDCT")
CPH <- coxph(Surv(CMTAR, CM) ~ Tx*my3 + strata(Parity) + DOSCC + factor(source)  + cluster(FARMID), data=cow)
out <- emmeans(CPH,pairwise ~ Tx | my3,adjust = "none",type = "response") %>% confint


tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["ratio"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT / A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Clinical mastitis"

tab.cm <- tab
tab.cm
##        production      contrast            estimate           outcome
## 1  Low production C_SDCT / BDCT 0.57 (0.21 to 1.53) Clinical mastitis
## 2  Low production A_SDCT / BDCT 0.99 (0.52 to 1.92) Clinical mastitis
## 3  Mid production C_SDCT / BDCT 0.86 (0.49 to 1.52) Clinical mastitis
## 4  Mid production A_SDCT / BDCT  0.71 (0.42 to 1.2) Clinical mastitis
## 5 High production C_SDCT / BDCT  0.74 (0.4 to 1.39) Clinical mastitis
## 6 High production A_SDCT / BDCT  0.7 (0.51 to 0.96) Clinical mastitis

Culling 1-120 DIM

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
summary(KM)
## Call: survfit(formula = Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
## 
##                 Tx=C_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    536       1    0.998 0.00186        0.994        1.000
##     2    535       2    0.994 0.00322        0.988        1.000
##     3    533       2    0.991 0.00415        0.983        0.999
##     4    531       2    0.987 0.00490        0.977        0.997
##     5    529       1    0.985 0.00524        0.975        0.995
##     7    528       1    0.983 0.00555        0.972        0.994
##     9    527       1    0.981 0.00584        0.970        0.993
##    11    526       1    0.979 0.00612        0.968        0.992
##    13    525       1    0.978 0.00639        0.965        0.990
##    21    524       1    0.976 0.00664        0.963        0.989
##    23    523       1    0.974 0.00689        0.960        0.987
##    24    522       1    0.972 0.00712        0.958        0.986
##    33    521       1    0.970 0.00735        0.956        0.985
##    34    520       1    0.968 0.00757        0.954        0.983
##    36    519       1    0.966 0.00778        0.951        0.982
##    37    518       2    0.963 0.00819        0.947        0.979
##    38    516       1    0.961 0.00838        0.945        0.977
##    42    515       1    0.959 0.00857        0.942        0.976
##    43    514       1    0.957 0.00875        0.940        0.974
##    44    513       2    0.953 0.00911        0.936        0.971
##    45    511       1    0.951 0.00928        0.933        0.970
##    49    510       1    0.950 0.00945        0.931        0.968
##    57    509       1    0.948 0.00961        0.929        0.967
##    58    508       1    0.946 0.00977        0.927        0.965
##    63    507       1    0.944 0.00993        0.925        0.964
##    69    506       1    0.942 0.01008        0.923        0.962
##    70    505       1    0.940 0.01023        0.920        0.961
##    74    504       1    0.938 0.01038        0.918        0.959
##    75    503       1    0.937 0.01053        0.916        0.957
##    76    502       1    0.935 0.01067        0.914        0.956
##    79    501       1    0.933 0.01081        0.912        0.954
##    84    500       1    0.931 0.01095        0.910        0.953
##    89    499       1    0.929 0.01109        0.908        0.951
##    91    498       2    0.925 0.01135        0.903        0.948
##    96    496       2    0.922 0.01161        0.899        0.945
##    99    494       1    0.920 0.01173        0.897        0.943
##   111    493       2    0.916 0.01198        0.893        0.940
##   114    491       1    0.914 0.01210        0.891        0.938
##   115    490       1    0.912 0.01222        0.889        0.937
##   116    489       2    0.909 0.01245        0.885        0.933
##   117    487       2    0.905 0.01267        0.880        0.930
##   119    485       1    0.903 0.01278        0.878        0.928
## 
##                 Tx=A_SDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    394       2    0.995 0.00358        0.988        1.000
##     2    392       1    0.992 0.00438        0.984        1.000
##     3    391       1    0.990 0.00505        0.980        1.000
##     4    390       1    0.987 0.00564        0.976        0.998
##     6    389       1    0.985 0.00617        0.973        0.997
##     9    388       1    0.982 0.00666        0.969        0.995
##    13    387       3    0.975 0.00792        0.959        0.990
##    18    384       1    0.972 0.00830        0.956        0.988
##    19    383       1    0.970 0.00866        0.953        0.987
##    21    382       1    0.967 0.00900        0.950        0.985
##    24    381       1    0.964 0.00933        0.946        0.983
##    33    380       1    0.962 0.00964        0.943        0.981
##    36    379       1    0.959 0.00994        0.940        0.979
##    41    378       1    0.957 0.01024        0.937        0.977
##    42    377       1    0.954 0.01052        0.934        0.975
##    44    376       1    0.952 0.01079        0.931        0.973
##    45    375       1    0.949 0.01106        0.928        0.971
##    51    374       1    0.947 0.01132        0.925        0.969
##    52    373       1    0.944 0.01157        0.922        0.967
##    54    372       1    0.942 0.01181        0.919        0.965
##    55    371       1    0.939 0.01205        0.916        0.963
##    58    370       1    0.937 0.01228        0.913        0.961
##    60    369       1    0.934 0.01251        0.910        0.959
##    62    368       1    0.931 0.01273        0.907        0.957
##    74    367       1    0.929 0.01294        0.904        0.955
##    77    366       2    0.924 0.01336        0.898        0.950
##    81    364       1    0.921 0.01356        0.895        0.948
##    85    363       1    0.919 0.01376        0.892        0.946
##    86    362       1    0.916 0.01396        0.889        0.944
##    87    361       1    0.914 0.01415        0.886        0.942
##    88    360       1    0.911 0.01433        0.884        0.940
##    90    359       1    0.909 0.01452        0.881        0.938
##    97    358       1    0.906 0.01470        0.878        0.935
##    99    357       1    0.904 0.01487        0.875        0.933
##   102    356       1    0.901 0.01505        0.872        0.931
##   105    355       1    0.898 0.01522        0.869        0.929
##   111    354       1    0.896 0.01538        0.866        0.927
##   116    353       1    0.893 0.01555        0.863        0.924
## 
##                 Tx=BDCT 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     3    555       1    0.998 0.00180        0.995        1.000
##     4    554       1    0.996 0.00254        0.991        1.000
##     5    553       1    0.995 0.00311        0.989        1.000
##     6    552       2    0.991 0.00401        0.983        0.999
##     7    550       2    0.987 0.00474        0.978        0.997
##     9    548       1    0.986 0.00506        0.976        0.996
##    11    547       2    0.982 0.00565        0.971        0.993
##    13    545       2    0.978 0.00617        0.966        0.991
##    15    543       1    0.977 0.00642        0.964        0.989
##    19    542       2    0.973 0.00688        0.960        0.987
##    21    540       2    0.969 0.00731        0.955        0.984
##    22    538       2    0.966 0.00772        0.951        0.981
##    23    536       2    0.962 0.00810        0.946        0.978
##    27    534       2    0.959 0.00846        0.942        0.975
##    28    532       2    0.955 0.00880        0.938        0.972
##    29    530       1    0.953 0.00897        0.936        0.971
##    30    529       1    0.951 0.00913        0.934        0.969
##    36    528       2    0.948 0.00945        0.929        0.966
##    38    526       1    0.946 0.00960        0.927        0.965
##    39    525       1    0.944 0.00975        0.925        0.963
##    41    524       2    0.941 0.01004        0.921        0.960
##    49    522       2    0.937 0.01032        0.917        0.957
##    54    520       1    0.935 0.01045        0.915        0.956
##    61    519       1    0.933 0.01059        0.913        0.954
##    64    518       1    0.932 0.01072        0.911        0.953
##    65    517       1    0.930 0.01085        0.909        0.951
##    72    516       1    0.928 0.01098        0.907        0.950
##    75    515       1    0.926 0.01110        0.905        0.948
##    78    514       1    0.924 0.01123        0.903        0.947
##    79    513       1    0.923 0.01135        0.901        0.945
##    83    512       1    0.921 0.01147        0.899        0.943
##    93    511       2    0.917 0.01170        0.894        0.940
##    99    509       2    0.914 0.01193        0.890        0.937
##   101    507       1    0.912 0.01204        0.888        0.936
##   112    506       1    0.910 0.01215        0.886        0.934
##   115    505       1    0.908 0.01226        0.884        0.932
##   116    504       1    0.906 0.01237        0.882        0.931
##   117    503       1    0.905 0.01248        0.880        0.929
##   118    502       1    0.903 0.01258        0.878        0.928
ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.2),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           ylab="Cumulative incidence of \n clinical mastitis",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Culling - all production groups combined

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow)
km.all <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "All production levels", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Low production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="Low production"))
km.low <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "Low production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

Mid production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="Mid production"))
km.mid <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),legend.labs = c("Culture SDCT","Algorithm SDCT","Blanket DCT"),
           legend = "none",
           title = "Mid production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)

High production cows

KM <- survfit(Surv(Cull2TAR, Cull2) ~ Tx, data = cow %>% filter(my3=="High production"))
km.high <- ggsurvplot(KM, data = cow,
           pval.coord = c(0.03, 0.20),
           fun = "event",xlim=c(0,120),ylim=c(0,0.25),
           legend = "bottom",
           title = "High production", pval = F, censor=F, conf.int = F,risk.table = F,
           surv.plot.height = 5, tables.theme = theme_cleantable(),
           ggtheme = theme_bw(base_family = "Times"), xlab="Days in milk",
           #ylab="Cumulative incidence of \n clinical mastitis",
           ylab = "",
           palette = "Set1",risk.table.fontsize=3,break.time.by = 20)
#png("plot.png", width=500*10, height=720*10, res=100*15)
splots <- list(km.all,
               km.low,
               km.mid,
               km.high)

arrange_ggsurvplots(splots,ncol=2,nrow=2)

#dev.off()

Cox regression

All production groups pooled together

cow$Tx <- fct_relevel(cow$Tx,"BDCT","C_SDCT","A_SDCT")
SR <- coxph(Surv(Cull2TAR, Cull2) ~ Tx + my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(SR)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df Chisq Pr(>Chisq)    
## Tx              2  0.42      0.812    
## my3             2  2.87      0.238    
## factor(Parity)  2 27.43    1.1e-06 ***
## DOSCC           1 16.82    4.1e-05 ***
## factor(source)  1  3.34      0.068 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(SR)
## Call:
## coxph(formula = Surv(Cull2TAR, Cull2) ~ Tx + my3 + factor(Parity) + 
##     DOSCC + factor(source), data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 148 
##    (1 observation deleted due to missingness)
## 
##                       coef exp(coef) se(coef) robust se     z Pr(>|z|)    
## TxC_SDCT            0.0143    1.0144   0.1951    0.1724  0.08    0.934    
## TxA_SDCT            0.0726    1.0753   0.2132    0.2183  0.33    0.739    
## my3Mid production  -0.2828    0.7536   0.2044    0.1849 -1.53    0.126    
## my3High production -0.1157    0.8908   0.2058    0.2349 -0.49    0.622    
## factor(Parity)2     0.2358    1.2660   0.2349    0.1953  1.21    0.227    
## factor(Parity)3     0.9556    2.6003   0.2081    0.2116  4.52  6.3e-06 ***
## DOSCC               0.1546    1.1671   0.0717    0.0377  4.10  4.1e-05 ***
## factor(source)US    0.2910    1.3378   0.2401    0.1593  1.83    0.068 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT               1.014      0.986     0.724      1.42
## TxA_SDCT               1.075      0.930     0.701      1.65
## my3Mid production      0.754      1.327     0.525      1.08
## my3High production     0.891      1.123     0.562      1.41
## factor(Parity)2        1.266      0.790     0.863      1.86
## factor(Parity)3        2.600      0.385     1.718      3.94
## DOSCC                  1.167      0.857     1.084      1.26
## factor(source)US       1.338      0.747     0.979      1.83
## 
## Concordance= 0.648  (se = 0.021 )
## Likelihood ratio test= 43.6  on 8 df,   p=7e-07
## Wald test            = 428  on 8 df,   p=<2e-16
## Score (logrank) test = 46.6  on 8 df,   p=2e-07,   Robust = 7.97  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).
cull.main <- tidy(SR,conf.int = T,exp=T)[1:2,] %>% select(term,estimate,conf.low,conf.high)
cull.main <- cull.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

cull.main$production <- "All production levels"
cull.main$contrast <- "none"
cull.main$outcome <- "Culling"

Model diagnostics

SR <- cox.zph(SR)
SR
##                chisq df    p
## Tx             1.442  2 0.49
## my3            3.043  2 0.22
## factor(Parity) 1.159  2 0.56
## DOSCC          0.785  1 0.38
## factor(source) 1.176  1 0.28
## GLOBAL         6.486  8 0.59
ggcoxzph(SR)

Interaction model

CPH <- coxph(Surv(Cull2TAR, Cull2) ~ Tx*my3 + Parity + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df Chisq Pr(>Chisq)    
## Tx              2  1.14      0.564    
## my3             2 43.52    3.5e-10 ***
## Parity          2 27.92    8.7e-07 ***
## DOSCC           1 16.57    4.7e-05 ***
## factor(source)  1  3.01      0.083 .  
## Tx:my3          4 11.33      0.023 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(CPH)
## Call:
## coxph(formula = Surv(Cull2TAR, Cull2) ~ Tx + my3 + Parity + DOSCC + 
##     factor(source) + Tx:my3, data = cow, cluster = FARMID)
## 
##   n= 1484, number of events= 148 
##    (1 observation deleted due to missingness)
## 
##                                coef exp(coef) se(coef) robust se     z Pr(>|z|)
## TxC_SDCT                     0.2839    1.3283   0.3019    0.3528  0.80    0.421
## TxA_SDCT                     0.3359    1.3993   0.3214    0.2772  1.21    0.226
## my3Mid production            0.0867    1.0905   0.3284    0.3656  0.24    0.813
## my3High production           0.1032    1.1088   0.3378    0.4134  0.25    0.803
## Parity2                      0.2163    1.2414   0.2352    0.1893  1.14    0.253
## Parity3                      0.9361    2.5499   0.2083    0.2109  4.44  9.1e-06
## DOSCC                        0.1571    1.1701   0.0717    0.0386  4.07  4.7e-05
## factor(source)US             0.2813    1.3249   0.2404    0.1620  1.74    0.083
## TxC_SDCT:my3Mid production  -0.7973    0.4505   0.5005    0.5421 -1.47    0.141
## TxA_SDCT:my3Mid production  -0.4060    0.6663   0.4807    0.3554 -1.14    0.253
## TxC_SDCT:my3High production -0.2283    0.7959   0.4549    0.5424 -0.42    0.674
## TxA_SDCT:my3High production -0.5517    0.5760   0.5246    0.5257 -1.05    0.294
##                                
## TxC_SDCT                       
## TxA_SDCT                       
## my3Mid production              
## my3High production             
## Parity2                        
## Parity3                     ***
## DOSCC                       ***
## factor(source)US            .  
## TxC_SDCT:my3Mid production     
## TxA_SDCT:my3Mid production     
## TxC_SDCT:my3High production    
## TxA_SDCT:my3High production    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                             exp(coef) exp(-coef) lower .95 upper .95
## TxC_SDCT                        1.328      0.753     0.665      2.65
## TxA_SDCT                        1.399      0.715     0.813      2.41
## my3Mid production               1.091      0.917     0.533      2.23
## my3High production              1.109      0.902     0.493      2.49
## Parity2                         1.241      0.806     0.857      1.80
## Parity3                         2.550      0.392     1.686      3.86
## DOSCC                           1.170      0.855     1.085      1.26
## factor(source)US                1.325      0.755     0.964      1.82
## TxC_SDCT:my3Mid production      0.451      2.220     0.156      1.30
## TxA_SDCT:my3Mid production      0.666      1.501     0.332      1.34
## TxC_SDCT:my3High production     0.796      1.256     0.275      2.30
## TxA_SDCT:my3High production     0.576      1.736     0.206      1.61
## 
## Concordance= 0.655  (se = 0.018 )
## Likelihood ratio test= 47.3  on 12 df,   p=4e-06
## Wald test            = 3448  on 12 df,   p=<2e-16
## Score (logrank) test = 50.4  on 12 df,   p=1e-06,   Robust = 10.4  p=0.6
## 
##   (Note: the likelihood ratio and score tests assume independence of
##      observations within a cluster, the Wald and robust score tests do not).

Model diagnostics

SR <- cox.zph(CPH)
SR
##                chisq df    p
## Tx             1.477  2 0.48
## my3            3.045  2 0.22
## Parity         1.159  2 0.56
## DOSCC          0.766  1 0.38
## factor(source) 1.172  1 0.28
## Tx:my3         5.543  4 0.24
## GLOBAL         9.940 12 0.62
ggcoxzph(SR)

cow$Tx <- fct_relevel(cow$Tx,"C_SDCT","A_SDCT","BDCT")
CPH <- coxph(Surv(Cull2TAR, Cull2) ~ Tx*my3 + factor(Parity) + DOSCC + factor(source) + cluster(FARMID), data=cow)
car::Anova(CPH)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Surv(Cull2TAR, Cull2)
##                Df Chisq Pr(>Chisq)    
## Tx              2  1.14      0.564    
## my3             2 43.52    3.5e-10 ***
## factor(Parity)  2 27.92    8.7e-07 ***
## DOSCC           1 16.57    4.7e-05 ***
## factor(source)  1  3.01      0.083 .  
## Tx:my3          4 11.33      0.023 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
out <- emmeans(CPH,pairwise ~ Tx | my3,adjust = "none",type = "response") %>% confint


tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["ratio"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT / A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Culling"

tab.cull <- tab
tab.cull
##        production      contrast            estimate outcome
## 1  Low production C_SDCT / BDCT 1.33 (0.67 to 2.65) Culling
## 2  Low production A_SDCT / BDCT  1.4 (0.81 to 2.41) Culling
## 3  Mid production C_SDCT / BDCT  0.6 (0.35 to 1.02) Culling
## 4  Mid production A_SDCT / BDCT 0.93 (0.57 to 1.52) Culling
## 5 High production C_SDCT / BDCT 1.06 (0.51 to 2.19) Culling
## 6 High production A_SDCT / BDCT 0.81 (0.34 to 1.92) Culling

IMI post calving

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(imipc ~ Tx + my3 + Parity + factor(source) + DOSCC, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

imipc.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
imipc.main <- imipc.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

imipc.main$production <- "All production levels"
imipc.main$contrast <- "none"
imipc.main$outcome <- "IMI at calving"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(imipc ~ Tx*my3 + Parity + factor(source) + DOSCC, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ Tx * my3 + Parity + factor(source) + 
##     DOSCC, family = binomial(link = "identity"), data = qtr %>% 
##     filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                             Estimate  Std.err Wald Pr(>|W|)   
## (Intercept)                  0.05659  0.05118 1.22   0.2688   
## TxA_SDCT                    -0.03113  0.01775 3.07   0.0796 . 
## TxBDCT                      -0.02591  0.03550 0.53   0.4655   
## my3Mid production            0.00515  0.02419 0.05   0.8315   
## my3High production           0.00419  0.02207 0.04   0.8493   
## Parity2                      0.00789  0.02127 0.14   0.7108   
## Parity3                      0.01872  0.01725 1.18   0.2778   
## factor(source)US             0.06181  0.04640 1.78   0.1828   
## DOSCC                        0.02627  0.00892 8.68   0.0032 **
## TxA_SDCT:my3Mid production   0.00629  0.04122 0.02   0.8787   
## TxBDCT:my3Mid production    -0.00619  0.03319 0.03   0.8521   
## TxA_SDCT:my3High production  0.02749  0.03016 0.83   0.3620   
## TxBDCT:my3High production    0.01395  0.03543 0.16   0.6938   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.143
## Number of clusters:   16  Maximum cluster size: 1270
emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.213 0.0270 Inf    0.1597     0.266
##  A_SDCT Low production   0.181 0.0273 Inf    0.1280     0.235
##  BDCT   Low production   0.187 0.0419 Inf    0.1045     0.269
##  C_SDCT Mid production   0.218 0.0270 Inf    0.1648     0.271
##  A_SDCT Mid production   0.193 0.0249 Inf    0.1440     0.242
##  BDCT   Mid production   0.186 0.0445 Inf    0.0983     0.273
##  C_SDCT High production  0.217 0.0191 Inf    0.1794     0.254
##  A_SDCT High production  0.213 0.0219 Inf    0.1701     0.256
##  BDCT   High production  0.205 0.0200 Inf    0.1656     0.244
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100

tab$outcome <- "IMI at calving"

tab.imipc <- tab
tab.imipc
##        production      contrast              estimate        outcome
## 1  Low production C_SDCT - BDCT   0.03 (-0.04 to 0.1) IMI at calving
## 2  Low production A_SDCT - BDCT -0.01 (-0.05 to 0.04) IMI at calving
## 3  Mid production C_SDCT - BDCT   0.03 (-0.03 to 0.1) IMI at calving
## 4  Mid production A_SDCT - BDCT   0.01 (-0.08 to 0.1) IMI at calving
## 5 High production C_SDCT - BDCT  0.01 (-0.03 to 0.05) IMI at calving
## 6 High production A_SDCT - BDCT  0.01 (-0.03 to 0.04) IMI at calving
tab.imipc
##        production      contrast              estimate        outcome
## 1  Low production C_SDCT - BDCT   0.03 (-0.04 to 0.1) IMI at calving
## 2  Low production A_SDCT - BDCT -0.01 (-0.05 to 0.04) IMI at calving
## 3  Mid production C_SDCT - BDCT   0.03 (-0.03 to 0.1) IMI at calving
## 4  Mid production A_SDCT - BDCT   0.01 (-0.08 to 0.1) IMI at calving
## 5 High production C_SDCT - BDCT  0.01 (-0.03 to 0.05) IMI at calving
## 6 High production A_SDCT - BDCT  0.01 (-0.03 to 0.04) IMI at calving

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(imipc ~ Tx*my3 + Parity + DOSCC + factor(source)  + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(imipc ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: imipc ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##     5110     5201    -2541     5082     5079 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -0.891 -0.602 -0.432 -0.242  4.778 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.392    0.626   
## Number of obs: 5093, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                  -2.0413     0.2964   -6.89  5.7e-12 ***
## TxA_SDCT                     -0.1741     0.1574   -1.11   0.2685    
## TxBDCT                       -0.0821     0.1394   -0.59   0.5559    
## my3Mid production             0.0195     0.1445    0.13   0.8929    
## my3High production            0.0172     0.1434    0.12   0.9044    
## Parity2                       0.1661     0.0869    1.91   0.0561 .  
## Parity3                       0.2447     0.0894    2.74   0.0062 ** 
## DOSCC                         0.0837     0.0335    2.50   0.0125 *  
## factor(source)US             -0.1503     0.3374   -0.45   0.6559    
## TxA_SDCT:my3Mid production    0.1049     0.2163    0.49   0.6276    
## TxBDCT:my3Mid production     -0.0806     0.2018   -0.40   0.6896    
## TxA_SDCT:my3High production   0.1210     0.2191    0.55   0.5807    
## TxBDCT:my3High production     0.0283     0.1992    0.14   0.8871    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: imipc
##                Chisq Df Pr(>Chisq)  
## Tx              1.76  2      0.414  
## my3             0.44  2      0.805  
## Parity          8.03  2      0.018 *
## DOSCC           6.24  1      0.013 *
## factor(source)  0.20  1      0.656  
## Tx:my3          0.91  4      0.922  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: imipc ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 5292
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.0186 -0.6560 -0.4066 -0.0993  2.4722 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.0078   0.0883  
##  Residual             0.1621   0.4026  
## Number of obs: 5093, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  0.10875    0.04478    2.43
## TxA_SDCT                    -0.02940    0.02600   -1.13
## TxBDCT                      -0.01322    0.02289   -0.58
## my3Mid production            0.00334    0.02400    0.14
## my3High production           0.00324    0.02367    0.14
## Parity2                      0.02561    0.01415    1.81
## Parity3                      0.03888    0.01471    2.64
## DOSCC                        0.01379    0.00557    2.47
## factor(source)US            -0.00573    0.04765   -0.12
## TxA_SDCT:my3Mid production   0.01755    0.03546    0.49
## TxBDCT:my3Mid production    -0.01334    0.03279   -0.41
## TxA_SDCT:my3High production  0.02002    0.03599    0.56
## TxBDCT:my3High production    0.00390    0.03237    0.12
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: imipc
##                Chisq Df Pr(>Chisq)  
## Tx              1.84  2      0.399  
## my3             0.46  2      0.795  
## Parity          7.49  2      0.024 *
## DOSCC           6.12  1      0.013 *
## factor(source)  0.01  1      0.904  
## Tx:my3          0.96  4      0.916  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.167 0.0270 Inf     0.120     0.226
##  A_SDCT Low production  0.144 0.0255 Inf     0.101     0.201
##  BDCT   Low production  0.156 0.0249 Inf     0.113     0.211
##  C_SDCT Mid production  0.169 0.0271 Inf     0.123     0.229
##  A_SDCT Mid production  0.160 0.0272 Inf     0.114     0.221
##  BDCT   Mid production  0.148 0.0246 Inf     0.106     0.203
##  C_SDCT High production 0.169 0.0268 Inf     0.123     0.228
##  A_SDCT High production 0.162 0.0282 Inf     0.114     0.225
##  BDCT   High production 0.162 0.0263 Inf     0.117     0.220
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT  0.01 (-0.03 to 0.06)
## 2  Low production A_SDCT - BDCT -0.02 (-0.06 to 0.03)
## 3  Mid production C_SDCT - BDCT  0.03 (-0.02 to 0.07)
## 4  Mid production A_SDCT - BDCT  0.01 (-0.03 to 0.06)
## 5 High production C_SDCT - BDCT  0.01 (-0.04 to 0.05)
## 6 High production A_SDCT - BDCT     0 (-0.05 to 0.05)

IMI cure

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")
qtr <- qtr %>% arrange(FARMID,CowID)

gee <- geeglm(cure ~ Tx + my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

imicure.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
imicure.main <- imicure.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

imicure.main$production <- "All production levels"
imicure.main$contrast <- "none"
imicure.main$outcome <- "IMI cure"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")
gee <- geeglm(cure ~ Tx*my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = cure ~ Tx * my3 + Parity + DOSCC + factor(source), 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(cure) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                              Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)                  1.027043  0.033706 928.47  < 2e-16 ***
## TxA_SDCT                    -0.015212  0.019302   0.62  0.43064    
## TxBDCT                      -0.037237  0.031191   1.43  0.23255    
## my3Mid production           -0.032254  0.021755   2.20  0.13817    
## my3High production           0.000819  0.019762   0.00  0.96696    
## Parity2                     -0.032772  0.014248   5.29  0.02144 *  
## Parity3                     -0.034443  0.029314   1.38  0.24000    
## DOSCC                       -0.015560  0.005848   7.08  0.00780 ** 
## factor(source)US            -0.048110  0.031144   2.39  0.12241    
## TxA_SDCT:my3Mid production   0.080852  0.021165  14.59  0.00013 ***
## TxBDCT:my3Mid production     0.053197  0.035965   2.19  0.13910    
## TxA_SDCT:my3High production -0.022433  0.051764   0.19  0.66474    
## TxBDCT:my3High production   -0.005079  0.029420   0.03  0.86295    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.327
## Number of clusters:   16  Maximum cluster size: 432
emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.909 0.0248 Inf     0.860     0.958
##  A_SDCT Low production   0.894 0.0278 Inf     0.839     0.948
##  BDCT   Low production   0.872 0.0332 Inf     0.807     0.937
##  C_SDCT Mid production   0.877 0.0231 Inf     0.831     0.922
##  A_SDCT Mid production   0.942 0.0173 Inf     0.908     0.976
##  BDCT   Mid production   0.893 0.0289 Inf     0.836     0.949
##  C_SDCT High production  0.910 0.0122 Inf     0.886     0.934
##  A_SDCT High production  0.872 0.0382 Inf     0.797     0.947
##  BDCT   High production  0.867 0.0152 Inf     0.838     0.897
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100

tab$outcome <- "IMI cure"

tab.imicure <- tab
tab.imicure
##        production      contrast              estimate  outcome
## 1  Low production C_SDCT - BDCT   0.04 (-0.02 to 0.1) IMI cure
## 2  Low production A_SDCT - BDCT  0.02 (-0.03 to 0.07) IMI cure
## 3  Mid production C_SDCT - BDCT -0.02 (-0.07 to 0.04) IMI cure
## 4  Mid production A_SDCT - BDCT   0.05 (0.01 to 0.09) IMI cure
## 5 High production C_SDCT - BDCT   0.04 (0.01 to 0.08) IMI cure
## 6 High production A_SDCT - BDCT     0 (-0.08 to 0.09) IMI cure

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(cure ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(cure ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: cure ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##      874      946     -423      846     1171 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -4.524  0.254  0.331  0.410  0.590 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.317    0.563   
## Number of obs: 1185, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                   3.0833     0.6303    4.89    1e-06 ***
## TxA_SDCT                     -0.1196     0.4231   -0.28    0.777    
## TxBDCT                       -0.1995     0.3621   -0.55    0.582    
## my3Mid production            -0.2094     0.3807   -0.55    0.582    
## my3High production            0.1331     0.3995    0.33    0.739    
## Parity2                      -0.4134     0.2230   -1.85    0.064 .  
## Parity3                      -0.2413     0.2362   -1.02    0.307    
## DOSCC                        -0.0819     0.0900   -0.91    0.363    
## factor(source)US             -0.0877     0.4114   -0.21    0.831    
## TxA_SDCT:my3Mid production    0.7015     0.5958    1.18    0.239    
## TxBDCT:my3Mid production      0.5562     0.5347    1.04    0.298    
## TxA_SDCT:my3High production  -0.2692     0.5852   -0.46    0.646    
## TxBDCT:my3High production    -0.1806     0.5157   -0.35    0.726    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## optimizer (Nelder_Mead) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 0.00795887 (tol = 0.002, component 1)
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: cure
##                Chisq Df Pr(>Chisq)
## Tx              0.21  2       0.90
## my3             0.63  2       0.73
## Parity          3.46  2       0.18
## DOSCC           0.83  1       0.36
## factor(source)  0.05  1       0.83
## Tx:my3          3.55  4       0.47
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: cure ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 747
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -2.995  0.190  0.329  0.466  0.691 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.00278  0.0527  
##  Residual             0.10364  0.3219  
## Number of obs: 1185, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  0.96860    0.06142   15.77
## TxA_SDCT                    -0.01222    0.04514   -0.27
## TxBDCT                      -0.02100    0.03810   -0.55
## my3Mid production           -0.02020    0.03958   -0.51
## my3High production           0.01263    0.03955    0.32
## Parity2                     -0.04005    0.02298   -1.74
## Parity3                     -0.02390    0.02413   -0.99
## DOSCC                       -0.00785    0.00931   -0.84
## factor(source)US            -0.00449    0.03790   -0.12
## TxA_SDCT:my3Mid production   0.06622    0.06034    1.10
## TxBDCT:my3Mid production     0.05478    0.05449    1.01
## TxA_SDCT:my3High production -0.02652    0.06132   -0.43
## TxBDCT:my3High production   -0.01725    0.05340   -0.32
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: cure
##                Chisq Df Pr(>Chisq)
## Tx              0.27  2       0.87
## my3             0.74  2       0.69
## Parity          3.11  2       0.21
## DOSCC           0.71  1       0.40
## factor(source)  0.01  1       0.91
## Tx:my3          3.29  4       0.51
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.920 0.0256 Inf     0.853     0.958
##  A_SDCT Low production  0.911 0.0314 Inf     0.827     0.956
##  BDCT   Low production  0.904 0.0268 Inf     0.837     0.945
##  C_SDCT Mid production  0.903 0.0281 Inf     0.833     0.946
##  A_SDCT Mid production  0.944 0.0217 Inf     0.883     0.974
##  BDCT   Mid production  0.930 0.0237 Inf     0.867     0.965
##  C_SDCT High production 0.929 0.0227 Inf     0.870     0.963
##  A_SDCT High production 0.899 0.0339 Inf     0.811     0.949
##  BDCT   High production 0.900 0.0283 Inf     0.829     0.943
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["lower.CL"]],
  ucl = out[["contrasts"]][["upper.CL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT   0.02 (-0.05 to 0.1)
## 2  Low production A_SDCT - BDCT  0.01 (-0.08 to 0.09)
## 3  Mid production C_SDCT - BDCT -0.03 (-0.11 to 0.04)
## 4  Mid production A_SDCT - BDCT   0.02 (-0.06 to 0.1)
## 5 High production C_SDCT - BDCT  0.04 (-0.03 to 0.11)
## 6 High production A_SDCT - BDCT     0 (-0.08 to 0.08)

NewIMI

All production groups pooled together

qtr$Tx <- fct_relevel(qtr$Tx,"BDCT","C_SDCT","A_SDCT")

gee <- geeglm(newimi ~ Tx + my3 + Parity + DOSCC + factor(source), data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

iminew.main <- tidy(gee,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
iminew.main <- iminew.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

iminew.main$production <- "All production levels"
iminew.main$contrast <- "none"
iminew.main$outcome <- "New IMI risk"

Interaction model

qtr$Tx <- fct_relevel(qtr$Tx,"C_SDCT","A_SDCT","BDCT")
gee <- geeglm(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) , data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ Tx * my3 + Parity + DOSCC + factor(source), 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(newimi) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                             Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)                  0.04878  0.04614  1.12  0.29040    
## TxA_SDCT                    -0.02134  0.02341  0.83  0.36193    
## TxBDCT                      -0.02619  0.03748  0.49  0.48468    
## my3Mid production            0.00165  0.02321  0.01  0.94332    
## my3High production           0.00701  0.02263  0.10  0.75662    
## Parity2                      0.00749  0.02219  0.11  0.73578    
## Parity3                      0.01620  0.01780  0.83  0.36290    
## DOSCC                        0.02606  0.00778 11.23  0.00081 ***
## factor(source)US             0.06057  0.05005  1.46  0.22620    
## TxA_SDCT:my3Mid production  -0.00260  0.03777  0.00  0.94513    
## TxBDCT:my3Mid production     0.00034  0.03382  0.00  0.99198    
## TxA_SDCT:my3High production  0.00558  0.02465  0.05  0.82085    
## TxBDCT:my3High production    0.00280  0.03877  0.01  0.94239    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.166
## Number of clusters:   16  Maximum cluster size: 1223
emmeans(gee, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3             emmean     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production   0.203 0.0290 Inf    0.1459     0.260
##  A_SDCT Low production   0.181 0.0330 Inf    0.1168     0.246
##  BDCT   Low production   0.177 0.0451 Inf    0.0881     0.265
##  C_SDCT Mid production   0.204 0.0270 Inf    0.1515     0.257
##  A_SDCT Mid production   0.180 0.0244 Inf    0.1327     0.228
##  BDCT   Mid production   0.179 0.0481 Inf    0.0843     0.273
##  C_SDCT High production  0.210 0.0202 Inf    0.1702     0.249
##  A_SDCT High production  0.194 0.0208 Inf    0.1532     0.235
##  BDCT   High production  0.186 0.0214 Inf    0.1445     0.228
## 
## Results are averaged over the levels of: Parity, source 
## Covariance estimate used: vbeta 
## Confidence level used: 0.95
out <- emmeans(gee,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

mean <- data.frame(
  production = out[["emmeans"]][["my3"]],
  tx = out[["emmeans"]][["Tx"]],
  est = out[["emmeans"]][["emmean"]]
)

mean$est <- mean$est * 100
#mean

tab$outcome <- "New IMI risk"

tab.newimi <- tab
tab.newimi
##        production      contrast             estimate      outcome
## 1  Low production C_SDCT - BDCT  0.03 (-0.05 to 0.1) New IMI risk
## 2  Low production A_SDCT - BDCT    0 (-0.04 to 0.05) New IMI risk
## 3  Mid production C_SDCT - BDCT  0.03 (-0.04 to 0.1) New IMI risk
## 4  Mid production A_SDCT - BDCT    0 (-0.09 to 0.09) New IMI risk
## 5 High production C_SDCT - BDCT 0.02 (-0.01 to 0.06) New IMI risk
## 6 High production A_SDCT - BDCT 0.01 (-0.03 to 0.04) New IMI risk

Using GLMM (logistic) and LMM (i.e. to estimate risk difference)

glmm <- glmer(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr, family=binomial())
lmm <- lmer(newimi ~ Tx*my3 + Parity + DOSCC + factor(source) + (1|FARMID), data=qtr)

summary(glmm)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: newimi ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
##      AIC      BIC   logLik deviance df.resid 
##     4486     4576    -2229     4458     4620 
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -0.867 -0.592 -0.400 -0.236  5.005 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  FARMID (Intercept) 0.419    0.647   
## Number of obs: 4634, groups:  FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                 -2.07924    0.30937   -6.72  1.8e-11 ***
## TxA_SDCT                    -0.15138    0.16842   -0.90   0.3687    
## TxBDCT                      -0.08989    0.14871   -0.60   0.5455    
## my3Mid production            0.00107    0.15416    0.01   0.9945    
## my3High production           0.02293    0.15263    0.15   0.8806    
## Parity2                      0.19924    0.09300    2.14   0.0322 *  
## Parity3                      0.26461    0.09565    2.77   0.0057 ** 
## DOSCC                        0.07700    0.03521    2.19   0.0288 *  
## factor(source)US            -0.18850    0.34989   -0.54   0.5901    
## TxA_SDCT:my3Mid production   0.08260    0.23066    0.36   0.7203    
## TxBDCT:my3Mid production    -0.04536    0.21507   -0.21   0.8330    
## TxA_SDCT:my3High production  0.02681    0.23619    0.11   0.9096    
## TxBDCT:my3High production   -0.04300    0.21474   -0.20   0.8413    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## optimizer (Nelder_Mead) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 0.00234589 (tol = 0.002, component 1)
car::Anova(glmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: newimi
##                Chisq Df Pr(>Chisq)  
## Tx              2.23  2      0.329  
## my3             0.02  2      0.989  
## Parity          8.66  2      0.013 *
## DOSCC           4.78  1      0.029 *
## factor(source)  0.29  1      0.590  
## Tx:my3          0.33  4      0.988  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: newimi ~ Tx * my3 + Parity + DOSCC + factor(source) + (1 | FARMID)
##    Data: qtr
## 
## REML criterion at convergence: 4612
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -1.005 -0.658 -0.372 -0.114  2.528 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  FARMID   (Intercept) 0.00784  0.0886  
##  Residual             0.15489  0.3936  
## Number of obs: 4634, groups:  FARMID, 16
## 
## Fixed effects:
##                              Estimate Std. Error t value
## (Intercept)                  0.108372   0.045094    2.40
## TxA_SDCT                    -0.024427   0.026839   -0.91
## TxBDCT                      -0.013818   0.023383   -0.59
## my3Mid production            0.000216   0.024524    0.01
## my3High production           0.003869   0.024161    0.16
## Parity2                      0.029519   0.014482    2.04
## Parity3                      0.040121   0.015054    2.67
## DOSCC                        0.012050   0.005622    2.14
## factor(source)US            -0.009240   0.047793   -0.19
## TxA_SDCT:my3Mid production   0.013322   0.036304    0.37
## TxBDCT:my3Mid production    -0.007517   0.033498   -0.22
## TxA_SDCT:my3High production  0.003706   0.037180    0.10
## TxBDCT:my3High production   -0.006786   0.033209   -0.20
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: newimi
##                Chisq Df Pr(>Chisq)  
## Tx              2.29  2      0.317  
## my3             0.02  2      0.989  
## Parity          8.05  2      0.018 *
## DOSCC           4.59  1      0.032 *
## factor(source)  0.04  1      0.847  
## Tx:my3          0.36  4      0.985  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
emmeans(glmm, ~ Tx*my3,type = "response",adjust = "none")
##  Tx     my3              prob     SE  df asymp.LCL asymp.UCL
##  C_SDCT Low production  0.158 0.0268 Inf    0.1117     0.217
##  A_SDCT Low production  0.139 0.0259 Inf    0.0951     0.197
##  BDCT   Low production  0.146 0.0247 Inf    0.1039     0.201
##  C_SDCT Mid production  0.158 0.0267 Inf    0.1120     0.217
##  A_SDCT Mid production  0.149 0.0267 Inf    0.1036     0.209
##  BDCT   Mid production  0.141 0.0245 Inf    0.0989     0.196
##  C_SDCT High production 0.161 0.0269 Inf    0.1146     0.220
##  A_SDCT High production 0.144 0.0272 Inf    0.0989     0.206
##  BDCT   High production 0.143 0.0251 Inf    0.1009     0.200
## 
## Results are averaged over the levels of: Parity, source 
## Confidence level used: 0.95 
## Intervals are back-transformed from the logit scale
out <- emmeans(glmm,pairwise ~ Tx | my3,adjust = "none",type="response") %>% confint
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT  0.01 (-0.03 to 0.06)
## 2  Low production A_SDCT - BDCT -0.01 (-0.06 to 0.04)
## 3  Mid production C_SDCT - BDCT  0.02 (-0.03 to 0.07)
## 4  Mid production A_SDCT - BDCT  0.01 (-0.04 to 0.06)
## 5 High production C_SDCT - BDCT  0.02 (-0.03 to 0.07)
## 6 High production A_SDCT - BDCT     0 (-0.05 to 0.05)

DHI Dataset

Somatic cell count (1-120 DIM)

All production groups pooled together

dhi$Tx <- fct_relevel(dhi$Tx,"BDCT","C_SDCT","A_SDCT")
dhi <- dhi %>% arrange(FARMID,CowID)
mm0 <- geeglm(scc ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID), 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID), data = dhi %>% filter(!is.na(DOSCC)), id = CowID)
## 
##  Coefficients:
##                    Estimate  Std.err   Wald Pr(>|W|)    
## (Intercept)         3.32311  0.18494 322.86  < 2e-16 ***
## TxC_SDCT            0.02880  0.06400   0.20   0.6527    
## TxA_SDCT            0.02785  0.06893   0.16   0.6862    
## my3Mid production   0.00323  0.06804   0.00   0.9622    
## my3High production  0.03985  0.06938   0.33   0.5657    
## TestDIMcat2030     -0.54180  0.06464  70.25  < 2e-16 ***
## TestDIMcat2050     -0.55646  0.05901  88.92  < 2e-16 ***
## TestDIMcat2070     -0.41845  0.06065  47.61  5.2e-12 ***
## TestDIMcat2090     -0.28694  0.06479  19.61  9.5e-06 ***
## TestDIMcat20110    -0.12394  0.06105   4.12   0.0423 *  
## DOSCC               0.17325  0.02696  41.29  1.3e-10 ***
## Parity2             0.13258  0.06260   4.49   0.0342 *  
## Parity3             0.30245  0.07346  16.95  3.8e-05 ***
## factor(FARMID)2     0.07295  0.16932   0.19   0.6666    
## factor(FARMID)3    -0.01979  0.13236   0.02   0.8811    
## factor(FARMID)4     0.06333  0.12810   0.24   0.6210    
## factor(FARMID)5     0.03105  0.13571   0.05   0.8190    
## factor(FARMID)6    -0.47480  0.17696   7.20   0.0073 ** 
## factor(FARMID)7    -0.27231  0.14619   3.47   0.0625 .  
## factor(FARMID)8    -0.07255  0.27972   0.07   0.7954    
## factor(FARMID)9    -0.06666  0.29768   0.05   0.8228    
## factor(FARMID)10   -0.24485  0.22459   1.19   0.2756    
## factor(FARMID)11   -0.40008  0.23970   2.79   0.0951 .  
## factor(FARMID)12   -0.47358  0.21677   4.77   0.0289 *  
## factor(FARMID)13    0.11772  0.30299   0.15   0.6976    
## factor(FARMID)14    0.10451  0.25583   0.17   0.6829    
## factor(FARMID)15    0.03146  0.37048   0.01   0.9323    
## factor(FARMID)16   -0.05681  0.23297   0.06   0.8074    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0567
## Number of clusters:   1364  Maximum cluster size: 4
scc.main <- tidy(mm0,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
scc.main <- scc.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

scc.main$production <- "All production levels"
scc.main$contrast <- "none"
scc.main$outcome <- "Log SCC (1-120 DIM)"
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(Tx,TestDIMcat20,SCC,LCL,UCL))
emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=Tx, colour=Tx) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$LCL, ymax=emm$UCL,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

    
curve

Interaction model (Tx * milk yield)

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(scc ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID), 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)

summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID), data = dhi %>% filter(!is.na(DOSCC)), id = CowID)
## 
##  Coefficients:
##                             Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)                   3.2182  0.1961 269.20  < 2e-16 ***
## TxA_SDCT                      0.2199  0.1235   3.17   0.0751 .  
## TxBDCT                        0.1150  0.1033   1.24   0.2657    
## my3Mid production             0.1954  0.1145   2.91   0.0879 .  
## my3High production            0.1936  0.1081   3.21   0.0733 .  
## TestDIMcat2030               -0.5369  0.0647  68.93  < 2e-16 ***
## TestDIMcat2050               -0.5550  0.0591  88.29  < 2e-16 ***
## TestDIMcat2070               -0.4130  0.0605  46.59  8.7e-12 ***
## TestDIMcat2090               -0.2844  0.0650  19.17  1.2e-05 ***
## TestDIMcat20110              -0.1197  0.0611   3.85   0.0499 *  
## DOSCC                         0.1761  0.0271  42.18  8.3e-11 ***
## Parity2                       0.1377  0.0627   4.82   0.0281 *  
## Parity3                       0.3037  0.0734  17.11  3.5e-05 ***
## factor(FARMID)2               0.0711  0.1683   0.18   0.6726    
## factor(FARMID)3              -0.0230  0.1321   0.03   0.8617    
## factor(FARMID)4               0.0610  0.1280   0.23   0.6334    
## factor(FARMID)5               0.0232  0.1354   0.03   0.8641    
## factor(FARMID)6              -0.4867  0.1775   7.52   0.0061 ** 
## factor(FARMID)7              -0.2762  0.1464   3.56   0.0592 .  
## factor(FARMID)8              -0.0840  0.2774   0.09   0.7620    
## factor(FARMID)9              -0.0626  0.2978   0.04   0.8334    
## factor(FARMID)10             -0.2720  0.2241   1.47   0.2247    
## factor(FARMID)11             -0.3806  0.2441   2.43   0.1189    
## factor(FARMID)12             -0.4640  0.2189   4.50   0.0340 *  
## factor(FARMID)13              0.1154  0.3017   0.15   0.7022    
## factor(FARMID)14              0.1116  0.2549   0.19   0.6616    
## factor(FARMID)15              0.0447  0.3672   0.01   0.9031    
## factor(FARMID)16             -0.0612  0.2334   0.07   0.7932    
## TxA_SDCT:my3Mid production   -0.3902  0.1646   5.62   0.0178 *  
## TxBDCT:my3Mid production     -0.1999  0.1581   1.60   0.2060    
## TxA_SDCT:my3High production  -0.2424  0.1699   2.03   0.1538    
## TxBDCT:my3High production    -0.2261  0.1502   2.26   0.1324    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0562
## Number of clusters:   1364  Maximum cluster size: 4
out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Log SCC (1-120 DIM)"

tab.scc <- tab
tab.scc
##        production      contrast              estimate             outcome
## 1  Low production C_SDCT - BDCT -0.12 (-0.32 to 0.09) Log SCC (1-120 DIM)
## 2  Low production A_SDCT - BDCT   0.1 (-0.13 to 0.34) Log SCC (1-120 DIM)
## 3  Mid production C_SDCT - BDCT  0.08 (-0.15 to 0.32) Log SCC (1-120 DIM)
## 4  Mid production A_SDCT - BDCT  -0.09 (-0.3 to 0.13) Log SCC (1-120 DIM)
## 5 High production C_SDCT - BDCT   0.11 (-0.1 to 0.32) Log SCC (1-120 DIM)
## 6 High production A_SDCT - BDCT  0.09 (-0.15 to 0.33) Log SCC (1-120 DIM)
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(my3,Tx,TestDIMcat20,SCC,LCL,UCL))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=Tx, colour=Tx) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$LCL, ymax=emm$UCL,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1") + facet_grid( ~ my3)

    
curve

Interaction model using linear mixed model

lmm <- lmer(scc ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + (1|FARMID/CowID), 
              data=dhi)

summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: scc ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + (1 | FARMID/CowID)
##    Data: dhi
## 
## REML criterion at convergence: 14781
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -5.123 -0.522 -0.107  0.439  4.124 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  CowID:FARMID (Intercept) 0.6998   0.837   
##  FARMID       (Intercept) 0.0127   0.113   
##  Residual                 1.0526   1.026   
## Number of obs: 4553, groups:  CowID:FARMID, 1364; FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                   3.1076     0.1554   20.00
## TxA_SDCT                      0.2463     0.1226    2.01
## TxBDCT                        0.1192     0.1108    1.08
## my3Mid production             0.1914     0.1154    1.66
## my3High production            0.2211     0.1138    1.94
## TestDIMcat2030               -0.5653     0.0576   -9.81
## TestDIMcat2050               -0.5733     0.0552  -10.39
## TestDIMcat2070               -0.4171     0.0548   -7.61
## TestDIMcat2090               -0.2994     0.0586   -5.11
## TestDIMcat20110              -0.1163     0.0548   -2.12
## DOSCC                         0.1876     0.0261    7.20
## Parity2                       0.1102     0.0684    1.61
## Parity3                       0.2989     0.0712    4.20
## TxA_SDCT:my3Mid production   -0.3728     0.1681   -2.22
## TxBDCT:my3Mid production     -0.1798     0.1578   -1.14
## TxA_SDCT:my3High production  -0.2410     0.1713   -1.41
## TxBDCT:my3High production    -0.2635     0.1576   -1.67
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: scc
##               Chisq Df Pr(>Chisq)    
## Tx             0.81  2    0.66624    
## my3            0.69  2    0.70905    
## TestDIMcat20 183.11  5    < 2e-16 ***
## DOSCC         51.83  1    6.1e-13 ***
## Parity        17.68  2    0.00014 ***
## Tx:my3         6.54  4    0.16232    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast             estimate
## 1  Low production C_SDCT - BDCT -0.12 (-0.34 to 0.1)
## 2  Low production A_SDCT - BDCT  0.13 (-0.1 to 0.36)
## 3  Mid production C_SDCT - BDCT 0.06 (-0.16 to 0.28)
## 4  Mid production A_SDCT - BDCT -0.07 (-0.3 to 0.16)
## 5 High production C_SDCT - BDCT 0.14 (-0.07 to 0.36)
## 6 High production A_SDCT - BDCT  0.15 (-0.1 to 0.39)

Milk Yield (1-120 DIM)

All production groups pooled together

dhi$Tx <- fct_relevel(dhi$Tx,"BDCT","C_SDCT","A_SDCT")
dhi <- dhi %>% arrange(FARMID,CowID)
mm0 <- geeglm(MY ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID) + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx + my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID) + DOMY, data = dhi %>% filter(!is.na(DOSCC)), 
##     id = CowID)
## 
##  Coefficients:
##                    Estimate  Std.err   Wald Pr(>|W|)    
## (Intercept)        32.38133  1.51991 453.90  < 2e-16 ***
## TxC_SDCT           -0.20428  0.41036   0.25   0.6186    
## TxA_SDCT           -0.95448  0.45459   4.41   0.0358 *  
## my3Mid production  -0.00291  0.55694   0.00   0.9958    
## my3High production  1.68138  0.81346   4.27   0.0387 *  
## TestDIMcat2030     10.64886  0.45885 538.60  < 2e-16 ***
## TestDIMcat2050     12.29294  0.40042 942.50  < 2e-16 ***
## TestDIMcat2070     11.57778  0.42798 731.80  < 2e-16 ***
## TestDIMcat2090     10.09220  0.44159 522.33  < 2e-16 ***
## TestDIMcat20110     7.94226  0.43717 330.05  < 2e-16 ***
## DOSCC               0.15269  0.17494   0.76   0.3827    
## Parity2             2.91192  0.42390  47.19  6.4e-12 ***
## Parity3             3.39658  0.49713  46.68  8.4e-12 ***
## factor(FARMID)2    -1.19254  1.05352   1.28   0.2577    
## factor(FARMID)3    -4.59538  0.88976  26.67  2.4e-07 ***
## factor(FARMID)4     5.47511  0.91717  35.64  2.4e-09 ***
## factor(FARMID)5     0.65044  0.90301   0.52   0.4713    
## factor(FARMID)6     2.36346  1.07481   4.84   0.0279 *  
## factor(FARMID)7     2.23025  1.03413   4.65   0.0310 *  
## factor(FARMID)8    -2.05747  1.63115   1.59   0.2072    
## factor(FARMID)9    -7.93355  1.67387  22.46  2.1e-06 ***
## factor(FARMID)10   -1.56270  1.23962   1.59   0.2074    
## factor(FARMID)11   -3.03963  1.70123   3.19   0.0740 .  
## factor(FARMID)12   -4.23579  1.71188   6.12   0.0133 *  
## factor(FARMID)13   -4.97843  1.28275  15.06   0.0001 ***
## factor(FARMID)14   -2.02688  2.43435   0.69   0.4051    
## factor(FARMID)15   -4.83048  1.70948   7.98   0.0047 ** 
## factor(FARMID)16   -2.35627  1.22689   3.69   0.0548 .  
## DOMY                0.13808  0.04720   8.56   0.0034 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     71.1    2.37
## Number of clusters:   1363  Maximum cluster size: 4
my.main <- tidy(mm0,conf.int = T)[2:3,] %>% select(term,estimate,conf.low,conf.high)
my.main <- my.main %>% mutate(
  estimate = paste0(round(estimate,2),
                    " (",
                    round(conf.low,2),
                    " to ",
                    round(conf.high,2),
                    ")"),
conf.low = NULL,
conf.high = NULL)

my.main$production <- "All production levels"
my.main$contrast <- "none"
my.main$outcome <- "Milk Yield (1-120 DIM)"
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm <- emm %>% subset(select=c(Tx,TestDIMcat20,estimate,conf.low,conf.high))
emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(30,55))) + aes(x=as.numeric(TestDIMcat20), y=estimate, group=Tx, colour=Tx) +
  labs(y = "Milk Yield)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$conf.low, ymax=emm$conf.high,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

    
curve

Interaction model (Tx * milk yield)

dhi$Tx <- fct_relevel(dhi$Tx,"C_SDCT","A_SDCT","BDCT")

mm0 <- geeglm(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + factor(FARMID) + DOMY, 
              data=dhi %>% filter(!is.na(DOSCC))
              ,id=CowID)


summary(mm0)
## 
## Call:
## geeglm(formula = MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + 
##     factor(FARMID) + DOMY, data = dhi %>% filter(!is.na(DOSCC)), 
##     id = CowID)
## 
##  Coefficients:
##                             Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)                  32.6753  1.5311 455.45  < 2e-16 ***
## TxA_SDCT                     -1.5043  0.8398   3.21   0.0732 .  
## TxBDCT                       -0.3414  0.7080   0.23   0.6296    
## my3Mid production            -0.3237  0.8131   0.16   0.6905    
## my3High production            0.8216  1.0734   0.59   0.4441    
## TestDIMcat2030               10.6440  0.4580 540.07  < 2e-16 ***
## TestDIMcat2050               12.2954  0.4001 944.43  < 2e-16 ***
## TestDIMcat2070               11.5710  0.4271 734.03  < 2e-16 ***
## TestDIMcat2090               10.0872  0.4414 522.35  < 2e-16 ***
## TestDIMcat20110               7.9326  0.4366 330.05  < 2e-16 ***
## DOSCC                         0.1420  0.1743   0.66   0.4151    
## Parity2                       2.8950  0.4226  46.92  7.4e-12 ***
## Parity3                       3.4053  0.4951  47.30  6.1e-12 ***
## factor(FARMID)2              -1.1698  1.0525   1.24   0.2664    
## factor(FARMID)3              -4.6148  0.8890  26.95  2.1e-07 ***
## factor(FARMID)4               5.4795  0.9171  35.70  2.3e-09 ***
## factor(FARMID)5               0.6655  0.9011   0.55   0.4602    
## factor(FARMID)6               2.4130  1.0759   5.03   0.0249 *  
## factor(FARMID)7               2.2174  1.0331   4.61   0.0318 *  
## factor(FARMID)8              -2.0777  1.6152   1.65   0.1983    
## factor(FARMID)9              -7.9371  1.6747  22.46  2.1e-06 ***
## factor(FARMID)10             -1.4768  1.2438   1.41   0.2351    
## factor(FARMID)11             -3.2404  1.6901   3.68   0.0552 .  
## factor(FARMID)12             -4.3655  1.6892   6.68   0.0098 ** 
## factor(FARMID)13             -4.9717  1.2759  15.18  9.8e-05 ***
## factor(FARMID)14             -1.9944  2.4385   0.67   0.4134    
## factor(FARMID)15             -4.8663  1.6905   8.29   0.0040 ** 
## factor(FARMID)16             -2.2892  1.2321   3.45   0.0632 .  
## DOMY                          0.1373  0.0474   8.37   0.0038 ** 
## TxA_SDCT:my3Mid production    0.9239  1.0880   0.72   0.3958    
## TxBDCT:my3Mid production      0.0794  0.9563   0.01   0.9338    
## TxA_SDCT:my3High production   1.1998  1.1562   1.08   0.2994    
## TxBDCT:my3High production     1.5158  1.0321   2.16   0.1419    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)       71    2.37
## Number of clusters:   1363  Maximum cluster size: 4
out <- emmeans(mm0,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab$outcome <- "Milk Yield"

tab.my <- tab
tab.my
##        production      contrast              estimate    outcome
## 1  Low production C_SDCT - BDCT  0.34 (-1.05 to 1.73) Milk Yield
## 2  Low production A_SDCT - BDCT -1.16 (-2.74 to 0.41) Milk Yield
## 3  Mid production C_SDCT - BDCT  0.26 (-1.01 to 1.53) Milk Yield
## 4  Mid production A_SDCT - BDCT -0.32 (-1.68 to 1.04) Milk Yield
## 5 High production C_SDCT - BDCT -1.17 (-2.64 to 0.29) Milk Yield
## 6 High production A_SDCT - BDCT -1.48 (-3.08 to 0.12) Milk Yield
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
#emm <- emmeans(lmm, ~Tx*my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()

emm <- emm %>% subset(select=c(my3,Tx,TestDIMcat20,estimate,conf.low,conf.high))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

emm$Tx <- fct_relevel(emm$Tx, "BDCT",
                       "C_SDCT",
                       "A_SDCT"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(30,55))) + aes(x=as.numeric(TestDIMcat20), y=estimate, group=Tx, colour=Tx) +
  labs(y = "Milk Yield)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$conf.low, ymax=emm$conf.high,colour=Tx,fill=Tx), linetype=0, alpha=0.1) + geom_line(aes(colour=Tx,linetype=Tx),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1") + facet_grid( ~ my3)

    
curve

Interaction model using linear mixed model

lmm <- lmer(MY ~ Tx*my3 + TestDIMcat20 + DOSCC + Parity + (1|FARMID/CowID), 
              data=dhi)

summary(lmm)
## Linear mixed model fit by REML ['lmerMod']
## Formula: MY ~ Tx * my3 + TestDIMcat20 + DOSCC + Parity + (1 | FARMID/CowID)
##    Data: dhi
## 
## REML criterion at convergence: 31699
## 
## Scaled residuals: 
##    Min     1Q Median     3Q    Max 
## -6.522 -0.463  0.019  0.511  4.150 
## 
## Random effects:
##  Groups       Name        Variance Std.Dev.
##  CowID:FARMID (Intercept) 28.7     5.36    
##  FARMID       (Intercept) 12.7     3.56    
##  Residual                 43.8     6.62    
## Number of obs: 4551, groups:  CowID:FARMID, 1363; FARMID, 16
## 
## Fixed effects:
##                             Estimate Std. Error t value
## (Intercept)                  33.6274     1.3459   24.98
## TxA_SDCT                     -1.3967     0.7923   -1.76
## TxBDCT                       -0.2261     0.7148   -0.32
## my3Mid production             0.8822     0.7428    1.19
## my3High production            2.9199     0.7343    3.98
## TestDIMcat2030               11.1944     0.3721   30.08
## TestDIMcat2050               12.2344     0.3562   34.35
## TestDIMcat2070               11.8726     0.3535   33.59
## TestDIMcat2090               10.2506     0.3784   27.09
## TestDIMcat20110               7.8865     0.3537   22.30
## DOSCC                         0.0388     0.1716    0.23
## Parity2                       2.9943     0.4409    6.79
## Parity3                       3.2631     0.4603    7.09
## TxA_SDCT:my3Mid production    0.8630     1.0809    0.80
## TxBDCT:my3Mid production     -0.1086     1.0173   -0.11
## TxA_SDCT:my3High production   1.1529     1.1018    1.05
## TxBDCT:my3High production     1.3083     1.0182    1.28
car::Anova(lmm)
## Analysis of Deviance Table (Type II Wald chisquare tests)
## 
## Response: MY
##                Chisq Df Pr(>Chisq)    
## Tx              3.93  2       0.14    
## my3            72.05  2    2.3e-16 ***
## TestDIMcat20 1653.27  5    < 2e-16 ***
## DOSCC           0.05  1       0.82    
## Parity         66.97  2    2.9e-15 ***
## Tx:my3          3.30  4       0.51    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
out <- emmeans(lmm,pairwise ~ Tx | my3,adjust = "none") %>% confint

tab <- data.frame(
  production = out[["contrasts"]][["my3"]],
  contrast = out[["contrasts"]][["contrast"]],
  est = out[["contrasts"]][["estimate"]],
  lcl = out[["contrasts"]][["asymp.LCL"]],
  ucl = out[["contrasts"]][["asymp.UCL"]]
)

tab <- tab %>% filter(contrast != "C_SDCT - A_SDCT")
tab <- tab %>% mutate(
  estimate = paste0(round(est,2),
                    " (",
                    round(lcl,2),
                    " to ",
                    round(ucl,2),
                    ")"),
  est = NULL,
  lcl = NULL,
  ucl = NULL
  )

tab
##        production      contrast              estimate
## 1  Low production C_SDCT - BDCT  0.23 (-1.17 to 1.63)
## 2  Low production A_SDCT - BDCT -1.17 (-2.65 to 0.31)
## 3  Mid production C_SDCT - BDCT   0.33 (-1.1 to 1.77)
## 4  Mid production A_SDCT - BDCT  -0.2 (-1.69 to 1.29)
## 5 High production C_SDCT - BDCT  -1.08 (-2.5 to 0.33)
## 6 High production A_SDCT - BDCT -1.33 (-2.91 to 0.26)
fulltab <- rbind(tab.cm,
                 tab.cull,
     tab.imipc,
     tab.imicure,
     tab.newimi,
     tab.scc,
     tab.my)



full.main <- rbind(cm.main,
                   cull.main,
                   imipc.main,
                   imicure.main,
                   iminew.main,
                   scc.main,
                   my.main)

full.main$contrast <- full.main$term
full.main$term <- NULL

tableout <- rbind(fulltab,full.main) %>% arrange(outcome,production)

write.csv(tableout,'tableout2.csv')

Milk yield at DO —

IMI at 1-13 DIM

Need to choose covariables

ggplot(qtr, aes(x=DOMY, y=imipc)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

IMI in early lactation

gee <- geeglm(imipc ~ my3, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)         0.22522  0.04249 28.10  1.2e-07 ***
## my3Mid production  -0.00908  0.01324  0.47     0.49    
## my3High production -0.00688  0.02235  0.09     0.76    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.163
## Number of clusters:   16  Maximum cluster size: 1270
gee <- geeglm(imipc ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = imipc ~ my3 + factor(Parity) + DOSCC + Tx, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(imipc) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err Wald Pr(>|W|)   
## (Intercept)         0.09729  0.04462 4.75   0.0292 * 
## my3Mid production   0.00473  0.01423 0.11   0.7394   
## my3High production  0.01632  0.02317 0.50   0.4811   
## factor(Parity)2     0.00897  0.02153 0.17   0.6770   
## factor(Parity)3     0.01476  0.01557 0.90   0.3433   
## DOSCC               0.02687  0.00858 9.80   0.0017 **
## TxA_SDCT           -0.00463  0.01370 0.11   0.7353   
## TxBDCT             -0.02333  0.02058 1.28   0.2571   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.167
## Number of clusters:   16  Maximum cluster size: 1270
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term               estimate std.error statistic p.value conf.low conf.high
##   <chr>                 <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.0973    0.0446      4.75  0.0292   0.00984    0.185 
## 2 my3Mid production   0.00473   0.0142      0.111 0.739   -0.0232     0.0326
## 3 my3High production  0.0163    0.0232      0.496 0.481   -0.0291     0.0617
## 4 factor(Parity)2     0.00897   0.0215      0.174 0.677   -0.0332     0.0512
## 5 factor(Parity)3     0.0148    0.0156      0.898 0.343   -0.0158     0.0453
## 6 DOSCC               0.0269    0.00858     9.80  0.00174  0.0100     0.0437
## 7 TxA_SDCT           -0.00463   0.0137      0.114 0.735   -0.0315     0.0222
## 8 TxBDCT             -0.0233    0.0206      1.28  0.257   -0.0637     0.0170

IMI cure

ggplot(qtr, aes(x=DOMY, y=cure)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

#Univariable
gee <- geeglm(cure ~ my3, data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)
summary(gee)
## 
## Call:
## geeglm(formula = cure ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate Std.err   Wald Pr(>|W|)    
## (Intercept)         0.86822 0.03074 797.91   <2e-16 ***
## my3Mid production   0.02719 0.01490   3.33    0.068 .  
## my3High production  0.00863 0.02560   0.11    0.736    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.306
## Number of clusters:   16  Maximum cluster size: 432
#Multivariable
gee <- geeglm(cure ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = cure ~ my3 + factor(Parity) + DOSCC + Tx, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(cure) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err   Wald Pr(>|W|)    
## (Intercept)         0.94419  0.05834 261.92   <2e-16 ***
## my3Mid production   0.01655  0.01531   1.17    0.280    
## my3High production -0.00132  0.02410   0.00    0.956    
## factor(Parity)2    -0.03250  0.01481   4.82    0.028 *  
## factor(Parity)3    -0.02221  0.02732   0.66    0.416    
## DOSCC              -0.01085  0.00714   2.31    0.129    
## TxA_SDCT            0.00109  0.01297   0.01    0.933    
## TxBDCT             -0.00855  0.01849   0.21    0.644    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1    0.35
## Number of clusters:   16  Maximum cluster size: 432
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term               estimate std.error statistic p.value conf.low conf.high
##   <chr>                 <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.944     0.0583  262.       0        0.830    1.06   
## 2 my3Mid production   0.0165    0.0153    1.17     0.280   -0.0135   0.0466 
## 3 my3High production -0.00132   0.0241    0.00299  0.956   -0.0486   0.0459 
## 4 factor(Parity)2    -0.0325    0.0148    4.82     0.0282  -0.0615  -0.00348
## 5 factor(Parity)3    -0.0222    0.0273    0.661    0.416   -0.0758   0.0313 
## 6 DOSCC              -0.0109    0.00714   2.31     0.129   -0.0249   0.00315
## 7 TxA_SDCT            0.00109   0.0130    0.00699  0.933   -0.0243   0.0265 
## 8 TxBDCT             -0.00855   0.0185    0.214    0.644   -0.0448   0.0277

NewIMI

ggplot(qtr, aes(x=DOMY, y=newimi)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", colour="blue", size=1.5)+
  xlab("Milk yield")+
  ylab("Probability of IMI at calving")+
  theme_bw()

gee <- geeglm(newimi ~ my3, data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ my3, family = binomial(link = "identity"), 
##     data = qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), 
##     id = FARMID)
## 
##  Coefficients:
##                    Estimate Std.err  Wald Pr(>|W|)    
## (Intercept)          0.2162  0.0462 21.93  2.8e-06 ***
## my3Mid production   -0.0103  0.0140  0.54     0.46    
## my3High production  -0.0135  0.0234  0.33     0.56    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1   0.196
## Number of clusters:   16  Maximum cluster size: 1223
gee <- geeglm(newimi ~ my3 + factor(Parity) + DOSCC + Tx, data=qtr %>% filter(!is.na(newimi) & !is.na(Parity) & !is.na(DOSCC)), family=binomial(link="identity"), id=FARMID)

summary(gee)
## 
## Call:
## geeglm(formula = newimi ~ my3 + factor(Parity) + DOSCC + Tx, 
##     family = binomial(link = "identity"), data = qtr %>% filter(!is.na(newimi) & 
##         !is.na(Parity) & !is.na(DOSCC)), id = FARMID)
## 
##  Coefficients:
##                    Estimate  Std.err  Wald Pr(>|W|)    
## (Intercept)         0.08938  0.04263  4.40   0.0360 *  
## my3Mid production   0.00204  0.01516  0.02   0.8929    
## my3High production  0.00951  0.02638  0.13   0.7186    
## factor(Parity)2     0.01004  0.02256  0.20   0.6563    
## factor(Parity)3     0.01270  0.01574  0.65   0.4197    
## DOSCC               0.02694  0.00724 13.84   0.0002 ***
## TxA_SDCT           -0.00512  0.01656  0.10   0.7573    
## TxBDCT             -0.02603  0.02135  1.49   0.2226    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)        1     0.2
## Number of clusters:   16  Maximum cluster size: 1223
tidy(gee,conf.int = T)
## # A tibble: 8 × 7
##   term               estimate std.error statistic  p.value conf.low conf.high
##   <chr>                 <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
## 1 (Intercept)         0.0894    0.0426     4.40   0.0360    0.00583    0.173 
## 2 my3Mid production   0.00204   0.0152     0.0181 0.893    -0.0277     0.0318
## 3 my3High production  0.00951   0.0264     0.130  0.719    -0.0422     0.0612
## 4 factor(Parity)2     0.0100    0.0226     0.198  0.656    -0.0342     0.0543
## 5 factor(Parity)3     0.0127    0.0157     0.651  0.420    -0.0181     0.0436
## 6 DOSCC               0.0269    0.00724   13.8    0.000199  0.0127     0.0411
## 7 TxA_SDCT           -0.00512   0.0166     0.0955 0.757    -0.0376     0.0273
## 8 TxBDCT             -0.0260    0.0213     1.49   0.223    -0.0679     0.0158

Post calving SCC

desc <- dhi %>% group_by(my3,TestDIMcat20) %>% summarise(
  atrisk = sum(!is.na(scc)),
  scm.n = sum(scc > log(200)),
  scm.risk = scm.n/atrisk 
)

ggplot(desc, aes(x=TestDIMcat20 %>% as.character() %>% as.numeric, y=scm.risk, color = my3)) + geom_point(size=2, alpha=0.4)+
  stat_smooth(method="loess", size=1.5)+
  xlab("Days in milk")+
  ylab("Risk of subclinical mastitis")+
  theme_bw()

mm0 <- geeglm(scc ~ my3 + TestDIMcat20 + factor(FARMID), data=dhi, id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(FARMID), data = dhi, 
##     id = CowID)
## 
##  Coefficients:
##                    Estimate Std.err    Wald Pr(>|W|)    
## (Intercept)          4.3440  0.1360 1020.15  < 2e-16 ***
## my3Mid production   -0.1230  0.0668    3.39   0.0656 .  
## my3High production  -0.1373  0.0673    4.16   0.0415 *  
## TestDIMcat2030      -0.5182  0.0656   62.36  2.9e-15 ***
## TestDIMcat2050      -0.5566  0.0594   87.92  < 2e-16 ***
## TestDIMcat2070      -0.4089  0.0613   44.50  2.5e-11 ***
## TestDIMcat2090      -0.2790  0.0661   17.81  2.4e-05 ***
## TestDIMcat20110     -0.1188  0.0613    3.76   0.0526 .  
## factor(FARMID)2     -0.0236  0.1734    0.02   0.8919    
## factor(FARMID)3     -0.1044  0.1350    0.60   0.4393    
## factor(FARMID)4      0.0372  0.1316    0.08   0.7776    
## factor(FARMID)5      0.1338  0.1394    0.92   0.3370    
## factor(FARMID)6     -0.5420  0.1784    9.24   0.0024 ** 
## factor(FARMID)7     -0.3900  0.1507    6.70   0.0097 ** 
## factor(FARMID)8      0.0359  0.2954    0.01   0.9034    
## factor(FARMID)9     -0.1008  0.2962    0.12   0.7337    
## factor(FARMID)10    -0.3590  0.2532    2.01   0.1562    
## factor(FARMID)11    -0.3386  0.2486    1.86   0.1732    
## factor(FARMID)12    -0.5843  0.2279    6.57   0.0104 *  
## factor(FARMID)13     0.1225  0.3199    0.15   0.7019    
## factor(FARMID)14     0.0831  0.2633    0.10   0.7523    
## factor(FARMID)15    -0.0348  0.3940    0.01   0.9296    
## factor(FARMID)16    -0.1811  0.2367    0.59   0.4442    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.78  0.0595
## Number of clusters:   1364  Maximum cluster size: 4
mm0 <- geeglm(scc ~ my3 + TestDIMcat20 + factor(Parity) + DOSCC + Tx + factor(FARMID), data=dhi, id=CowID)
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(Parity) + 
##     DOSCC + Tx + factor(FARMID), data = dhi, id = CowID)
## 
##  Coefficients:
##                     Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)         3.351910  0.188167 317.32  < 2e-16 ***
## my3Mid production   0.003227  0.068036   0.00   0.9622    
## my3High production  0.039849  0.069379   0.33   0.5657    
## TestDIMcat2030     -0.541797  0.064641  70.25  < 2e-16 ***
## TestDIMcat2050     -0.556459  0.059010  88.92  < 2e-16 ***
## TestDIMcat2070     -0.418450  0.060647  47.61  5.2e-12 ***
## TestDIMcat2090     -0.286936  0.064794  19.61  9.5e-06 ***
## TestDIMcat20110    -0.123939  0.061047   4.12   0.0423 *  
## factor(Parity)2     0.132579  0.062601   4.49   0.0342 *  
## factor(Parity)3     0.302455  0.073465  16.95  3.8e-05 ***
## DOSCC               0.173249  0.026961  41.29  1.3e-10 ***
## TxA_SDCT           -0.000951  0.068521   0.00   0.9889    
## TxBDCT             -0.028799  0.063997   0.20   0.6527    
## factor(FARMID)2     0.072952  0.169321   0.19   0.6666    
## factor(FARMID)3    -0.019794  0.132363   0.02   0.8811    
## factor(FARMID)4     0.063332  0.128095   0.24   0.6210    
## factor(FARMID)5     0.031047  0.135709   0.05   0.8190    
## factor(FARMID)6    -0.474796  0.176963   7.20   0.0073 ** 
## factor(FARMID)7    -0.272308  0.146192   3.47   0.0625 .  
## factor(FARMID)8    -0.072545  0.279720   0.07   0.7954    
## factor(FARMID)9    -0.066661  0.297679   0.05   0.8228    
## factor(FARMID)10   -0.244852  0.224595   1.19   0.2756    
## factor(FARMID)11   -0.400079  0.239705   2.79   0.0951 .  
## factor(FARMID)12   -0.473582  0.216774   4.77   0.0289 *  
## factor(FARMID)13    0.117716  0.302994   0.15   0.6976    
## factor(FARMID)14    0.104514  0.255829   0.17   0.6829    
## factor(FARMID)15    0.031459  0.370484   0.01   0.9323    
## factor(FARMID)16   -0.056807  0.232973   0.06   0.8074    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0567
## Number of clusters:   1364  Maximum cluster size: 4
summary(mm0)
## 
## Call:
## geeglm(formula = scc ~ my3 + TestDIMcat20 + factor(Parity) + 
##     DOSCC + Tx + factor(FARMID), data = dhi, id = CowID)
## 
##  Coefficients:
##                     Estimate   Std.err   Wald Pr(>|W|)    
## (Intercept)         3.351910  0.188167 317.32  < 2e-16 ***
## my3Mid production   0.003227  0.068036   0.00   0.9622    
## my3High production  0.039849  0.069379   0.33   0.5657    
## TestDIMcat2030     -0.541797  0.064641  70.25  < 2e-16 ***
## TestDIMcat2050     -0.556459  0.059010  88.92  < 2e-16 ***
## TestDIMcat2070     -0.418450  0.060647  47.61  5.2e-12 ***
## TestDIMcat2090     -0.286936  0.064794  19.61  9.5e-06 ***
## TestDIMcat20110    -0.123939  0.061047   4.12   0.0423 *  
## factor(Parity)2     0.132579  0.062601   4.49   0.0342 *  
## factor(Parity)3     0.302455  0.073465  16.95  3.8e-05 ***
## DOSCC               0.173249  0.026961  41.29  1.3e-10 ***
## TxA_SDCT           -0.000951  0.068521   0.00   0.9889    
## TxBDCT             -0.028799  0.063997   0.20   0.6527    
## factor(FARMID)2     0.072952  0.169321   0.19   0.6666    
## factor(FARMID)3    -0.019794  0.132363   0.02   0.8811    
## factor(FARMID)4     0.063332  0.128095   0.24   0.6210    
## factor(FARMID)5     0.031047  0.135709   0.05   0.8190    
## factor(FARMID)6    -0.474796  0.176963   7.20   0.0073 ** 
## factor(FARMID)7    -0.272308  0.146192   3.47   0.0625 .  
## factor(FARMID)8    -0.072545  0.279720   0.07   0.7954    
## factor(FARMID)9    -0.066661  0.297679   0.05   0.8228    
## factor(FARMID)10   -0.244852  0.224595   1.19   0.2756    
## factor(FARMID)11   -0.400079  0.239705   2.79   0.0951 .  
## factor(FARMID)12   -0.473582  0.216774   4.77   0.0289 *  
## factor(FARMID)13    0.117716  0.302994   0.15   0.6976    
## factor(FARMID)14    0.104514  0.255829   0.17   0.6829    
## factor(FARMID)15    0.031459  0.370484   0.01   0.9323    
## factor(FARMID)16   -0.056807  0.232973   0.06   0.8074    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation structure = independence 
## Estimated Scale Parameters:
## 
##             Estimate Std.err
## (Intercept)     1.71  0.0567
## Number of clusters:   1364  Maximum cluster size: 4
atx <- c(10,30,50,70,90,110)
emm <- emmeans(mm0, ~ my3*TestDIMcat20,adjust = "none", at=list(atx)) %>%confint %>% tidy()
emm$SCC <- exp(emm$estimate)
emm$LCL <- exp(emm$conf.low)
emm$UCL <- exp(emm$conf.high)
emm <- emm %>% subset(select=c(my3,TestDIMcat20,SCC,LCL,UCL))
emm$my3 <- fct_relevel(emm$my3, "Low production",
                       "Mid production",
                       "High production"
                         )

curve <- ggplot(emm) + coord_cartesian(ylim = (c(0,100))) + aes(x=as.numeric(TestDIMcat20), y=SCC, group=my3, colour=my3) +
  labs(y = "Somatic cell count \n(geometric mean x 1,000 cells/ml)", x = "Days in milk") + geom_point(size=3) + geom_ribbon(aes(ymin=emm$LCL, ymax=emm$UCL,colour=my3,fill=my3), linetype=0, alpha=0.1) + geom_line(aes(colour=my3,linetype=my3),size=1.1) + 
  scale_x_continuous(breaks = seq(10, 110, 20)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),axis.text=element_text(size=12,family="Times"),axis.title=element_text(size=14,face="bold",family="Times"),panel.background = element_rect(fill = "white", colour = "white",size = 0.5, linetype = "solid"),panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "grey"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "grey"),legend.position="bottom",legend.text = element_text(colour="black", size=12,face="bold",family="Times")) + scale_color_brewer(palette="Set1")

curve