-
Notifications
You must be signed in to change notification settings - Fork 60
/
summary.R
735 lines (663 loc) · 24.9 KB
/
summary.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
#' @title Summary Model Statistics
#'
#' @description Extracts and prints model statistics solved with \code{dcm}.
#'
#' @param object An \code{EpiModel} object of class \code{dcm}.
#' @param at Time step for model statistics.
#' @param run Model run number, for \code{dcm} class models with multiple runs
#' (sensitivity analyses).
#' @param digits Number of significant digits to print.
#' @param ... Additional summary function arguments (not used).
#'
#' @details
#' This function provides summary statistics for the main epidemiological
#' outcomes (state and transition size and prevalence) from a \code{dcm} model.
#' Time-specific summary measures are provided, so it is necessary to input a
#' time of interest. For multiple-run models (sensitivity analyses), input a
#' model run number. See examples below.
#'
#' @seealso \code{\link{dcm}}
#'
#' @method summary dcm
#' @keywords extract
#' @export
#'
#' @examples
#' ## Deterministic SIR model with varying act.rate
#' param <- param.dcm(inf.prob = 0.2, act.rate = 2:4, rec.rate = 1/3,
#' a.rate = 0.011, ds.rate = 0.01,
#' di.rate = 0.03, dr.rate = 0.01)
#' init <- init.dcm(s.num = 1000, i.num = 1, r.num = 0)
#' control <- control.dcm(type = "SIR", nsteps = 50)
#' mod <- dcm(param, init, control)
#' summary(mod, at = 25, run = 1)
#' summary(mod, at = 25, run = 3)
#' summary(mod, at = 26, run = 3)
#'
summary.dcm <- function(object, at, run = 1, digits = 3, ...) {
nruns <- object$control$nruns
type <- object$control$type
groups <- object$param$groups
vital <- object$param$vital
nsteps <- object$control$nsteps
if (!is.null(object$control$new.mod)) {
stop("summary method not available for new model types in dcm")
}
df <- as.data.frame(object, run = run)
if (missing(at) || (at > nsteps || at < 1)) {
stop("Specify at between 1 and ", nsteps)
}
df <- df[df$time == at, ]
## Prevalence calculations
df$s.prev <- df$s.num / df$num
df$i.prev <- df$i.num / df$num
if (type == "SIR") {
df$r.prev <- df$r.num / df$num
}
if (groups == 2) {
df$s.prev.g2 <- df$s.num.g2 / df$num.g2
df$i.prev.g2 <- df$i.num.g2 / df$num.g2
if (type == "SIR") {
df$r.prev.g2 <- df$r.num.g2 / df$num.g2
}
}
if (type == "SI") {
stats <- with(df, c(s.num, s.prev,
i.num, i.prev,
num, 1,
si.flow, NA))
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
rownames(mat) <- c("Suscept.", "Infect.", "Total", "S -> I")
if (vital == TRUE) {
stats <- with(df, c(a.flow, NA,
ds.flow, NA,
di.flow, NA))
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 2))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
if (groups == 2) {
stats <- with(df, c(s.num.g2, s.prev.g2,
i.num.g2, i.prev.g2,
num.g2, 1,
si.flow.g2, NA))
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
if (vital == TRUE) {
stats <- with(df, c(a.flow.g2, NA,
ds.flow.g2, NA,
di.flow.g2, NA))
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 2))
}
mat <- cbind(mat, mat.g2)
}
}
if (type == "SIR") {
stats <- with(df, c(s.num, s.prev,
i.num, i.prev,
r.num, r.prev,
num, 1,
si.flow, NA,
ir.flow, NA))
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
rownames(mat) <- c("Suscept.", "Infect.", "Recov.", "Total",
"S -> I", "I -> R")
if (vital == TRUE) {
stats <- c(df$a.flow, NA,
df$ds.flow, NA,
df$di.flow, NA,
df$dr.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 2))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->",
"S Departure ->",
"I Departure ->",
"R Departure ->")
}
if (groups == 2) {
stats <- with(df, c(s.num.g2, s.prev.g2,
i.num.g2, i.prev.g2,
r.num.g2, r.prev.g2,
num.g2, 1,
si.flow.g2, NA,
ir.flow.g2, NA))
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
if (vital == TRUE) {
stats <- with(df, c(a.flow.g2, NA,
ds.flow.g2, NA,
di.flow.g2, NA,
dr.flow.g2, NA))
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 2))
}
mat <- cbind(mat, mat.g2)
}
}
if (type == "SIS") {
stats <- with(df, c(s.num, s.prev,
i.num, i.prev,
num, 1,
si.flow, NA,
is.flow, NA))
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
rownames(mat) <- c("Suscept.", "Infect.", "Total", "S -> I", "I -> S")
if (vital == TRUE) {
stats <- c(df$a.flow, NA,
df$ds.flow, NA,
df$di.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 2))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
if (groups == 2) {
stats <- with(df, c(s.num.g2, s.prev.g2,
i.num.g2, i.prev.g2,
num.g2, 1,
si.flow.g2, NA,
is.flow.g2, NA))
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 2)
if (vital == TRUE) {
stats <- with(df, c(a.flow.g2, NA,
ds.flow.g2, NA,
di.flow.g2, NA))
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 2))
}
mat <- cbind(mat, mat.g2)
}
}
if (groups == 1) {
colnames(mat) <- c("n", "pct")
}
if (groups == 2) {
colnames(mat) <- c("n:g1", "pct:g1", "n:g2", "pct:g2")
}
mat <- round(mat, digits)
# print it
cat("EpiModel Summary")
cat("\n=======================")
cat("\nModel class:", class(object))
cat("\n\nSimulation Summary")
cat("\n-----------------------")
cat("\nModel type:", type)
cat("\nNo. runs:", nruns)
cat("\nNo. time steps:", object$nsteps)
cat("\nNo. groups:", groups)
if (groups == 1) {
statsep <- paste(rep("-", 30), collapse = "")
}
if (groups == 2) {
statsep <- paste(rep("-", 60), collapse = "")
}
cat("\n\nModel Statistics\n")
cat(statsep)
cat("\nTime:", at)
cat("\t Run:", run, "\n")
cat(statsep, "\n")
print(mat, print.gap = 2)
cat(statsep, "\n")
}
#' @title Summary Model Statistics
#'
#' @description Extracts and prints model statistics simulated with \code{icm}.
#'
#' @param object An \code{EpiModel} object of class \code{icm}.
#' @param at Time step for model statistics.
#' @param digits Number of significant digits to print.
#' @param ... Additional summary function arguments.
#'
#' @details
#' This function provides summary statistics for the main epidemiological
#' outcomes (state and transition size and prevalence) from an \code{icm} model.
#' Time-specific summary measures are provided, so it is necessary to input a
#' time of interest.
#'
#' @seealso \code{\link{icm}}
#'
#' @method summary icm
#' @keywords extract
#' @export
#'
#' @examples
#' ## Stochastic ICM SI model with 3 simulations
#' param <- param.icm(inf.prob = 0.2, act.rate = 1)
#' init <- init.icm(s.num = 500, i.num = 1)
#' control <- control.icm(type = "SI", nsteps = 50,
#' nsims = 5, verbose = FALSE)
#' mod <- icm(param, init, control)
#' summary(mod, at = 25)
#' summary(mod, at = 50)
#'
summary.icm <- function(object, at, digits = 3, ...) {
nsims <- object$control$nsims
type <- object$control$type
groups <- object$param$groups
vital <- object$param$vital
nsteps <- object$control$nsteps
if (missing(at) || (at > nsteps || at < 1)) {
stop("Specify a time step between 1 and ", nsteps)
}
df.mn <- as.data.frame(object, out = "mean")
df.mn <- df.mn[df.mn$time == at, ]
df.sd <- as.data.frame(object, out = "sd")
df.sd <- df.sd[df.sd$time == at, ]
if (type == "SI") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Total", "S -> I")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
} # end SI summary
if (type == "SIR") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
r.prev <- df.mn$r.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
r.prev.g2 <- df.mn$r.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$r.num, df.sd$r.num, r.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA,
df.mn$ir.flow, df.sd$ir.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Recov.", "Total",
"S -> I", "I -> R")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA,
df.mn$dr.flow, df.sd$dr.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->",
"S Departure ->",
"I Departure ->",
"R Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$r.num.g2, df.sd$r.num.g2, r.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA,
df.mn$ir.flow.g2, df.sd$ir.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA,
df.mn$dr.flow.g2, df.sd$dr.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
} # end SIR summary
if (type == "SIS") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA,
df.mn$is.flow, df.sd$is.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Total", "S -> I", "I -> S")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA,
df.mn$is.flow.g2, df.sd$is.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
} # end SIS summary
if (groups == 1) colnames(mat) <- c("mean", "sd", "pct")
if (groups == 2) colnames(mat) <- c("mean:g1", "sd:g1", "pct:g1",
"mean:g2", "sd:g2", "pct:g2")
mat <- round(mat, digits)
## Print it
cat("EpiModel Summary")
cat("\n=======================")
cat("\nModel class:", class(object))
cat("\n\nSimulation Details")
cat("\n-----------------------")
cat("\nModel type:", type)
cat("\nNo. simulations:", nsims)
cat("\nNo. time steps:", nsteps)
cat("\nNo. groups:", groups)
if (groups == 1) {
statsep <- paste(rep("-", 30), collapse = "")
}
if (groups == 2) {
statsep <- paste(rep("-", 60), collapse = "")
}
cat("\n\nModel Statistics\n")
cat(statsep)
cat("\nTime:", at, "\n")
cat(statsep, "\n")
print(mat, print.gap = 2)
cat(statsep, "\n")
}
#' @title Summary Model Statistics
#'
#' @description Extracts and prints model statistics simulated with
#' \code{netsim}.
#'
#' @param object An \code{EpiModel} object of class \code{netsim}.
#' @param at Time step for model statistics.
#' @param digits Number of significant digits to print.
#' @param ... Additional summary function arguments.
#'
#' @details
#' This function provides summary statistics for the main epidemiological
#' outcomes (state and transition size and prevalence) from a \code{netsim}
#' model. Time-specific summary measures are provided, so it is necessary to
#' input a time of interest.
#'
#' @seealso \code{\link{netsim}}
#'
#' @method summary netsim
#' @keywords extract
#' @export
#'
#' @examples
#' \dontrun{
#' ## SI Model without Network Feedback
#' # Initialize network and set network model parameters
#' nw <- network_initialize(n = 100)
#' nw <- set_vertex_attribute(nw, "group", rep(1:2, each = 50))
#' formation <- ~edges
#' target.stats <- 50
#' coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 20)
#'
#' # Estimate the ERGM models (see help for netest)
#' est1 <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)
#'
#' # Parameters, initial conditions, and controls for model
#' param <- param.net(inf.prob = 0.3, inf.prob.g2 = 0.15)
#' init <- init.net(i.num = 10, i.num.g2 = 10)
#' control <- control.net(type = "SI", nsteps = 100, nsims = 5, verbose.int = 0)
#'
#' # Run the model simulation
#' mod <- netsim(est1, param, init, control)
#'
#' summary(mod, at = 1)
#' summary(mod, at = 50)
#' summary(mod, at = 100)
#' }
#'
summary.netsim <- function(object, at, digits = 3, ...) {
nsims <- object$control$nsims
type <- object$control$type
groups <- object$param$groups
vital <- object$param$vital
nsteps <- object$control$nsteps
if (missing(at) || (at > nsteps || at < 1)) {
stop("Specify at between 1 and ", nsteps)
}
df.mn <- as.data.frame(object, out = "mean")
df.mn <- df.mn[df.mn$time == at, ]
df.sd <- as.data.frame(object, out = "sd")
df.sd <- df.sd[df.sd$time == at, ]
if (type == "SI") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Total", "S -> I")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = 4)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
} ## end SI summary
if (type == "SIR") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
r.prev <- df.mn$r.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
r.prev.g2 <- df.mn$r.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$r.num, df.sd$r.num, r.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA,
df.mn$ir.flow, df.sd$ir.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Recov.", "Total",
"S -> I", "I -> R")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA,
df.mn$dr.flow, df.sd$dr.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->",
"S Departure ->",
"I Departure ->",
"R Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$r.num.g2, df.sd$r.num.g2, r.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA,
df.mn$ir.flow.g2, df.sd$ir.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA,
df.mn$dr.flow.g2, df.sd$dr.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
}
if (type == "SIS") {
## Prevalence calcs
s.prev <- df.mn$s.num / df.mn$num
i.prev <- df.mn$i.num / df.mn$num
if (groups == 2) {
s.prev.g2 <- df.mn$s.num.g2 / df.mn$num.g2
i.prev.g2 <- df.mn$i.num.g2 / df.mn$num.g2
}
## Group 1 stats
stats <- c(df.mn$s.num, df.sd$s.num, s.prev,
df.mn$i.num, df.sd$i.num, i.prev,
df.mn$num, df.sd$num, 1,
df.mn$si.flow, df.sd$si.flow, NA,
df.mn$is.flow, df.sd$is.flow, NA)
mat <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
rownames(mat) <- c("Suscept.", "Infect.", "Total",
"S -> I", "I -> S")
if (vital == TRUE) {
stats <- c(df.mn$a.flow, df.sd$a.flow, NA,
df.mn$ds.flow, df.sd$ds.flow, NA,
df.mn$di.flow, df.sd$di.flow, NA)
mat <- rbind(mat, matrix(stats, byrow = TRUE, nrow = length(stats) / 3))
rownames(mat)[rownames(mat) == ""] <- c("Arrival ->", "S Departure ->",
"I Departure ->")
}
## Group 2 stats
if (groups == 2) {
stats <- c(df.mn$s.num.g2, df.sd$s.num.g2, s.prev.g2,
df.mn$i.num.g2, df.sd$i.num.g2, i.prev.g2,
df.mn$num.g2, df.sd$num.g2, 1,
df.mn$si.flow.g2, df.sd$si.flow.g2, NA,
df.mn$is.flow.g2, df.sd$is.flow.g2, NA)
mat.g2 <- matrix(stats, byrow = TRUE, nrow = length(stats) / 3)
if (vital == TRUE) {
stats <- c(df.mn$a.flow.g2, df.sd$a.flow.g2, NA,
df.mn$ds.flow.g2, df.sd$ds.flow.g2, NA,
df.mn$di.flow.g2, df.sd$di.flow.g2, NA)
mat.g2 <- rbind(mat.g2, matrix(stats, byrow = TRUE,
nrow = length(stats) / 3))
}
mat <- cbind(mat, mat.g2)
}
}
if (groups == 1) {
colnames(mat) <- c("mean", "sd", "pct")
}
if (groups == 2) {
colnames(mat) <- c("mean:g1", "sd:g1", "pct:g1",
"mean:g2", "sd:g2", "pct:g2")
}
mat <- round(mat, digits)
## Print it
cat("\nEpiModel Summary")
cat("\n=======================")
cat("\nModel class:", class(object))
cat("\n\nSimulation Details")
cat("\n-----------------------")
cat("\nModel type:", type)
cat("\nNo. simulations:", nsims)
cat("\nNo. time steps:", nsteps)
cat("\nNo. NW groups:", groups)
if (groups == 1) {
statsep <- paste(rep("-", 30), collapse = "")
}
if (groups == 2) {
statsep <- paste(rep("-", 60), collapse = "")
}
cat("\n\nModel Statistics\n")
cat(statsep)
cat("\nTime:", at, "\n")
cat(statsep, "\n")
print(mat, print.gap = 2)
cat(statsep, "\n")
}
#' @title Summary for Network Model Fit
#'
#' @description Prints the summary model fit statistics for an ERGM or STERGM
#' fit.
#'
#' @param object An \code{EpiModel} object of class \code{netest}.
#' @param ... Additional summary function arguments.
#'
#' @method summary netest
#' @keywords internal
#' @export
#'
#' @details
#' This function is simply a wrapper function for \code{summary.ergm}.
#' Additionally, if the edges dissolution approximation was used to fit the
#' temporal ERGM, then the dissolution coefficient information will be printed.
#'
#' If the \code{fit} object is attached to the \code{netest} object, then
#' \code{summary.netest} will call \code{summary} on \code{fit} using the
#' \code{...} passed to \code{summary.netest}. Otherwise,
#' \code{summary.netest} will print the stored summary of the fit generated
#' in the original \code{netest} call, using the \code{...} passed to
#' \code{netest}.
#'
summary.netest <- function(object, ...) {
if (!is.null(object$fit)) {
print(summary(object$fit, ...))
} else {
print(object$summary)
}
if (object$edapprox == TRUE) {
cat("\n")
print(object$coef.diss)
}
}