-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.bipod.R
163 lines (138 loc) · 5.15 KB
/
print.bipod.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
#' Print for class \code{'bipod'}.
#'
#' @param x An obj of class \code{'bipod'}.
#' @param ... Default S3 method parameter.
#'
#' @return Nothing.
#'
#' @exportS3Method print bipod
#' @export print.bipod
print.bipod = function(x, ...) {
stopifnot(inherits(x, "bipod"))
nobs <- nrow(x$counts)
nwindows <- length(unique(x$counts$group))
cli::cli_rule(
paste(
crayon::bgYellow(
crayon::black(
paste0("[ biPOD ]", x$sample))),
'{.field {nobs}} observations divided in {.field {nwindows}} time windows.'
)
)
task1 = !is.null(x$fit)
task2 = !is.null(x$breakpoints_fit)
task3 = !is.null(x$two_pop_fit)
# task 2 ####
if (task2) {
cat('\n')
if (!is.null(x$metadata$breakpoints)) {
rhats <- x$breakpoints_fit$rhat %>% unlist()
mean_rhat <- rhats[grepl("b[", x$breakpoints_fit$parameters, fixed = TRUE)] %>% mean() %>% round(2)
x$breakpoints_fit$rhat
if (mean_rhat <= 1.1) {
pstatus = function()
"{crayon::bgGreen(crayon::black(\" PASS \"))}"
} else {
pstatus = function()
"{crayon::bgRed(crayon::black(\" FAIL \"))}"
}
cli::cli_h1(
paste0(
"Break-points inference ",
pstatus(),
" Mean rhat = {crayon::green(mean_rhat)}."
)
)
cli::cli_alert_info(paste0(" Number of breakpoints inferred : ", crayon::blue(length(x$metadata$breakpoints)), ""))
par_tibble <- lapply(x$breakpoints_fit$parameters[grepl("b[", x$breakpoints_fit$parameters, fixed = TRUE)], function(p) {
draws <- x$breakpoints_fit$draws[,grepl(p, colnames(x$breakpoints_fit$draws), fixed = T)] %>% as.vector() %>% unlist()
dplyr::tibble(
Parameter = p,
Mean = mean(draws),
Sd = stats::sd(draws),
p05 = stats::quantile(draws, .05),
p50 = stats::quantile(draws, .50),
p95 = stats::quantile(draws, .95),
Rhat = x$breakpoints_fit$rhat[p] %>% as.numeric())
}) %>% do.call("bind_rows", .)
print(par_tibble)
} else {
cli::cli_h1(
paste0(
"Task 0 ",
" Break-points inference"
)
)
cli::cli_alert_info(paste0(" Number of breakpoints inferred : ", crayon::blue(length(x$metadata$breakpoints)), ""))
}
}
# task 3 ####
if (task3) {
cat('\n')
if (x$metadata$status == "PASS") {
pstatus = function()
"{crayon::bgGreen(crayon::black(\" PASS \"))}"
} else {
pstatus = function()
"{crayon::bgRed(crayon::black(\" FAIL \"))}"
}
mean_rhat <- x$two_pop_fit$rhat %>% unlist() %>% mean() %>% round(2)
cli::cli_h1(
paste0(
"Two population inference ",
pstatus(),
" Using {crayon::bold(x$metadata$sampling)}. Mean rhat = {crayon::green(mean_rhat)}."
)
)
x$two_pop_fit
if (x$metadata$factor_size != 1) {
cli::cli_alert_info(paste0(" Scale factor : ", crayon::blue(x$metadata$factor_size), ". Instant of birth might be incorrect."))
} else {
cli::cli_alert_info(paste0(" Scale factor : ", crayon::blue(x$metadata$factor_size), ""))
}
cat("\n")
cli::cli_alert_info(" Inferred parameters")
par_tibble <- lapply(x$two_pop_fit$parameters, function(p) {
draws <- x$two_pop_fit$draws[,grepl(p, colnames(x$two_pop_fit$draws), fixed = T)] %>% as.vector() %>% unlist()
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = stats::sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
}) %>% do.call("bind_rows", .)
print(par_tibble)
}
# task 1 ####
if (task1) {
cat('\n')
if (x$metadata$status == "PASS") {
pstatus = function()
"{crayon::bgGreen(crayon::black(\" PASS \"))}"
} else {
pstatus = function()
"{crayon::bgRed(crayon::black(\" FAIL \"))}"
}
mean_rhat <- x$fit$rhat %>% unlist() %>% mean() %>% round(2)
cli::cli_h1(
paste0(
"Single population inference ",
pstatus(),
" Using {crayon::bold(x$metadata$sampling)}. Mean rhat = {crayon::green(mean_rhat)}."
)
)
cli::cli_alert_info(paste0(" Growth pattern : ", crayon::blue(x$metadata$growth_type), ""))
if (x$metadata$t0_inferred) {
cli::cli_alert_info(paste0(" Instant of birth : ", crayon::blue("inferred"), ""))
} else {
cli::cli_alert_info(paste0(" Instant of birth : ", crayon::blue("not inferred"), ""))
}
if (x$metadata$factor_size != 1) {
cli::cli_alert_info(paste0(" Scale factor : ", crayon::blue(x$metadata$factor_size), ". Instant of birth might be incorrect."))
} else {
cli::cli_alert_info(paste0(" Scale factor : ", crayon::blue(x$metadata$factor_size), ""))
}
cat("\n")
cli::cli_alert_info(" Inferred parameters")
par_tibble <- lapply(x$fit$parameters, function(p) {
draws <- x$fit$draws[,grepl(p, colnames(x$fit$draws), fixed = T)] %>% as.vector() %>% unlist()
dplyr::tibble(Parameter = p, Mean = mean(draws), Sd = stats::sd(draws), p05 = stats::quantile(draws, .05), p50 = stats::quantile(draws, .50), p95 = stats::quantile(draws, .95))
}) %>% do.call("bind_rows", .)
print(par_tibble)
}
}