Skip to content

Commit

Permalink
Release fixes (#179)
Browse files Browse the repository at this point in the history
* make 2x2 error consistent between bayes and freq

* bayesian log-linear reg. uses first level as the reference level

* more user friendly error message

* remove base:: and use nlevels

Co-authored-by: Don van den Bergh <[email protected]>

---------

Co-authored-by: Don van den Bergh <[email protected]>
  • Loading branch information
Kucharssim and vandenman authored Nov 29, 2023
1 parent fe71901 commit 79d1735
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
4 changes: 2 additions & 2 deletions R/contingencytablesbayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ ContingencyTablesBayesianInternal <- function(jaspResults, dataset = NULL, optio

if (!identical(dim(counts.matrix), as.integer(c(2,2)))) {
message <- gettext("Odds ratio restricted to 2 x 2 tables")
analysisContainer[["contTabBasLogOdds"]]$addFootnote(message)

analysisContainer[["contTabBasLogOdds"]]$setError(message)
return()
} else if ( options$samplingModel == "hypergeometric") {
row[["value[oddsRatio]"]] <- NaN
row[["low[oddsRatio]"]] <- row[["up[oddsRatio]"]] <- "" #Really? What order is this evaluated in?
Expand Down
23 changes: 17 additions & 6 deletions R/regressionloglinearbayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ RegressionLogLinearBayesianInternal <- function(jaspResults, dataset = NULL, opt

# Preprocessing functions
.basRegLogLinReadData <- function(dataset, options) {
if (!is.null(dataset))
return(dataset)
else {
if (is.null(dataset)) {
counts <- factors <- NULL
if(options$count != "")
counts <- options$count
if(length(options$modelTerms) > 0)
factors <- options$factors
return(.readDataSetToEnd(columns.as.factor = factors,
columns.as.numeric = counts))
dataset <- .readDataSetToEnd(columns.as.factor = factors,
columns.as.numeric = counts)

# conting uses the last level as the reference level,
# but elsewhere we use the first level instead.
# So, we shift the first level to be the last level to keep the output consistent
for (fac in factors) {
var <- dataset[[fac]]
if (nlevels(var) > 1) {
lev <- levels(var)
dataset[[fac]] <- factor(var, levels = c(lev[-1], lev[1]))
}
}
}

return(dataset)
}

.basRegLogLinCheckErrors <- function(dataset, options) {
Expand Down Expand Up @@ -164,7 +175,7 @@ RegressionLogLinearBayesianInternal <- function(jaspResults, dataset = NULL, opt
if(grepl(pattern = "the leading minor of order [0-9]+ is not positive definite", x = msg)) {
msg <- gettext("Cannot compute the results; a numerical error occurred during sampling. Try to change (e.g., simplify) the model or adjust priors.")
}
stop(gettextf("R Package error: %s", msg))
stop(gettextf("R package 'conting' error: %s <br> It is possible that a numerical error occured during sampling. Try to change (e.g., simplify) the model, adjust priors, change seed, or change the number of MCMC samples.", msg))
}
}

Expand Down
57 changes: 31 additions & 26 deletions tests/testthat/test-regressionloglinearbayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ test_that("General summary statistics table matches", {
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)
list("(Intercept)", 2.12746929269443, 2.28118363693666, 1, 0.00959232762655234,
2.43233357540468, "contBinom = 1", -0.1140552657318, -0.0335221198542702,
1, 0.002756395445979, 0.0475020520551498, "facFive = 2", -0.195453489777316,
-0.00282655167800188, 1, 0.0102248921960175, 0.155047320962659,
"facFive = 3", -0.164264352414958, 0.000154400018643484, 1,
0.00940048687056697, 0.161656677341903, "facFive = 4", -0.14973534001884,
0.00369626302015217, 1, 0.00974587350979484, 0.189539723263921,
"facFive = 5", -0.155273680656904, 0.0016839574489477, 1, 0.0108516685524461,
0.166831941585999, "contBinom = 1*facFive = 2", -0.0819863860420921,
0.0507141965203517, 0.447023809523808, 0.0117408712962367, 0.225608774669159,
"contBinom = 1*facFive = 3", -0.172855306187846, -0.0133761570465655,
0.447023809523808, 0.00900801924647208, 0.144539535306823, "contBinom = 1*facFive = 4",
-0.0818337842044022, 0.0427001178126396, 0.447023809523808,
0.00995671151898941, 0.241519319460282, "contBinom = 1*facFive = 5",
-0.289531236785577, -0.0761428540661306, 0.447023809523808,
0.0132782059648042, 0.0542974169718703)
)
})

Expand All @@ -81,15 +80,21 @@ test_that("Submodel summary statistics table matches", {
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)
)
list("(Intercept)", 2.12746929269443, 2.27823739725555, 0.00954231418605421,
2.47945144261042, "contBinom = 1", -0.152571436550678, -0.0308665527935553,
0.00251737809993112, 0.0428060084103773, "facFive = 2", -0.172121681790293,
0.00224525307377201, 0.00878383066077853, 0.230919007122397,
"facFive = 3", -0.196710458257331, -0.0044781058298241, 0.00835331749889998,
0.208709120970735, "facFive = 4", -0.241047231509551, 0.00449593617720325,
0.00801996356268645, 0.162572205036627, "facFive = 5", -0.226987657684912,
0.000215113407450374, 0.011947132025284, 0.232769962304245,
"contBinom = 1*facFive = 2", -0.153533409328021, 0.0507141965203517,
0.0117408712962367, 0.307061040408055, "contBinom = 1*facFive = 3",
-0.213739201091917, -0.0133761570465655, 0.00900801924647208,
0.184451333453313, "contBinom = 1*facFive = 4", -0.148024819514679,
0.0427001178126396, 0.00995671151898941, 0.248503021545585,
"contBinom = 1*facFive = 5", -0.344769999413831, -0.0761428540661306,
0.0132782059648042, 0.0669949682186052))
})

test_that("Analysis handles errors - Infinity", {
Expand Down

0 comments on commit 79d1735

Please sign in to comment.