Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Encoding: UTF-8
URL: https://github.com/openpharma/DoseFinding, https://openpharma.github.io/DoseFinding/
BugReports: https://github.com/openpharma/DoseFinding/issues
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion R/optContr.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ plotContr <- function(optContrObj, xlab = "Dose", ylab = "Contrast coefficients"
model = factor(rep(mod_nams, each = nD), levels=mod_nams),
levels = dimnames(cM)[[2]])
ggplot2::ggplot(cMtr, ggplot2::aes(.data$dose, .data$resp, col=.data$model))+
ggplot2::geom_line(size=1.2)+
ggplot2::geom_line(linewidth=1.2)+
ggplot2::geom_point()+
ggplot2::theme_bw()+
ggplot2::geom_point(size=1.8)+
Expand Down
12 changes: 5 additions & 7 deletions tests/testthat/test-MCTtest.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("multiple contrast test")

# TODO:
# * maybe define common candidate models outside of test_that() calls?
# * how do we check for equal p-values (calculated with MC algorighm)?
Expand Down Expand Up @@ -57,7 +55,7 @@ test_that("MCTtest gives the same output as multcomp::glht (beta and sigEmax mod
fit <- lm(y~x, data=dd_x_factor)
mcp <- glht(fit, linfct = mcp(x = t(obj$contMat)), alternative = "greater")
expect_equal(tstat(obj), tstat(mcp))
expect_equal(pval(obj), pval(mcp), tolerance = 0.001)
expect_equal(pval(obj), pval(mcp), tolerance = 0.01)
})

test_that("MCTtest gives the same output as multcomp::glht (logistic, exponential, quadratic models)", {
Expand All @@ -79,13 +77,13 @@ test_that("MCTtest gives the same output as multcomp::glht (logistic, exponentia
fit <- lm(y~x+cov1+cov2, data=dd_x_factor)
mcp <- glht(fit, linfct = mcp(x = t(obj$contMat)), alternative = "greater")
expect_equal(tstat(obj), tstat(mcp))
expect_equal(pval(obj), pval(mcp), tolerance = 0.001)
expect_equal(pval(obj), pval(mcp), tolerance = 0.005)
# model without covariates
obj <- MCTtest(x,y, dd, models=models, addCovars = ~1, pVal = TRUE)
fit <- lm(y~x, data=dd_x_factor)
mcp <- glht(fit, linfct = mcp(x = t(obj$contMat)), alternative = "greater")
expect_equal(tstat(obj), tstat(mcp))
expect_equal(pval(obj), pval(mcp), tolerance = 0.001)
expect_equal(pval(obj), pval(mcp), tolerance = 0.005)
})

test_that("MCTtest works with contrast matrix handed over", {
Expand Down Expand Up @@ -215,7 +213,7 @@ test_that("unordered values in MCTtest work (placebo adjusted scale)", {
# we don't compare stuff we want to be different
attr(fit_orig, "data") <- attr(fit_perm, "data") <- NULL
attr(fit_orig, "doseRespNam") <- attr(fit_perm, "doseRespNam") <- NULL
expect_equal(fit_orig, fit_perm)
expect_equal(fit_orig, fit_perm, ignore_formula_env = TRUE)
expect_equal(tstat(test_orig), tstat(test_perm))
})

Expand All @@ -240,6 +238,6 @@ test_that("unordered values in MCTtest work (unadjusted scale)", {
# we don't compare stuff we want to be different
attr(fit_orig, "data") <- attr(fit_perm, "data") <- NULL
attr(fit_orig, "doseRespNam") <- attr(fit_perm, "doseRespNam") <- NULL
expect_equal(fit_orig, fit_perm)
expect_equal(fit_orig, fit_perm, ignore_formula_env = TRUE)
expect_equal(tstat(test_orig), tstat(test_perm))
})
19 changes: 8 additions & 11 deletions tests/testthat/test-bMCTtest.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Bayesian multiple contrast test")

# TODO:
# * maybe define common candidate models outside of test_that() calls?
# * how do we check for equal p-values (calculated with MC algorighm)?
Expand Down Expand Up @@ -74,7 +72,7 @@ test_that("bMCTtest with uninformative prior produces same results as frequentis
mcp_freq <- MCTtest(x,y , dd, models = models, df = Inf, critV = TRUE)
mcp_bayes <- bMCTtest(x,y, dd, models=models, prior = prior)
expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("bMCTtest works with contrast matrix handed over and produces same results", {
Expand All @@ -96,7 +94,7 @@ test_that("bMCTtest works with contrast matrix handed over and produces same res
mcp_freq <- MCTtest(x,y , dd, models = models, df = Inf, critV = TRUE)
mcp_bayes <- bMCTtest(x,y, dd, models=models, prior = prior, contMat = mcp_freq$contMat)
expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("bMCTtest works with binary data (1)", {
Expand All @@ -118,7 +116,7 @@ test_that("bMCTtest works with binary data (1)", {
mcp_freq <- MCTtest(dose, dePar, S=vCov, models=models, type = "general", df = Inf, critV = TRUE)
mcp_bayes <- bMCTtest(dose, dePar, S=vCov, models=models, prior = prior, type = "general")
expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("MCTtest works with binary data (2)", {
Expand All @@ -140,7 +138,7 @@ test_that("MCTtest works with binary data (2)", {
mcp_freq <- MCTtest(dose, dePar, S=vCov, models=models, type = "general", df = Inf, critV = TRUE)
mcp_bayes <- bMCTtest(dose, dePar, S=vCov, models=models, prior = prior, type = "general")
expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("MCTtest works with binary data (3)", {
Expand All @@ -162,7 +160,7 @@ test_that("MCTtest works with binary data (3)", {
mcp_freq <- MCTtest(dose, dePar, S=vCov, models=models, type = "general", df = Inf, critV = TRUE)
mcp_bayes <- bMCTtest(dose, dePar, S=vCov, models=models, prior = prior, type = "general")
expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("a one-dimensional test works", {
Expand All @@ -177,12 +175,11 @@ test_that("a one-dimensional test works", {
prior <- vector("list", length(dose))
for(i in 1:length(dose))
prior[[i]] <- mixnorm(c(1, 0, 10000))
mcp_freq <- expect_warning(MCTtest(dose, dePar, S=vCov, models=model, type = "general", critV = TRUE, df=Inf),
"univariate: using pnorm")
mcp_freq <- suppressWarnings(MCTtest(dose, dePar, S=vCov, models=model, type = "general", critV = TRUE, df=Inf))
mcp_bayes <- bMCTtest(dose, dePar, S=vCov, models=model, type = "general", prior = prior)

expect_equal(tstat(mcp_freq), tstat(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.001)
expect_equal(1-pnorm(critVal2(mcp_freq)), critVal2(mcp_bayes), tolerance = 0.1)
})

test_that("unordered values in MCTtest work (unadjusted scale)", {
Expand All @@ -206,7 +203,7 @@ test_that("unordered values in MCTtest work (unadjusted scale)", {
doses2 <- doses[ord]
test_perm <- bMCTtest(doses2, drEst2, S = vc2, models = modlist, type = "general", prior = prior)
expect_equal(tstat(test_orig), tstat(test_perm))
expect_equal(critVal2(test_orig), critVal2(test_perm), tolerance = 0.001)
expect_equal(critVal2(test_orig), critVal2(test_perm), tolerance = 0.1)
})

test_that("bMCTtest gives same results as RBesT two-sample analysis with non-informative prior", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-drmodels.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("dose response model functions")

ud <- function(x) unname(drop(x))

test_that("betaMod does not produce NaN for large delta1, delta2", {
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-fitMod.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Model Fitting")

source("generate_test_datasets.R")

# Generate data sets and compare results of fitDRModel to the result of nls and
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-guesst.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("guesstimates")

test_that("emax", {
emx1 <- guesst(d=0.3, p=0.8, model="emax")
expect_equal(unname(emax(0.3,0,1,emx1)),
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-maFitMod.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("maFitMod")

data(biom)
anMod <- lm(resp~factor(dose)-1, data=biom)
drFit <- coef(anMod)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-optContr.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Optimal Contrasts")

require_extra_packages <- function() {
if (!(require("quadprog") && require("Rsolnp"))) {
skip("packages quadprog and Rsolnp not available")
Expand Down
18 changes: 8 additions & 10 deletions tests/testthat/test-optDesign.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("optimal designs")

# TODO
# * mixed Paper p. 1233, l. 2 (note the off and probably also the scal
# parameter were treated as unknown in this example in the paper, hence the
Expand Down Expand Up @@ -37,12 +35,12 @@ test_that("the emax model (table 2, line 5) gives the same results", {
optimizer="Nelder-Mead")
deswgts2 <- optDesign(fMod, probs, doses, Delta=0.2, designCrit = "TD",
optimizer="nlminb")
expect_equal(deswgts1$design, deswgts2$design, tolerance = 1e-4)
expect_equal(deswgts1$design, deswgts2$design, tolerance = 1e-3)
expect_equal(deswgts1$design, c(0.442, 0.5, 0.058), tolerance = 1e-3)
## efficiency compared to standard design (last column)
crt <- calcCrit(rep(1/6, 6), fMod, probs, c(0, 10, 25, 50, 100, 150),
Delta=0.2, designCrit = "TD")
expect_equal(exp(deswgts1$crit - crt), 0.5099, tolerance = 1e-4)
expect_equal(exp(deswgts1$crit - crt), 0.5099, tolerance = 1e-3)
})

test_that("the emax model (table 2, line 2) gives the same results", {
Expand Down Expand Up @@ -84,7 +82,7 @@ test_that("the logistic model (table 4, line 7) gives the same results", {
## efficiency compared to standard design (last column)
crt <- calcCrit(rep(1/6, 6), fMod, probs, c(0, 10, 25, 50, 100, 150),
Delta=0.05, designCrit = "TD")
expect_equal(exp(deswgts$crit - crt), 0.1853, tolerance = 1e-4)
expect_equal(exp(deswgts$crit - crt), 0.1853, tolerance = 1e-3)
})

test_that("the logistic model (table 4, line 1) gives the same results", {
Expand All @@ -102,7 +100,7 @@ test_that("the beta model (table 5, line 5) gives the same results", {
probs <- 1
deswgts <- optDesign(fMod, probs, doses, Delta=0.1,
control=list(maxit=1000), designCrit = "TD")
expect_equal(deswgts$design, c(0.45, 0.48, 0.05, 0.02, 0), tolerance = 1e-2)
expect_equal(deswgts$design, c(0.45, 0.48, 0.05, 0.02, 0), tolerance = 5e-2)
## efficiency compared to standard design (last column)
crt <- calcCrit(rep(1/6, 6), fMod, probs, c(0, 10, 25, 50, 100, 150),
Delta=0.1, designCrit = "TD")
Expand All @@ -115,7 +113,7 @@ test_that("the beta model (table 5, line 10) gives the same results", {
doses <- c(0, 27, 94.89, 150)
probs <- 1
deswgts <- optDesign(fMod, probs, doses, Delta=0.1, designCrit = "TD")
expect_equal(deswgts$design, c(0.45, 0.48, 0.05, 0.02), tolerance = 1e-2)
expect_equal(deswgts$design, c(0.45, 0.48, 0.05, 0.02), tolerance = 5e-2)
## efficiency compared to standard design (last column)
crt <- calcCrit(rep(1/6, 6), fMod, probs, c(0, 10, 25, 50, 100, 150),
Delta=0.1, designCrit = "TD")
Expand All @@ -132,7 +130,7 @@ test_that("the beta model (table 5, line 1) gives the same results", {
## efficiency compared to standard design (last column)
crt <- calcCrit(rep(1/6, 6), fMod, probs, c(0, 10, 25, 50, 100, 150),
Delta=0.2, designCrit = "TD")
expect_equal(exp(deswgts$crit - crt), 0.056, tolerance = 1e-3)
expect_equal(exp(deswgts$crit - crt), 0.056, tolerance = 5e-3)
})

test_that("standardized Dopt and Dopt&TD criteria work", {
Expand All @@ -145,7 +143,7 @@ test_that("standardized Dopt and Dopt&TD criteria work", {
## des1 and des2 should be exactly the same
des1 <- optDesign(fMod1, w1, doses, designCrit = "Dopt", standDopt = FALSE)
des2 <- optDesign(fMod1, w1, doses, designCrit = "Dopt", standDopt = TRUE)
expect_equal(des1$design, des2$design, tolerance =1e-6)
expect_equal(des1$design, des2$design, tolerance =1e-5)
## des1 and des2 should be different (as linear and emax have different
## number of parameters)
des1 <- optDesign(fMod2, w2, doses, designCrit = "Dopt", standDopt = FALSE,
Expand Down Expand Up @@ -175,7 +173,7 @@ test_that("feasible starting values are used when on boundary", {
trueModels <- Mods(linear=NULL, doses=doses, placEff = 0, maxEff = 1)
des <- optDesign(models=trueModels, probs=1, doses=doses, designCrit="Dopt",
lowbnd=lowbnd,uppbnd=uppbnd)
expect_equal(des$design, c(0.5, 0, 0, 0, 0.5))
expect_equal(des$design, c(0.5, 0, 0, 0, 0.5), tolerance = 1e-6)
})

test_that("there are no instabilities for numerical gradients", {
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-planMod.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("planning models")

# TODO
# * what do we want to do with tests #3-5 (mostly plots)
# * test #4 crashes in planMod
Expand Down Expand Up @@ -57,7 +55,7 @@ test_that("get_{TD,ED,Pred}Var gives the same result as a simulation", {
}
sim <- replicate(100, one_sim()) # for a real check use 10000
expect_equal(unname(rowMeans(sim)), true_values, tolerance = 0.01)
expect_equal(unname(apply(sim, 1, var)), true_variances, tolerance = 0.01)
expect_equal(unname(apply(sim, 1, var)), true_variances, tolerance = 0.02)

edt7 <- ED(mm, p=0.7)
edt3 <- ED(mm, p=0.3)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-powMCTBinCount.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("power calculation binary and count data")

## general options
mvt_control <- DoseFinding:::mvtnorm.control(maxpts=1e5, abseps = 0.0001)

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-powMCTInterim.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test_that("powMCTInterim works as expected with conditional power", {
mu_assumed = example_data$mu_assumed,
type = "conditional"
)
expect_equal(as.numeric(result), 0.2739, tolerance = 1e-4)
expect_equal(as.numeric(result), 0.2739, tolerance = 5e-4)
})

test_that("powMCTInterim works as expected when using NULL explicitly for mu_assumed", {
Expand Down Expand Up @@ -156,7 +156,7 @@ test_that("powMCTInterim gives same conditional power result as with simulation
alpha = 0.025
)

expect_equal(result, expected, tolerance = 1e-2, check.attributes = FALSE)
expect_equal(result, expected, tolerance = 1e-2, ignore_attr = TRUE)
})

test_that("powMCTInterim gives same predictive power result as with simulation based approach", {
Expand Down Expand Up @@ -184,5 +184,5 @@ test_that("powMCTInterim gives same predictive power result as with simulation b
alpha = 0.025
)

expect_equal(result, expected, tolerance = 1e-2, check.attributes = FALSE)
expect_equal(result, expected, tolerance = 1e-2, ignore_attr = TRUE)
})