From b556b1c3c407cef7c2e3058b2224053b8f3b4024 Mon Sep 17 00:00:00 2001 From: Mabene Yasmine Date: Wed, 21 Feb 2024 21:22:40 +0000 Subject: [PATCH] version 0.2.1 --- DESCRIPTION | 6 ++--- MD5 | 20 ++++++++--------- NEWS.md | 3 +++ R/create_fisher_interval.R | 16 ++++++++++++-- R/find_counternull_values.R | 28 +++++++++++++++++++----- R/find_counternull_values_int.R | 6 +++-- inst/doc/counternull_vignette.html | 10 ++++----- man/create_fisher_interval.Rd | 6 ++++- man/find_counternull_values.Rd | 6 ++++- tests/testthat/test-counternull_values.R | 9 ++++++++ tests/testthat/test-fisher.R | 8 +++++++ 11 files changed, 88 insertions(+), 30 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 928f096..ad431d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: Counternull Type: Package Title: Randomization-Based Inference -Version: 0.2.0 +Version: 0.2.1 Authors@R: c( person("Mabene", "Yasmine", email = "ymabene@stanford.edu", role = c("aut", "cre")), @@ -27,10 +27,10 @@ Suggests: knitr, rmarkdown, testthat (>= 3.0.0) Config/testthat/edition: 3 VignetteBuilder: knitr NeedsCompilation: no -Packaged: 2023-08-27 22:23:17 UTC; yasmi +Packaged: 2024-02-21 21:09:32 UTC; yasmi Author: Mabene Yasmine [aut, cre], Bind Marie [aut], Harvard University [cph] Maintainer: Mabene Yasmine Repository: CRAN -Date/Publication: 2023-08-27 22:50:15 UTC +Date/Publication: 2024-02-21 21:22:40 UTC diff --git a/MD5 b/MD5 index 73763d9..94ce85d 100644 --- a/MD5 +++ b/MD5 @@ -1,15 +1,15 @@ -0bf5b14f7c6a3b19d16a579c1b69d631 *DESCRIPTION +3f63b171b3fd3f3a2a0dc81d6fbc3e17 *DESCRIPTION 6c4ba1102ca1c174c5f97ed59f8b2c25 *LICENSE 21032c967f9967ffebe0d9084c7088f3 *NAMESPACE -052e67f88438bb237d1467bea0067dbd *NEWS.md +3f71bb93b2688a28a78a7d34618410b6 *NEWS.md 5ba21ea823a030cf48b025c0a5f59179 *R/adjust_pvalues.R a024334833410d1dca8577deab85731b *R/count.R bab5430e02509d87097ac635a99a714f *R/counternull_dist.R -677f20eefa5fb0bac9624852b35b6149 *R/create_fisher_interval.R +ff7026de5ee407d61989bb1be8d63a77 *R/create_fisher_interval.R c61ad826e9d8713bcc5b497e991c309d *R/create_null_rand.R 5c6dd896095268a3f2cdd9b31355672e *R/create_randomization_matrix.R -3d893381082c420e4313e3fa93cfa665 *R/find_counternull_values.R -c7724ee30df7897eb8d491d689d69270 *R/find_counternull_values_int.R +f2ff013e0f18669ce8bad2140df6bac6 *R/find_counternull_values.R +303e3550ca652af0086838a85b2df2cd *R/find_counternull_values_int.R 898b4af2d69cd62ac3f2eed59b69271d *R/find_set.R c75a908a47d9550584c9bdb8212c4e68 *R/find_test_stat.R 06861e8c7dd106c205b37c32226f3b6a *R/fisher_binary.R @@ -26,23 +26,23 @@ a32d679bfae7d065e30f119daebe0075 *R/joint_p.R ce83e567f086289c23403cc64bb97b06 *data/sample_matrix.rda 16110a211e3538ed2a7a5a0b788c4ff0 *inst/doc/counternull_vignette.R d77d543d537246591f4c600c741be3fe *inst/doc/counternull_vignette.Rmd -dcb88274abd7caad21d7f8324c1071fc *inst/doc/counternull_vignette.html +fa8e659e2ab5148c2171a5a4705d5925 *inst/doc/counternull_vignette.html 3b40681c9988b2a8b56ced892f1573ce *inst/extdata/sample_data.csv 7abc83daf9fa24b663f73ca81a423882 *man/adjust_pvalues.Rd -525e25356bb59f2f46616e78a4e4cae5 *man/create_fisher_interval.Rd +d4869c97c16d836ae62b476b6f7f0fa1 *man/create_fisher_interval.Rd 1f175ccc23d188e419d20d0fc027027e *man/create_null_rand.Rd b135c76c34525aa6f37623de248af7b0 *man/create_randomization_matrix.Rd 105fb9596a598efe33a8a1b8a98f2ef6 *man/figures/README-example-1.png 61208a593b81487d03611541dae002d1 *man/figures/README-example-2.png ba8a79becb052f4a4932186874510f5f *man/figures/README-example-3.png -662d7e444a7559c846d36370e4de0e23 *man/find_counternull_values.Rd +179114a24badc670a130aeb79e69e304 *man/find_counternull_values.Rd 098e13e7484034fef2c6bffd774dda57 *man/find_test_stat.Rd add8460edc8927258a210b9d8653ae27 *man/sample_data.Rd ae0766960ff42ed4847b46b6154faf6d *man/sample_matrix.Rd e4704ce9cd1f9135d74a92e1828581e3 *tests/testthat.R 053610e865096e024900ff40c9912db0 *tests/testthat/Rplots.pdf 4ef2d5b88d0ab855c0ff8132412b7afe *tests/testthat/test-adjust_pvalues.R -23d70e75f344ef97c25ef276bf09609a *tests/testthat/test-counternull_values.R -94ee9b58423d3f59f02b289da8a9a728 *tests/testthat/test-fisher.R +1bdad3ba3c8c2ac968038784c5d500b6 *tests/testthat/test-counternull_values.R +3ab67e7fb2bb610ca7be36ebd994edb7 *tests/testthat/test-fisher.R 529b137139bf3c4f400e926ea76dce31 *tests/testthat/test-rand_dist.R d77d543d537246591f4c600c741be3fe *vignettes/counternull_vignette.Rmd diff --git a/NEWS.md b/NEWS.md index 9f8a143..362ac16 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,3 +11,6 @@ * Added new examples to vignette +# Counternull 0.2.1 (2024-02-21) + +* Added "width" parameter to increase accuracy in counternull set diff --git a/R/create_fisher_interval.R b/R/create_fisher_interval.R index 67dcf81..35f4bbf 100644 --- a/R/create_fisher_interval.R +++ b/R/create_fisher_interval.R @@ -5,6 +5,9 @@ #' @param null_r "null_rand" object corresponding to data used for interval #' @param alpha Significance level for Fisher Interval (default = .05 #' for 95\% confidence) +#' @param width Integer indicating the number of values to search for to construct +#' Fisher Interval. Default value = 10000. (Increasing this argument may result +#' in increased accuracy of interval.) (Optional) #' #' @examples #' \donttest{ @@ -43,7 +46,7 @@ #' @references \doi{10.48550/arXiv.2105.03996} #' @export -create_fisher_interval=function(null_r,alpha=NULL){ +create_fisher_interval=function(null_r,alpha=NULL,width = NULL){ if(is.null(alpha)){ alpha = .05 @@ -57,7 +60,12 @@ create_fisher_interval=function(null_r,alpha=NULL){ stop('Argument "null_r" must be of "null_rand" class.' ) } + if(!is.null(width)){ + if(!is.numeric(width) | length(width) != 1){ + stop('Argument "width" must be an integer.') + } + } bounds=vector(length=2) low_c=(alpha/2)*length(null_r$null_dist) @@ -65,7 +73,11 @@ create_fisher_interval=function(null_r,alpha=NULL){ # initial bounds are 4 times the observed effect size low = 1 - high = 10000 + if(!is.null(width)){ + high = max(10000, width) + } else{ + high = 10000 + } bounds_l = -4*abs(null_r$t_obs) bounds_h = 4*abs(null_r$t_obs) i = 0 diff --git a/R/find_counternull_values.R b/R/find_counternull_values.R index 748d19f..b50da95 100644 --- a/R/find_counternull_values.R +++ b/R/find_counternull_values.R @@ -5,6 +5,9 @@ #' @param counts Vector containing lower and upper bounds for number of #' test statistics more extreme than observed test statistic in #' counternull randomization distribution (optional) +#' @param width Integer indicating the number of values to search for to retrieve +#' counternull set. Default value = 10000. (Increasing this argument may result +#' in additional counternull values being found.) (optional) #' @param bw Histogram bin width (optional) #' #' @examples @@ -42,11 +45,13 @@ #' If no counternull values are found, all entries in class are set to null. #' If only one set of counternull values are found, "perm_two", low_two" #' and "high_two" are set to null. +#' +#' #' @references \doi{10.1111/j.1467-9280.1994.tb00281.x} #' @export -find_counternull_values=function(null_r, counts = NULL, bw = NULL){ +find_counternull_values=function(null_r, counts = NULL,width = NULL, bw = NULL){ if(!inherits(null_r,"null_rand")){ stop('Argument "null_r" must be of class "null_rand".') @@ -59,6 +64,13 @@ find_counternull_values=function(null_r, counts = NULL, bw = NULL){ } + if(!is.null(width)){ + if(!is.numeric(width) | length(width) != 1){ + stop('Argument "width" must be an integer.') + } + + } + if(is.null(counts)){ counts = null_r$counts } else { @@ -87,19 +99,23 @@ find_counternull_values=function(null_r, counts = NULL, bw = NULL){ # bounds are 4 times the observed effect size low = 1 - high = 10000 + if(!is.null(width)){ + high = max(10000, width) + } else{ + high = 10000 + } search=seq(-4*abs(null_r$t_obs),4*abs(null_r$t_obs), 8*abs(null_r$t_obs)/(high-1)) if(null_r$alternative == "two-sided"){ s_p=search[search>=0] s_n=search[search<0] - p=find_counternull_values_int(counts, search, null_r$t_obs, null_r$y, + p=find_counternull_values_int(counts, search,high, null_r$t_obs, null_r$y, null_r$w, null_r$alternative, null_r$rand_matrix,null_r$test_stat, null_r$fun, s=0) - n=find_counternull_values_int(counts, search, null_r$t_obs, null_r$y, + n=find_counternull_values_int(counts, search, high,null_r$t_obs, null_r$y, null_r$w, null_r$alternative, null_r$rand_matrix, null_r$test_stat, null_r$fun, s=1) @@ -141,7 +157,7 @@ find_counternull_values=function(null_r, counts = NULL, bw = NULL){ } else{ - c <- find_counternull_values_int(counts, search, null_r$t_obs, + c <- find_counternull_values_int(counts, search,high, null_r$t_obs, null_r$y,null_r$w, null_r$alternative,null_r$rand_matrix, null_r$test_stat, null_r$fun, s=0) @@ -285,7 +301,7 @@ plot.counternull=function(x, ...){ if(!is.null(x$low) & is.null(x$low_two)){ - da=data.frame(xx = c(x$counternull_perm, null_r$null_dist), + dat=data.frame(xx = c(x$counternull_perm, null_r$null_dist), group = rep(1:0, each = length(x$counternull_perm))) } diff --git a/R/find_counternull_values_int.R b/R/find_counternull_values_int.R index 8073dbc..19a9e93 100644 --- a/R/find_counternull_values_int.R +++ b/R/find_counternull_values_int.R @@ -2,6 +2,8 @@ #' #' @param obs_c Number of extreme test statistics in null distribution #' @param search Range of effect sizes to test as counternull values +#' @param width Integer indicating the number of values to search for to retrieve +#' counternull set. #' @param t_obs Observed test statistic #' @param y Vector of observed outcomes #' @param w Vector indicating treatment assignments @@ -19,12 +21,12 @@ -find_counternull_values_int=function(obs_c, search, t_obs, y,w, +find_counternull_values_int=function(obs_c, search,width, t_obs, y,w, alternative,rand_matrix, test_stat, fun, s){ low = 1 - high = 10000 + high = width index = (round((low + high) / 2)) counternull_value=search[index] # estimated counternull value diff --git a/inst/doc/counternull_vignette.html b/inst/doc/counternull_vignette.html index 0a20136..450db9a 100644 --- a/inst/doc/counternull_vignette.html +++ b/inst/doc/counternull_vignette.html @@ -522,11 +522,11 @@

Fisher-Adjusted P-Values and Randomization Matrices

alternative = "greater") summary(n_rand_two) #> Observed test statistic: 0.2948718 -#> Number of extreme test statistics: 4 -#> P-value: 0.004 +#> Number of extreme test statistics: 6 +#> P-value: 0.006 #> Alternative: greater plot(n_rand_two) -

+

In this example, we use 1,000 permutations in our randomization matrix. But how do you determine the ideal amount of permutations to use? If your data set is small enough, then you can use the maximum @@ -543,9 +543,9 @@

Fisher-Adjusted P-Values and Randomization Matrices

randomization distribution and does not substantially change the p-value, then you may prefer to use less permutations.

adjusted_pvalues = adjust_pvalues(list(n_rand,n_rand_two))
-

+

cat(adjusted_pvalues)
-#> 0.093 0.008
+#> 0.095 0.012

Now, we have adjusted our p-values. Here we can visualize the joint p-value distribution from our two comparisons as well as the adjusted p-values.

diff --git a/man/create_fisher_interval.Rd b/man/create_fisher_interval.Rd index 14e00aa..b563ce6 100644 --- a/man/create_fisher_interval.Rd +++ b/man/create_fisher_interval.Rd @@ -4,13 +4,17 @@ \alias{create_fisher_interval} \title{Compute Fisher Interval} \usage{ -create_fisher_interval(null_r, alpha = NULL) +create_fisher_interval(null_r, alpha = NULL, width = NULL) } \arguments{ \item{null_r}{"null_rand" object corresponding to data used for interval} \item{alpha}{Significance level for Fisher Interval (default = .05 for 95\% confidence)} + +\item{width}{Integer indicating the number of values to search for to construct +Fisher Interval. Default value = 10000. (Increasing this argument may result +in increased accuracy of interval.) (Optional)} } \value{ Class "fisher_interval" with 4 entries: diff --git a/man/find_counternull_values.Rd b/man/find_counternull_values.Rd index 5cefb70..423cecf 100644 --- a/man/find_counternull_values.Rd +++ b/man/find_counternull_values.Rd @@ -4,7 +4,7 @@ \alias{find_counternull_values} \title{Find Counternull Values} \usage{ -find_counternull_values(null_r, counts = NULL, bw = NULL) +find_counternull_values(null_r, counts = NULL, width = NULL, bw = NULL) } \arguments{ \item{null_r}{"null_rand" object corresponding to data} @@ -13,6 +13,10 @@ find_counternull_values(null_r, counts = NULL, bw = NULL) test statistics more extreme than observed test statistic in counternull randomization distribution (optional)} +\item{width}{Integer indicating the number of values to search for to retrieve +counternull set. Default value = 10000. (Increasing this argument may result +in additional counternull values being found.) (optional)} + \item{bw}{Histogram bin width (optional)} } \value{ diff --git a/tests/testthat/test-counternull_values.R b/tests/testthat/test-counternull_values.R index 5fd65d9..8b4ac0d 100644 --- a/tests/testthat/test-counternull_values.R +++ b/tests/testthat/test-counternull_values.R @@ -8,6 +8,13 @@ test_that("assess counternull values", { test_stat = c("diffmeans")) expected = find_counternull_values(n_r) + expect_s3_class(expected, "counternull") + expect_named(expected, c("counternull_perm", "low", "high", + "counternull_perm_two", + "low_two", "high_two", "null_r", "bw")) + + expected = find_counternull_values(n_r, width = -100) + expect_s3_class(expected, "counternull") expect_named(expected, c("counternull_perm", "low", "high", "counternull_perm_two", @@ -15,6 +22,8 @@ test_that("assess counternull values", { expect_error(find_counternull_values(y)) + expect_error(find_counternull_values(n_r,width = c(10000,100))) + t_r = n_r t_r$rand_matrix = n_r$rand_matrix[1:7,] expect_error(find_counternull_values(t_r)) diff --git a/tests/testthat/test-fisher.R b/tests/testthat/test-fisher.R index b36c1b1..d5502d8 100644 --- a/tests/testthat/test-fisher.R +++ b/tests/testthat/test-fisher.R @@ -11,6 +11,14 @@ test_that("assess fisher ",{ "pvalue_lower", "pvalue_upper", "range", "null_r")) + expected = create_fisher_interval(n_r, width = -100) + expect_s3_class(expected, "fisher_interval") + expect_named(expected, c("lower_bound", "upper_bound", "alpha", + "pvalue_lower", "pvalue_upper", "range", + "null_r")) + + expect_error(create_fisher_interval(n_r, width = "a")) + fun = function (x,y){ return(invisible(6)) }