Skip to content

Commit 3cbe567

Browse files
authored
Merge pull request #116 from svteichman/main
if `k` or `j` in `test_kj` are strings that correspond to covariate or taxon names, replace them with the correct indices
2 parents c331af7 + 91c4ce4 commit 3cbe567

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

.github/workflows/test-coverage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
shell: Rscript {0}
2929

3030
- name: Restore R package cache
31-
uses: actions/cache@v2
31+
uses: actions/cache@v3
3232
with:
3333
path: ${{ env.R_LIBS_USER }}
3434
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}

R/emuFit.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ emuFit <- function(Y,
212212
X <- check_results$X
213213
cluster <- check_results$cluster
214214
B_null_list <- check_results$B_null_list
215+
test_kj <- check_results$test_kj
215216

216217
if (length(constraint_fn) == 1 & is.numeric(constraint_fn)) {
217218
constraint_cat <- constraint_fn

R/emuFit_check.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ ignoring argument 'cluster'.")
186186
}
187187
}
188188

189+
# check that test kj values are numbers, if they are strings that convert if possible to numbers
190+
if (!is.null(test_kj)) {
191+
if (!(is.numeric(test_kj$k))) {
192+
if (sum(!(test_kj$k %in% colnames(X))) == 0) {
193+
test_kj$k <- as.vector(sapply(test_kj$k, function(x) {which(colnames(X) == x)}))
194+
} else {
195+
stop("Make sure that the values of `k` in `test_kj` are numeric or correspond to column names of the `X` matrix.")
196+
}
197+
}
198+
if (!(is.numeric(test_kj$j))) {
199+
if (sum(!(test_kj$j %in% colnames(Y))) == 0) {
200+
test_kj$j <- as.vector(sapply(test_kj$j, function(x) {which(colnames(Y) == x)}))
201+
} else {
202+
stop("Make sure that the values of `j` in `test_kj` are numeric or correspond to column names of the `Y` matrix.")
203+
}
204+
}
205+
}
206+
189207
# check for valid argument remove_zero_comparison_pvals
190208
if (remove_zero_comparison_pvals != TRUE & remove_zero_comparison_pvals != FALSE) {
191209
if (!(is.numeric(remove_zero_comparison_pvals) & remove_zero_comparison_pvals <= 1 &
@@ -194,6 +212,6 @@ ignoring argument 'cluster'.")
194212
}
195213
}
196214

197-
return(list(Y = Y, X = X, cluster = cluster, B_null_list = B_null_list))
215+
return(list(Y = Y, X = X, cluster = cluster, B_null_list = B_null_list, test_kj = test_kj))
198216

199217
}

tests/testthat/test-emuFit.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,13 @@ test_that("emuFit refits starting at provided value if `B` or `fitted_model` are
658658
expect_true(length(message1) > length(message3))
659659

660660
})
661+
662+
test_that("giving test_kj as valid strings works", {
663+
colnames(Y) <- paste0("taxon", 1:6)
664+
colnames(X) <- c("int", "group")
665+
res <- emuFit(Y = Y, X = X, compute_cis = FALSE, test_kj = data.frame(k = "group", j = "taxon3"),
666+
penalize = FALSE, tolerance = 0.1)
667+
expect_true(!is.na(res$coef$pval[3]))
668+
expect_error(emuFit(Y = Y, X = X, compute_cis = FALSE, test_kj = data.frame(k = "group", j = "taxa3"),
669+
penalize = FALSE, tolerance = 0.1))
670+
})

0 commit comments

Comments
 (0)