-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add butcher methods for mixOmics output (#249)
* add butcher methods for mixOmics output * correct PR number * install mixOmics for pkg check * install via DESCRIPTION `Config/Needs` * skip eval if suggested package is missing
- Loading branch information
1 parent
71d6914
commit 82065a1
Showing
7 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#' Axing mixOmics models | ||
#' | ||
#' `mixo_pls` (via `pls()`), `mixo_spls` (via `spls()`), and `mixo_plsda` | ||
#' (via `plsda()`) objects are created with the mixOmics package, | ||
#' leveraged to fit partial least squares models. | ||
#' | ||
#' The mixOmics package is not available on CRAN, but can be installed | ||
#' from the Bioconductor repository via `remotes::install_bioc("mixOmics")`. | ||
#' | ||
#' @inheritParams butcher | ||
#' | ||
#' @return Axed `mixo_pls`, `mixo_spls`, or `mixo_plsda` object. | ||
#' | ||
#' @examplesIf rlang::is_installed("mixOmics") | ||
#' library(butcher) | ||
#' do.call(library, list(package = "mixOmics")) | ||
#' | ||
#' # pls ------------------------------------------------------------------ | ||
#' fit_mod <- function() { | ||
#' boop <- runif(1e6) | ||
#' pls(matrix(rnorm(2e4), ncol = 2), rnorm(1e4), mode = "classic") | ||
#' } | ||
#' | ||
#' mod_fit <- fit_mod() | ||
#' mod_res <- butcher(mod_fit) | ||
#' | ||
#' weigh(mod_fit) | ||
#' weigh(mod_res) | ||
#' | ||
#' new_data <- matrix(1:2, ncol = 2) | ||
#' colnames(new_data) <- c("X1", "X2") | ||
#' predict(mod_fit, new_data) | ||
#' predict(mod_res, new_data) | ||
#' | ||
#' @name axe-pls | ||
#' @aliases axe-mixo_pls | ||
NULL | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_call.mixo_pls <- function(x, verbose = FALSE, ...) { | ||
old <- x | ||
x <- exchange(x, "call", call("dummy_call")) | ||
|
||
add_butcher_attributes( | ||
x, | ||
old, | ||
verbose = verbose | ||
) | ||
} | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_call.mixo_spls <- axe_call.mixo_pls | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_data.mixo_pls <- function(x, verbose = FALSE, ...) { | ||
old <- x | ||
x <- exchange(x, "input.X", character(0L)) | ||
|
||
add_butcher_attributes( | ||
x, | ||
old, | ||
verbose = verbose | ||
) | ||
} | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_data.mixo_spls <- axe_data.mixo_pls | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_fitted.mixo_pls <- function(x, verbose = FALSE, ...) { | ||
old <- x | ||
x$names <- exchange(x$names, "sample", matrix(NA)) | ||
|
||
add_butcher_attributes( | ||
x, | ||
old, | ||
verbose = verbose | ||
) | ||
} | ||
|
||
#' @rdname axe-pls | ||
#' @export | ||
axe_fitted.mixo_spls <- axe_fitted.mixo_pls |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
test_that("pls + predict() works", { | ||
skip_on_cran() | ||
skip_if_not_installed("mixOmics") | ||
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics"))) | ||
fit <- pls(matrix(rnorm(2e2), ncol = 2), rnorm(1e2), mode = "classic") | ||
x <- axe_call(fit) | ||
expect_equal(x$call, rlang::expr(dummy_call())) | ||
x <- axe_data(fit) | ||
expect_identical(x$input.X, character(0L)) | ||
x <- axe_fitted(fit) | ||
expect_equal(x$names$sample, matrix(NA)) | ||
x <- butcher(fit) | ||
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2")) | ||
expect_equal( | ||
predict(x, new_data) %>% purrr::discard_at("call"), | ||
predict(fit, new_data) %>% purrr::discard_at("call") | ||
) | ||
}) | ||
|
||
test_that("spls + predict() works", { | ||
skip_on_cran() | ||
skip_if_not_installed("mixOmics") | ||
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics"))) | ||
fit <- spls(matrix(rnorm(2e2), ncol = 2), rnorm(1e2)) | ||
x <- axe_call(fit) | ||
expect_equal(x$call, rlang::expr(dummy_call())) | ||
x <- axe_data(fit) | ||
expect_identical(x$input.X, character(0L)) | ||
x <- axe_fitted(fit) | ||
expect_equal(x$names$sample, matrix(NA)) | ||
x <- butcher(fit) | ||
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2")) | ||
expect_equal(predict(x, new_data) %>% purrr::discard_at("call"), | ||
predict(fit, new_data) %>% purrr::discard_at("call")) | ||
}) | ||
|
||
test_that("plsda + predict() works", { | ||
skip_on_cran() | ||
skip_if_not_installed("mixOmics") | ||
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics"))) | ||
fit <- plsda(matrix(rnorm(2e2), ncol = 2), sample(c("a", "b"), 1e2, replace = TRUE)) | ||
x <- axe_call(fit) | ||
expect_equal(x$call, rlang::expr(dummy_call())) | ||
x <- axe_data(fit) | ||
expect_identical(x$input.X, character(0L)) | ||
x <- axe_fitted(fit) | ||
expect_equal(x$names$sample, matrix(NA)) | ||
x <- butcher(fit) | ||
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2")) | ||
expect_equal(predict(x, new_data) %>% purrr::discard_at("call"), | ||
predict(fit, new_data) %>% purrr::discard_at("call")) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters