-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples and tests and renaming the binary evidence find_p command.
- Loading branch information
Showing
5 changed files
with
67 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(sens_analysis) | ||
importFrom(BiasedUrn,dFNCHypergeo) | ||
importFrom(stats,uniroot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' Find a p-value given a certain number of observations in favor of the | ||
#' working hypothesis among a total number of observations | ||
#' | ||
#' | ||
#' | ||
#' @param obs_support An integer representing the number of observations in favor of the working hypothesis. Must be less than or equal to the total. | ||
#' @param total_obs An integer representing the total number of observations | ||
#' @param odds The odds of seeing | ||
#' @param interpretation TRUE if the function returns text helping to interpret the result, FALSE (default option) to return just the p-value | ||
#' @return Either a p-value (numeric, scalar) or a list containing the p-value and text containing an interpretation | ||
#' @export | ||
find_p_two_types <- function(obs_support, total_obs, odds = 1, interpretation = FALSE) { | ||
## Test to make sure that obs_support is less than or equal to total_obs | ||
stopifnot("The number of observations in favor of the working hypothesis must be less than or equal to the total number of observations" = obs_support <= total_obs) | ||
obs_oppose <- obs_support + 1 | ||
stopifnot("Observations are already compatible with the null. The number of observations in favor of the working hypothesis must be greater than or equal to half of the total number of observations" = obs_support >= (total_obs / 2)) | ||
## We assume odds=1 here | ||
thep <- dFNCHypergeo( | ||
x = obs_support, m1 = obs_support, m2 = obs_oppose, | ||
n = total_obs, odds = odds | ||
) | ||
if (!interpretation) { | ||
return(thep) | ||
} else { | ||
interp <- paste0("The probability of drawing ", obs_support, " observations which support the working theory from an urn model supporting a rival theory, where the odds of observing working theory information is odds=", odds, ", is p=", round(thep, 4)) | ||
message(interp) | ||
return(list(thep = thep, interp = interp)) | ||
} | ||
} | ||
#' @examples | ||
#' ... | ||
#' # Equal probability, 2 kinds of evidence | ||
#' find_p_two_types(obs_support = 7, total_obs = 10) | ||
#' # Equal probability, 2 kinds of evidence with interpretation printed | ||
#' find_p_two_types(obs_support = 7, total_obs = 10, interpretation = TRUE) | ||
#' # Unequal probability, 2 kinds of evidence with interpretation printed | ||
#' find_p_two_types(obs_support = 7, total_obs = 10, interpretation = TRUE, odds = .5) | ||
#' find_p_two_types(obs_support = 7, total_obs = 10, interpretation = TRUE, odds = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
# Test the p creation function | ||
testthat::context("The simple unbiased p function test") | ||
|
||
## The next lines are for use when creating the tests. Change interactive<-FALSE for production | ||
interactive <- FALSE | ||
if (interactive) { | ||
library(devtools) | ||
load_all() ## use this during debugging | ||
} | ||
|
||
test_that("It gives the correct p-value", { | ||
res <- find_p_two_types(obs_support = 7, total_obs = 10) | ||
expect_true(all.equal(res, 0.018648018648018651472)) | ||
}) | ||
|
||
test_that("It prints the correct interpretation", { | ||
res <- find_p_two_types(obs_support = 7, total_obs = 10, interpretation = TRUE) | ||
expect_true(all.equal(res[["thep"]], 0.018648018648018651472)) | ||
expect_true(all.equal(res[["interp"]], "The probability of drawing 7 observations which support the working theory from an urn model supporting a rival theory, where the odds of observing working theory information is odds=1, is p=0.0186")) | ||
}) | ||
|
||
|
||
test_that("P-values with odds < 1 are smaller than p-values with odds > 2", { | ||
res_odds_half <- find_p_two_types(obs_support = 7, total_obs = 10, odds = .5, interpretation = FALSE) | ||
res_odds_equal <- find_p_two_types(obs_support = 7, total_obs = 10, odds = 1, interpretation = FALSE) | ||
res_odds_double <- find_p_two_types(obs_support = 7, total_obs = 10, odds = 2, interpretation = FALSE) | ||
expect_lt(res_odds_half, res_odds_equal) | ||
expect_lt(res_odds_equal, res_odds_double) | ||
}) | ||
|
||
test_that("Warnings work", { | ||
expect_error(find_p(num_support=10,total_info=5)) | ||
expect_error(find_p(num_support=2,total_info=10)) | ||
expect_error(find_p_two_types(obs_support = 10, total_obs = 5)) | ||
expect_error(find_p_two_types(obs_support = 2, total_obs = 10)) | ||
}) | ||
|
||
test_that("It gives the correct p-value",{ | ||
res <- find_p(num_support=7,total_info=10) | ||
expect_true(all.equal(res,0.018648018648018651472)) | ||
}) |