-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.r
81 lines (60 loc) · 1.69 KB
/
meta.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
RestoreSizes <- function(dat)
{
100 * dat[, 1] / dat[, 2]
}
GetCIs <- function(dat)
{
x <- dat[, 1]
n <- dat[, 2]
tbl <- data.frame()
for (i in 1:length(x))
{
if (is.na(x[i]))
{
tbl <- rbind(tbl, c(NA, NA, NA))
}
else
{
bt_res <- binom.test(x[i], n[i])
tbl <- rbind(tbl, c(bt_res$estimate, bt_res$conf.int))
}
}
names(tbl) <- c("mean", "lower", "upper")
bt_res <- binom.test(sum(x[!is.na(x)]), sum(n[!is.na(x)]))
sm <- c(bt_res$estimate, bt_res$conf.int)
list(table = tbl, summary = sm)
}
FormatCI <- function(ci)
{
paste(format(ci[1], digits = 3, nsmall = 3), " (", format(ci[2], digits = 3, nsmall = 3), ", ", format(ci[3], digits = 3, nsmall = 3), ")", sep = "")
}
# Need forestplot and dplyr libraries
PlotCIs <- function(names, tbl, sm)
{
na <- is.na(tbl$mean)
ci <- apply(tbl, 1, FormatCI)
base_data <- tibble(
mean = tbl$mean[!na],
lower = tbl$lower[!na],
upper = tbl$upper[!na],
ci = ci[!na],
study = names[!na])
summary <- tibble(
mean = sm[1],
lower = sm[2],
upper = sm[3],
ci = FormatCI(sm),
study = "Summary",
summary = TRUE)
header <- tibble(
study = "Study",
ci = "95% CI",
summary = TRUE)
empty_row <- tibble(mean = NA_real_)
output <- bind_rows(header, base_data, empty_row, summary)
m <- floor(100 * min(base_data$lower)) / 100; M <- ceiling(100 * max(base_data$upper)) / 100; t <- c(m, (1:10)/10, M);
t <- t[t <= M & t >= m]
output %>%
forestplot(labeltext = c(study, ci), is.summary = summary, xlog = FALSE, zero = sm[1], xticks = t,
col = fpColors(box = "royalblue", line = "darkblue", summary = "royalblue"))
}