/
plot_sprt.R
157 lines (144 loc) · 6.29 KB
/
plot_sprt.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
#' Plot Sequential ANOVA Results
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' Creates plots for the results of the seq_anova() function.
#'
# #' @param sample_size sample size.
# #' @param lr_log log-likelihood-ratio.
# #' @param A_boundary_log Log of the A boundary.
# #' @param B_boundary_log Log of the B boundary.
#' @param anova_results result object of the seq_anova() function (argument must be of class `seq_anova_results`).
#' @param labels show labels in the plot.
#' @param position_labels_x position of the boundary labels on the x-axis. 0 positions the center on the 0 of the x-axis.
#' @param position_labels_y position of the boundary labels on the y-axis. 0 positions the labels on the dotted lines.
#' @param position_lr_x scales the position of the LR label on the x-axis. 0 positions the label directly under the last calculated LR.
#' @param position_lr_y scales the position of the LR label on the x-axis. 0 positions the label on the 0 of the y-axis
#' @param font_size font size of the plot.
#' @param line_size line size of the plot.
#' @param highlight_color highlighting color, default is "#CD2626" (red).
#'
#' @import ggplot2 glue purrr
#'
#' @return returns a plot
#' @export
#'
#' @example inst/examples/plot_anova.R
#'
plot_anova <- function(anova_results,
labels = TRUE,
position_labels_x = 0.15,
position_labels_y = 0.075,
position_lr_x = 0.05,
position_lr_y = NULL,
font_size = 25,
line_size = 1.5,
highlight_color = "#CD2626"
) {
if (inherits(anova_results, c("seq_ttest_results"))) {
stop("The plot_anova() function only works for sequential anovas (anova_results argument must be of class seq_anova_results).")
}
if (!inherits(anova_results, c("seq_anova_results"))) {
stop("anova_results argument must be of class seq_anova_results.")
}
if (is.null(anova_results@plot)) {
stop("The anova_results@plot is NULL. Solution: The function argument `plot` must be set to TRUE in the seq_anova() function.")
}
A_boundary_log <- anova_results@plot$A_boundary_log
B_boundary_log <- anova_results@plot$B_boundary_log
results <- data.frame(lr_log = anova_results@plot$lr_log,
sample_size = anova_results@plot$sample_size) %>%
mutate(decision = case_when(
.data$lr_log >= A_boundary_log ~ "H1",
.data$lr_log <= B_boundary_log ~ "H0",
TRUE ~ "CS"
))
N_steps <- nrow(results)
N <- results$sample_size[N_steps]
if (any(results$decision == "H1" | results$decision == "H0")) {
decision_sample_position <- which(results$decision != "CS")[1]
} else{
decision_sample_position <- 0
}
max_lr <- max(abs(results$lr_log))
# set defaults ---------------------------------------------------------------
# palette <- "Dark2"
# green <- "#69b3a2"
# blue <- "#404080"
# red <- "#CD2626" #"#8B0000"
theme_set(theme_bw(base_size = font_size))
# create plot ----------------------------------------------------------------
plot <- results %>%
# slice(1:decision_sample_position) %>%
ggplot(aes(x = .data$sample_size, y = .data$lr_log)) +
xlim(0 , results$sample_size[N_steps] + results$sample_size[N_steps]*0.15) +
geom_line(linewidth = line_size) +
geom_hline(yintercept = A_boundary_log, linetype = "dashed", linewidth = line_size) +
geom_hline(yintercept = B_boundary_log, linetype = "dashed", linewidth = line_size) +
labs(#title = glue("f expected = {f_expected}, f simulated = {f_simulated}"),
x = "Sample Size N",
y = "Log-Likelihood Ratio") +
theme_bw(base_size = font_size)
if (decision_sample_position > 0) {
plot <- plot +
geom_point(aes(x = .data$sample_size[decision_sample_position], y = .data$lr_log[decision_sample_position]),
color = highlight_color, size = line_size*4)
if (labels == TRUE) {
LR <- round(exp(results$lr_log[decision_sample_position]), 2)
nLR <- results$sample_size[decision_sample_position]
plot <- plot +
annotate(geom = "text",
x = results$sample_size[decision_sample_position] + results$sample_size[decision_sample_position]*position_lr_x, y = 0,
label = glue("LR[{nLR}] ==~ {LR}"),
parse = TRUE,
size = font_size/.pt, color = highlight_color)
}
} else{
plot <- plot +
geom_point(aes(x = .data$sample_size[N_steps], y = .data$lr_log[N_steps]),
color = highlight_color, size = line_size*4)
if (labels == TRUE) {
LR <- round(exp(results$lr_log[N_steps]), 2)
nLR <- results$sample_size[N_steps]
if (is.null(position_lr_y)) {
if (results$lr_log[N_steps]>0) {position_lr_y = -0.5} else{position_lr_y = 0.5}
}
plot <- plot +
annotate(geom = "text",
x = results$sample_size[N_steps] + results$sample_size[N_steps]*position_lr_x,
y = position_lr_y,
label = glue("LR[{nLR}] ==~ {LR}"),
parse = TRUE,
size = font_size/.pt, color = highlight_color)
}
}
if (labels == TRUE) {
if (decision_sample_position == 0) {
h0_col <- "black"
h1_col <- "black"
} else if (results$decision[decision_sample_position] == "H1") {
h0_col <- "black"
h1_col <- highlight_color
} else if (results$decision[decision_sample_position] == "H0") {
h0_col <- highlight_color
h1_col <- "black"
}
distance_h <- ceiling(max_lr*position_labels_y)
plot <- plot +
annotate(geom = "text", x = N*position_labels_x, y = A_boundary_log + distance_h,
label = "Accept~ H[1]", size = font_size/.pt, parse = TRUE, color = h1_col) +
annotate(geom = "text", x = N*position_labels_x, y = B_boundary_log - distance_h,
label = "Accept~ H[0]", size = font_size/.pt, parse = TRUE, color = h0_col)
# annotate(geom = "text", x = 10, y = B_boundary_log + distance_h/2,
# label = "Continue Sampling")
}
plot
}
# labels = TRUE
# position_labels_x = 0.15
# position_labels_y = 0.075
# font_size = 25
# line_size = 1.5
# highlight_color = "#CD2626"
# position_lr_x = 0.05