-
Notifications
You must be signed in to change notification settings - Fork 2
/
within_herd_nov17.R
1247 lines (1037 loc) · 72.8 KB
/
within_herd_nov17.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Creation of own rpert function
rpert <- function(n,a,l,b) {
mu <- (a+4*l+b)/6
if (mu==l) v <- w <- 3 else {
v <- (mu-a)*(2*l-a-b)/(l-mu)/(b-a)
w <- v*(b-mu)/(mu-a)
}
a+(b-a)*rbeta(n,v,w)
}
#Building a function to generate the herd
GenerateAHerd <- function(NAnimals=7266){ #Number of animals set to 7266
Bes <- data.frame(Animal=1:NAnimals,Infected=FALSE,Status=1) #Create a data frame with this no. of animals
Lit_Table <- c(358 , 505 , 652 , 799 , 946 ,1093 ,1240 ,1387) #Vector for 8 litters (sow age in days upon farrowing)
#Introduction of different animal agegroups/types ("Type"). The categories are: 1 = Sows, 2 = WF, 3 = G, 4 = Boars
Type=rep_len(rep(c(1:4), c(23,322,1,0)),length(Bes$Animal)) #Type==4 (boars) is currently not included
Bes$Type=rep_len(rep(c(1:4), c(23,322,1,0)),length(Bes$Animal))
#Agedistribution in days (Dist) within the different agegroups
Dist <- c('rep_len(rep(seq(from=239,to=1388, by=7), each=23),sum(Bes$Type==1))','rep_len(rep(seq (from=1, to=170, by=7), each=322),sum(Bes$Type==2))','239','150')
#Distribution of batch numbers within the different agegroups. Set to one fot all gilts and boars, since batch production is not relevant for these
Dist2 <- c('rep_len(rep(seq(from=1,to=21, by=1), each=23),sum(Bes$Type==1))','rep_len(rep(seq (from=1, to=1000, by=1), each=12),sum(Bes$Type==2))', 'rep_len(rep(seq (1, 1000, by=1), each=5),sum(Bes$Type==3))','1')
#Assign the ages and batch numbers defined above to the animals
Bes$Age <- 0
for(i in unique(Bes$Type)){
Index <- Bes$Type==i
Bes$Age[Index] <- eval(parse(text=rep(Dist[i],sum(Index))))
Bes$teamo[Index] <- eval(parse(text=rep(Dist2[i],sum(Index))))
}
#Apply mixed sow age within the batches
for (teamo in 1:21) {
teamdiff <- rep_len(7*(teamo-1), 23)
Sowage <- sample(seq(240, 1269, by=147), 23, rep=T, prob=c(0.22, 0.20, 0.17, 0.14, 0.11, 0.085, 0.05, 0.025)) + teamdiff
Bes$Age[Bes$teamo==teamo&Bes$Type==1] <- Sowage
}
#Define and assign slaughterage to the slaughterpigs
sage <- c(rep(165, times=40),rep(c(158, 172), times=8), rep(c(151, 179), times=2)) #Andel restgrise sat til ca. 20%
Bes$Slaughterage <- ifelse(Bes$Type==2, sample(sage, replace=TRUE, nrow(Bes)),0)
#Define housing unit (stable) based on age on a given day
Tmate <-c(240:244) #First stay in the mating unit - time per cycle = 5 days, starting from 8 months of age. Sows/gilts are moved to the gestation unit 4 weeks after mating
Scycmat<-rep(0:7, each=length(Tmate))#Sow cycle number, repeats depend on length of stay in the relevant unit
Sclength<-147 #Length of a full sow cycle (No. of dsyd between the sow is returning to a given stable unit)
Mate<-Tmate+Scycmat*Sclength #Vector including the days in a sows' life spend in the mating unit (based on the sows (adjusted) age in days)
Tgest <- c(245:353) #First stay in the gestation unit - it is assumed that the duration of the gestation period is 114 days, and that the sow is moved to the farrowing stable, approximately 5 days before farrowing. Time per cycle in thsi unit = 109 days
Scycges<-rep(0:7, each=length(Tgest)) #Sow cycle number, repeats depend on length of stay in the relevant unit
Gest<-Tgest+Scycges*Sclength #Vector including the days in a sows' life spend in the gestation unit (based on the sows (adjusted) age in days)
TFarr <-c(354:386) #First stay in the farrowing unit - time per cycle = 33 days
Scycfar<-rep(0:7, each=length(TFarr)) #Sow cycle number, repeats depend on length of stay in the relevant unit
Farr<-TFarr+Scycfar*Sclength #Vector including the days in a sows' life spend in the farrowing unit (based on the sows (adjusted) age in days)
#Extension of the stay in the mating unit to include the first four weeks of the gestation period
Lit_no <- c(1:8) #for a given sow (no to be confused with the global litter no: "litter") #This variable is also used in rel. to culling
Farrowdays <- (Lit_no-1)*Sclength+358 #Day of farrowing
Matdays <- Farrowdays - 114 #Days of insemination
Matxtra <- rep(Matdays, each=28) + 1:28
Mate <- c(Mate, Matxtra)
Gest <- Gest[!(Gest%in%Mate)]
#Division into stables (age-based)
Bes$stable <- ifelse(Bes$Type==2&Bes$Age>=80, 5, ifelse(Bes$Type==2&Bes$Age<80&Bes$Age>28, 4,
ifelse(Bes$Type==2&Bes$Age<29, 3, ifelse(Bes$Type==1&Bes$Age%in%Gest, 2, ifelse(Bes$Type==1&Bes$Age%in%Farr, 3, 1)))))
#Categories for Stable 1 = Mate, 2 = Gest, 3 = Farr, 4 = Wean, 5 = Fini.
#In the beginning litternumber + pen no. etc are set to zero
Bes$litter <- 0
Bes$dam[Bes$Type %in% c(1,3)] <- Bes$Animal[Bes$Type %in% c(1,3)]
Bes$Sti <- 0
Bes$LCount <- 0
Bes$LCount[Bes$Type==1] <- sapply(Bes$Age[Bes$Type==1],function(x) sum(x>=Lit_Table))
#To balance the herd at start - 3 extra finisher batches are added
for(i in c(1:3)) {
tmp10 <- as.numeric(max(Bes$Animal)+1:322)
tmp11 <- as.data.frame(cbind(tmp10,FALSE,1, 2,148+((i-1)*7),max(Bes$teamo+1),0,5,0,9999,0,0))
names(tmp11) <- names(Bes)
Bes <- rbind(Bes,tmp11)
}
Slaage <- sample(sage, replace=TRUE, 644)
Bes$Slaughterage[Bes$teamo %in% c(max(Bes$teamo), (max(Bes$teamo)-1))] <- Slaage
if (any(Bes$Age>Bes$Slaughterage)) {
rem1 <- Bes$Animal[Bes$Type==2&Bes$Age>Bes$Slaughterage]
if (length(rem1)>0) {
Bes <- Bes[-which(Bes$Animal%in%rem1),]
}
}
Bes$dam[Bes$Type %in% c(1,3)] <- Bes$Animal[Bes$Type %in% c(1,3)]
Bes$dam[Bes$Type==2 & Bes$Age>28] <- 9999 #No need for assigning a dam to these, 9999 is used as "dummy" number
#Assignment of dam id to the piglets (which sow, were they born by)
for (i in 0:3) {
sid <- Bes$Animal[Bes$Type==1 & Bes$Age %in% (Farrowdays+1+(7*i))]
Bes$dam[Bes$Type==2&Bes$Age==((7*i)+1)] <- rep(sid, each=14) #14 is calibrated to match the 322 animals
}
sid <- Bes$Animal[Bes$Type==1 & Bes$Age %in% c(Farrowdays+29, 240)] #Needs to be different from above, as it bspecifically neeeds to match the 240 olds
Bes$dam[Bes$Type==2&Bes$Age==29] <- rep(sid, each=14)
#Addition of extra pigs to team 1 to balance batch size (only needed at simulation initiation, because it is the first batch - later batches will receive sows that have undergone re-insemination)
for(i in c(1:3)) {
tmp8 <- as.numeric(max(Bes$Animal+1))
tmp9 <- as.data.frame(cbind(tmp8,FALSE,1, 1,240+147*(i-1),1,0,1,0,tmp8,0,(i-1)))
names(tmp9) <- names(Bes)
Bes <- rbind(Bes,tmp9)
}
return(Bes)
}#End of GenerateAHerd
##################
### UpdateHerd ###
##################
#updateHerd <- function(runID="MRSA",MaxIterations=1,Days=26,DayOption=1,seed=1){
MaxIterations <- 25 #No. of interations to run
Days <- 3650 #No. of days to run
DayOption <- 1
seed <- 10 #Specify seed (if want to run with this)
runID <- "Test_output" #Name to identify output file
DiseaseStart <- 1460 #Day after simualation start, where the infection is first introduced
set.seed(seed)
#Prepare output file
Outputherd <- matrix(numeric(0),ncol=65)
NAME <- paste(runID,"MRSA.txt",sep="-")
write.table(Outputherd,NAME,sep=" ")
Bes <- GenerateAHerd()
for(iter in 1:MaxIterations){
#set various baseline values to be used later
gTime=0
TBes<-Bes
iteration <- iter
basefrss <- NULL
amsti <- NULL
d <- as.data.frame(NULL)
fasec <- 0
wsec <- 7
finsec <- 0
TBes$nsr <- 0
TBes$ns <- 0
TBes$nsow <- TBes$dam
TBes$nsd <- 0
TBes$ri <- 0
TBes$rid <- 0
TBes$teama <-TBes$teamo #Adjusted batch no is equal to the original batch no. at simulation start
TBes$PACount <-TBes$Age #PACount = adjusted age counter, wheres Age = chronological age counter
TBes$snoutcont <- 0
TBes$ProbInfInd <- 0 #Probability of infection
TBes$dcd <- 0 #Duration of carriage
TBes$Load <- 0 #
TBes$sec <- 0 #Housing section set to zero here, but specified later
TBes$sti <- 0
h <- seq(0,3650, by=14) #Will need extension if the model should ever need to run for more than 10 years
secfrach <-0.20 #Fraction of between pen spread, the between section spread will be equal to in workintensive units
secfrac <- 0.15 #As above for other units
stafrac <- 0.02 #Fraction of between pen spread, the between stables spread will be eqaul to
#Calibration factors (index in the name indicates stable followed by type, i.e. 11=sows in the mating stable (animal type 1 in stable unit 1))
kal11 <- 1
kal13 <- 1
kal21 <- 1
kal31 <- 1
kal32 <- 1
kal42 <- 1
kal52 <- 1
#Transmission parameters with use of risk antimicrobials - inputs for distributions
BetaWDW <-0.17931 #Based on Broens et al., 2012a: Value for post-weaning, ab:+ and pIP=1 and Broens, 2011 (Duration of infection=17.4 days)
BetaWDWMin <-0.06609
BetaWDWMax <-0.48621
BetaWDF <-0.17931 #Currently the same as for weaners
BetaWDFMin <-0.06609
BetaWDFMax <-0.48621
BetaWDO <-0.17931 #Within pen for others (gilts and pregnant sows sharing pen in the gestation stable). Currently the same as for finishers
BetaWDOMin <-0.06609
BetaWDOMax <-0.48621
BetaBPDW <-0.03448 #The value from Broens for post-weaning, ab:+ and pIP=0
BetaBPDWMin <-0.01954
BetaBPDWMax <-0.06092
BetaBPDF <-0.03448 #Same as for weaners
BetaBPDFMin <-0.01954
BetaBPDFMax <-0.06092
BetaBPDO <-0.03448 #This value needs to be somewhere between BPDF and BPDW (currently the same value).
BetaBPDOMin <-0.01954
BetaBPDOMax <-0.06092
BetaBPDX <-0.08966 #Pre-weaning values
BetaBPDXMin <-0.03736
BetaBPDXMax <-0.21667
BetaSOD <-0.46437 #Currently the same as between piglets
BetaSOMin <-0.12471 #Currently the same as between piglets
BetaSOMax <-1.73103 #Currently the same as between piglets
BetaPID <-0.46437 #Broens value for pre-weaning pigs, ab:+, pIP=1
BetaPIMin <-0.12471
BetaPIMax <-1.73103
#Transmission paramters without use of risk antimicrobials
# BetaWDW <-0.0711 #%AB, Based on Broens et al., 2012a: Value for post-weaning, ab:% and pIP=1 and Broens, 2011 (Duration of infection=17.4 days)
# BetaWDWMin <-0.0345 #%AB
# BetaWDWMax <-0.1425 #%AB
# BetaWDF <-0.0711 #%AB #Currently the same as for weaners
# BetaWDFMin <-0.0345 #%AB
# BetaWDFMax <-0.1425 #%AB
# BetaWDO <-0.0711 #%AB #Within pen for others (gilts and pregnant sows sharing pen in the gestation stable). Currently the same as for finishers
# BetaWDOMin <-0.0345 #%AB
# BetaWDOMax <-0.1425 #%AB
# BetaBPDW <-0.01379 #%AB #The value from Broens for post-weaning, ab:+ and pIP=0
# BetaBPDWMin <-0.01034 #%AB
# BetaBPDWMax <-0.01782 #%AB
# BetaBPDF <-0.01379 #%AB #Same as for weaners
# BetaBPDFMin <-0.01034 #%AB
# BetaBPDFMax <-0.01782 #%AB
# BetaBPDO <-0.01379 #%AB #This value needs to be somewhere between BPDF and BPDW (current the same value).
# BetaBPDOMin <-0.01034 #%AB
# BetaBPDOMax <-0.01782 #%AB
# BetaBPDX <-0.03506 #%AB #Pre-weaning values
# BetaBPDXMin <-0.01954 #%AB
# BetaBPDXMax <-0.06322 #%AB
# BetaSOD <-0.18161 #Currently the same as between piglets
# BetaSOMin <-0.06552 #Currently the same as between piglets
# BetaSOMax <-0.50690 #Currently the same as between piglets
# BetaPID <-0.18161 #Broens value for pre-weaning pigs, ab:+, pIP=1 0.18161 0.06552 0.50690
# BetaPIMin <-0.06552
# BetaPIMax <-0.50690
#Intermediate transmission parameters
# BetaWDW <-0.124713 #Avg. of +ab and %ab
# BetaWDWMin <-0.050287 #Avg. of +ab and %ab
# BetaWDWMax <-0.314368 #Avg. of +ab and %ab
# BetaWDF <-0.124713 #Avg. of +ab and %ab
# BetaWDFMin <-0.050287 #Avg. of +ab and %ab
# BetaWDFMax <-0.314368 #Avg. of +ab and %ab
# BetaWDO <-0.124713 #Avg. of +ab and %ab
# BetaWDOMin <-0.050287 #Avg. of +ab and %ab
# BetaWDOMax <-0.314368 #Avg. of +ab and %ab
# BetaBPDW <-0.024138 #Avg. of +ab and %ab
# BetaBPDWMin <-0.014943 #Avg. of +ab and %ab
# BetaBPDWMax <-0.039368 #Avg. of +ab and %ab
# BetaBPDF <-0.024138 #Avg. of +ab and %ab
# BetaBPDFMin <-0.014943 #Avg. of +ab and %ab
# BetaBPDFMax <-0.039368 #Avg. of +ab and %ab
# BetaBPDO <-0.024138 #Avg. of +ab and %ab
# BetaBPDOMin <-0.014943 #Avg. of +ab and %ab
# BetaBPDOMax <-0.039368 #Avg. of +ab and %ab
# BetaBPDX <-0.062356 #Avg. of +ab and %ab
# BetaBPDXMin <-0.028448 #Avg. of +ab and %ab
# BetaBPDXMax <-0.139943 #Avg. of +ab and %ab
# BetaSOD <-0.322989 #Avg. of +ab and %ab #Currently the same as between piglets
# BetaSOMin <-0.095115 #Avg. of +ab and %ab #Currently the same as between piglets
# BetaSOMax <-1.118966 #Avg. of +ab and %ab #Currently the same as between piglets
# BetaPID <-0.322989 #Avg. of +ab and %ab
# BetaPIMin <-0.095115 #Avg. of +ab and %ab
# BetaPIMax <-1.118966 #Avg. of +ab and %ab
#Probabilities for tranmission during day 1 in the life of a piglet
ProbPND <-0.76 #Perinatal + all other transmission at day zero. Probability of the offspring being pos given that the dam is pos, not transmission rate. Based on Verhegghe et al., 2013. (Broens: 0.81 or 0.78)
ProbPNMin <-0.56 #Based on the error bars in Verhegghe et al., 2013 (which I assume illustrate the 95% CI's even if it is not stated)
ProbPNMax <-0.91 #Based on the error bars in Verhegghe et al., 2013 (which I assume illustrate the 95% CI's even if it is not stated)
ProbNPND <-0.35 #Perinatal + all other transmission at day zero. Probability of the offspring being pos given that the dam is neg., not transmission rate. Based on Verhegghe et al., 2013. (Broens: 0.81 or 0.78)
ProbNPNMin <-0.26 #Based on the error bars in Verhegghe et al., 2013 (which I assume illustrate the 95% CI's even if it is not stated)
ProbNPNMax <-0.46 #Based on the error bars in Verhegghe et al., 2013 (which I assume illustrate the 95% CI's even if it is not stated)
#Distributions for transmission parameters
BetaWPW <- rpert(1,BetaWDWMin, BetaWDW,BetaWDWMax) #Within pen for weaners, mean value used as most likely value, min and max: guestimate based on R0 95% CIs
BetaWPF <- rpert(1,BetaWDFMin,BetaWDF,BetaWDFMax) #Within pen for finishers, mean value used as most likely value, min and max: guestimate based on R0 95% CIs
BetaWPO <- rpert(1,BetaWDOMin,BetaWDO,BetaWDOMax) #Within pen for other post-weaning pigs housed in groups (gilts and pregnant sows)
BetaBPW <- rpert(1,BetaBPDWMin, BetaBPDW, BetaBPDWMax) #Between pens for weaners
BetaBPF <- rpert(1,BetaBPDFMin,BetaBPDFMin,BetaBPDFMax) #Between pens for finishers
BetaBPO <- rpert(1,BetaBPDOMin,BetaBPDO, BetaBPDOMax) #Between pens for other post-weaning pigs housed in groups (gilts and pregnant sows)
BetaBPX <- rpert(1,BetaBPDXMin,BetaBPDX, BetaBPDXMax) #Between pens for pens housing mixed ageagroups (sow+ piglets) in the farrow stable
BetaPI <- rpert(1,BetaPIMin, BetaPID,BetaPIMax) #Between piglets housed within the same pen
BetaSO <- rpert(1,BetaSOMin, BetaSOD,BetaSOMax) #Between a sow and the piglets suckling at her (no difference between own offspring or piglets she might be functioning as nursery sow for) #All spread at day zero including perinatal transmission included in PND
BetaBSE <- secfrac*BetaBPX
BetaBSEh <- secfrach*BetaBPX #Between sections in work-intensive units (farrowing and mating)
BetaBSE0 <- 0 #Used for the gestation stable, where there is no sectioning
BetaBSTA <-stafrac*BetaBPX
PrevCuttOff <- rpert(1,0.5,0.7,1.0)
ProbPershigh <- rpert(1,0.5,0.75,1)
ProbPersLow <- rpert(1,0.01,0.10,0.40)
#Other paramters
prob.pers.mean <- 0.24 #Mean probability of having the potential to become a persistent shedder #Partly based on Espinosa-Gongora et al., 2015
prob.pers.sd <- 0.01 #Standard deviation for the above - a guess
prob.pers <- rnorm(1,prob.pers.mean, prob.pers.sd)
prob.pers[prob.pers<0] <- 0 #Truncation of the distribution (negative values set to zero)
DurCarMed <- 7.5 #Median duration of MRSA carriage= 7.5 # / Mean = 10.3 days / SD= 7.7 based on Broens et al., 2012 (no distinction between persistent and intermittent carriers, defined as positive if testet positive once (17.4 if testing positive at two consequtive samplings are required))
DurCarMin <- 1 #Min duration of MRSA carriage. Broens et al 2012 (10.3 scenario)
DurCarMax <- 26 #Max duration of MRSA carriage. Broens et al 2012 (10.3 scenario)
NewInf <- numeric(0)
InfPens <- numeric(0)
TBes$Infected <- FALSE
Pers.carriage <- sample(2:4,length(TBes$Animal), rep=TRUE, prob=c(1-prob.pers,prob.pers*0.99, prob.pers*0.01))
TBes$Status <- Pers.carriage #Pigs are assigned the potential to either become intermittent shedders upon exposure (Status=2),
#or to become persistent shedders without (Satus=3) or with (Satus=4) probability of recovery
TBes$Sec <- 0
TBes$BetaLUF <- 0
TBes$BetaWPT <- 0
TBes$BetaBPT <- 0
TBes$BetaBST <- 0
TBes$BetaBStT <- 0
TBes$NumWPInf <- 0
TBes$NumBPInf <- 0
TBes$NumBSInf <- 0
TBes$NumWPAll <- 0
TBes$NumBPAll <- 0
TBes$NumBSAll <- 0
TBes$ShedStatus <- 0 #Current shedder status for the pig
NewInfected <- NULL
#Repetions from the generate herd function, that also need to be run every day
#Define housing unit (stable) based on age on a given day
Tmate <-c(240:244) #First stay in the mating unit - time per cycle = 5 days, starting from 8 months of age. Sows/gilts are moved to the gestation unit 4 weeks after mating
Scycmat<-rep(0:7, each=length(Tmate))#Sow cycle number, repeats depend on length of stay in the relevant unit
Sclength<-147 #Length of a full sow cycle (No. of dsyd between the sow is returning to a given stable unit)
Mate<-Tmate+Scycmat*Sclength #Vector including the days in a sows' life spend in the mating unit (based on the sows (adjusted) age in days)
Tgest <- c(245:353) #First stay in the gestation unit - it is assumed that the duration of the gestation period is 114 days, and that the sow is moved to the farrowing stable, approximately 5 days before farrowing. Time per cycle in thsi unit = 109 days
Scycges<-rep(0:7, each=length(Tgest)) #Sow cycle number, repeats depend on length of stay in the relevant unit
Gest<-Tgest+Scycges*Sclength #Vector including the days in a sows' life spend in the gestation unit (based on the sows (adjusted) age in days)
TFarr <-c(354:386) #First stay in the farrowing unit - time per cycle = 33 days
Scycfar<-rep(0:7, each=length(TFarr)) #Sow cycle number, repeats depend on length of stay in the relevant unit
Farr<-TFarr+Scycfar*Sclength #Vector including the days in a sows' life spend in the farrowing unit (based on the sows (adjusted) age in days)
#Extension of the stay in the mating unit to include the first four weeks of the gestation period
Lit_no <- c(1:8) #for a given sow (no to be confused with the global litter no: "litter") #This variable is also used in rel. to culling
Farrowdays <- (Lit_no-1)*Sclength+358 #Day of farrowing
Matdays <- Farrowdays - 114 #Days of insemination
Matxtra <- rep(Matdays, each=28) + 1:28
Mate <- c(Mate, Matxtra)
Gest <- Gest[!(Gest%in%Mate)]
#Define and assign slaughterage to the slaughterpigs
sage <- c(rep(168, times=40),rep(c(161, 175), times=20), rep(c(154, 182), times=10))
print(iteration) #Print iteration no. if want to follow run
### Start of daily loop
#DaysToSelectInf <- 1:DayOption
while(gTime<=Days & length(TBes$Animal[TBes$Infected==0])>1){
gTime <- gTime + 1
TBes$Age <- TBes$Age + 1 #Cronological age of the pig
TBes$PACount <- TBes$PACount + 1 #Activity-adjusted age counter
if(sum(is.na(TBes))>0) warning('NAs have been introduced')
### Farrowing
Lit_no <- c(1:8) #for a given sow (no to be confused with the global litter no: "litter") #This variable is also used in rel. to culling
Farrowdays <- (Lit_no-1)*Sclength+358 #We assumed fixed farrowing days (but the will be some variation due to adjustment of the agecounter)
#Generation of ne litters (For newborns batchnumber is determined by litter)
IndexSow <- TBes$PACount%in%Farrowdays & TBes$Type==1
if(sum(IndexSow)>0){
TBes$LCount[match(TBes$Animal[IndexSow],TBes$Animal)] <- TBes$LCount[match(TBes$Animal[IndexSow],TBes$Animal)] + 1
for(i in TBes$Animal[IndexSow]){
tmp <- as.numeric(max(TBes$Animal)+(1:round(rnorm(n=1, mean=15.9,sd=1.5)))) #Baseret on no. of weaned piglets per litter = 13.8, lifeborn per litter = 15,9
tmp2 <- as.data.frame(cbind(tmp,FALSE,1,2,0,(max(TBes$teamo[TBes$Type==2])+1), sample(sage, replace=TRUE, length(tmp)),3,(max(TBes$litter)+1),i,0,0,0,0,i,0,0,0,(max(TBes$teama[TBes$Type==2])+1),0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
names(tmp2) <- names(TBes) #Piglets are temporaily all placed in pen 0"
TBes <- rbind(TBes,tmp2)
}
}
###Cross-fostering / creation of nursery sows to ensure that each sow has a max. of 14 piglets to take care of
# It is assumed that two-step nursery sows are used
tmp7 <- NULL
tmp7 <- subset(TBes, TBes$Type==2&TBes$Age==1) #Get datalines for all day-old piglets on the farm
tmp7 <- tmp7[order(tmp7$litter),] #Sort them
tmp7$ind <- ave(tmp7$Animal, tmp7$litter, FUN = seq_along)
ForDistAge1 <- subset(tmp7, tmp7$ind>14&tmp7$litter>0&tmp7$Age==1) #Day-old piglets for distribution among nursery sows
fsti <- 1:45
if(dim(ForDistAge1)[1]>0){
### Sows that have 8 days old piglets and are selected as nursery sows are getting these replaced by day-old piglets.
SowsToGetPigletsAge1 <- TBes$Animal[TBes$PACount%in%(Farrowdays+8)&TBes$Type==1&!TBes$ns%in%c(1,2)]
NoPigletsFroDistAge1 <- tmp7$Animal[tmp7$ind>14]
SowsGotAge1 <- SowsToGetPigletsAge1[1:(ceiling(length(NoPigletsFroDistAge1)/14))]
AnimalSeq <- rep(1:ceiling(length(NoPigletsFroDistAge1)/14),each=14) #It is assumed taht each sow can maximum handle 14 piglets
AnimalSeq <- AnimalSeq[1:length(NoPigletsFroDistAge1)]
Tmp <- sapply(unique(AnimalSeq),function(x)sum(AnimalSeq==x))
SowPig <- cbind(NoPigletsFroDistAge1 ,rep(SowsGotAge1,times=Tmp))
Newsec <- unique(TBes$sec[TBes$Animal %in% NoPigletsFroDistAge1]) #Identify section in the farrowing tsbale the nursery sows will be housed in
EmptyPens <- fsti[!fsti%in%unique(TBes$sti[TBes$sec==Newsec & TBes$stable==3])] #Identify empty pens
## Removal in case of "pig overflow" - will introduce a warning
if(length(EmptyPens)<length(SowsGotAge1 )){
SowsGotAge12 <- SowsGotAge1
SowsGotAge1 <- SowsGotAge1[1:length(EmptyPens)]
SowsGotAge12 <- SowsGotAge12[(length(SowsGotAge1 )+1):length(SowsGotAge12)]
ToRemove <- SowPig[SowPig[,2]%in%SowsGotAge12,1]
SowPig <- SowPig[SowPig[,2]%in%SowsGotAge1,]
ToRemAnimals <- match(ToRemove,TBes$Animal)
TBes <- TBes[-ToRemAnimals,]
#warning("piglets were removed")
}
TBes$sec[match(SowsGotAge1 ,TBes$Animal)] <- Newsec #Put the nursery sows intro the relevant section
ToTakePens <- EmptyPens[1:length(SowsGotAge1 )] #Select empty pens and put the new nursery sows into them
TBes$sti[match(SowsGotAge1 ,TBes$Animal)] <- ToTakePens
TBes$ns[match(SowsGotAge1 ,TBes$Animal)] <- 1 #"Nursery sow indicator": If=1, the pig is currently used as nursery sow
TBes$nsd[match(SowsGotAge1 ,TBes$Animal)] <- gTime + 28 #"Nursery sows duration": Untill which gTime step will the sow remain a nursery sow
TBes$PACount[match(SowsGotAge1 ,TBes$Animal)] <- TBes$PACount[match(SowsGotAge1 ,TBes$Animal)] - 7 #Down-adjust the activity-based agecounter for the nursery sows by one week (since they now got one week younger piglets compared to before)
Tmp2 <- match(SowPig[,1],TBes$Animal)
TBes$nsow[Tmp2] <- SowPig[,2] #This variables indicates who have been nursery sows for a given pig (can be rqual to TBes$dam = the biological mother animal)
for(i in unique(SowPig[,2])){
Index <- SowPig[,2]==i
Pigs <- match(SowPig[Index,1],TBes$Animal)
TBes$sti[Pigs] <- TBes$sti[match(i,TBes$Animal)]
}
#Same procedure for the 8 day old piglets, that now will have to be taken over by other sows
ForDistAge8 <- NULL
ForDistAge8 <- TBes[TBes$dam%in%SowsGotAge1 & TBes$dam==TBes$nsow & TBes$Age%in%c(7,8),c(1,15)] #Changed from 7 to 8
if(dim(ForDistAge8)[1]>0){
SowsToGetPigletsAge8 <- TBes$Animal[TBes$PACount%in%(Farrowdays+29) & TBes$Type==1 & !TBes$ns%in%c(1,2)]
TMP3 <- rep(1:length(unique(ForDistAge8$nsow)),times=sapply(unique(ForDistAge8$nsow),function(x)sum(ForDistAge8$nsow==x)))
ForDistAge8 <- cbind(ForDistAge8,SowsToGetPigletsAge8[TMP3])
TBes$nsow[match(ForDistAge8[,1],TBes$Animal)] <- ForDistAge8[,3]
TBes$sec[match(ForDistAge8[,3],TBes$Animal)] <- unique(TBes$sec[match(ForDistAge8[,1],TBes$Animal)])
TBes$sti[match(ForDistAge8[,3],TBes$Animal)] <- TBes$sti[match(ForDistAge8[,1],TBes$Animal)]
TBes$ns[match(ForDistAge8[,3],TBes$Animal)] <- 1
TBes$nsd[match(ForDistAge8[,3],TBes$Animal)] <- gTime + 21
TBes$PACount[match(ForDistAge8[,3],TBes$Animal)] <- TBes$PACount[match(ForDistAge8[,3],TBes$Animal)] - 21
}
TBes$teama <- ifelse(TBes$ns==1,22, TBes$teamo) #Nursery sows are temporaily given batch no. 22
}
if (any(TBes$nsd==gTime&TBes$ns==1)) {
nm <- Farrowdays+29
TBes$teamo[TBes$nsd==gTime&TBes$ns==1] <- TBes$teama[TBes$PACount %in% nm & TBes$Type==1 & TBes$ns%in%c(0,9999) & TBes$teama<22] [1]
TBes$teama[TBes$nsd==gTime&TBes$ns==1] <- TBes$teamo[TBes$nsd==gTime&TBes$ns==1]
TBes$ns[TBes$nsd==gTime&TBes$ns==1] <- 0
}
SowsGotAge1 <- NULL
### Sows needing re-insemination
ProbOm <- 0.12 #Probability of an inseminated sow not farrowing after first insemination ( in principle 0.88 for 1 and 8 litters, 0.90 for the rest)
TBes$ri[TBes$rid==gTime] <- 0 #If -1 this indicates that the sow is reinseminated (used as indicator, when "moving" pigs to a new team)
Matdays <- Farrowdays - 114 #The length of the gestation period might have to be adjusted to 118 days all the way thorughout the model
heatcontroldays <- Matdays + 21 #3 weeks after insemination it is checked whether any of the sows have become in heat again and therefore need re-insemination
pregcontroldays <- Matdays + 28 #4 weeks after insemination it is checked whether the sows are pregnant
probreins <- c(1.00, 0.70, 0.55, 0.50, 0.40, 0.30, 0.20, 0) #Probability of re-insemination #Depends on litter count (prob. for 1-8 litters are from vsp, 1.0 for gilts (0 litters) are an assumption)
Sow_mat <- TBes[TBes$Type==1&TBes$stable==1&TBes$PACount%in%heatcontroldays,1] #Sows in the mating stable ready for insemination #ns=-1 is not excluded, since they are inseminated on the day they are identified
notreins <- NULL
if (length(Sow_mat)>0) {
if(any(TBes$PACount[TBes$Type==1]%in%heatcontroldays)){ #
tmp5 <- Sow_mat[runif(length(Sow_mat))>=(1-ProbOm)] #Binomial process coded, as an alternative to rbinom
tmp6 <- TBes[TBes$Animal %in% tmp5,c(1,12)] #Column 11 contains LCount, which indicates what value to use in the probreins vector
if (nrow(tmp6)==1) {
tmp6$oml <- rbinom(1,1,probreins[(tmp6[1,2]+1)])
} else {
tmp6$oml <- rbinom(tmp6[,1],1,probreins[(tmp6[,2]+1)])
}
if(length(tmp6[tmp6$oml==1,1])>0){
TBes$ri[TBes$Animal%in%tmp6[tmp6$oml==1,1]] <- -1 #Just put in to indicate newcomers to a team
TBes$rid[TBes$Animal%in%tmp6[tmp6$oml==1,1]] <- 1 + gTime #Just to keep the animals tagged for one day untill moved to the right section and pen
TBes$PACount[TBes$Animal%in%tmp6[tmp6$oml==1,1]] <- TBes$PACount[TBes$Animal%in%tmp6[tmp6$oml==1,1]] - 21
VectorX <- c(19:21,1:18)
for(i in 1:length(tmp6[tmp6$oml==1,1])){
TBes$teamo[TBes$Animal%in%tmp6[tmp6$oml==1,1][i]] <- VectorX[TBes$teamo[TBes$Animal%in%tmp6[tmp6$oml==1,1][i]]]
TBes$teama[TBes$Animal%in%tmp6[tmp6$oml==1,1][i]] <- TBes$teamo[TBes$Animal%in%tmp6[tmp6$oml==1,1][i]] }
}
TBes$sec[TBes$ri<0] <- 0
TBes$sti[TBes$ri<0] <- 0
}
notreins <- tmp5[-which(tmp5%in%tmp6[tmp6$oml==1,1])]
}
### Movement between stables
TBes$stable <- ifelse(TBes$teama==22&TBes$Type==1, 3,
ifelse(TBes$ri==-1, 1,
ifelse(TBes$Type==2&TBes$PACount>=80, 5,
ifelse(TBes$Type==2&TBes$PACount<80&TBes$PACount>28, 4,
ifelse(TBes$Type==2&TBes$PACount<29, 3, #Changed so that day 28 now is the weaning stable
ifelse(TBes$Type==1&TBes$PACount%in%Gest, 2,
ifelse((TBes$Type==1&TBes$PACount%in%Farr), 3, 1)))))))
#Categories for Stable 1 = Mati, 2 = Gest, 3 = Farr, 4 = Wean, 5 = Fini.
### "Left-over pigs": Approx. 20% 'slow growing leftover-pigs' are to remain in the weaner stable at the time where movement from weaner to finisher stable should usually have taken place.
#Movement is postponed a week for each time a pig is sampled
#Pigs are ssumed to be moved to the finisher stable when the reach an age of 81 days
ProbRWF <- 0.20 #Probability of remaining in the weaner stable, when the rest of the team is moved to the finisher stable
if (any(TBes$PACount==80)) {
WeFi <- TBes$Animal[TBes$PACount==80&TBes$Type==2&TBes$Age<110] #Inserted age maximum, as a pig else in principle could be sampled to remain in the stable again and again
RWeFi <- sample(WeFi,ProbRWF*length(WeFi), replace=FALSE) #Inserted vhronological age counter maximum, as a pig else in principle could be sampled to remain again and again
TBes$teamo <- ifelse(TBes$Animal%in%RWeFi, TBes$teamo + 1, TBes$teamo + 0)
TBes$PACount <- ifelse(TBes$Animal%in%RWeFi, TBes$PACount - 7, TBes$PACount)
TBes$stable[TBes$Animal%in%RWeFi] <- 4
TBes$sec[TBes$Animal%in%RWeFi] <- -1 #Currently, these are put in a separate section of the weaner stable, but then leftover pigs from different teams will be mixed there
penrest <- rep(1:ceiling((length(RWeFi))/30), each=30)
TBes$sti[TBes$Animal%in%RWeFi] <- penrest[1:length(RWeFi)] #It is assumed that all pigs leaves after 7 days or is selected again, so no update of empty pens are needed
}
###Pigs are sent for slaughter during 3-5 weeks at one specific weekday. The majority of pigs will go at an estimated age around 168 days (24 weeks)
#Is based on PACount and not Age. Slaugtherage has already been assigned: a) When the herd was generated or b) when the litter was generated
#Leftover pigs are moved to the buffertsbale at the same time
#ugt <- seq(from=3, to=Days, by=7)
ugt <- round(seq(from=3, to=Days, by=3.5)) #Days where pigs are sent for slaughter
Sla <- NULL
if (gTime%in%ugt&length(TBes$Animal[TBes$PACount >= TBes$Slaughterage & TBes$Type==2])>0) {
Sla <- TBes[TBes$PACount>=TBes$Slaughterage&TBes$Type==2,1]
if (length(Sla)>0) {
TBes <- TBes[-which(TBes$Animal%in%Sla),]
}
#Leftover pigs are put together and moved into the buffer stable (which is just a separate section in the finisher stable named section -1 (to avoid that this sec ever gets selected as max)):
#It needs to be ensured that this is only done for pigs close to slaughterage (chronological age limit put to 158)
per_sec <- aggregate(data=TBes[TBes$stable==5,], Animal ~ sec, function(x) length(unique(x))) #Gives the no. of animals per section
colnames(per_sec) <- c("sec", "No_of_animals")
#halfempt <- max(ifelse(per_sec$No_of_animals<140, per_sec$sec, 0))
halfempt <- unique(ifelse(per_sec$No_of_animals<140, per_sec$sec, 0))
halfempt <- halfempt[halfempt>0]
#halfempt <- as.vector(ifelse(per_sec$No_of_animals<32&TBes[TBes$Animal%in%per_sec$Animal,]>TBes$Slaughterage[TBes$Animal%in%per_sec$Animal], per_sec$sec, 0)) #Dvs. kun svin med justeret PACount kan ende i bufferstalden.
movtobuff <- TBes$Animal[TBes$stable==5&TBes$sec%in%halfempt&TBes$PACount>158]
if (length(movtobuff)>150) {
buffage <- TBes[TBes$Animal %in% movtobuff, c(1,20)]
agegrad <- buffage[order(-buffage$PACount),]
notoberem <- length(movtobuff)-150
Sla2 <- agegrad$Animal[1:notoberem]
TBes <- TBes[-which(TBes$Animal%in%Sla2),]
}
if (length(movtobuff)>0) {
TBes$sec[TBes$Animal%in%movtobuff] <- -1 #The buffer section if referred to as sec= -1
TBes$sti[TBes$Animal%in%movtobuff] <- 0 # In case of movetobuff consisting of pigs from more than one pen, I am not keeping track of which ones are from which pen, but just assume they are sorted after size
Bsti <- 1:10
EmptyBufPens <- Bsti[!Bsti%in%unique(TBes$sti[TBes$stable==5&TBes$sec==-1])] #identify empty pens in the buffer section
pensneeded <- ceiling(length(movtobuff)/15) #How many pens are needed (with 15 animals per pen)
if (pensneeded > length(EmptyBufPens)) {
per_pen <- aggregate(data=TBes[TBes$stable==5&TBes$sec==-1,], Animal ~ sti, function(x) length(unique(x)))
pens_to_be_emptied <- pensneeded - length(EmptyBufPens)
less_fill_pens <- per_pen$sti[order(per_pen$Animal)] #less than full pens?
pens_empt <- less_fill_pens[1:pens_to_be_emptied]
Sla3 <- TBes$Animal[TBes$stable==5&TBes$sec==-1&TBes$sti%in%pens_empt]
if (length(Sla3)>0) {
TBes <- TBes[-which(TBes$Animal%in%Sla3),]
}
EmptyBufPens <- Bsti[!Bsti%in%unique(TBes$sti[TBes$stable==5&TBes$sec==-1])]
}
pensassigned <- rep(EmptyBufPens[1:pensneeded], each=15)
TBes$sti[TBes$Animal%in%movtobuff] <- pensassigned[1:length(movtobuff)] #The pigs have now been put into the empty pens
}
}
### Replacement of removed sows by gilts
#With a goal of 23 farrowings per team 0.22*23 -> approx. 5 first parity sows per team
#This means that (as a start) we will model a weekly purchase of 4 gilts (none of the sows will have reached 8 parities, so in the beginning replacement will be slow)- these gilts are bought approx. 7 weeks before they are expected to become ready for the firt insemination
#It is assumed that the gilts will come in heat and be inseminated for the first time, when they are approx. 240 days old (->age at purchase = approx. 191 dage)
### Purchase of gilts: Gilts are in separate teams untill first insemination.
#Definition of h moved to the very start of the update herd code (also used for defining placement into pens)
if (gTime%in%ugt & length(TBes$Animal[TBes$Type==3])<50) { #50 gilts is in principle a bit too much, but nescessary due to variation
tmp3 <- as.numeric(max(TBes$Animal)+(1:(50-length(TBes$Animal[TBes$Type==3]))))
tmp4 <- as.data.frame(cbind(tmp3,FALSE,1,3,191,(max(TBes$teamo[TBes$Type==3])+1),0,1,0,tmp3,0,0,0,0,tmp3,0,0,0,(max(TBes$teamo[TBes$Type==3])+1),191,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))
names(tmp4) <- names(TBes)
TBes <- rbind(TBes,tmp4)
}
### First insemination of gilts / integration into the sow teams
#Expected age when the gilt come in oestrus for the first time: 238-242 days
#k is no. of sows already available for insemination in the mating section
k <- TBes$Animal[TBes$stable==1&TBes$Type==1&TBes$PACount%in%(Matdays-3)] #Gilts available for insemination #Evaluation day changed to matdays-3 to ensure that this will not be evaluated untill after culling of sows
l <- NULL
if (length(k)>0 & length(k)<23 & any(TBes$PACount%in%(Matdays-3))) { #New gilts are added to a sow team if it consistes of less than 23 sows
samplingpool <- sort(TBes$Animal[TBes$Type==3&TBes$Age>=238]) #Gilts to select from
l <- samplingpool[1:(26-length(k))]
l <- l[!is.na(l)]
TBes$Type[TBes$Animal%in%l] <- 1 #Update status for selected gilts from gilts (type=3) to sows (type=1)
TBes$teamo[TBes$Animal%in%l] <- rep(unique(TBes$teamo[TBes$Animal%in%k]), length(l)) #Update their batch no
TBes$teama[TBes$Animal%in%l] <- TBes$teamo[TBes$Animal%in%l] #Update their adjusted batch no.
#TBes$PACount[TBes$Animal%in%l] <- rep(min(TBes$PACount[TBes$Animal%in%k]), length(l))
TBes$PACount[TBes$Animal%in%l] <- rep((Matdays[1]-3), length(l)) #not really realisitc, but solves problems with adjusting to a far to high age, which is unrealistic too
TBes$ns[TBes$Animal%in%l] <- 1000 #ns=1000 is temporaily used as tag for imported gilts
}
##################################
#Placement into sections and pens
#Housing in the farrowing unit
movedays <- Farrowdays - 4
if (gTime==1) { #Assignment of places in the farrowing unit from the beginning
teama <- sort(unique(TBes[TBes$Type==1&TBes$stable==3&TBes$teama<22,19]), decreasing=FALSE) #Type needs to be set to one, as the piglets have diff team numbers#Sorted to ensure that team 22 (nursery sows from diff teams) will always end up together in section 6
FSec <- 1:5 #Sections available for sows that are not nursery sows
for (i in 1:length(teama)) {
TBes$sec[TBes$stable==3&TBes$Type==1&TBes$teama==teama[i]] <- i
TBes$sti[TBes$stable==3&TBes$Type==1&TBes$teama==teama[i]] <- 1:length(TBes$Animal[TBes$stable==3&TBes$Type==1&TBes$teama==teama[i]])
}
TBes$sec[TBes$stable==3&TBes$Type==2] <- TBes$sec[match(TBes$nsow[TBes$stable==3&TBes$Type==2],TBes$Animal)]
TBes$sti[TBes$stable==3&TBes$Type==2] <- TBes$sti[match(TBes$nsow[TBes$stable==3&TBes$Type==2],TBes$Animal)]
}
#Update of places when new are filled/emptied
if (any(TBes$PACount%in%movedays & TBes$Type==1 | TBes$PACount==0)) { #Husk at team 22 skal kunne opdateres på andre dage end movedays
TBes$sec[TBes$PACount%in%movedays & TBes$Type==1&TBes$stable==3&TBes$ns %in% c(0,9999)] <- 0 #Nulstilling af sektion og dyr for myligt indflyttede dyr
TBes$sti[TBes$PACount%in%movedays & TBes$Type==1&TBes$stable==3&TBes$ns %in% c(0,9999)] <- 0
#FSec <- 1:5 #Sections available for sows that are not nursery sows
EmptyFSec <- FSec[!FSec%in%unique(TBes$sec[TBes$stable==3&TBes$Type==1])]
TBes$sec[TBes$PACount%in%movedays & TBes$Type==1&TBes$stable==3&TBes$ns %in% c(0,9999)] <- EmptyFSec[1]
EmptyFPens <- fsti[!fsti%in%unique(TBes$sti[TBes$stable==3&TBes$Type==1&TBes$sec==EmptyFSec[1]])]
incomfarpigs <- TBes$Animal[TBes$PACount%in%movedays & TBes$Type==1&TBes$stable==3&TBes$ns %in% c(0,9999)&TBes$sec==EmptyFSec[1]]
if (length(EmptyFPens)<length(incomfarpigs)) { #If the no. of animals exceeds stable capacity, these are removed for sale or culled - shouldn't be nescessary in the farrowing stable, these sows should be removed before mating
fasold <- sample(incomfarpigs, (length(incomfarpigs)-length(EmptyFPens)), rep=F)
if (length(fasold)>0) {
TBes <- TBes[-which(TBes$Animal%in%fasold),]
incomfarpigs <- incomfarpigs[!incomfarpigs %in% fasold]
}
}
TakenFPens <- 1:length(incomfarpigs)
TBes$sti[TBes$Animal %in% incomfarpigs] <- TakenFPens
}
#.... updates when piglets are born..
if (any(TBes$PACount%in%Farrowdays&TBes$stable==3)) {
TestPen <- merge(TBes[TBes$stable==3&TBes$PACount==0,c(1:24)],TBes[TBes$stable==3&TBes$PACount%in%(Farrowdays),c(15,25:39)], by=c("nsow"), all.x="TRUE") [, union(names(TBes[TBes$stable==3&TBes$PACount==0,c(1:24)]), names(TBes[TBes$PACount%in%Farrowdays,c(16,25:39)]))]
TBes <- rbind(TBes[-which(TBes$Animal%in%TestPen$Animal),], TestPen)
newborns <- TBes$Animal[TBes$PACount==0&TBes$stable==3&TBes$Type==2] #Riskikerer at flytte daggamle grise, der er flyttet til en ammeso tilbage, hvis ikke der indekseres på dette
#TBes$sec[match(newborns, TBes$Animal)] <- TBes$sec[match(TBes$nsow[TBes$Animal %in% newborns],TBes$Animal[TBes$ns==0])]
TBes$sec[match(newborns, TBes$Animal)] <- TBes$sec[match(TBes$nsow[TBes$Animal %in% newborns],TBes$Animal)]
TBes$sti[match(newborns, TBes$Animal)] <- TBes$sti[match(TBes$nsow[TBes$Animal %in% newborns],TBes$Animal)]
}
#########################################################
#Housing in the gestation unit
gmovedays <- Matdays + 29 #Age where the gestation stable can be enteres
gmoveoutdays <- Farrowdays - 5
gsti <- c(1:12)
if (gTime==1&any(TBes$stable==2)) { #Assignment of places in the gestation unit from the beginning
teama <- unique(TBes$teama[TBes$stable==2&TBes$Type==1])
gsti <- seq_along(teama)
TBes$sti[TBes$stable==2&TBes$Type==1] <- gsti[match(TBes$teama[TBes$stable==2&TBes$Type==1], teama)] #if want to run with sectioning
#TBes$sec[TBes$stable==2&TBes$Type==1] <- TBes$sec[TBes$stable==2&TBes$Type==1] #if want to run with sectioning
TBes$sec[TBes$stable==2&TBes$Type==1] <- 10 #No sectioning in the gestation unit, so all pigs will be assigned section 10
}
if (any(TBes$PACount%in%gmovedays&TBes$Type==1&TBes$stable==2)) { #Need to reset these to zero, when the animals are moved into a new stable or else the pens inhabited in the old stable, will also be taken as occupied in the new stable.
TBes$sti[TBes$PACount%in%gmovedays&TBes$Type==1&TBes$stable==2] <- 0
TBes$sec[TBes$PACount%in%gmovedays&TBes$Type==1&TBes$stable==2] <- 0
}
#Update of places when new are filled/emptied
if (any(TBes$PACount%in%gmovedays&TBes$Type==1&TBes$stable==2) | any(TBes$PACount%in%gmoveoutdays&TBes$Type==1&TBes$stable==2)) {
gteamin <- unique(TBes$teama[TBes$PACount%in%gmovedays&TBes$Type==1&TBes$stable==2])
EmptyGPens <- gsti[!gsti%in%unique(TBes$sti[TBes$stable==2&TBes$Type==1])]
TakenGPens <- EmptyGPens[1:length(gteamin)] #Indicate the pen we want to take, not the pens already taken
TBes$sti[TBes$teama%in%gteamin&TBes$stable==2&TBes$Type==1] <- TakenGPens #It is assumed that in the gestation stable, there is only one pen per section
#TBes$sec[TBes$teama%in%gteamin & TBes$stable==2&TBes$Type==1] <- TBes$sti[TBes$teama%in%gteamin & TBes$stable==2] #If want to run with sectioning in the gestation stable
TBes$sec[TBes$teama%in%gteamin & TBes$stable==2&TBes$Type==1] <- 10 #No sectioning
}
######################################################################################################
##Housing in the mating unit
mssti <- c(1:40) #40 pens for individual housing of sows #Sows ready for mating + sows that failed to get pregnant, and are waiting for becoming ready for re-insemination again
mpsti <- c(1:12) #12 pens for housing of gilts with room for 5 animals within each pen. Placed in section 5
#Placements of the sows at simulation start
if (gTime==1&any(TBes$stable==1&TBes$Type==1)) {
teama <- TBes$teama[TBes$stable==1&TBes$Type==1] #When sows to be re-inseminated are not included all sows should belong to the same team
TBes$sec[TBes$stable==1&TBes$Type==1] <- teama
for(i in unique(TBes$sec[TBes$stable==1&TBes$Type==1])){
Index <- TBes$sec[TBes$stable==1&TBes$Type==1]==i
TBes$sti[TBes$stable==1&TBes$Type==1][Index]<- 1:sum(Index)
}
}
mmovedays <- c(240,Farrowdays + 29) # Age at arrival in the mating unit
if (any(TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1)){
if (any(TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999))) {
secc <- 1:5 #Sows to be re-inseminated are in a separate section untill this have taken place. Following that they will be moved to the same section as the batch they will now be part of
# here we reset the pen and section to give them new values
TBes$sti[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)] <- 0
TBes$sec[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)] <- 0
#Identification of absolutely empty and partly empty sections and pens, and putting pigs into these
AbsEmptySec <- secc[!secc%in%unique(TBes$sec[TBes$stable==1&TBes$Type==1])]
if (length(AbsEmptySec)>0) {
if (length(mssti)<length(TBes$Animal[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)])) { #If the no. of animals exceeds stable capacity, these are removed for sale or culled
influx <- length(TBes$Animal[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)])
#if there is to many pigs - some of them will be selected for sale/removal:
msold <- sample(TBes$Animal[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)], influx-length(mssti), rep=F)
if (length(msold)>0) {
TBes <- TBes[-which(TBes$Animal%in%msold),]
}
}
msteamin <- unique(TBes$teama[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)])
TBes$sec[TBes$teama%in%msteamin&TBes$stable==1&TBes$Type==1&TBes$ri %in% c(0,9999)] <- rep(AbsEmptySec[1], length(TBes$sti[TBes$teama%in%msteamin&TBes$stable==1&TBes$Type==1&TBes$ri %in% c(0,9999)]))
EmptyMSPens <- mssti[!mssti%in%unique(TBes$sti[TBes$stable==1&TBes$sec==AbsEmptySec[1]])]
TakenMSPens <- EmptyMSPens[1:length(TBes$Animal[TBes$PACount%in%mmovedays&TBes$Type==1&TBes$stable==1&TBes$ri %in% c(0,9999)])] #Indicate the pen we want to take, not the pens already taken
TBes$sti[TBes$PACount%in%mmovedays&TBes$stable==1&TBes$Type==1&TBes$ri %in% c(0,9999)] <- TakenMSPens
} else
{
OccupiedMSec <- as.data.frame(table(unlist(TBes$sec[TBes$stable==1&TBes$Type==1])))
colnames(OccupiedMSec) <- c("Sec", "Freq")
OccupiedMSec$Free <- max(mssti)-OccupiedMSec$Freq
PartlyEmptyMSec <- OccupiedMSec[OccupiedMSec$Free>0,]
}
}
}
#Placements of the gilts at start
if (gTime==1&any(TBes$stable==1&TBes$Type==3)) {
Gteama <- unique(TBes$teama[TBes$stable==1&TBes$Type==3])
ppens <- as.data.frame(cbind(Gteama,seq_along(Gteama)))
colnames(ppens) <- c("teama", "sti")
ppens$sec <- rep(6, nrow(ppens))
MaGiSecPen <- merge(TBes[TBes$stable==1&TBes$Type==3,c(1:24,27:39)], ppens, by=c("teama"), all.x="TRUE")[, union(names(TBes[TBes$stable==1&TBes$Type==3,c(1:24,27:39)]), names(ppens))]
TBes <- rbind(TBes[-which(TBes$Animal%in%MaGiSecPen$Animal),], MaGiSecPen)
}
#Update when new gilts are purchased
if (any(TBes$PACount==191&TBes$Type==3&TBes$stable==1) | gTime%in%(h+5) & any(TBes$PACount==191&TBes$Type==3&TBes$stable==1)) {
TBes$sec[TBes$PACount==191&TBes$Type==3&TBes$stable==1] <- 6
TBes$sti[TBes$PACount==191&TBes$Type==3&TBes$stable==1] <- 0
TotallyEmptyMPPens <- mpsti[!mpsti%in%unique(TBes$sti[TBes$stable==1&TBes$sec==6])]
#giltteamin <- NULL
Sharedpenspacesneeded <- 0
giltteamin <- TBes$Animal[TBes$PACount==191&TBes$Type==3&TBes$stable==1]
if (length(TotallyEmptyMPPens)>0) {
#giltteamin <- TBes$Animal[TBes$PACount==192&TBes$Type==3&TBes$stable==1]
spacesneeded <- length(giltteamin)
TotEmpSpace <- rep(TotallyEmptyMPPens, each=5)
TakenMPPens <- TotEmpSpace[1:spacesneeded] #Indicate the pens we want to take, not the pens already taken
Sharedpenspacesneeded <- sum(is.na(TakenMPPens))
TotEmpTaken <- length(TakenMPPens) - Sharedpenspacesneeded
TBes$sti[TBes$Animal%in%giltteamin&TBes$stable==1&TBes$Type==3][1:TotEmpTaken] <- na.omit(TakenMPPens)
}
if (length(TotallyEmptyMPPens)==0 | Sharedpenspacesneeded>0) {
#giltteamin <- TBes$Animal[TBes$PACount==192&TBes$Type==3&TBes$stable==1]
Occupied <- as.data.frame(table(unlist(TBes$sti[TBes$stable==1&TBes$sec==6])))
colnames(Occupied) <- c("Pen", "Freq")
Occupied$Free <- ifelse(Occupied$Freq>4, 0, 5-Occupied$Freq)
PartlyEmptyMPPens <- Occupied[Occupied$Free>0,]
placevector <- c(rep(PartlyEmptyMPPens$Pen, times=PartlyEmptyMPPens$Free))
Sharedpenspacesneeded <- ifelse(Sharedpenspacesneeded==0, length(giltteamin), Sharedpenspacesneeded)
Takenplace <- placevector[1:Sharedpenspacesneeded]
TBes$sti[TBes$Animal%in%giltteamin&TBes$stable==1&TBes$Type==3&TBes$sti==0] <- Takenplace #TBes$sti==0 to avoid overwriting
}
}
### Placement, when gilts are inserted as sows (not done on mmovedays to allow for culling to happen before)
if (any(TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000)){
teamnewins <- unique(TBes$teama[TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000])
nsec <- unique(TBes$sec[TBes$stable==1&TBes$Type==1&TBes$teama==teamnewins&TBes$ri==0&TBes$ns==0])
TBes$sec[TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000] <- nsec
EmptyNPens <- mssti[!mssti%in%unique(TBes$sti[TBes$stable==1&TBes$sec==nsec])]
TakenNPens <- EmptyNPens[1:length(TBes$Animal[TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000])] #Indicate the pens we want to take, not the pens already taken
TBes$sti[TBes$Animal %in% TBes$Animal[TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000]] <- TakenNPens
TBes$ns[TBes$Animal %in% TBes$Animal[TBes$PACount%in%(Matdays-3)&TBes$stable==1&TBes$Type==1&TBes$ns==1000]] <- 0
}
#### Alternative strategy for sows to be re-inseminated: identification 3 weeks after insemination and immediate re-semination and movement to the section of the other newly inseminated sows
#As the sows remains with their old team for the first three weeks, only one movement needs to be coded
omlmat <-TBes$Animal[TBes$ri==-1] #Only tagged for one day now, so can be used as sole condition
if (length(omlmat)>0) {
nhsec <- unique(TBes$sec[TBes$PACount%in%(Matdays)&TBes$stable==1&TBes$Type==1&TBes$sec>0])
TBes$sec[TBes$Animal %in% omlmat] <- nhsec
EmptyolPens <- mssti[!mssti%in%unique(TBes$sti[TBes$stable==1&TBes$sec==nhsec])]
if (length(omlmat)>length(EmptyolPens)) {
m2sold <- sample(omlmat, (length(omlmat)-length(EmptyolPens)), rep=F)
TBes$sec[TBes$Animal %in% m2sold] <- 7
TBes$sti[TBes$Animal %in% m2sold] <- 1
IndexOmlMat <- which(omlmat%in%m2sold)
omlmat <- omlmat[-IndexOmlMat]
}
TakenolPens <- EmptyolPens[1:length(omlmat)] #Indicate the pens we want to take, not the pens already taken
TBes$sti[TBes$Animal %in% omlmat] <- TakenolPens
}
###############################################################################################
##Housing in the weaner unit (stable=4)
#It is assumed that pigs are sorted according to size, when assigning pen mates (=random mixing of litters)
rwsti <- rep(1:14, each=30) #30 pigs per pen in 14 pens per section
wdaysp1 <- 29 #Age when entering this unit
#Housing of weaners at the very start:
if (gTime==1&any(TBes$stable==4)) {
TBes$sti[TBes$stable==4] <- 0
TBes$sec[TBes$stable==4] <- 0
penvec <- NULL
weagevec <- seq(from=30, to=79, by=7)
TBes$sec[TBes$stable==4] <- match(TBes$PACount[TBes$stable==4], weagevec)
for (i in 1:8) {
nstart <- length(TBes[TBes$stable==4&TBes$sec==i,1])
penvec <- rep(1:ceiling(nstart/30), each=30)
TBes$sti[TBes$stable==4&TBes$sec==i] <- penvec[1:nstart] #assigmment of pen
}
}
#Due to the way the herd was initiated the 8 sections might not nescessarily fill up in chronological order
if (any(TBes$PACount%in%wdaysp1&TBes$stable==4)&gTime>1) {
nw <- length(TBes[TBes$PACount%in%wdaysp1&TBes$stable==4,1]) #No. weaned on the day in question
if (length(rwsti)<nw) { #If the no. of animals exceeds the units capacity, surplus pigs are removed for sale or culled
wsold <- sample(TBes$Animal[TBes$PACount%in%wdaysp1&TBes$stable==4], nw-length(rwsti), rep=F)
if (length(wsold)>0) {
TBes <- TBes[-which(TBes$Animal%in%wsold),]
nw <- length(TBes[TBes$PACount%in%wdaysp1&TBes$stable==4,1])
}
}
sti <- sample(rwsti, nw, replace=FALSE)
WeaSec <- 1:8
TBes$sti[TBes$PACount%in%wdaysp1&TBes$stable==4] <- 0
TBes$sec[TBes$PACount%in%wdaysp1&TBes$stable==4] <- 0
EmptyWSec <- WeaSec[!WeaSec%in%unique(TBes$sec[TBes$stable==4])][1]
TBes$sti[TBes$PACount%in%wdaysp1&TBes$stable==4] <- rwsti[1:length(TBes$Animal[TBes$PACount%in%wdaysp1&TBes$stable==4])]
TBes$sec[TBes$PACount%in%wdaysp1&TBes$stable==4] <- EmptyWSec
}
###############################################################################################
##Housing in the finisher unit (stable=5)
fimoveday <- 80 #PACount, when entering this unit
frsti <- rep(1:24, each=15) #24 pens per section with room for 15 animals in each
redsti <- rep(1:3, each=15) #3 pens in the buffer section
#Housing of finishers at the very start:
if (gTime==1&any(TBes$stable==5)) {
fiagevec <- seq(from=86, to=177, by=7)
TBes$sec[TBes$stable==5] <- match(TBes$Age[TBes$stable==5], fiagevec)
for (i in 1:12) {
nfstart <- length(TBes[TBes$stable==5&TBes$sec==i,1])
penfvec <- rep(1:ceiling(nfstart/15), each=15)
TBes$sti[TBes$stable==5&TBes$sec==i] <- penfvec[1:nfstart]
}
}
#update when animals are moved
if (any(TBes$PACount%in%fimoveday&TBes$stable==5)) {
TBes$sti[TBes$PACount%in%fimoveday&TBes$stable==5] <- 0
TBes$sec[TBes$PACount%in%fimoveday&TBes$stable==5] <- 0
nf <- length(TBes[TBes$PACount%in%fimoveday&TBes$stable==5,1]) #No. of weaners moved into the slaughterpig stable on the day in question
if (length(frsti)<nf) { #If the no. of animals exceeds stable capacity, these are removed for sale or culled
fisold <- sample(TBes$Animal[TBes$PACount%in%fimoveday&TBes$stable==5], nf-length(frsti), rep=F)
if (length(fisold)>0) {
TBes <- TBes[-which(TBes$Animal%in%fisold),]
}
nf <- length(TBes[TBes$PACount%in%fimoveday&TBes$stable==5,1])
}
if (nf>150) {
sti <- sample(frsti, nf, replace=FALSE)
} else {
fipenvec <- rep(1:ceiling(nf/15), each=15)
sti <- fipenvec[1:nf]
}
FinSec <- 1:14
EmptyFinSec <- FinSec[!FinSec%in%unique(TBes$sec[TBes$stable==5])][1]
TBes$sec[TBes$PACount%in%fimoveday&TBes$stable==5] <- EmptyFinSec
TBes$sti[TBes$PACount%in%fimoveday&TBes$stable==5&TBes$sec==EmptyFinSec] <- frsti[1:length(TBes$Animal[TBes$PACount%in%fimoveday&TBes$stable==5&TBes$sec==EmptyFinSec])]
}
#### Snout contact between pens (two and two)
#Note: curently not used in the model
#Need to be "cut-off" if exceeding the max. no. of pens within the section and/or all pens don't have a partner pen (or else this will just have to be taken into account in the colonization model)
#Definition: Pigs in pens with an odd pen no. can have snout contact to pigs in the pen having their pen no. + 1 / if even: own pen no. -1
is.even <- function(x) x %% 2 == 0
z <- TBes$sti[is.even(TBes$sti)=="TRUE"]
TBes$snoutcont <- ifelse(TBes$sti%in%z,TBes$sti-1, TBes$sti+1)
if (any(TBes$sti==23&TBes$stable==5) | any(TBes$sti==15&TBes$Type==3)) {
TBes$snoutcont[TBes$sti==21&TBes$stable==5 | TBes$sti==15&TBes$Type==3] <- 0
}
#####################################
### Deaths/culling of sows
### Probability vectors
Sow_raw_mat_far<-c(0,0.020, 0.016, 0.017, 0.027, 0.018, 0.023, 0.020, 0.034,0.034) # Sow fatality rate between mating and farrowing, based on no. og litters, source: "VSP/SEGES (2013): Udsætningstrategi"
Sow_raw_post_far<-c(0,0.028, 0.026, 0.031, 0.034, 0.031, 0.025, 0.042, 0.028,0.028) #As above, after farrowing (source: "VSP/SEGES (2013): Udsætningstrategi")
Sow_raw_cull_post_far<-c(0,0.04, 0.06, 0.08, 0.11, 0.18, 0.34, 0.45, 1.00,1) #Prop of sows culled after the a given litter is weaned #source: "VSP/SEGES (2013): Udsætningstrategi".(Prop. of sows alive after haven given birth to the relevant no. of litters)
## Day_leav_fst<-c(386, 533, 680, 827, 974, 1121, 1268, 1415) #Day of leaving the farrowing stable after having the given litter no.
Sow_mat_far_daily <-Sow_raw_mat_far/114 #Daily probability of removal between mating and farrowing
Sow_post_far_daily <-Sow_raw_post_far/33 #Daily probability of removal post-farrowing (and pre-mating)
#Removal of sows of high age (nescessary because the litter counter is starting from zero at the the start of model run for sows of higher age)
if (any(TBes$PACount > 1415)) {
sowsout0 <- TBes$Animal[TBes$PACount>1415]
if (length(sowsout0)>0) {
TBes <- TBes[-which(TBes$Animal%in%sowsout0),]
}
}
##################################
#Removal of sows after weaning
if (length(notreins)>0) {
TBes <- TBes[-which(TBes$Animal%in%notreins),]
}
lit_dam <- matrix(numeric(0),ncol=3) #Nescessary to avoid carry-over
lit_dam2 <- matrix(numeric(0),ncol=3)
lit_dam3 <- matrix(numeric(0),ncol=3)
#"Strategic culling: selection and removal of sows immediately after weaning
Postwean_newprob <- Sow_raw_cull_post_far - c(0, 0.12*(1-probreins), 1) #probreins is the probability of re-insemination
#Farrowdays + 29 = Matdays - 4 =Flyttedag fra farestalden til løbestalden
if (any(TBes$PACount %in% (Farrowdays + 29)&TBes$Type==1)) {
Movedsows <- TBes$Animal[TBes$PACount %in% (Farrowdays + 29)&TBes$Type==1]
lit_dam <- cbind(Movedsows,TBes$LCount[match(Movedsows,TBes$Animal)])
lit_dam <- cbind(lit_dam,rbinom(lit_dam[,1], 1, Postwean_newprob[(lit_dam[,2]+1)]))
if (sum(lit_dam[,3])>0) {
TBes <- TBes[-which(TBes$Animal%in%lit_dam[lit_dam[,3]==1,1]),]
}
}
######################################
#Removal of sows during any other time
#Between insemination and farrowing
gestdur <- 147 -28 -5 -1 #One sow cycle is 147 days, minus the 28 days spent on lactation and the 5 days as "dry sow"
Matfardays <- rep(Matdays, each=gestdur) + c(1:gestdur)
matfarsow <- TBes$Animal[TBes$PACount %in% Matfardays & TBes$Type==1]
if (any(TBes$PACount %in% Matfardays & TBes$Type==1)) {
lit_dam2 <- cbind(matfarsow,TBes$LCount[match(matfarsow,TBes$Animal)])
lit_dam2 <- cbind(lit_dam2,rbinom(lit_dam2[,1], 1, Sow_mat_far_daily[(lit_dam2[,2]+1)]))
if (sum(lit_dam2[,3])>0) {
TBes <- TBes[-which(TBes$Animal%in%lit_dam2[lit_dam2[,3]==1,1]),]
}
}
######################################
#Between farrowing and weaning
wdays <- rep(Farrowdays, each=28) + 1:28
lactsows <- TBes$Animal[TBes$PACount %in% wdays & TBes$Type==1 &TBes$ns %in% c(0,9999)] #This is a simplification, beacause something can happen to sows with ns %in% c(1,2) as well.
if (any(TBes$PACount %in% wdays & TBes$Type==1)) {
lit_dam3 <- cbind(lactsows,TBes$LCount[match(lactsows,TBes$Animal)])
lit_dam3 <- cbind(lit_dam3,rbinom(lit_dam3[,1], 1, Sow_post_far_daily[(lit_dam3[,2]+1)]))