Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
svteichman committed Jul 11, 2024
1 parent 98d104f commit 358be2e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
21 changes: 21 additions & 0 deletions R/fastEmuTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ fastEmuTest <- function(constraint_cats,

if (run_score) {
# prepare to run score tests

# get X matrix
if (is.null(X)) {
if (is.null(formula) | is.null(data)) {
stop("If design matrix X not provided, both formula and data containing
covariates in formula must be provided.")
}
X <- model.matrix(formula, data)
}
if ("data.frame" %in% class(X)) {
X <- as.matrix(X)
if (!is.numeric(X)) {
stop("X is a data frame that cannot be coerced to a numeric matrix. Please fix and try again.")
}
}
p <- ncol(X)

if (is.null(test_kj)) {
test_kj <- data.frame(expand.grid(k = 2:p, j = 1:J))
}

n_test <- nrow(test_kj)
score_res <- NULL
included_categories <- vector(mode = "list", length = n_test)
Expand Down
33 changes: 32 additions & 1 deletion tests/testthat/test-fastEmuTest.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ test_that("fastEmu still works with deprecated aggregated model", {
model = "agg")})

res_drop <- fastEmuTest(constraint_cats = 1:5, Y = Y, X = X, test_kj = data.frame(k = 2, j = 6))
expect_true(all.equal(res_agg$coef$pval[6], res_drop$coef$pval[6], tol = 0.001))
expect_true(all.equal(res_agg$coef$pval[6], res_drop$coef$pval[6], tol = 0.05))

})

test_that("fastEmu controls Type I error rate when it should", {

skip("skipping this in automatic tests because it is slow")

n <- 40
J <- 10
set.seed(1569)
nsim <- 100
ps <- rep(NA, nsim)
bs <- simulateBs(J = J, constraint_cats = 1:5, test_j = 6, constraint_mag = 1,
other_mag = 5, under_null = TRUE)
X = cbind(1, rep(0:1, each = n /2))

for (i in 1:nsim) {
print(i)
dat <- simulateData(X = X,
B = rbind(bs$b0, bs$b1), distn = "Poisson", mean_count_before_ZI = 50)
emu_res <- fastEmuTest(constraint_cats = 1:5, Y = dat, X = X, test_kj = data.frame(k = 2, j = 6),
estimate_full_model = FALSE)
ps[i] <- emu_res$coef$pval
}

})

test_that("fastEmu has power that increases with sample size and signal magnitude", {

skip("skipping this in automatic tests because it is slow")


})

0 comments on commit 358be2e

Please sign in to comment.