-
Notifications
You must be signed in to change notification settings - Fork 19
/
inference.R
687 lines (618 loc) 路 29.7 KB
/
inference.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#' Hypothesis tests and confidence intervals
#' @param y Response variable, can be numerical or categorical
#' @param x Explanatory variable, categorical (optional)
#' @param data Name of data frame that y and x are in
#' @param statistic parameter to estimate: mean, median, or proportion
#' @param success which level of the categorical variable to call "success", i.e. do inference on
#' @param order when x is given, order of levels of x in which to subtract parameters
#' @param method of inference; "theoretical" (CLT based) or "simulation" (randomization/bootstrap)
#' @param type of inference; "ci" (confidence interval) or "ht" (hypothesis test)
#' @param null null value for a hypothesis test
#' @param alternative direction of the alternative hypothesis; "less","greater", or "twosided"
#' @param sig_level significance level, value between 0 and 1 (used only for ANOVA to determine if posttests are necessary)
#' @param conf_level confidence level, value between 0 and 1
#' @param boot_method bootstrap method; "perc" (percentile) or "se" (standard error)
#' @param nsim number of simulations
#' @param seed seed to be set, default is NULL
#' @param verbose whether output should be verbose or not, default is TRUE
#' @param show_var_types print variable types, set to verbose by default
#' @param show_summ_stats print summary stats, set to verbose by default
#' @param show_eda_plot print EDA plot, set to verbose by default
#' @param show_inf_plot print inference plot, set to verbose by default
#' @param show_res print results, set to verbose by default
#' @return Results of inference task performed
#' @export
inference <- function(y, x = NULL, data,
type = c("ci", "ht"),
statistic = c("mean", "median", "proportion"),
success = NULL, order = NULL,
method = c("theoretical", "simulation"),
null = NULL, alternative = c("less","greater","twosided"),
sig_level = 0.05,
conf_level = 0.95,
boot_method = c("perc", "se"),
nsim = 15000, seed = NULL,
verbose = TRUE,
show_var_types = verbose, show_summ_stats = verbose,
show_eda_plot = verbose, show_inf_plot = verbose, show_res = verbose){
# if required packages are not installed, stop
installed_packages <- names(utils::installed.packages()[,"Package"])
required_packages <- c("ggplot2", "gridExtra", "broom")
if(!all(required_packages %in% installed_packages)){
missing_packages <- required_packages[which(!(required_packages %in% installed_packages))]
stop(paste("The following required packages are not installed:", missing_packages,
"Please install these packages before running the inference function."), call. = FALSE)
}
# save axis labels for use later
y_name <- paste(substitute(y))
x_name <- paste(substitute(x))
# assign x and y
x <- eval(substitute(x), data, parent.frame())
y <- eval(substitute(y), data, parent.frame())
# error: weird y
if (length(y) == 1) {stop("Sample size of y is 1", call. = FALSE)}
# error: y or x is character, make factor
if (is.character(y)) {y = as.factor(y)}
if (is.character(x)) {x = as.factor(x)}
if (is.logical(y)) {y = as.factor(y)}
if (is.logical(x)) {x = as.factor(x)}
# set variable type for y: numerical or categorical
y_type = "categorical"
if (is.numeric(y)) {y_type = "numerical"}
# set variable type for y: numerical or categorical
y_type = "categorical"
if (is.numeric(y)) {y_type = "numerical"}
# set variable type for x: categorical, numerical (unused), or only1var
if (!is.null(x)) {
x_type = "categorical"
if (is.numeric(x)) {x_type = "numerical"}
}
if (is.null(x)) {x_type = "only1var"}
# warning: explanatory variable numerical, convert to categorical
if(x_type == "numerical"){
x = as.factor(x)
x_type = "categorical"
warning("Explanatory variable was numerical, it has been converted
to categorical. In order to avoid this warning, first convert
your explanatory variable to a categorical variable using the
as.factor() function", call. = FALSE)
}
# error: explanatory variable only has one level
if(x_type == "categorical"){
if(length(levels(x)) == 1){
stop("Explanatory variable has only one level, it should have at least two
levels", call. = FALSE)
}
}
# error: response variable is categorical, but only has one level
if(y_type == "categorical"){
if(length(levels(y)) == 1){
stop("Response variable has only one level, it should have at least two
levels", call. = FALSE)
}
}
# set number of levels
x_levels = 0 # numerical variable
y_levels = 0 # numerical variable
if (x_type == "categorical") {x_levels = length(levels(x))}
if (y_type == "categorical") {y_levels = length(levels(y))}
# error: missing type, method, statistic
if (length(type) > 1) {
stop("Missing type: ci (confidence interval) or ht (hypothesis test)", call. = FALSE)
}
if (length(method) > 1) {
stop("Missing method: theoretical or simulation", call. = FALSE)
}
if (length(statistic) > 1) {
stop("Missing statistic: mean, median, or proportion", call. = FALSE)
}
# error: method isn't theoretical or simulation
method_list = c("theoretical", "simulation")
method = tolower(gsub("\\s","", method))
which_method = pmatch(method, method_list)
if(is.na(which_method)){
stop("Method should be theoretical or simulation", call. = FALSE)
}
method = method_list[which_method]
# error: type isn't ci or ht
type_list = c("ci", "ht")
type = tolower(gsub("\\s","", type))
which_type = pmatch(type, type_list)
if(is.na(which_type)){stop("Type should be ci or ht", call. = FALSE)}
type = type_list[which_type]
# error: missing boot_method
if (method == "simulation" & type == "ci" & length(boot_method) > 1){
stop("Missing boot_method: perc (percentile method) or se (standard error method)", call. = FALSE)
}
# error: boot_method isn't perc or se
if (method == "simulation" & type == "ci"){
boot_method_list = c("perc", "se")
boot_method = tolower(gsub("\\s","", boot_method))
which_boot_method = pmatch(boot_method, boot_method_list)
if(is.na(which_boot_method)){stop("boot_method should be perc or se", call. = FALSE)}
boot_method = boot_method_list[which_boot_method]
}
# error: alternative isn't less, greater, or twosided
if(type == "ht"){
alternative_list = c("less", "greater", "twosided")
alternative = tolower(gsub("\\s","", alternative))
which_alternative = pmatch(alternative, alternative_list)
if((length(which_alternative) == 1) & any(is.na(which_alternative))){
stop("alternative should be less, greater or twosided", call. = FALSE)
}
if(any(which_alternative == 4)) {which_alternative = 3}
alternative = alternative_list[which_alternative]
}
# error / warning: issues with null values
if (!is.null(null) & !is.numeric(null)) {stop("Null value must be numeric", call. = FALSE)}
if (type == "ht" & x_type == "only1Var" & is.null(null)) {stop("Missing null value", call. = FALSE)}
if (type == "ht" & (x_levels == 2 & y_levels <= 2) & is.null(null)) {
null = 0
warning("Missing null value, set to 0", call. = FALSE)
}
if (type == "ht" & (x_levels > 2 | y_levels > 2) & !is.null(null)) {
if(y_type == "numerical"){
warning("Ignoring null value since it's undefined for ANOVA", call. = FALSE)
}
if(y_type == "categorical"){
warning("Ignoring null value since it's undefined for chi-square test of independence", call. = FALSE)
}
}
# error / warning: issues with alternative
if (type == "ht" & length(alternative) > 1) {
if(x_levels <= 2 & y_levels <= 2) {stop("Missing alternative: less, greater, or twosided", call. = FALSE)}
if(x_levels > 2 | y_levels > 2) {
if(y_type == "numerical") {
alternative = "greater"
warning('Use alternative = "greater" for ANOVA', call. = FALSE)
}
if(y_type == "categorical") {
alternative = "greater"
warning('Use alternative = "greater" for chi-square test', call. = FALSE)
}
}
}
# error: categorical variables have more than two levels, but type is ci
if ((x_levels > 2 | y_levels > 2) & type == "ci") {
if(y_type == "numerical"){
stop("Categorical variable has more than 2 levels, confidence interval is undefined,
use ANOVA to test for a difference between means", call. = FALSE)
}
if(y_type == "categorical"){
stop("Categorical variable has more than 2 levels, confidence interval is not defined,
use chi-square test of independence", call. = FALSE)
}
}
if ((x_levels > 2 | y_levels > 2) & statistic == "median") {
if(y_type == "numerical"){
stop('This function cannot be used to compare medians across more than 2 groups,
use statistic = "mean" to compare means across many groups using ANOVA', call. = FALSE)
}
}
# error: statistic isn't mean, median, or proportion
if (statistic %in% c("mean", "median", "proportion") == FALSE) {
stop("Statistic should be mean, median, or proportion", call. = FALSE)
}
# error: wrong statistic
if (y_type == "numerical" & statistic == "proportion") {
stop("Response variable is numerical, sample statistic cannot be a proportion,
use either mean or median", call. = FALSE)
}
if (y_type == "categorical" & (statistic == "mean" | statistic == "median")) {
stop("Response variable is categorical, sample statistic cannot be a mean or
a median, use proportion", call. = FALSE)
}
# error: x variable has more than two levels, but alternative is not defined properly (chi-square and ANOVA)
if (x_type == "categorical" & x_levels > 2 & length(alternative) == 1) {
if(alternative != "greater"){
stop('Use alternative = "greater" for ANOVA or chi-square test', call. = FALSE)
}
}
# errors about success
if ((y_type == "categorical" & x_levels == 2 & y_levels == 2) | (y_type == "categorical" & is.null(x))) {
# error: success not provided for categorical variable for 1 or 2 proportion ci or ht
if (is.null(success)) {
y_level_names = levels(y)
stop(paste("Response variable is categorical, specify which level to call success: ",
y_level_names[1], " or ", y_level_names[2]), call. = FALSE)
}
# error: success provided is not a level of the categorical variable
if (success %in% levels(y) == FALSE) {
stop(paste(success,"is not a level of the success variable"), call. = FALSE)
}
}
# warning: success provided for numerical variable
if (y_type == "numerical" & !is.null(success)) {
warning("Ignoring success since y is numerical", call. = FALSE)
}
# warning: confidence level greater than 1
if (conf_level > 1) {
conf_level = conf_level / 100
warning(paste("Confidence level converted to ", conf_level, sep = ""), call. = FALSE)
}
# warning: significance level greater than 1
if (sig_level > 1) {
sig_level = sig_level / 100
warning(paste("Significance level converted to ", sig_level, sep = ""), call. = FALSE)
}
# ci
if(type == "ci"){
# only one
if(is.null(x)){
# variable assignment
y <- eval(substitute(y), data, parent.frame())
# remova NAs
y <- y[!is.na(y)]
# one mean
if(statistic == "mean"){
if(method == "theoretical"){
res <- ci_single_mean_theo(y = y, conf_level = conf_level, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = FALSE,
show_res = show_res)
return(invisible(list(df = res$df, SE = res$SE, ME = res$ME, CI = res$CI)))
}
if(method == "simulation"){
res <- ci_single_mean_sim(y = y, conf_level = conf_level, y_name = y_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
# one median
if(statistic == "median"){
if(method == "theoretical"){
stop("Use simulation methods for inference for the median", call. = FALSE)
}
if(method == "simulation"){
res <- ci_single_median_sim(y = y, conf_level = conf_level, y_name = y_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
# one proportion
if(statistic == "proportion"){
if(method == "theoretical"){
res <- ci_single_prop_theo(y = y, success = success,
conf_level = conf_level, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = FALSE,
show_res = show_res)
return(invisible(list(SE = res$SE, ME = res$ME, CI = res$CI)))
}
if(method == "simulation"){
res <- ci_single_prop_sim(y = y, success = success,
conf_level = conf_level, y_name = y_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
}
# compare two
if(!is.null(x)){
if(length(levels(x)) == 2){
# remove NAs
d <- na.omit(data.frame(y = y, x = x))
x <- d$x
y <- d$y
# fix order, if needed
if(!is.null(order)){
if(order[1] != levels(x)[1]){
x <- relevel(x, ref = levels(x)[2])
}
}
# compare two means
if(statistic == "mean"){
if(method == "theoretical"){
res <- ci_two_mean_theo(y = y, x = x, conf_level = conf_level,
y_name = y_name, x_name = x_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = FALSE,
show_res = show_res)
return(invisible(list(df = res$df, SE = res$SE, ME = res$ME, CI = res$CI)))
}
if(method == "simulation"){
res <- ci_two_mean_sim(y = y, x = x, conf_level = conf_level,
y_name = y_name, x_name = x_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
# compare two medians
if(statistic == "median"){
if(method == "theoretical"){
stop("Use simulation methods for inference for the median", call. = FALSE)
}
if(method == "simulation"){
res <- ci_two_median_sim(y = y, x = x, conf_level = conf_level,
y_name = y_name, x_name = x_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
# compare two proportions
if(statistic == "proportion"){
if(method == "theoretical"){
res <- ci_two_prop_theo(y = y, x = x, conf_level = conf_level,
success = success,
y_name = y_name, x_name = x_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = FALSE,
show_res = show_res)
return(invisible(list(SE = res$SE, ME = res$ME, CI = res$CI)))
}
if(method == "simulation"){
res <- ci_two_prop_sim(y = y, x = x, conf_level = conf_level,
success = success,
y_name = y_name, x_name = x_name,
boot_method = boot_method, nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
if(boot_method == "perc"){
return(invisible(list(sim_dist = res$sim_dist, CI = res$CI)))
} else {
return(invisible(list(sim_dist = res$sim_dist, SE = res$SE, ME = res$ME, CI = res$CI)))
}
}
}
}
}
}
# ht
if(type == "ht"){
# only one
if(is.null(x)){
# remove NAs
y <- y[!is.na(y)]
# one mean
if(statistic == "mean"){
if(method == "theoretical"){
res <- ht_single_mean_theo(y = y, null = null, alternative = alternative,
y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(SE = res$SE, t = res$t, df = res$df, p_value = res$p_value)))
}
if(method == "simulation"){
res <- ht_single_mean_sim(y = y, null = null, alternative = alternative,
y_name = y_name,
nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
# one median
if(statistic == "median"){
if(method == "theoretical"){
stop("Use simulation methods for inference for the median", call. = FALSE)
}
if(method == "simulation"){
res <- ht_single_median_sim(y = y, null = null, alternative = alternative,
y_name = y_name,
nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
# one proportion
if(statistic == "proportion"){
if(method == "theoretical"){
res <- ht_single_prop_theo(y = y, success = success, null = null,
alternative = alternative,
y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(SE = res$SE, z = res$z, p_value = res$p_value)))
}
if(method == "simulation"){
res <- ht_single_prop_sim(y = y, success = success, null = null,
alternative = alternative,
y_name = y_name,
nsim = nsim, seed = seed,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
}
# compare two
if(!is.null(x)){
if(length(levels(x)) == 2){
# remove NAs
d <- na.omit(data.frame(y = y, x = x))
x <- d$x
y <- d$y
# fix order, if needed
if(!is.null(order)){
if(order[1] != levels(x)[1]){
x <- relevel(x, ref = levels(x)[2])
}
}
# compare two means
if(statistic == "mean"){
if(method == "theoretical"){
res <- ht_two_mean_theo(y = y, x = x, null = null, alternative = alternative,
y_name = y_name, x_name = x_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(SE = res$SE, df = res$df, t = res$t, p_value = res$p_value)))
}
if(method == "simulation"){
res <- ht_two_mean_sim(y = y, x = x, null = null, alternative = alternative,
nsim = nsim, seed = seed,
y_name = y_name, x_name = x_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
# compare two medians
if(statistic == "median"){
if(method == "theoretical"){
stop("Use simulation methods for inference for the median", call. = FALSE)
}
if(method == "simulation"){
res <- ht_two_median_sim(y = y, x = x, null = null, alternative = alternative,
nsim = nsim, seed = seed,
y_name = y_name, x_name = x_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
# compare two proportions
if(statistic == "proportion"){
if(method == "theoretical"){
res <- ht_two_prop_theo(y = y, x = x, success = success,
null = null, alternative = alternative,
x_name = x_name, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(SE = res$SE, z = res$z, p_value = res$p_value)))
}
if(method == "simulation"){
res <- ht_two_prop_sim(y = y, x = x, success = success,
nsim = nsim, seed = seed,
null = null, alternative = alternative,
x_name = x_name, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(sim_dist = res$sim_dist, p_value = res$p_value)))
}
}
}
if(length(levels(x)) > 2){
# compare many means
if(statistic == "mean"){
if(method == "theoretical"){
res <- ht_many_mean_theo(y = y, x = x,
null = null, alternative = alternative,
sig_level = sig_level,
x_name = x_name, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(F = res$F, df1 = res$df1, df2 = res$df2,
p_value = res$p_value)))
}
if(method == "simulation"){
stop("Simulation based testing for ANOVA is not implemented in this function.", call. = FALSE)
}
}
# compare many proportions
if(statistic == "proportion"){
if(method == "theoretical"){
res <- ht_many_prop_theo(y = y, x = x,
x_name = x_name, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = show_inf_plot,
show_res = show_res)
return(invisible(list(chi_sq = res$chi_sq, df = res$df, p_value = res$p_value)))
}
if(method == "simulation"){
res <- ht_many_prop_sim(y = y, x = x,
nsim = nsim, seed = seed,
x_name = x_name, y_name = y_name,
show_var_types = show_var_types,
show_summ_stats = show_summ_stats,
show_eda_plot = show_eda_plot,
show_inf_plot = FALSE,
show_res = show_res)
return(invisible(list(chi_sq = res$chi_sq, p_value = res$p_value)))
}
}
}
}
}
}