-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_confidence_intervals_within_groups.R
77 lines (57 loc) · 1.58 KB
/
get_confidence_intervals_within_groups.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
#' Get Confidence Intervals Within Groups
#'
#' @param data data
#' @param x var 1
#' @param y var 2
#' @param conf_level Confidence level
#' @importFrom dplyr bind_cols rename summarise pull
#' @importFrom tibble as_tibble
#' @return results
#' @export
#' @examples
#' A <- c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1)
#' B <- c(0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0)
#' data <- data.frame(A, B)
#' get_confidence_intervals_within_groups(data, A, B)
get_confidence_intervals_within_groups <- function(data, x, y, conf_level = 0.95) {
z <- abs(qnorm((1 - conf_level) / 2))
adj_pairs <- get_concordant_discordant_pairs(data, x, y) |>
mutate(adj = (z^2) / 8) |>
mutate(adj_n = n + adj)
N_adj <- adj_pairs |>
summarise(sum = sum(adj_n)) |>
pull()
p1_adj <- adj_pairs |>
filter(pairs %in% c("a", "b")) |>
pull(adj_n) |>
sum() |>
divide_by(N_adj)
p2_adj <- adj_pairs |>
filter(pairs %in% c("a", "c")) |>
pull(adj_n) |>
sum() |>
divide_by(N_adj)
p12_adj <- adj_pairs |>
filter(pairs == "b") |>
pull(adj_n) |>
divide_by(N_adj)
p21_adj <- adj_pairs |>
filter(pairs == "c") |>
pull(adj_n) |>
divide_by(N_adj)
ci <- ((p12_adj + p21_adj) - raise_to_power((p21_adj - p12_adj), 2)) |>
divide_by(N_adj) |>
sqrt() |>
multiply_by(z)
if (p1_adj > p2_adj) {
lower_ci <- (p1_adj - p2_adj) - ci
upper_ci <- (p1_adj - p2_adj) + ci
} else {
lower_ci <- (p2_adj - p1_adj) - ci
upper_ci <- (p2_adj - p1_adj) + ci
}
return(data.frame(
lower_ci = lower_ci,
upper_ci = upper_ci
))
}