Skip to content

Commit 82065a1

Browse files
authored
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
1 parent 71d6914 commit 82065a1

File tree

7 files changed

+222
-1
lines changed

7 files changed

+222
-1
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ VignetteBuilder:
7676
knitr
7777
Config/Needs/website:
7878
tidyverse/tidytemplate
79+
Config/Needs/check:
80+
bioc::mixOmics
7981
Config/testthat/edition: 3
8082
Encoding: UTF-8
8183
RoxygenNote: 7.2.3

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ S3method(axe_call,kknn)
1818
S3method(axe_call,ksvm)
1919
S3method(axe_call,lm)
2020
S3method(axe_call,mda)
21+
S3method(axe_call,mixo_pls)
22+
S3method(axe_call,mixo_spls)
2123
S3method(axe_call,ml_model)
2224
S3method(axe_call,model_fit)
2325
S3method(axe_call,multnet)
@@ -57,6 +59,8 @@ S3method(axe_data,gausspr)
5759
S3method(axe_data,glm)
5860
S3method(axe_data,kproto)
5961
S3method(axe_data,ksvm)
62+
S3method(axe_data,mixo_pls)
63+
S3method(axe_data,mixo_spls)
6064
S3method(axe_data,ml_model)
6165
S3method(axe_data,model_fit)
6266
S3method(axe_data,regbagg)
@@ -124,6 +128,8 @@ S3method(axe_fitted,kproto)
124128
S3method(axe_fitted,ksvm)
125129
S3method(axe_fitted,lm)
126130
S3method(axe_fitted,mda)
131+
S3method(axe_fitted,mixo_pls)
132+
S3method(axe_fitted,mixo_spls)
127133
S3method(axe_fitted,ml_model)
128134
S3method(axe_fitted,model_fit)
129135
S3method(axe_fitted,nnet)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# butcher (development version)
22

3+
* Added butcher methods for `mixOmics::pls()`, `mixOmics::spls()`,
4+
and `mixOmics::plsda()` (#249).
5+
36
* Added butcher methods for `klaR::rda()` and `klaR::NaiveBayes()` (#246).
47

58
* Added butcher methods for `ipred::bagging()` (#245).

R/mixOmics.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#' Axing mixOmics models
2+
#'
3+
#' `mixo_pls` (via `pls()`), `mixo_spls` (via `spls()`), and `mixo_plsda`
4+
#' (via `plsda()`) objects are created with the mixOmics package,
5+
#' leveraged to fit partial least squares models.
6+
#'
7+
#' The mixOmics package is not available on CRAN, but can be installed
8+
#' from the Bioconductor repository via `remotes::install_bioc("mixOmics")`.
9+
#'
10+
#' @inheritParams butcher
11+
#'
12+
#' @return Axed `mixo_pls`, `mixo_spls`, or `mixo_plsda` object.
13+
#'
14+
#' @examplesIf rlang::is_installed("mixOmics")
15+
#' library(butcher)
16+
#' do.call(library, list(package = "mixOmics"))
17+
#'
18+
#' # pls ------------------------------------------------------------------
19+
#' fit_mod <- function() {
20+
#' boop <- runif(1e6)
21+
#' pls(matrix(rnorm(2e4), ncol = 2), rnorm(1e4), mode = "classic")
22+
#' }
23+
#'
24+
#' mod_fit <- fit_mod()
25+
#' mod_res <- butcher(mod_fit)
26+
#'
27+
#' weigh(mod_fit)
28+
#' weigh(mod_res)
29+
#'
30+
#' new_data <- matrix(1:2, ncol = 2)
31+
#' colnames(new_data) <- c("X1", "X2")
32+
#' predict(mod_fit, new_data)
33+
#' predict(mod_res, new_data)
34+
#'
35+
#' @name axe-pls
36+
#' @aliases axe-mixo_pls
37+
NULL
38+
39+
#' @rdname axe-pls
40+
#' @export
41+
axe_call.mixo_pls <- function(x, verbose = FALSE, ...) {
42+
old <- x
43+
x <- exchange(x, "call", call("dummy_call"))
44+
45+
add_butcher_attributes(
46+
x,
47+
old,
48+
verbose = verbose
49+
)
50+
}
51+
52+
#' @rdname axe-pls
53+
#' @export
54+
axe_call.mixo_spls <- axe_call.mixo_pls
55+
56+
#' @rdname axe-pls
57+
#' @export
58+
axe_data.mixo_pls <- function(x, verbose = FALSE, ...) {
59+
old <- x
60+
x <- exchange(x, "input.X", character(0L))
61+
62+
add_butcher_attributes(
63+
x,
64+
old,
65+
verbose = verbose
66+
)
67+
}
68+
69+
#' @rdname axe-pls
70+
#' @export
71+
axe_data.mixo_spls <- axe_data.mixo_pls
72+
73+
#' @rdname axe-pls
74+
#' @export
75+
axe_fitted.mixo_pls <- function(x, verbose = FALSE, ...) {
76+
old <- x
77+
x$names <- exchange(x$names, "sample", matrix(NA))
78+
79+
add_butcher_attributes(
80+
x,
81+
old,
82+
verbose = verbose
83+
)
84+
}
85+
86+
#' @rdname axe-pls
87+
#' @export
88+
axe_fitted.mixo_spls <- axe_fitted.mixo_pls

man/axe-pls.Rd

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-mixOmics.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
test_that("pls + predict() works", {
2+
skip_on_cran()
3+
skip_if_not_installed("mixOmics")
4+
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics")))
5+
fit <- pls(matrix(rnorm(2e2), ncol = 2), rnorm(1e2), mode = "classic")
6+
x <- axe_call(fit)
7+
expect_equal(x$call, rlang::expr(dummy_call()))
8+
x <- axe_data(fit)
9+
expect_identical(x$input.X, character(0L))
10+
x <- axe_fitted(fit)
11+
expect_equal(x$names$sample, matrix(NA))
12+
x <- butcher(fit)
13+
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2"))
14+
expect_equal(
15+
predict(x, new_data) %>% purrr::discard_at("call"),
16+
predict(fit, new_data) %>% purrr::discard_at("call")
17+
)
18+
})
19+
20+
test_that("spls + predict() works", {
21+
skip_on_cran()
22+
skip_if_not_installed("mixOmics")
23+
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics")))
24+
fit <- spls(matrix(rnorm(2e2), ncol = 2), rnorm(1e2))
25+
x <- axe_call(fit)
26+
expect_equal(x$call, rlang::expr(dummy_call()))
27+
x <- axe_data(fit)
28+
expect_identical(x$input.X, character(0L))
29+
x <- axe_fitted(fit)
30+
expect_equal(x$names$sample, matrix(NA))
31+
x <- butcher(fit)
32+
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2"))
33+
expect_equal(predict(x, new_data) %>% purrr::discard_at("call"),
34+
predict(fit, new_data) %>% purrr::discard_at("call"))
35+
})
36+
37+
test_that("plsda + predict() works", {
38+
skip_on_cran()
39+
skip_if_not_installed("mixOmics")
40+
suppressPackageStartupMessages(do.call(library, list(package = "mixOmics")))
41+
fit <- plsda(matrix(rnorm(2e2), ncol = 2), sample(c("a", "b"), 1e2, replace = TRUE))
42+
x <- axe_call(fit)
43+
expect_equal(x$call, rlang::expr(dummy_call()))
44+
x <- axe_data(fit)
45+
expect_identical(x$input.X, character(0L))
46+
x <- axe_fitted(fit)
47+
expect_equal(x$names$sample, matrix(NA))
48+
x <- butcher(fit)
49+
new_data <- matrix(1:2, ncol = 2) %>% `colnames<-`(c("X1", "X2"))
50+
expect_equal(predict(x, new_data) %>% purrr::discard_at("call"),
51+
predict(fit, new_data) %>% purrr::discard_at("call"))
52+
})

vignettes/available-axe-methods.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ vignette: >
1111
knitr::opts_chunk$set(
1212
collapse = TRUE,
1313
comment = "#>",
14-
eval = requireNamespace("dplyr", quietly = TRUE)
14+
eval = requireNamespace("dplyr", quietly = TRUE) &
15+
requireNamespace("clisymbols", quietly = TRUE)
1516
)
1617
```
1718

0 commit comments

Comments
 (0)