10
10
# ' @param xlab The title of the x axis
11
11
# ' @param ylab The title of the y axis
12
12
# ' @param color The color for the plot
13
- # ' @param level The desired amount of area between the lower and upper bounds. Default is \code{0.99}.
13
+ # ' @param support_level The desired amount of area between the lower and upper bounds. Default is \code{0.99}.
14
14
# '
15
15
# ' @return NULL. A plot is generated
16
16
plot_beta <- function (betas
17
17
, title = ' Beta Distribution'
18
18
, xlab = ' Rate that the Event Occurs'
19
19
, ylab = ' Density of that Rate'
20
20
, color = ' #f65335'
21
- , level = 0.99
21
+ , support_level = 0.99
22
22
) {
23
23
24
24
n_samp <- 1e5
@@ -33,8 +33,8 @@ plot_beta <- function(betas
33
33
34
34
# remove the lower and upper extremes of the data
35
35
beta_vec <- beta_vec [data.table :: between(beta_vec
36
- , quantile(beta_vec , 0.005 )
37
- , quantile(beta_vec , 0.995 ))]
36
+ , quantile(beta_vec , ( 1 - support_level ) / 2 )
37
+ , quantile(beta_vec , support_level + ( 1 - support_level ) / 2 ))]
38
38
39
39
beta_dt <- data.table :: rbindlist(list (beta_dt , data.table :: data.table(' variant' = rep(var_name , length(beta_vec ))
40
40
, ' betas' = beta_vec )))
@@ -70,7 +70,11 @@ plot_beta <- function(betas
70
70
# ' @param normals A list of lists of normal distributions
71
71
# ' @inheritParams plot_beta
72
72
# ' @return NULL. A plot is generated
73
- plot_normal <- function (normals ) {
73
+ plot_normal <- function (normals
74
+ , title = ' Normal Distribution'
75
+ , color = ' #f65335'
76
+ , support_level = 0.99
77
+ ) {
74
78
n_samp <- 1e5
75
79
76
80
sd_dt <- NULL
@@ -91,12 +95,12 @@ plot_normal <- function(normals) {
91
95
92
96
# remove the lower and upper extremes of the data
93
97
sd_vec <- sd_vec [data.table :: between(sd_vec
94
- , quantile(sd_vec , 0.005 )
95
- , quantile(sd_vec , 0.995 ))]
98
+ , quantile(sd_vec , ( 1 - support_level ) / 2 )
99
+ , quantile(sd_vec , support_level + ( 1 - support_level ) / 2 ))]
96
100
n_sd <- length(sd_vec )
97
101
mu_vec <- mu_vec [data.table :: between(mu_vec
98
- , quantile(mu_vec , 0.005 )
99
- , quantile(mu_vec , 0.995 ))]
102
+ , quantile(mu_vec , ( 1 - support_level ) / 2 )
103
+ , quantile(mu_vec , support_level + ( 1 - support_level ) / 2 ))]
100
104
n_mu <- length(mu_vec )
101
105
102
106
sd_dt <- data.table :: rbindlist(list (sd_dt , data.table :: data.table(' variant' = rep(var_name , n_sd )
@@ -108,7 +112,7 @@ plot_normal <- function(normals) {
108
112
if (length(normals ) > 1 ) {
109
113
col_vals <- c(' darkred' , ' darkblue' )
110
114
} else {
111
- col_vals <- ' black '
115
+ col_vals <- color
112
116
}
113
117
114
118
mu_plot <- ggplot(mu_dt , aes(x = mus , colour = variant , fill = variant )) +
@@ -168,7 +172,11 @@ plot_normal <- function(normals) {
168
172
# ' @param gammas A list of lists of gamma distributions
169
173
# ' @inheritParams plot_beta
170
174
# ' @return NULL. A plot is generated
171
- plot_gamma <- function (gammas , title = ' Density of Gamma Distribution' , level = 0.99 ) {
175
+ plot_gamma <- function (gammas
176
+ , title = ' Density of Gamma Distribution'
177
+ , color = ' #f65335'
178
+ , support_level = 0.99
179
+ ) {
172
180
173
181
n_samp <- 1e5
174
182
gamma_dt <- NULL
@@ -182,8 +190,8 @@ plot_gamma <- function(gammas, title = 'Density of Gamma Distribution', level =
182
190
183
191
# remove the lower and upper extremes of the data
184
192
gamma_vec <- gamma_vec [data.table :: between(gamma_vec
185
- , quantile(gamma_vec , 0.005 )
186
- , quantile(gamma_vec , 0.995 ))]
193
+ , quantile(gamma_vec , ( 1 - support_level ) / 2 )
194
+ , quantile(gamma_vec , support_level + ( 1 - support_level ) / 2 ))]
187
195
188
196
gamma_dt <- data.table :: rbindlist(list (gamma_dt , data.table :: data.table(' variant' = rep(var_name , length(gamma_vec ))
189
197
, ' gammas' = gamma_vec )))
@@ -192,10 +200,10 @@ plot_gamma <- function(gammas, title = 'Density of Gamma Distribution', level =
192
200
if (length(gammas ) > 1 ) {
193
201
col_vals <- c(' darkred' , ' darkblue' )
194
202
} else {
195
- col_vals <- ' black '
203
+ col_vals <- color
196
204
}
197
205
198
- lambda_plot <- ggplot(gamma_dt , aes(x = gammas , colour = variant , fill = variant )) +
206
+ lambda_plot <- ggplot(gamma_dt , aes(x = gammas , colour = variant , fill = variant )) +
199
207
geom_density(size = 1 , alpha = 0.1 ) +
200
208
ggtitle(title ) +
201
209
xlab(' Expected Amount of Times Event Occurrs' ) +
@@ -211,3 +219,23 @@ plot_gamma <- function(gammas, title = 'Density of Gamma Distribution', level =
211
219
return (lambda_plot )
212
220
}
213
221
222
+ # ' @title Plot Relative Gain
223
+ # ' @name plot_relative_gain
224
+ # ' @description Plot the cumulative density of the ratio of the metric under variant B to the metric under variant A
225
+ # ' @importFrom purrr map
226
+ # ' @export
227
+ # ' @param dists A list of distribution objects, with elements named \code{'a'} and \code{'b'}
228
+ # ' @param sim_batch_size The number of objects to simulate
229
+ # ' @return A plot
230
+ plot_relative_gain <- function (dists , sim_batch_size = 1e5 , title = ' Cumulative Density of B / A' ) {
231
+ thetas <- purrr :: map(dists , function (dist ) simulate_data(dist = dist , n = sim_batch_size ))
232
+ ratios <- thetas [[' b' ]] / thetas [[' a' ]]
233
+ df <- data.frame (x = ratios )
234
+ ecdf_plot <- ggplot(df , aes(x , colour = ' #f65335' )) + stat_ecdf(size = 1.5 ) +
235
+ ggtitle(title ) + xlab(' Relative Gain (B / A)' ) + ylab(' Cumulative Density' ) +
236
+ theme(plot.title = element_text(hjust = 0.5 , size = 22 )
237
+ , axis.title = element_text(size = 18 )
238
+ , axis.text = element_text(size = 14 )
239
+ , legend.position = ' none' )
240
+ return (ecdf_plot )
241
+ }
0 commit comments