diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 00000000..91114bf2 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..698f46c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +# History files +.Rhistory +.Rapp.history + +# Session Data files +.RData + +# User-specific files +.Ruserdata + +# Example code in package build process +*-Ex.R + +# Output files from R CMD build +/*.tar.gz + +# Output files from R CMD check +/*.Rcheck/ + +# produced vignettes +vignettes/*.html +vignettes/*.pdf + +# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 +.httr-oauth + +# knitr and R markdown default cache directories +*_cache/ +/cache/ + +# Temporary files created by R markdown +*.utf8.md +*.knit.md + +# R Environment Variables +.Renviron + +# pkgdown site +docs/ + +# translation temp files +po/*~ + +# OSX files +*.DS_Store +Thumbs.db + +# JASP analysis +state + +# RStudio files +.Rproj.user/* +Rproj.user +.Rproj.user diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..f7d43dea --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +os: osx +language: r +r: 3.6.1 +before_install: + - "git clone --branch=MacOS-Original https://github.com/jasp-stats/jasp-required-files.git ~/pkgs" +install: + - RScript -e ".libPaths(c(.libPaths(), '~/pkgs')); install.packages('remotes'); remotes::install_github('jasp-stats/jaspTools', upgrade = 'never')" +script: + - R < tests/testthat.R --no-save +env: + global: + - R_REMOTES_NO_ERRORS_FROM_WARNINGS=true + - VDIFFR_RUN_TESTS=true + - REQUIRED_PKGS=~/pkgs diff --git a/DESCRIPTION b/DESCRIPTION index f0d5cdbd..463f8ba2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,4 +7,19 @@ Author: JASP Team Website: jasp-stats.org Maintainer: JASP Team Description: This module offers the standard Frequentist and Bayesian frequency analyses. -License: GPL (>= 2) \ No newline at end of file +License: GPL (>= 2) +Encoding: UTF-8 +Imports: + abtest, + BayesFactor, + conting, + ggplot2, + jaspBase, + jaspGraphs, + plyr, + stringr, + vcd, + vcdExtra +Remotes: + jasp-stats/jaspBase, + jasp-stats/jaspGraphs diff --git a/NAMESPACE b/NAMESPACE index 2a7b4b38..c0ab8c23 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,15 @@ -exportPattern('^[[:alpha:]]+') +import(jaspBase) +export(ABTestBayesian) +export(BinomialTest) +export(BinomialTestBayesian) +export(MultinomialTest) +export(MultinomialTestBayesian) +export(ContingencyTables) +export(ContingencyTablesBayesian) +export(RegressionLogLinear) +export(RegressionLogLinearBayesian) +export(.bayesBinomialTest) +export(.dfPointsPP) +export(.dfLinesPP) +export(.credibleIntervalPlusMedian) +export(.abTestMain) diff --git a/R/abtestbayesian.R b/R/abtestbayesian.R index f9de3a11..03d77ef5 100644 --- a/R/abtestbayesian.R +++ b/R/abtestbayesian.R @@ -16,36 +16,41 @@ # -ABTestBayesian <- function(jaspResults, dataset, options, ...) { - +ABTestBayesian <- function(jaspResults, dataset = NULL, options) { + ready <- (options$n1 != "" && options$y1 != "" && options$n2 != "" && options$y2 != "") - + ### READ DATA ### if (ready) dataset <- .abTestBayesianReadData(dataset, options) + + ### This function is shared with summary stats a/b test + .abTestMain(jaspResults, dataset, options, ready) +} +.abTestMain <- function(jaspResults, dataset, options, ready) { ## COMPUTE AB OBJECT ### ab_obj <- .calcModel.abTest(jaspResults, dataset, options, ready) - + ### RESULTS - MAIN TABLE ### .abTestBayesianTableMain(jaspResults, ab_obj, options, ready, position = 1) - + ### DESCRIPTIVES TABLE ### if (options$descriptives) .abTestBayesianDescriptivesTable(jaspResults, dataset, options, ready, position = 2) - + ### PRIOR AND POSTERIOR PLOT ### if (options$plotPriorAndPosterior) .abTestPlotPriorPosterior(jaspResults, ab_obj, options, ready, position = 3) - + ### SEQUENTIAL PLOT ### if (options$plotSequentialAnalysis) .abTestPlotSequential(jaspResults, ab_obj, ready, position = 4) - + ### ROBUSTNESS PLOT ### if (options$plotRobustness) .abTestPlotRobustness(jaspResults, ab_obj, options, ready, position = 5) - + ### PRIOR PLOT ### if (options$plotPriorOnly) .abTestPlotPriorOnly(jaspResults, options, position = 6) @@ -61,75 +66,75 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { # # Return: # The (numeric) columns given as dependent/covariates/wlsWeights - + if (!is.null(dataset)) return(dataset) - + asNum <- c(options$n1, options$y1, options$n2, options$y2) dataset <- .readDataSetToEnd(columns.as.numeric = asNum, excluse.na.listwise = asNum) new_names <- setNames(c("n1", "n2", "y1", "y2"), c(.v(options$n1), .v(options$n2), .v(options$y1), .v(options$y2))) colnames(dataset) <- stringr::str_replace_all(colnames(dataset), new_names) - + return(dataset) } .abTestBayesianTableMain <- function(jaspResults, ab_obj, options, ready, position) { - + if (!is.null(jaspResults[["abTestBayesianTable"]])) return() - + # Create the main table abTestBayesianTable <- createJaspTable(title = gettext("Bayesian A/B Test")) abTestBayesianTable$dependOn(options = c("bayesFactorType", "bayesFactorOrder"), optionsFromObject = jaspResults[["model"]]) abTestBayesianTable$position <- position - + # abTestBayesianTable$addCitation("JASP Team (2018). JASP (Version 0.9.3) [Computer software].") - + if (options$bayesFactorType == "BF10") { - bf.title <- "BF10" + bf.title <- "BF10" } else if (options$bayesFactorType == "BF01") { - bf.title <- "BF01" + bf.title <- "BF01" } else if (options$bayesFactorType == "LogBF10") { - bf.title <- "Log(BF10)" + bf.title <- "Log(BF10)" } - + abTestBayesianTable$addColumnInfo(name = "Models", title = gettext("Models"), type = "string") abTestBayesianTable$addColumnInfo(name = "P(M)", title = gettext("P(M)"), type = "number", format = "sf:4;dp:3") abTestBayesianTable$addColumnInfo(name = "P(M|data)", title = gettext("P(M|data)"), type = "number", format = "sf:4;dp:3") abTestBayesianTable$addColumnInfo(name = "BF", title = bf.title, type = "number") - + jaspResults[["abTestBayesianTable"]] <- abTestBayesianTable - + if (!ready) return() - + .abTestBayesianFillTableMain(abTestBayesianTable, ab_obj, options) - + abTestBayesianTable$addFootnote(gettext("A positive log odds ratio means that the success rate in Group 2 is higher than in Group 1.")) } .abTestBayesianFillTableMain <- function(abTestBayesianTable, ab_obj, options) { - + # Normalize prior probabilities sum_logor <- options$orGreaterThan1Prob + options$orLessThan1Prob + options$orEqualTo1Prob + options$orNotEqualTo1Prob orGreaterThan1Prob <- options$orGreaterThan1Prob / sum_logor orLessThan1Prob <- options$orLessThan1Prob / sum_logor orEqualTo1Prob <- options$orEqualTo1Prob / sum_logor orNotEqualTo1Prob <- options$orNotEqualTo1Prob / sum_logor - + if (inherits(ab_obj, "try-error")) { errorMessage <- as.character(ab_obj) abTestBayesianTable$setError(errorMessage) - + return() } - + output.rows <- list() rowCount <- 0 - + if (orEqualTo1Prob > 0) { rowCount = rowCount + 1 output.rows[[rowCount]] <- list( @@ -139,7 +144,7 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { "P(M)" = orEqualTo1Prob ) } - + if (orGreaterThan1Prob > 0) { rowCount = rowCount + 1 output.rows[[rowCount]] <- list( @@ -149,7 +154,7 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { "P(M)" = orGreaterThan1Prob ) } - + if (orLessThan1Prob > 0) { rowCount = rowCount + 1 output.rows[[rowCount]] <- list( @@ -159,7 +164,7 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { "P(M)" = orLessThan1Prob ) } - + if (orNotEqualTo1Prob > 0) { rowCount = rowCount + 1 output.rows[[rowCount]] <- list( @@ -169,18 +174,18 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { "P(M)" = orNotEqualTo1Prob ) } - + if (options$bayesFactorOrder == "bestModelTop") { ordered <- output.rows[order(sapply(output.rows, "[[", "P(M|data)"), decreasing = TRUE)] best_model_bf <- ordered[[1]]$BF output.rows <- list() - + for (r in 1:rowCount) { ordered[[r]]$BF <- ordered[[r]]$BF / best_model_bf } output.rows <- ordered } - + for (r in 1:rowCount) { if (options$bayesFactorType == "BF01") { output.rows[[r]]$BF <- 1 / output.rows[[r]]$BF @@ -188,101 +193,101 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { output.rows[[r]]$BF <- base::log(output.rows[[r]]$BF) } } - + abTestBayesianTable$addRows(output.rows) } .calcModel.abTest <- function(jaspResults, dataset, options, ready) { - + if (!is.null(jaspResults[["model"]])) return(jaspResults[["model"]]$object) - + # we copy dependencies from this state object in a few places, so it must always exist jaspResults[["model"]] <- createJaspState() jaspResults[["model"]]$dependOn(c("n1", "y1", "n2", "y2", "normal_mu", "normal_sigma", "orEqualTo1Prob", "orLessThan1Prob", "orGreaterThan1Prob", "orNotEqualTo1Prob", "numSamples", "setSeed", "seed")) - + if (!ready) return(NULL) - + prior_par <- list(mu_psi = options$normal_mu, sigma_psi = options$normal_sigma, mu_beta = 0, sigma_beta = 1) - + # Normalize prior probabilities sum_logor <- options$orGreaterThan1Prob + options$orLessThan1Prob + options$orEqualTo1Prob + options$orNotEqualTo1Prob orGreaterThan1Prob <- options$orGreaterThan1Prob / sum_logor orLessThan1Prob <- options$orLessThan1Prob / sum_logor orEqualTo1Prob <- options$orEqualTo1Prob / sum_logor orNotEqualTo1Prob <- options$orNotEqualTo1Prob / sum_logor - + prior_prob <- c(orNotEqualTo1Prob, orGreaterThan1Prob, orLessThan1Prob, orEqualTo1Prob) names(prior_prob) <- c("H1", "H+", "H-", "H0") - + .setSeedJASP(options) ab <- try(abtest::ab_test(data = dataset, prior_par = prior_par, prior_prob = prior_prob, posterior = TRUE, nsamples = options$numSamples)) - + jaspResults[["model"]]$object <- ab - + return(ab) } .abTestBayesianDescriptivesTable <- function(jaspResults, dataset, options, ready, position) { - + if (!is.null(jaspResults[["abTestBayesianDescriptivesTable"]])) return() - + abTestBayesianDescriptivesTable <- createJaspTable(title = gettext("Descriptives")) - + abTestBayesianDescriptivesTable$dependOn(c("n1", "y1", "n2", "y2", "descriptives")) abTestBayesianDescriptivesTable$position <- position - + abTestBayesianDescriptivesTable$addColumnInfo(name = "group", title = "", type = "string") abTestBayesianDescriptivesTable$addColumnInfo(name = "counts", title = gettext("Counts"), type = "integer") abTestBayesianDescriptivesTable$addColumnInfo(name = "total", title = gettext("Total"), type = "integer") abTestBayesianDescriptivesTable$addColumnInfo(name = "proportion", title = gettext("Proportion"), type = "number", format = "sf:4;dp:3") - + jaspResults[["abTestBayesianDescriptivesTable"]] <- abTestBayesianDescriptivesTable - + if (!ready) return() - + .abTestBayesianFillDescriptivesTable(abTestBayesianDescriptivesTable, dataset) - + return() } .abTestBayesianFillDescriptivesTable <- function(abTestBayesianDescriptivesTable, dataset) { - + output.rows <- list() - + num_rows = length(dataset$y1) counts = dataset$y1[num_rows] total = dataset$n1[num_rows] output.rows[[1]] <- list(group = gettext("Group 1"), counts = counts, total = total, proportion = counts / total) - + counts = dataset$y2[num_rows] total = dataset$n2[num_rows] output.rows[[2]] <- list(group = gettext("Group 2"), counts = counts, total = total, proportion = counts / total) - + abTestBayesianDescriptivesTable$addRows(output.rows) } .abTestPlotPriorPosterior <- function(jaspResults, ab_obj, options, ready, position) { - + abTestPriorAndPosteriorPlot <- createJaspPlot(title = gettext("Prior and Posterior"), width = 530, height = 400) abTestPriorAndPosteriorPlot$dependOn(c("n1", "y1", "n2", "y2", "normal_mu", "normal_sigma", "numSamples", "plotPosteriorType", "plotPriorAndPosterior", "setSeed", "seed")) abTestPriorAndPosteriorPlot$position <- position - + jaspResults[["abTestPriorAndPosteriorPlot"]] <- abTestPriorAndPosteriorPlot - + if (!ready) return() - + abTestPriorAndPosteriorPlot$plotObject <- .plotPosterior.abTest(ab_obj, options$plotPosteriorType) } @@ -294,33 +299,33 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { # ab_obj: ab test object # posteriorPlotType what <- switch( - posteriorPlotType, - "LogOddsRatio" = "logor", - "OddsRatio" = "or", - "RelativeRisk" = "rrisk", - "AbsoluteRisk" = "arisk", - "p1&p2" = "p1p2" + posteriorPlotType, + "LogOddsRatio" = "logor", + "OddsRatio" = "or", + "RelativeRisk" = "rrisk", + "AbsoluteRisk" = "arisk", + "p1&p2" = "p1p2" ) - + plotFunc <- function() { - abtest::plot_posterior(x = ab_obj, what = what, hypothesis = "H1") + abtest::plot_posterior(x = ab_obj, what = what, hypothesis = "H1") } - + return(plotFunc) } .abTestPlotSequential <- function(jaspResults, ab_obj, ready, position) { - + abTestSequentialPlot <- createJaspPlot(title = gettext("Sequential Analysis"), width = 530, height = 400) abTestSequentialPlot$dependOn(options = "plotSequentialAnalysis", optionsFromObject = jaspResults[["model"]]) abTestSequentialPlot$position <- position - + jaspResults[["abTestSequentialPlot"]] <- abTestSequentialPlot - + if (!ready) return() - + abTestSequentialPlot$plotObject <- .plotSequentialAnalysis.abTest(ab_obj) } @@ -328,26 +333,26 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { .plotSequentialAnalysis.abTest <- function(ab_obj) { # Args: # ab_obj: ab test object - + plotFunc <- function() { - abtest::plot_sequential(x = ab_obj) + abtest::plot_sequential(x = ab_obj) } - + return (plotFunc) } .abTestPlotRobustness <- function(jaspResults, ab_obj, options, ready, position) { - + abTestRobustnessPlot <- createJaspPlot(title = gettext("Bayes Factor Robustness Check"), width = 530, height = 400) abTestRobustnessPlot$dependOn(c("n1", "y1", "n2", "y2", "normal_mu", "normal_sigma", "mu_stepsize", "sigma_stepsize", "mu_stepsize_lower", "mu_stepsize_upper", "sigma_stepsize_lower", "sigma_stepsize_upper", "plotRobustnessBFType", "numSamples", "plotRobustness", "setSeed", "seed")) abTestRobustnessPlot$position <- position - + jaspResults[["abTestRobustnessPlot"]] <- abTestRobustnessPlot - + if (!ready) return() - + abTestRobustnessPlot$plotObject <- .plotRobustness.abTest(ab_obj, options) } @@ -355,12 +360,12 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { .plotRobustness.abTest <- function(ab_obj, options) { # Args: # ab_obj: ab test object - + mu_range = c(options$mu_stepsize_lower, options$mu_stepsize_upper) sigma_range = c(options$sigma_stepsize_lower, options$sigma_stepsize_upper) - + plotFunc <- function() { - + abtest::plot_robustness( x = ab_obj, mu_steps = options$mu_stepsize, @@ -370,19 +375,19 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { bftype = options$plotRobustnessBFType ) } - + return (plotFunc) } .abTestPlotPriorOnly <- function(jaspResults, options, position) { - + abTestPriorPlot <- createJaspPlot(title = gettext("Prior"), width = 530, height = 400) abTestPriorPlot$dependOn(c("normal_mu", "normal_sigma", "plotPriorType", "plotPriorOnly")) abTestPriorPlot$position <- position - + jaspResults[["abTestPriorPlot"]] <- abTestPriorPlot - + abTestPriorPlot$plotObject <- .plotPrior.abTest(options) } @@ -392,23 +397,23 @@ ABTestBayesian <- function(jaspResults, dataset, options, ...) { # # Args: # options - + prior_par <- list(mu_psi = options$normal_mu, sigma_psi = options$normal_sigma, mu_beta = 0, sigma_beta = 1) - + what <- switch( - options$plotPriorType, - "LogOddsRatio" = "logor", - "OddsRatio" = "or", - "RelativeRisk" = "rrisk", - "AbsoluteRisk" = "arisk", - "p1&p2" = "p1p2", - "p1" = "p1", - "p2" = "p2" + options$plotPriorType, + "LogOddsRatio" = "logor", + "OddsRatio" = "or", + "RelativeRisk" = "rrisk", + "AbsoluteRisk" = "arisk", + "p1&p2" = "p1p2", + "p1" = "p1", + "p2" = "p2" ) - + plotFunc <- function() { - abtest::plot_prior(prior_par = prior_par, what = what, hypothesis = "H1") + abtest::plot_prior(prior_par = prior_par, what = what, hypothesis = "H1") } - + return(plotFunc) } diff --git a/R/binomialtest.R b/R/binomialtest.R index d934033a..6ad5ebcb 100644 --- a/R/binomialtest.R +++ b/R/binomialtest.R @@ -99,7 +99,7 @@ BinomialTest <- function(jaspResults, dataset = NULL, options, ...) { total = length(data), proportion = counts / length(data), p = p, - VovkSellkeMPR = .VovkSellkeMPR(p), + VovkSellkeMPR = VovkSellkeMPR(p), lowerCI = tableResults$conf.int[1], upperCI = tableResults$conf.int[2] ) diff --git a/R/contingencytables.R b/R/contingencytables.R index 7fa895e0..773337c5 100644 --- a/R/contingencytables.R +++ b/R/contingencytables.R @@ -936,7 +936,7 @@ ContingencyTables <- function(jaspResults, dataset, options, ...) { row[["value[chiSquared]"]] <- unname(chi.result$statistic) row[["df[chiSquared]"]] <- unname(chi.result$parameter) row[["p[chiSquared]"]] <- unname(chi.result$p.value) - row[["MPR[chiSquared]"]] <- .VovkSellkeMPR(row[["p[chiSquared]"]]) + row[["MPR[chiSquared]"]] <- VovkSellkeMPR(row[["p[chiSquared]"]]) } } else row[["value[chiSquared]"]] <- "." @@ -969,7 +969,7 @@ ContingencyTables <- function(jaspResults, dataset, options, ...) { row[["df[chiSquared-cc]"]] <- unname(chi.result$parameter) pVal <- unname(chi.result$p.value) row[["p[chiSquared-cc]"]] <- pVal - row[["MPR[chiSquared-cc]"]] <- .VovkSellkeMPR(pVal) + row[["MPR[chiSquared-cc]"]] <- VovkSellkeMPR(pVal) } } else @@ -996,7 +996,7 @@ ContingencyTables <- function(jaspResults, dataset, options, ...) { pVal <- chi.result$chisq_tests[5] row[["p[likelihood]"]] <- pVal if (options$VovkSellkeMPR) - row[["MPR[likelihood]"]] <- .VovkSellkeMPR(pVal) + row[["MPR[likelihood]"]] <- VovkSellkeMPR(pVal) } } else row[["value[likelihood]"]] <- "." @@ -1218,7 +1218,7 @@ ContingencyTables <- function(jaspResults, dataset, options, ...) { else { row[["value[kTauB]"]] <- unname(chi.result$estimate) row[["p[kTauB]"]] <- chi.result$p.value - if (options$VovkSellkeMPR) row[["MPR[kTauB]"]] <- .VovkSellkeMPR(row[["p[kTauB]"]]) + if (options$VovkSellkeMPR) row[["MPR[kTauB]"]] <- VovkSellkeMPR(row[["p[kTauB]"]]) row[["statistic[kTauB]"]] <- unname(chi.result$statistic) } } else { diff --git a/R/multinomialtest.R b/R/multinomialtest.R index b3eff2c5..9805a9d9 100644 --- a/R/multinomialtest.R +++ b/R/multinomialtest.R @@ -203,7 +203,7 @@ MultinomialTest <- function(jaspResults, dataset, options, ...) { dataframe <- cbind(case = names(chisqResults), dataframe) if (options$VovkSellkeMPR) - dataframe <- cbind(dataframe, VovkSellkeMPR = .VovkSellkeMPR(dataframe$p)) + dataframe <- cbind(dataframe, VovkSellkeMPR = VovkSellkeMPR(dataframe$p)) jaspResults[["chisqTable"]]$setData(dataframe) diff --git a/R/regressionloglinear.R b/R/regressionloglinear.R index add54c46..beb2058f 100644 --- a/R/regressionloglinear.R +++ b/R/regressionloglinear.R @@ -170,7 +170,7 @@ RegressionLogLinear <- function(jaspResults, dataset = NULL , options, ...) { pVal <- as.numeric(loglm.estimates$"Pr(>Chi)"[var]) results[[ len.logreg ]]$p <- pVal if (options$VovkSellkeMPR) - results[[ len.logreg ]]$VovkSellkeMPR <- .VovkSellkeMPR(pVal) + results[[ len.logreg ]]$VovkSellkeMPR <- VovkSellkeMPR(pVal) } results[[ len.logreg ]]$resDf <- as.integer(loglm.estimates$"Resid. Df"[var]) res <- as.numeric(loglm.estimates$"Resid. Dev"[var]) @@ -227,7 +227,7 @@ RegressionLogLinear <- function(jaspResults, dataset = NULL , options, ...) { Z <- as.numeric(loglm.estimates[i,3]) p <- as.numeric(loglm.estimates[i,4]) if (options$VovkSellkeMPR) - VovkSellkeMPR <- .VovkSellkeMPR(p) + VovkSellkeMPR <- VovkSellkeMPR(p) results[[len.logreg]] <- list( name = name, Coeff = Coefficient, diff --git a/jaspFrequencies.Rproj b/jaspFrequencies.Rproj new file mode 100644 index 00000000..497f8bfc --- /dev/null +++ b/jaspFrequencies.Rproj @@ -0,0 +1,20 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/tests/figs/ABTestBayesian/abtestbayesian-posterior.svg b/tests/figs/ABTestBayesian/abtestbayesian-posterior.svg new file mode 100644 index 00000000..fd2ee2a6 --- /dev/null +++ b/tests/figs/ABTestBayesian/abtestbayesian-posterior.svg @@ -0,0 +1,54 @@ + + + + + + + + + + + +median = 0.351 +95% CI: [-1.301, 2.004] + + +Posterior +Prior +Log Odds Ratio + + + + + + + + +-3 +-2 +-1 +0 +1 +2 +3 + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +Density + diff --git a/tests/figs/ABTestBayesian/abtestbayesian-prioronly.svg b/tests/figs/ABTestBayesian/abtestbayesian-prioronly.svg new file mode 100644 index 00000000..08dc9ff4 --- /dev/null +++ b/tests/figs/ABTestBayesian/abtestbayesian-prioronly.svg @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-4 +-3 +-2 +-1 +0 +1 +2 +3 +4 + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +Log Odds Ratio +Density + diff --git a/tests/figs/ABTestBayesian/abtestbayesian-robustness.svg b/tests/figs/ABTestBayesian/abtestbayesian-robustness.svg new file mode 100644 index 00000000..ddf044eb --- /dev/null +++ b/tests/figs/ABTestBayesian/abtestbayesian-robustness.svg @@ -0,0 +1,236 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.90 +0.95 +1.00 +1.05 +1.10 + +B +F +1 +0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.05 +0.10 +0.15 +0.20 +0.25 +0.30 + + + + + + + + + +0.3 +0.4 +0.5 +0.6 +0.7 +0.8 +0.9 +1.0 + + + + + + +μ +ψ + + + + + +σ +ψ + diff --git a/tests/figs/BinomialTest/binomialtest-descriptives-1.svg b/tests/figs/BinomialTest/binomialtest-descriptives-1.svg new file mode 100644 index 00000000..8b598784 --- /dev/null +++ b/tests/figs/BinomialTest/binomialtest-descriptives-1.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 + + + + +1 +BinomialTest-descriptives-1 + diff --git a/tests/figs/BinomialTest/binomialtest-descriptives-2.svg b/tests/figs/BinomialTest/binomialtest-descriptives-2.svg new file mode 100644 index 00000000..583a8de2 --- /dev/null +++ b/tests/figs/BinomialTest/binomialtest-descriptives-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 + + + + +0 +BinomialTest-descriptives-2 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-1.svg new file mode 100644 index 00000000..52b06fc9 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-1.svg @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + +BinomialTestBayesian-prior-posterior-1-subplot-1 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-2.svg new file mode 100644 index 00000000..ad2328ef --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + +BinomialTestBayesian-prior-posterior-1-subplot-2 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-3.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-3.svg new file mode 100644 index 00000000..7906a3c8 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-3.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + +95% CI: [0.482, 0.672] +Median: 0.579 + + + + + +BinomialTestBayesian-prior-posterior-1-subplot-3 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-4.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-4.svg new file mode 100644 index 00000000..f7cd9dd6 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1-subplot-4.svg @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +2 +4 +6 +8 +10 + + + + + + + + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +1 +Population proportion + +θ +Density + + + + + + +Posterior +Prior +BinomialTestBayesian-prior-posterior-1-subplot-4 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1.svg new file mode 100644 index 00000000..9c1cfd2a --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-1.svg @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +1 + + + + + + + + +0.0 +2.0 +4.0 +6.0 +8.0 +10.0 +12.0 +Density +Population proportion + +θ + + + + + + + + + + + + + + + +B +F +10 += +0.444 +B +F +0 +1 += +2.252 +median = 0.579 +95% CI: [0.482, 0.672] + + +data|H0 + + +Posterior +Prior + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-1.svg new file mode 100644 index 00000000..39170b55 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-1.svg @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + +BinomialTestBayesian-prior-posterior-2-subplot-1 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-2.svg new file mode 100644 index 00000000..b6ae9677 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + +BinomialTestBayesian-prior-posterior-2-subplot-2 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-3.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-3.svg new file mode 100644 index 00000000..a94979b0 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-3.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + +95% CI: [0.328, 0.518] +Median: 0.421 + + + + + +BinomialTestBayesian-prior-posterior-2-subplot-3 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-4.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-4.svg new file mode 100644 index 00000000..70a60a2e --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2-subplot-4.svg @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +2 +4 +6 +8 +10 + + + + + + + + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +1 +Population proportion + +θ +Density + + + + + + +Posterior +Prior +BinomialTestBayesian-prior-posterior-2-subplot-4 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2.svg new file mode 100644 index 00000000..c7a4c1e8 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-prior-posterior-2.svg @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +1 + + + + + + + + +0.0 +2.0 +4.0 +6.0 +8.0 +10.0 +12.0 +Density +Population proportion + +θ + + + + + + + + + + + + + + + +B +F +10 += +0.444 +B +F +0 +1 += +2.252 +median = 0.421 +95% CI: [0.328, 0.518] + + +data|H0 + + +Posterior +Prior + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-1.svg new file mode 100644 index 00000000..c1e7f1a7 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-1.svg @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + +BinomialTestBayesian-sequential-analysis-1-subplot-1 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-2.svg new file mode 100644 index 00000000..d46774e0 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + +BinomialTestBayesian-sequential-analysis-1-subplot-2 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-3.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-3.svg new file mode 100644 index 00000000..53be6a07 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-3.svg @@ -0,0 +1,51 @@ + + + + + + + + + + + + + +A +n +e +c +d +o +t +a +l +E +v +i +d +e +n +c +e + +for + +H +0 +: + + + + + +BinomialTestBayesian-sequential-analysis-1-subplot-3 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-4.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-4.svg new file mode 100644 index 00000000..7c4bd29e --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1-subplot-4.svg @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + +E +v +i +d +e +n +c +e + +for + +H +0 +E +v +i +d +e +n +c +e + +for + +H +1 + + + + + + + + + + +1 + +10 +1 + +3 +1 +3 +10 +30 + + + + + + + + + + + + + + + + + + +Moderate +Anecdotal +Anecdotal +Moderate +Strong + + + + + + + +0 +20 +40 +60 +80 +100 +n +B +F +1 +0 +Evidence +BinomialTestBayesian-sequential-analysis-1-subplot-4 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1.svg new file mode 100644 index 00000000..d74bdf01 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-1.svg @@ -0,0 +1,219 @@ + + + + + + + + + + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + + + + + + + + + + + + + + + + + +A +n +e +c +d +o +t +a +l +Evidence for +H +0 +: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +E +v +i +d +e +n +c +e + +for + +H +0 +E +v +i +d +e +n +c +e + +for + +H +1 + + + + + + + + +1 + +10 +1 + +3 +1 +3 +10 +30 + + + + + + + + + + + + + + + + + +Moderate +Anecdotal +Anecdotal +Moderate +Strong + + + + + + +0 +20 +40 +60 +80 +100 +n +B +F +1 +0 +Evidence + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-1.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-1.svg new file mode 100644 index 00000000..fb23d3ed --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-1.svg @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + +BinomialTestBayesian-sequential-analysis-2-subplot-1 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-2.svg new file mode 100644 index 00000000..7b659ca5 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + +BinomialTestBayesian-sequential-analysis-2-subplot-2 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-3.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-3.svg new file mode 100644 index 00000000..9cc025ff --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-3.svg @@ -0,0 +1,51 @@ + + + + + + + + + + + + + +A +n +e +c +d +o +t +a +l +E +v +i +d +e +n +c +e + +for + +H +0 +: + + + + + +BinomialTestBayesian-sequential-analysis-2-subplot-3 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-4.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-4.svg new file mode 100644 index 00000000..53c07f8b --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2-subplot-4.svg @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + +E +v +i +d +e +n +c +e + +for + +H +0 +E +v +i +d +e +n +c +e + +for + +H +1 + + + + + + + + + + +1 + +10 +1 + +3 +1 +3 +10 +30 + + + + + + + + + + + + + + + + + + +Moderate +Anecdotal +Anecdotal +Moderate +Strong + + + + + + + +0 +20 +40 +60 +80 +100 +n +B +F +1 +0 +Evidence +BinomialTestBayesian-sequential-analysis-2-subplot-4 + diff --git a/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2.svg b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2.svg new file mode 100644 index 00000000..d74bdf01 --- /dev/null +++ b/tests/figs/BinomialTestBayesian/binomialtestbayesian-sequential-analysis-2.svg @@ -0,0 +1,219 @@ + + + + + + + + + + + + + + + + + + + + + + +B +F +0 +1 += +2.252 +B +F +1 +0 += +0.444 + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + + + + + + + + + + + + + + + + + +A +n +e +c +d +o +t +a +l +Evidence for +H +0 +: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +E +v +i +d +e +n +c +e + +for + +H +0 +E +v +i +d +e +n +c +e + +for + +H +1 + + + + + + + + +1 + +10 +1 + +3 +1 +3 +10 +30 + + + + + + + + + + + + + + + + + +Moderate +Anecdotal +Anecdotal +Moderate +Strong + + + + + + +0 +20 +40 +60 +80 +100 +n +B +F +1 +0 +Evidence + diff --git a/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-1.svg b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-1.svg new file mode 100644 index 00000000..1f843cee --- /dev/null +++ b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-1.svg @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +B +F +0 +1 += +2.994 +B +F +1 +0 += +0.334 + + + + + +ContingencyTablesBayesian-log-odds-ratio-subplot-1 + diff --git a/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-2.svg b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-2.svg new file mode 100644 index 00000000..d6582a66 --- /dev/null +++ b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-2.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +data | H0 +data | H1 + + + + + +ContingencyTablesBayesian-log-odds-ratio-subplot-2 + diff --git a/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-3.svg b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-3.svg new file mode 100644 index 00000000..4528e0c9 --- /dev/null +++ b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-3.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + +95% CI: [-1.111, 0.466] +median Log OR = -0.323 + + + + + +ContingencyTablesBayesian-log-odds-ratio-subplot-3 + diff --git a/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-4.svg b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-4.svg new file mode 100644 index 00000000..b59fc845 --- /dev/null +++ b/tests/figs/ContingencyTablesBayesian/contingencytablesbayesian-log-odds-ratio-subplot-4.svg @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +0.2 +0.4 +0.6 +0.8 +1 + + + + + + + + + + + + + + +-2 +-1.5 +-1 +-0.5 +0 +0.5 +1 +Log Odds Ratio +Density +ContingencyTablesBayesian-log-odds-ratio-subplot-4 + diff --git a/tests/figs/MultinomialTest/multinomialtest-descriptives-1.svg b/tests/figs/MultinomialTest/multinomialtest-descriptives-1.svg new file mode 100644 index 00000000..60e65da9 --- /dev/null +++ b/tests/figs/MultinomialTest/multinomialtest-descriptives-1.svg @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +5 +4 +3 +2 +1 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Observed counts +facFive +MultinomialTest-descriptives-1 + diff --git a/tests/figs/MultinomialTestBayesian/multinomialtestbayesian-multinomialbayesiandescriptivesplot.svg b/tests/figs/MultinomialTestBayesian/multinomialtestbayesian-multinomialbayesiandescriptivesplot.svg new file mode 100644 index 00000000..70dd69c8 --- /dev/null +++ b/tests/figs/MultinomialTestBayesian/multinomialtestbayesian-multinomialbayesiandescriptivesplot.svg @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 +0 + + + + + + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 +0.5 +0.6 +0.7 +Observed Proportions +contBinom +MultinomialTestBayesian-multinomialBayesianDescriptivesPlot + diff --git a/tests/figs/deps.txt b/tests/figs/deps.txt new file mode 100644 index 00000000..0f64e23e --- /dev/null +++ b/tests/figs/deps.txt @@ -0,0 +1,3 @@ +- vdiffr-svg-engine: 1.0 +- vdiffr: 0.3.1 +- freetypeharfbuzz: 0.2.5 diff --git a/tests/figs/jasp-deps.txt b/tests/figs/jasp-deps.txt new file mode 100644 index 00000000..6ee89706 --- /dev/null +++ b/tests/figs/jasp-deps.txt @@ -0,0 +1 @@ +- jaspGraphs: 0.5 diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 00000000..a744b3c8 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(jaspTools) +library(testthat) + +jaspTools::runTestsTravis(module = getwd()) diff --git a/tests/testthat/ab_data.csv b/tests/testthat/ab_data.csv new file mode 100644 index 00000000..5c27acc2 --- /dev/null +++ b/tests/testthat/ab_data.csv @@ -0,0 +1,9 @@ +y1,n1,y2,n2 +1,1,0,0 +1,1,1,1 +1,2,1,1 +1,2,2,2 +1,3,2,2 +1,3,3,3 +2,4,3,3 +2,4,3,4 diff --git a/tests/testthat/test-abtestbayesian.R b/tests/testthat/test-abtestbayesian.R new file mode 100644 index 00000000..f89dc2f9 --- /dev/null +++ b/tests/testthat/test-abtestbayesian.R @@ -0,0 +1,180 @@ +context("AB Test Bayesian") + +# abtest will fail on the first run, but not on subsequent runs. +# Clearly something requires loading (options?), but it is not fixed by loadNamespace("abtest"). +# This ensures the subsequent runs pass: +options <- jaspTools::analysisOptions("ABTestBayesian") +options$n1 <- "n1" +options$y1 <- "y1" +options$n2 <- "n2" +options$y2 <- "y2" +jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) +# Up to here. + + +test_that("Main table results match", { + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + table <- results[["results"]][["abTestBayesianTable"]][["data"]] + refTable <- list("Log odds ratio = 0", 1 , 0.517752872701024, 0.5, + "Log odds ratio > 0", 1.23201979244712 , 0.318940893382007, 0.25, + "Log odds ratio < 0", 0.630826954431096, 0.163306233916969, 0.25) + + jaspTools::expect_equal_tables(table, refTable) +}) + + +test_that("Descriptives table results match", { + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$descriptives <- TRUE + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + table <- results[["results"]][["abTestBayesianDescriptivesTable"]][["data"]] + + refTable <- list("Group 1", 2, 4, 0.500, + "Group 2", 3, 4, 0.750) + + jaspTools::expect_equal_tables(table, refTable) +}) + + +test_that("Main table results, log odds neq 0", { + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$orEqualTo1Prob <- 0.495 + options$orGreaterThan1Prob <- 0.168 + options$orLessThan1Prob <- 0.168 + options$orNotEqualTo1Prob <- 0.168 + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + table <- results[["results"]][["abTestBayesianTable"]][["data"]] + + refTable <- list("Log odds ratio = 0" , 1 , 0.51649969761886 , 0.495495495495495, + "Log odds ratio > 0" , 1.23201979244712 , 0.215969209785004, 0.168168168168168, + "Log odds ratio 0", 0.895333193589823, 0.15694910382065 , 0.168168168168168, + "Log odds ratio < 0" , 0.630826954431096, 0.110581988775487, 0.168168168168168) + + jaspTools::expect_equal_tables(table, refTable) +}) + + +test_that("Main table results, only 2 hypotheses", { + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$orEqualTo1Prob <- 0.5 + options$orGreaterThan1Prob <- 0.5 + options$orLessThan1Prob <- 0 + options$orNotEqualTo1Prob <- 0 + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + table <- results[["results"]][["abTestBayesianTable"]][["data"]] + + refTable <- list("Log odds ratio > 0", 1 , 0.55197529906147, 0.5, + "Log odds ratio = 0" , 0.811675271883202, 0.44802470093853, 0.5) + + jaspTools::expect_equal_tables(table, refTable) +}) + + +test_that("Prior plot matches", { + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$orEqualTo1Prob <- 0.5 + options$orGreaterThan1Prob <- 0.5 + options$orLessThan1Prob <- 0 + options$orNotEqualTo1Prob <- 0 + options$plotPriorOnly <- TRUE + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + + jaspTools::expect_equal_plots(testPlot, "prioronly", dir="ABTestBayesian") +}) + + +test_that("Posterior plot matches", { + skip("Needs to be verified.") + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$orEqualTo1Prob <- 0.5 + options$orGreaterThan1Prob <- 0.5 + options$orLessThan1Prob <- 0 + options$orNotEqualTo1Prob <- 0 + options$plotPriorAndPosterior <- TRUE + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + testPlot <- results[["state"]][["figures"]][[1]] + + jaspTools::expect_equal_plots(testPlot, "posterior", dir="ABTestBayesian") +}) + + +test_that("Sequential plot matches", { + skip("This test need to be verified") + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$orEqualTo1Prob <- 0.5 + options$orGreaterThan1Prob <- 0.5 + options$orLessThan1Prob <- 0 + options$orNotEqualTo1Prob <- 0 + options$plotSequentialAnalysis <- TRUE + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + testPlot <- results[["state"]][["figures"]][[1]] + + jaspTools::expect_equal_plots(testPlot, "sequential", dir="ABTestBayesian") +}) + + +test_that("plotRobustness plot matches", { + skip("Have to set a global theme.") + set.seed(0) + + options <- jaspTools::analysisOptions("ABTestBayesian") + options$n1 <- "n1" + options$y1 <- "y1" + options$n2 <- "n2" + options$y2 <- "y2" + options$orEqualTo1Prob <- 0.5 + options$orGreaterThan1Prob <- 0.5 + options$orLessThan1Prob <- 0 + options$orNotEqualTo1Prob <- 0 + options$plotRobustness <- TRUE + + results <- jaspTools::runAnalysis("ABTestBayesian", "ab_data.csv", options) + testPlot <- results[["state"]][["figures"]][[1]] + + jaspTools::expect_equal_plots(testPlot, "robustness", dir="ABTestBayesian") +}) diff --git a/tests/testthat/test-binomialtest.R b/tests/testthat/test-binomialtest.R new file mode 100644 index 00000000..56d398ea --- /dev/null +++ b/tests/testthat/test-binomialtest.R @@ -0,0 +1,31 @@ +context("Binomial Test") + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("BinomialTest") + options$variables <- "contBinom" + options$testValue <- 0.6 + options$hypothesis <- "greaterThanTestValue" + options$confidenceInterval <- TRUE + options$VovkSellkeMPR <- TRUE + results <- jaspTools::runAnalysis("BinomialTest", "test.csv", options) + table <- results[["results"]][["binomialTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list("TRUE", 1, 58, 0, 0.492841460660175, 0.696739870156555, 0.58, 100, 1, "contBinom", + "FALSE", 1, 42, 1, 0.336479745077558, 0.999903917924738, 0.42, 100, 1, "contBinom") + ) +}) + +test_that("Descriptives plots match", { + skip("This test need to be verified") + options <- jaspTools::analysisOptions("BinomialTest") + options$variables <- "contBinom" + options$descriptivesPlots <- TRUE + options$descriptivesPlotsConfidenceInterval <- 0.90 + results <- jaspTools::runAnalysis("BinomialTest", "test.csv", options) + + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "descriptives-1", dir="BinomialTest") + + testPlot <- results[["state"]][["figures"]][[2]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "descriptives-2", dir="BinomialTest") +}) diff --git a/tests/testthat/test-binomialtestbayesian.R b/tests/testthat/test-binomialtestbayesian.R new file mode 100644 index 00000000..eb4fe1d6 --- /dev/null +++ b/tests/testthat/test-binomialtestbayesian.R @@ -0,0 +1,50 @@ +context("Binomial Test Bayesian") + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("BinomialTestBayesian") + options$variables <- "contBinom" + options$bayesFactorType <- "BF01" + options$hypothesis <- "notEqualToTestValue" + options$priorA <- 1 + options$priorB <- 2 + options$testValue <- 0.2 + results <- jaspTools::runAnalysis("BinomialTestBayesian", "test.csv", options) + table <- results[["results"]][["binomTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list("TRUE", 4.32337507642424e-15, "contBinom", 58, 0, 0.58, 100, + "FALSE", 3.43240614623212e-05, "contBinom", 42, 1, 0.42, 100) + ) +}) + +test_that("Prior posterior plots match", { + options <- jaspTools::analysisOptions("BinomialTestBayesian") + options$priorA <- 1 + options$priorB <- 1 + options$testValue <- 0.5 + options$variables <- "contBinom" + options$plotPriorAndPosterior <- TRUE + options$plotPriorAndPosteriorAdditionalInfo <- TRUE + results <- jaspTools::runAnalysis("BinomialTestBayesian", "test.csv", options) + + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "prior-posterior-1", dir="BinomialTestBayesian") + + testPlot <- results[["state"]][["figures"]][[2]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "prior-posterior-2", dir="BinomialTestBayesian") +}) + +test_that("Sequential analysis plots match", { + options <- jaspTools::analysisOptions("BinomialTestBayesian") + options$priorA <- 1 + options$priorB <- 1 + options$testValue <- 0.5 + options$variables <- "contBinom" + options$plotSequentialAnalysis <- TRUE + results <- jaspTools::runAnalysis("BinomialTestBayesian", "test.csv", options) + + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "sequential-analysis-1", dir="BinomialTestBayesian") + + testPlot <- results[["state"]][["figures"]][[2]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "sequential-analysis-2", dir="BinomialTestBayesian") +}) diff --git a/tests/testthat/test-contingencytables.R b/tests/testthat/test-contingencytables.R new file mode 100644 index 00000000..ed1fba99 --- /dev/null +++ b/tests/testthat/test-contingencytables.R @@ -0,0 +1,163 @@ +context("Contingency Tables") + +# does not test +# - row/column order (ascending/descending) + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$counts <- "facFifty" + options$layers <- list(list( + name = "Layer 1", + variables = "facGender" + )) + options$countsExpected <- TRUE + options$percentagesRow <- TRUE + options$percentagesColumn <- TRUE + options$percentagesTotal <- TRUE + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabMain"]][["data"]] + jaspTools::expect_equal_tables(table, + list(0.489296636085627, 320, 394.529977794227, 0.236861584011843, 0.392638036809816, + 0.710186513629842, 495, 420.470022205773, 0.36639526276832, + 0.607361963190184, "control", "f", 0.603256846780163, 815, 815, + 0.603256846780163, 1, " % within column", "Count", "Expected count", + " % of total", " % within row", 0.510703363914373, 334, 259.470022205773, + 0.247224278312361, 0.623134328358209, 0.289813486370158, 202, + 276.529977794227, 0.149518874907476, 0.376865671641791, "experimental", + "f", 0.396743153219837, 536, 536, 0.396743153219837, 1, " % within column", + "Count", "Expected count", " % of total", " % within row", 1, + 654, 654, 0.484085862324204, 0.484085862324204, 1, 697, 697, + 0.515914137675796, 0.515914137675796, "Total", "f", 1, 1351, 1351, + 1, 1, " % within column", "Count", "Expected count", " % of total", + " % within row", 0.338688085676037, 253, 271.013344453712, 0.211009174311927, + 0.581609195402299, 0.402654867256637, 182, 163.986655546289, + 0.151793160967473, 0.418390804597701, "control", "m", 0.362802335279399, + 435, 435, 0.362802335279399, 1, " % within column", "Count", + "Expected count", " % of total", " % within row", 0.661311914323963, + 494, 475.986655546288, 0.412010008340284, 0.646596858638743, + 0.597345132743363, 270, 288.013344453712, 0.225187656380317, + 0.353403141361257, "experimental", "m", 0.6371976647206, 764, + 764, 0.6371976647206, 1, " % within column", "Count", "Expected count", + " % of total", " % within row", 1, 747, 747, 0.62301918265221, + 0.62301918265221, 1, 452, 452, 0.37698081734779, 0.37698081734779, + "Total", "m", 1, 1199, 1199, 1, 1, " % within column", "Count", "Expected count", + " % of total", " % within row", 0.408993576017131, 573, 686.764705882353, + 0.224705882352941, 0.4584, 0.589208006962576, 677, 563.235294117647, + 0.265490196078431, 0.5416, "control", "Total", 0.490196078431372, + 1250, 1250, 0.490196078431373, 1, " % within column", "Count", + "Expected count", " % of total", " % within row", 0.591006423982869, + 828, 714.235294117647, 0.324705882352941, 0.636923076923077, + 0.410791993037424, 472, 585.764705882353, 0.185098039215686, + 0.363076923076923, "experimental", "Total", 0.509803921568627, + 1300, 1300, 0.509803921568627, 1, " % within column", "Count", + "Expected count", " % of total", " % within row", 1, 1401, 1401, + 0.549411764705882, 0.549411764705882, 1, 1149, 1149, 0.450588235294118, + 0.450588235294118, "Total", "Total", 1, 2550, 2550, 1, 1, " % within column", + "Count", "Expected count", " % of total", " % within row") + ) +}) + +test_that("Multiple row and column variables give multiple main tables", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- c("facExperim", "facGender") + options$columns <- c("contBinom", "facFive") + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + + pairs <- list( + c("facExperim", "contBinom"), + c("facExperim", "facFive"), + c("facGender", "contBinom"), + c("facGender", "facFive") + ) + + for (i in 1:4) { + rows <- results[["results"]][[paste0("container", i)]][["collection"]][[paste0("container", i, "_crossTabMain")]][["schema"]][["fields"]][[1]][["name"]] + cols <- results[["results"]][[paste0("container", i)]][["collection"]][[paste0("container", i, "_crossTabMain")]][["schema"]][["fields"]][[2]][["overTitle"]] + expect_identical(c(rows, cols), pairs[[i]], label=paste("Table", i)) + } +}) + +test_that("Chi-Squared test table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$counts <- "facFifty" + options$chiSquared <- TRUE + options$chiSquaredContinuityCorrection <- TRUE + options$likelihoodRatio <- TRUE + options$VovkSellkeMPR <- TRUE + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabChisq"]][["data"]] + jaspTools::expect_equal_tables(table, + list("N", "", "", "", 2550, + "", 82.0397085317219, 1, 1.33379878452991e-19, 63462127883801120, + " continuity correction", 81.3201582621313, 1, 1.91958529099645e-19, 44468347240355080, + "Likelihood ratio", 82.4643894680383, 1, 0, "") + ) +}) + +test_that("Nominal table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$contingencyCoefficient <- TRUE + options$phiAndCramersV <- TRUE + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabNominal"]][["data"]] + jaspTools::expect_equal_tables(table, + list("Contingency coefficient", 0.0807792391722019, "Phi-coefficient", + 0.0810440898473108, "Cramer's V ", 0.0810440898473108) + ) +}) + +test_that("Log Odds Ratio table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$oddsRatio <- TRUE + options$oddsRatioConfidenceIntervalInterval <- 0.90 + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabLogOdds"]][["data"]] + jaspTools::expect_equal_tables(table, + list("Odds ratio", -0.329205575243527, -0.998167649205055, 0.339756498718001,"", + "Fisher's exact test ", -0.325882968750928, -1.07370478788709, + 0.415368461868818, 0.5435617) + ) +}) + +test_that("Ordinal Gamma table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$gamma <- TRUE + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabGamma"]][["data"]] + jaspTools::expect_equal_tables(table, + list(-0.163132137030995, 0.197938461395245, -0.551084392520947, 0.224820118458957) + ) +}) + +test_that("Kendall's Tau table results match", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$kendallsTauB <- TRUE + options$VovkSellkeMPR <- TRUE + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabKendallTau"]][["data"]] + jaspTools::expect_equal_tables(table, + list(-0.0810440898473108, 0.420024632711394, 1, -0.806378512498144) + ) +}) + +test_that("Analysis handles errors", { + options <- jaspTools::analysisOptions("ContingencyTables") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$counts <- "contNormal" + results <- jaspTools::runAnalysis("ContingencyTables", "test.csv", options) + errorMsg <- results[["results"]][["errorMessage"]] + expect_is(errorMsg, "character") +}) diff --git a/tests/testthat/test-contingencytablesbayesian.R b/tests/testthat/test-contingencytablesbayesian.R new file mode 100644 index 00000000..4cf891f3 --- /dev/null +++ b/tests/testthat/test-contingencytablesbayesian.R @@ -0,0 +1,165 @@ +context("Bayesian Contingency Tables") + +# does not test +# - row/column order (ascending/descending) +# - different hypothesis options +# - bftype (01, 10, log) +# - log odds for different sampling models +# - error handling in plots + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$counts <- "facFifty" + options$layers <- list(list( + name = "Layer 1", + variables = "facGender") + ) + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_crossTabMain"]][["data"]] + jaspTools::expect_equal_tables(table, + list("TRUE", 320, 495, "control", "f", 815, 334, 202, "experimental", + "f", 536, "TRUE", 654, 697, "Total", "f", 1351, + "TRUE", 253, 182, "control", "m", 435, 494, 270, "experimental", + "m", 764, "TRUE", 747, 452, "Total", "m", 1199, + "TRUE", 573, 677, "control", "Total", 1250, 828, 472, + "experimental", "Total", 1300, "TRUE", 1401, 1149, + "Total", "Total", 2550) + ) +}) + +test_that("Multiple row and column variables give multiple main tables", { + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- c("facExperim", "facGender") + options$columns <- c("contBinom", "facFive") + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + + pairs <- list( + c("facExperim", "contBinom"), + c("facExperim", "facFive"), + c("facGender", "contBinom"), + c("facGender", "facFive") + ) + + for (i in 1:4) { + rows <- results[["results"]][[paste0("container", i)]][["collection"]][[paste0("container", i, "_crossTabMain")]][["schema"]][["fields"]][[1]][["name"]] + cols <- results[["results"]][[paste0("container", i)]][["collection"]][[paste0("container", i, "_crossTabMain")]][["schema"]][["fields"]][[2]][["overTitle"]] + expect_identical(c(rows, cols), pairs[[i]], label=paste("Table", i)) + } +}) + +test_that("Bayesian Contingency Tables Tests table results match", { + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$priorConcentration <- 1.5 + + samplingModels <- c("poisson", "jointMultinomial", "independentMultinomialRowsFixed", + "independentMultinomialColumnsFixed", "hypergeometric") + refTables <- list( + poisson = list("BF Poisson", "N", 100, 0.523118843924781), + jointMultinomial = list("BF joint multinomial", "N", 100, 0.440084106793853), + independentMultinomialRowsFixed = list("BF independent multinomial", "N", 100, 0.35545254779504), + independentMultinomialColumnsFixed = list("BF independent multinomial", "N", 100, 0.364069579256729), + hypergeometric = list("BF hypergeometric", "N", 100, 0.269648117146104) + ) + + for (samplingModel in samplingModels) { + options$samplingModel <- samplingModel + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_contTabBasBF"]][["data"]] + jaspTools::expect_equal_tables(table, refTables[[samplingModel]], label=paste("Sampling model", samplingModel)) + } +}) + +test_that("Bayesian Contingency Tables Tests table results match - different hypotheses", { + set.seed(0) + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$priorConcentration <- 1.5 + + samplingModels <- c("poisson", "jointMultinomial", "independentMultinomialRowsFixed", + "independentMultinomialColumnsFixed") + hypotheses <- c("groupsNotEqual", "groupOneGreater", "groupTwoGreater") + + refTables <- list( + poisson = list( + groupsNotEqual = list("BF Poisson", "N", 100, 0.523118843924781), + groupOneGreater = list("BF Poisson", "N", 100, 0.224941102887656), + groupTwoGreater = list("BF Poisson", "N", 100, 0.818576366973497)), + jointMultinomial = list( + groupsNotEqual = list("BF joint multinomial", "N", 100, 0.440084106793853), + groupOneGreater = list("BF joint multinomial", "N", 100, 0.181490685641785), + groupTwoGreater = list("BF joint multinomial", "N", 100, 0.683978718779006)), + independentMultinomialRowsFixed = list( + groupsNotEqual = list("BF independent multinomial", "N", 100, 0.35545254779504), + groupOneGreater = list("BF independent multinomial", "N", 100, 0.149361160583476), + groupTwoGreater = list("BF independent multinomial", "N", 100, 0.560761939401455)), + independentMultinomialColumnsFixed = list( + groupsNotEqual = list("BF independent multinomial", "N", 100, 0.364069579256729), + groupOneGreater = list("BF independent multinomial", "N", 100, 0.153564548530488), + groupTwoGreater = list("BF independent multinomial", "N", 100, 0.571734867264767)) + ) + + for (samplingModel in samplingModels) { + options$samplingModel <- samplingModel + for(hypothesis in hypotheses) { + options$hypothesis <- hypothesis + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options, view = FALSE) + table <- results[["results"]][["container1"]][["collection"]][["container1_contTabBasBF"]][["data"]] + #makeTestTable(table) + jaspTools::expect_equal_tables(table, refTables[[samplingModel]][[hypothesis]], label=paste("Sampling model", samplingModel, "; hypothesis", hypothesis)) + } + } +}) + +test_that("Log Odds Ratio table results match", { + set.seed(0) + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$oddsRatio <- TRUE + options$oddsRatioCredibleIntervalInterval <- 0.90 + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_contTabBasLogOdds"]][["data"]] + jaspTools::expect_equal_tables(table, + list(-0.322626350332064, -0.98462219921522, 0.339369498551093) + ) +}) + +test_that("Cramer's V table results match", { + set.seed(0) + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$effectSize <- TRUE + options$effectSizeCredibleIntervalInterval <- 0.90 + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + table <- results[["results"]][["container1"]][["collection"]][["container1_contTabBasCramersV"]][["data"]] + jaspTools::expect_equal_tables(table, + list(0.0698837782777569, 1.02065076979155e-16, 0.216657039422164) + ) +}) + +test_that("Log Odds Ratio Plot matches", { + set.seed(0) + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$plotPosteriorOddsRatio <- TRUE + options$plotPosteriorOddsRatioAdditionalInfo <- TRUE + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + jaspTools::expect_equal_plots(results[["state"]][["figures"]][[1]][["obj"]], "log-odds-ratio", dir="ContingencyTablesBayesian") +}) + +test_that("Analysis handles errors", { + options <- jaspTools::analysisOptions("ContingencyTablesBayesian") + options$rows <- "facExperim" + options$columns <- "contBinom" + options$counts <- "contNormal" + results <- jaspTools::runAnalysis("ContingencyTablesBayesian", "test.csv", options) + errorMsg <- results[["results"]][["errorMessage"]] + expect_is(errorMsg, "character") +}) diff --git a/tests/testthat/test-multinomialtest.R b/tests/testthat/test-multinomialtest.R new file mode 100644 index 00000000..e90ed77f --- /dev/null +++ b/tests/testthat/test-multinomialtest.R @@ -0,0 +1,92 @@ +context("Multinomial Test") + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("MultinomialTest") + options$VovkSellkeMPR <- TRUE + options$countProp <- "descProps" + options$descriptives <- TRUE + options$factor <- "facOutlier" + options$hypothesis <- "expectedProbs" + options$tableWidget <- list( + list( + levels = list("f1", "f2", "f3", "totallyridiculoussuperlongfactorme"), + name = "H1", + values = list(50, 42, 5, 3) + ) + ) + results <- jaspTools::runAnalysis("MultinomialTest", + "test.csv", options) + maintable <- results[["results"]][["chisqTable"]][["data"]] + desctable <- results[["results"]][["descriptivesTable"]][["data"]] + + expected <- jaspTools:::collapseTestTable( + list(list(case = "H\u2080 (a)", + chisquare = 5.72, + df = 3, + p = 0.126056548007017, + VovkSellkeMPR = 1.40914224189199))) + + jaspTools::expect_equal_tables(maintable, expected) + jaspTools::expect_equal_tables(desctable, + list("f1", 0.49, 0.5, + "f2", 0.49, 0.42, + "f3", 0.01, 0.05, + "totallyridiculoussuperlongfactorme", 0.01, 0.03)) +}) + +test_that("Descriptives plot matches", { + options <- jaspTools::analysisOptions("MultinomialTest") + options$factor <- "facFive" + options$descriptivesPlot <- TRUE + results <- jaspTools::runAnalysis("MultinomialTest", "test.csv", options) + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "descriptives-1", dir="MultinomialTest") +}) + +test_that("Analysis handles errors - Negative Values", { + options <- jaspTools::analysisOptions("MultinomialTest") + options$factor <- "facExperim" + options$counts <- "contNormal" + options$tableWidget <- list( + list( + levels = list("control", "experimental"), + name = "H1", + values = list(50, 50) + ) + ) + results <- jaspTools::runAnalysis("MultinomialTest", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - wrong levels", { + options <- jaspTools::analysisOptions("MultinomialTest") + options$factor <- "facExperim" + options$counts <- "debSame" + options$tableWidget <- list( + list( + levels = list("control", "experimental"), + name = "H1", + values = list(50, 50) + ) + ) + results <- jaspTools::runAnalysis("MultinomialTest", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - Infinities", { + options <- jaspTools::analysisOptions("MultinomialTest") + options$factor <- "facExperim" + options$counts <- "debInf" + options$tableWidget <- list( + list( + levels = list("control", "experimental"), + name = "H1", + values = list(50, 50) + ) + ) + results <- jaspTools::runAnalysis("MultinomialTest", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) \ No newline at end of file diff --git a/tests/testthat/test-multinomialtestbayesian.R b/tests/testthat/test-multinomialtestbayesian.R new file mode 100644 index 00000000..7ef80588 --- /dev/null +++ b/tests/testthat/test-multinomialtestbayesian.R @@ -0,0 +1,87 @@ +context("Multinomial Test Bayesian") + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("MultinomialTestBayesian") + options$factor <- "contBinom" + options$bayesFactorType <- "BF10" + options$hypothesis <- "multinomialTest" + options$priorCounts <- list(list(levels = paste0('level', 1:2), + name = c('Counts'), + values = rep(1, 2))) + results <- jaspTools::runAnalysis("MultinomialTestBayesian", "test.csv", options) + table <- results[["results"]][["multinomialTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list(0.44414455326433, "Multinomial", 2) + ) +}) + +test_that("Descriptives table results match", { + options <- jaspTools::analysisOptions("MultinomialTestBayesian") + options$factor <- "debString" + options$countProp <- "descProps" + options$credibleInterval <- TRUE + options$descriptives <- TRUE + options$credibleIntervalInterval <- 0.10 + options$priorCounts <- list(list(levels =letters, + name = c('Counts'), + values = rep(1, length(letters)))) + results <- jaspTools::runAnalysis("MultinomialTestBayesian", "test.csv", options) + table <- results[["results"]][["multinomialDescriptivesTable"]][["data"]] + jaspTools::expect_equal_tables(table[[1]], + list(0.0384615384615385, "a", 0.0439729779027027, 0.05, 0.0594407448532589) + ) +}) + +test_that("Descriptives plots match", { + options <- jaspTools::analysisOptions("MultinomialTestBayesian") + options$factor <- "contBinom" + options$countProp <- "descProps" + options$credibleInterval <- TRUE + options$descriptivesPlot <- TRUE + options$priorCounts <- list(list(levels = c("0", "1"), + name = c('Counts'), + values = rep(1, 2))) + results <- jaspTools::runAnalysis("MultinomialTestBayesian", "test.csv", options) + testPlot <- results[["state"]][["figures"]][[1]][["obj"]] + jaspTools::expect_equal_plots(testPlot, "multinomialBayesianDescriptivesPlot", dir="MultinomialTestBayesian") +}) + +test_that("Bayesian Multinomial Test table results match in short data format", { + options <- jaspTools::analysisOptions("MultinomialTestBayesian") + options$factor <- "Month" + options$counts <- "Stress.frequency" + options$tableWidget <- list(list(levels = c("1", "2", "3", "4", "5", "6", "7", "8", + "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"), name = "H₀ (a)", + values = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1))) + options$priorCounts <- list(list(levels = c("1", "2", "3", "4", "5", "6", "7", "8", + "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"), name = "Counts", + values = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1))) + set.seed(1) + results <- jaspTools::runAnalysis("MultinomialTestBayesian", "Memory of Life Stresses.csv", options) + table <- results[["results"]][["multinomialTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list(27.1062505863656, "Multinomial", 18)) +}) + +test_that("Descriptives table correctly shows reordered factor levels", { + options <- jaspTools::analysisOptions("MultinomialTestBayesian") + options$factor <- "Month" + options$counts <- "Stress.frequency" + options$exProbVar <- "Expected.counts" + options$descriptives <- TRUE + options$tableWidget <- list(list(levels = c("3", "1", "2", "4", "5", "6", "7", "8", + "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"), name = "H₀ (a)", + values = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1))) + options$priorCounts <- list(list(levels = c("3", "1", "2", "4", "5", "6", "7", "8", + "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"), name = "Counts", + values = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1))) + results <- jaspTools::runAnalysis("MultinomialTestBayesian", "Memory of Life Stresses.csv", options) + table <- results[["results"]][["multinomialDescriptivesTable"]][["data"]] + jaspTools::expect_equal_tables(table[1:4], list(7, 3, 14, 17, 1, 15, 5, 2, 11, 15, 4, 17)) +}) + + diff --git a/tests/testthat/test-regressionloglinear.R b/tests/testthat/test-regressionloglinear.R new file mode 100644 index 00000000..57f4016d --- /dev/null +++ b/tests/testthat/test-regressionloglinear.R @@ -0,0 +1,96 @@ +context("Log-Linear Regression") + +test_that("Main table results match", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$counts <- "facFifty" + options$factors <- c("contBinom", "facGender") + options$modelTerms <- list( + list(components="contBinom"), + list(components="facGender"), + list(components=c("contBinom", "facGender")) + ) + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + table <- results[["results"]][["AnovaTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list(" ", " ", "NULL", " ", 936.356249443911, 99, 9.73545292814663, + 1, "contBinom", 0.00180747470901472, 926.620796515764, 98, 7.02546792150429, + 1, "facGender", 0.00803584702758609, 919.59532859426, 97, 0.769509416901883, + 1, "contBinomfacGender", + 0.380368860922424, 918.825819177358, 96) + ) +}) + +test_that("Coefficients table matches", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$counts <- "facFifty" + options$factors <- c("contBinom", "facFive") + options$modelTerms <- list( + list(components="contBinom"), + list(components="facFive"), + list(components=c("contBinom", "facFive")) + ) + options$regressionCoefficientsEstimates <- TRUE + options$regressionCoefficientsConfidenceIntervals <- TRUE + options$regressionCoefficientsConfidenceIntervalsInterval <- 0.95 + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + table <- results[["results"]][["CoefficientsTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list("(Intercept)", 3.36441813015886, 0.0536828127084252, 3.25920175066154, + 3.46963450965618, 62.6721656414034, 0, "contBinom = 1", -0.63167531645938, + 0.104937579028612, -0.837349191980285, -0.426001440938476, -6.01953392013314, + 1.74920000092307e-09, "facFive = 2", -0.379859157202991, 0.0921944777734458, + -0.560557013212423, -0.199161301193559, -4.1201942499901, 3.78553110594005e-05, + "facFive = 3", -0.343993244014492, 0.0833479734033365, -0.507352270069434, + -0.18063421795955, -4.1271938592897, 3.67216813648207e-05, "facFive = 4", + -0.250902820948484, 0.085593742903271, -0.418663474340876, -0.0831421675560923, + -2.93132199198285, 0.00337522739489266, "facFive = 5", -0.0685812641545293, + 0.0731503942633908, -0.211953402365681, 0.0747908740566221, + -0.937537860802095, 0.348481958057894, "contBinom = 1*facFive = 2", + 0.992224258514625, 0.140836788661736, 0.716189225039344, 1.26825929198991, + 7.04520649713029, 1.85186666239764e-12, "contBinom = 1*facFive = 3", + 1.10775799178149, 0.137349243064596, 0.838558422071047, 1.37695756149194, + 8.06526462807306, 7.30773999647178e-16, "contBinom = 1*facFive = 4", + 0.935886690862371, 0.136875960028964, 0.667614738856258, 1.20415864286848, + 6.83748037759394, 8.05981239553115e-12, "contBinom = 1*facFive = 5", + 0.703134280441524, 0.142750071638859, 0.423349281238848, 0.982919279644201, + 4.92563171681182, 8.40882439228307e-07) + ) +}) + +test_that("Analysis handles errors - infinity", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$factors <- "facGender" + options$counts <- "debInf" + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - missing values (factors)", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$factors <- "debBinMiss20" + options$modelTerms <- list( + list(components="debBinMiss20") + ) + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - missing values (counts)", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$factors <- "contBinom" + options$counts <- "debMiss30" + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - negatives", { + options <- jaspTools::analysisOptions("RegressionLogLinear") + options$factors <- "facGender" + options$counts <- "contNormal" + results <- jaspTools::runAnalysis("RegressionLogLinear", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) \ No newline at end of file diff --git a/tests/testthat/test-regressionloglinearbayesian.R b/tests/testthat/test-regressionloglinearbayesian.R new file mode 100644 index 00000000..317e8598 --- /dev/null +++ b/tests/testthat/test-regressionloglinearbayesian.R @@ -0,0 +1,115 @@ +context("Bayesian Log-Linear Regression") + +initOpts <- function() { + options <- jaspTools::analysisOptions("RegressionLogLinearBayesian") + options$sampleMode <- "manual" + options$fixedSamplesNumber <- 100 + return(options) +} + +test_that("Main table results match", { + set.seed(0) + options <- initOpts() + options$counts <- "facFifty" + options$factors <- c("contBinom", "facGender") + options$modelTerms <- list( + list(components="contBinom"), + list(components="facGender"), + list(components=c("contBinom", "facGender")) + ) + options$priorScale <- 1 + options$priorShape <- 0 + options$maxModels <- 2 + options$posteriorProbabilityCutOff <- 0.001 + results <- jaspTools::runAnalysis("RegressionLogLinearBayesian", "test.csv", options) + table <- results[["results"]][["Container"]][["collection"]][["Container_MainTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list(1, "contBinom + facGender", 0.963333333333333, 1, 2, + "contBinom + facGender + contBinomfacGender", + 0.0366666666666667, 0.0380622837370242) + ) +}) + +test_that("General summary statistics table matches", { + set.seed(0) + options <- initOpts() + options$factors <- c("contBinom", "facFive") + options$modelTerms <- list( + list(components="contBinom"), + list(components="facFive"), + list(components=c("contBinom", "facFive")) + ) + options$regressionCoefficientsEstimates <- TRUE + options$regressionCoefficientsCredibleIntervals <- TRUE + options$regressionCoefficientsCredibleIntervalsInterval <- 0.90 + results <- jaspTools::runAnalysis("RegressionLogLinearBayesian", "test.csv", options) + table <- results[["results"]][["Container"]][["collection"]][["Container_SummaryTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list("(Intercept)", 1, 2.28941355597128, 0.0114477565469203, 2.12177466183418, + 2.45495750281602, "contBinom = 0", 1, 0.0303566571915708, 0.00356265251723887, + -0.0378564465631073, 0.144271031109649, "facFive = 1", 1, -0.00552214172853799, + 0.00720162344071968, -0.130345670875483, 0.138580850953902, + "facFive = 2", 1, -0.00240692298068409, 0.00784162472732633, + -0.121705317175139, 0.165537141029262, "facFive = 3", 1, 0.00293548076176623, + 0.00745020526106448, -0.0900898563442242, 0.187558384342406, + "facFive = 4", 1, 0.00138448207875663, 0.00779485819569601, + -0.122842441315205, 0.155851155312602, "contBinom = 0*facFive = 1", + 0.571428571428571, 0.00197519410432235, 0.00569094808602158, + -0.125500914290167, 0.103584632765005, "contBinom = 0*facFive = 2", + 0.571428571428571, -0.0270367686155361, 0.00624820819787772, + -0.180746332353481, 0.0610871410555307, "contBinom = 0*facFive = 3", + 0.571428571428571, 0.00343309539396836, 0.00598953855547496, + -0.145370964229942, 0.0973020746027855, "contBinom = 0*facFive = 4", + 0.571428571428571, -0.0218844778500034, 0.00501003480774965, + -0.131413707533984, 0.101000867077934) + ) +}) + +test_that("Submodel summary statistics table matches", { + set.seed(0) + options <- initOpts() + options$factors <- c("contBinom", "facFive") + options$modelTerms <- list( + list(components="contBinom"), + list(components="facFive"), + list(components=c("contBinom", "facFive")) + ) + options$regressionCoefficientsSubmodel <- TRUE + options$regressionCoefficientsSubmodelCredibleIntervals <- TRUE + options$regressionCoefficientsSubmodelEstimates <- TRUE + options$regressionCoefficientsSubmodelNo <- 2 + results <- jaspTools::runAnalysis("RegressionLogLinearBayesian", "test.csv", options) + table <- results[["results"]][["Container"]][["collection"]][["Container_SubSummaryTable"]][["data"]] + jaspTools::expect_equal_tables(table, + list("(Intercept)", 2.29560729883006, 0.00945972825329099, 2.12809954463567, + 2.52220705393406, "contBinom = 0", 0.045757962353209, 0.00487119274553831, + -0.0378564465631073, 0.235518792759572, "facFive = 1", -0.00411654631578624, + 0.00919225838347475, -0.189678327954162, 0.225524507296169, + "facFive = 2", -0.00755884938209783, 0.0107688538955323, -0.230464286621307, + 0.203303292163532, "facFive = 3", 0.00693535371456428, 0.00958840576151784, + -0.151633708350598, 0.242411127225971, "facFive = 4", 0.00306711588492548, + 0.0107120223942474, -0.234264000793185, 0.230287649489829) + ) +}) + +test_that("Analysis handles errors - Infinity", { + set.seed(0) + options <- initOpts() + options$factors <- c("contBinom", "facFive") + options$counts <- "debInf" + results <- jaspTools::runAnalysis("RegressionLogLinearBayesian", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +}) + +test_that("Analysis handles errors - Missing values (factor)", { + set.seed(0) + options <- initOpts() + options$factors <- c("debBinMiss20", "contBinom") + options$modelTerms <- list( + list(components="debBinMiss20") + ) + results <- jaspTools::runAnalysis("RegressionLogLinearBayesian", "test.csv", options) + status <- results[["status"]] + expect_identical(status, "validationError") +})