Skip to content

Commit

Permalink
fix bug with logical sel in nlpMarginal (#16)
Browse files Browse the repository at this point in the history
* fix test names

* add test checking bool and int give same result

git-svn-id: svn+ssh://scm.r-forge.r-project.org/svnroot/mombf/pkg/mombf@413 edb9625f-4e0d-4859-8d74-9fd3b1da38cb
  • Loading branch information
oriolabril committed Nov 7, 2019
1 parent 6eadf08 commit b49c645
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion R/nlpMarginal.R
Expand Up @@ -43,7 +43,7 @@ nlpMarginal <- function(
r= tmp$r; prior= tmp$prior; priorgr= tmp$priorgr; tau=tmp$tau; taugroup=tmp$taugroup; alpha=tmp$alpha; lambda=tmp$lambda; taualpha=tmp$taualpha; fixatanhalpha=tmp$fixatanhalpha

if (!is_formula) {
check_sel_groups(sel, groups)
sel <- check_sel_groups(sel, groups)
sel <- as.integer(sel-1); nsel <- as.integer(length(sel))
} else {
if (!missing(sel)) warning("y is of type formula: ignoring sel argument")
Expand All @@ -61,6 +61,7 @@ check_sel_groups <- function(sel, groups) {
if (any(sel > p)) stop("found index in sel larger than ncol(x). Please make sure all indexes refer to existing variables")
invsel <- seqp[!(seqp %in% sel)]
if (any(groups[sel] %in% groups[invsel])) stop("selected indexes incompatible with defined groups. Make sure each group is selected or discarded at once")
return(sel)
}

##############################################################################################
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-nlpMarginal-groups.R
Expand Up @@ -11,9 +11,9 @@ patrick::with_parameters_test_that(
},
patrick::cases(
sel_outofbounds=list(sel=c(1,7,20), error_msg="sel larger than"),
sel_outofbounds=list(sel=c(1,2,7), error_msg="incompatible with .+ groups"),
sel_outofbounds=list(sel=c(2,3,4,7,8), error_msg=NA),
sel_outofbounds=list(sel=c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE, FALSE), error_msg=NA)
sel_incompatible_groups=list(sel=c(1,2,7), error_msg="incompatible with .+ groups"),
sel_good_int=list(sel=c(2,3,4,7,8), error_msg=NA),
sel_good_bool=list(sel=c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE, FALSE), error_msg=NA)
)
)

Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-nlpMarginal.R
Expand Up @@ -87,3 +87,17 @@ patrick::with_parameters_test_that(
emomprior=list(pCoef=emomprior(tau=0.328), pSkew=emomprior(tau=0.3), expected_max=-38.85072, expected_all=-47.06661)
)
)

test_that(
"logical or integer sel give equal results", {
pVar <- igprior(alpha=0.01, lambda=0.01)
pCoef=momprior(tau=0.328, r=1)
ans_int <- nlpMarginal(
theta3_truth_idx, y3, X3, family="normal", priorCoef=pCoef, priorVar=pVar
)
ans_bool <- nlpMarginal(
theta3_truth_bool, y3, X3, family="normal", priorCoef=pCoef, priorVar=pVar
)
expect_equal(ans_int, ans_bool)
}
)

0 comments on commit b49c645

Please sign in to comment.