Skip to content

Commit

Permalink
Fixed bug in confint.boot related to car::Confint
Browse files Browse the repository at this point in the history
  • Loading branch information
droglenc committed Apr 8, 2022
1 parent ad2a65f commit 98cf3ca
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 38 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Updated `pkgdown.yaml` GitHub action to [v2](https://github.com/r-lib/actions/tree/v2-branch/examples#build-pkgdown-site). Changed action to only run on a release (rather than a push) but it can be [run manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) as well.
* Updated `R-CMD-check.yaml` GitHub action to [v2](https://github.com/r-lib/actions/tree/v2-branch/examples#standard-ci-workflow). Note that I had to add the [extra code for dealing with graphics on the Mac version](https://github.com/r-lib/actions#common-questions).
* `alkIndivAge()`: Modified. Added a catch for `NA`s in the length sample. Also added a test. This addresses [#88](https://github.com/fishR-Core-Team/FSA/issues/88)).
* `confint.boot()`: Modified. Changed hard-coding of columns that contained the confidence interval values to find those columns by `grep()`ing for the `%` sign. This fixes an issue related to `car::Confint()` returning the `coef()` results for functions that have a `coef()` method but not for those that do not. Also updated tests to use results from `car::Boot()` rather than the old `car::bootCase()`.

# FSA 0.9.3 15-Feb-21
* Moved `dplyr` from `imports` to `suggests` (needed because functions were removed in last version; however it is still used in some examples; partially addresses [#87](https://github.com/fishR-Core-Team/FSA/issues/87)).
Expand Down
5 changes: 3 additions & 2 deletions R/bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
#'
#' @rdname boot
#' @export
confint.boot <- function(object,parm=NULL,level=conf.level,conf.level=0.95,
confint.boot <- function(object,parm=NULL,
level=conf.level,conf.level=0.95,
type=c("bca","norm","basic","perc"),
plot=FALSE,err.col="black",err.lwd=2,
rows=NULL,cols=NULL,...) {
Expand All @@ -95,7 +96,7 @@ confint.boot <- function(object,parm=NULL,level=conf.level,conf.level=0.95,
}
}
res <- car::Confint(object,parm=parm,level=conf.level,type=type)
colnames(res)[2:3] <- iCILabel(conf.level)
colnames(res)[grep("%",colnames(res))] <- iCILabel(conf.level)
if (plot) {
tmp <- object$t
np <- ncol(tmp)
Expand Down
Binary file added inst/extdata/Boot1.RData
Binary file not shown.
Binary file removed inst/extdata/bootCase1.RData
Binary file not shown.
Binary file modified inst/extdata/nlsBoot1.RData
Binary file not shown.
75 changes: 39 additions & 36 deletions tests/testthat/testthat_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ fnx <- function(days,B1,B2,B3) {
# Fit the nonlinear model
nl1 <- nls(cells~fnx(days,B1,B2,B3),data=Ecoli,start=list(B1=6,B2=7.2,B3=-1.45))

# Get the bootstrap results saved previously because nlsBoot() and bootCase()
# will not run within a testing environment
# Get the results saved previously because nlsBoot() and bootCase() will not
# run within a testing environment. This is the code to create the files.
# nlsBoot1 <- nlstools::nlsBoot(nl1)
# save(nlsBoot1,file="inst/extdata/nlsBoot1.RData")
# Boot1 <- car::Boot(nl1)
# save(Boot1,file="inst/extdata/Boot1.RData")
load(system.file("extdata", "nlsBoot1.RData", package="FSA"))
load(system.file("extdata", "bootCase1.RData", package="FSA"))

load(system.file("extdata", "Boot1.RData", package="FSA"))


## Test Messages ----
Expand Down Expand Up @@ -46,53 +49,53 @@ test_that("nlsBoot() methods messages",{
expect_error(predict(nlsBoot1,fnx,days=2,digits=0),"must be positive")
})

test_that("bootCase() methods messages",{
test_that("Boot() methods messages",{
# testing confint()
expect_error(confint(bootCase1,"derek"),
expect_error(confint(Boot1,"derek"),
"does not exist in")
expect_error(confint(bootCase1,c("B1","derek")),
expect_error(confint(Boot1,c("B1","derek")),
"does not exist in")
expect_error(confint(bootCase1,4),
expect_error(confint(Boot1,4),
"exceeds number of columns")
expect_error(confint(bootCase1,-4),
expect_error(confint(Boot1,-4),
"exceeds number of columns")
expect_error(confint(bootCase1,c(1,4)),
expect_error(confint(Boot1,c(1,4)),
"exceeds number of columns")
expect_error(confint(bootCase1,-c(1,4)),
expect_error(confint(Boot1,-c(1,4)),
"exceeds number of columns")
expect_error(confint(bootCase1,c(-1,2)),
expect_error(confint(Boot1,c(-1,2)),
"cannot be both positive and negative")
expect_error(confint(bootCase1,conf.level=0),
expect_error(confint(Boot1,conf.level=0),
"must be between 0 and 1")
expect_error(confint(bootCase1,conf.level=1),
expect_error(confint(Boot1,conf.level=1),
"must be between 0 and 1")
expect_error(confint(bootCase1,conf.level="R"),
expect_error(confint(Boot1,conf.level="R"),
"must be numeric")
# testing htest()
expect_error(htest(bootCase1,"derek"),
expect_error(htest(Boot1,"derek"),
"does not exist in")
expect_error(htest(bootCase1,c("B1","derek")),
expect_error(htest(Boot1,c("B1","derek")),
"must be of length 1")
expect_error(htest(bootCase1,4),
expect_error(htest(Boot1,4),
"exceeds number of columns")
expect_error(htest(bootCase1,-4),
expect_error(htest(Boot1,-4),
"must be positive")
expect_error(htest(bootCase1,c(1,4)),
expect_error(htest(Boot1,c(1,4)),
"must be of length 1")
expect_error(htest(bootCase1),
expect_error(htest(Boot1),
"must select a parameter")
# testing predict()
expect_error(predict(bootCase1,1:7,days=2),
expect_error(predict(Boot1,1:7,days=2),
"is not a function")
expect_error(predict(bootCase1,fnx,derek=2),
expect_error(predict(Boot1,fnx,derek=2),
"unused argument")
expect_error(predict(bootCase1,fnx,days=2,conf.level=0),
expect_error(predict(Boot1,fnx,days=2,conf.level=0),
"must be between 0 and 1")
expect_error(predict(bootCase1,fnx,days=2,conf.level=1),
expect_error(predict(Boot1,fnx,days=2,conf.level=1),
"must be between 0 and 1")
expect_error(predict(bootCase1,fnx,days=2,conf.level="R"),
expect_error(predict(Boot1,fnx,days=2,conf.level="R"),
"must be numeric")
expect_error(predict(bootCase1,fnx,days=2,digits=0),
expect_error(predict(Boot1,fnx,days=2,digits=0),
"must be positive")
})

Expand Down Expand Up @@ -161,61 +164,61 @@ test_that("nlsBoot() methods output types",{
expect_equal(tmp[,"days"],1:5)
})

test_that("bootCase() methods output types",{
test_that("Boot() methods output types",{
# testing confint()
tmp <- confint(bootCase1)
tmp <- confint(Boot1)
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("Estimate","95% LCI","95% UCI"))
expect_equal(rownames(tmp),c("B1","B2","B3"))
expect_equal(nrow(tmp),3)
expect_equal(ncol(tmp),3)
tmp <- confint(bootCase1,"B1")
tmp <- confint(Boot1,"B1")
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("Estimate","95% LCI","95% UCI"))
expect_equal(rownames(tmp),c("B1"))
expect_equal(nrow(tmp),1)
expect_equal(ncol(tmp),3)
tmp <- confint(bootCase1,c(1,3))
tmp <- confint(Boot1,c(1,3))
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("Estimate","95% LCI","95% UCI"))
expect_equal(rownames(tmp),c("B1","B3"))
expect_equal(nrow(tmp),2)
expect_equal(ncol(tmp),3)
# testing htest()
tmp <- htest(bootCase1,"B1")
tmp <- htest(Boot1,"B1")
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("Ho Value","p value"))
expect_equal(rownames(tmp),c("B1"))
expect_equal(nrow(tmp),1)
expect_equal(ncol(tmp),2)
tmp <- htest(bootCase1,1)
tmp <- htest(Boot1,1)
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("Ho Value","p value"))
expect_equal(rownames(tmp),c("B1"))
expect_equal(nrow(tmp),1)
expect_equal(ncol(tmp),2)
# testing predict()
tmp <- predict(bootCase1,fnx,days=3)
tmp <- predict(Boot1,fnx,days=3)
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("days","Median","95% LCI","95% UCI"))
expect_equal(nrow(tmp),1)
expect_equal(ncol(tmp),4)
expect_equal(tmp[,"days"],c(days=3))
tmp <- predict(bootCase1,fnx,days=1:5)
tmp <- predict(Boot1,fnx,days=1:5)
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("days","Median","95% LCI","95% UCI"))
expect_equal(nrow(tmp),5)
expect_equal(ncol(tmp),4)
expect_equal(tmp[,"days"],1:5)
# get same output when digits are used?
tmp <- predict(bootCase1,fnx,days=1:5,digits=2)
tmp <- predict(Boot1,fnx,days=1:5,digits=2)
expect_is(tmp,"matrix")
expect_equal(mode(tmp),"numeric")
expect_equal(colnames(tmp),c("days","Median","95% LCI","95% UCI"))
Expand Down

0 comments on commit 98cf3ca

Please sign in to comment.