Skip to content

Commit

Permalink
Merge pull request #259 from tidymodels/cran-dimRed-ex-fix
Browse files Browse the repository at this point in the history
Fixes for CRAN notice about failing examples
  • Loading branch information
topepo authored Nov 18, 2018
2 parents 3b41401 + 335ac68 commit 9003792
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 82 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: recipes
Title: Preprocessing Tools to Create Design Matrices
Version: 0.1.3.9002
Version: 0.1.4
Authors@R: c(
person("Max", "Kuhn", , "[email protected]", c("aut", "cre")),
person("Hadley", "Wickham", , "[email protected]", "aut"),
Expand Down Expand Up @@ -48,6 +48,7 @@ Suggests:
rmarkdown,
rpart,
rsample,
RSpectra,
testthat
License: GPL-2
VignetteBuilder: knitr
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `recipes` 0.1.3.9002

## Breaking Changes

* Several argument names were changed to be consistent with other `tidymodels` packages (e.g. `dials`) and the general tidyverse naming conventions.
* `K` in `step_knnimpute` was changed to `neighbors`. `step_isomap` had the number of neighbors promoted to a main argument called `neighbors `
* `step_pca`, `step_pls`, `step_kpca`, `step_ica` now use `num_comp` instead of `num`. , `step_isomap` uses `num_terms` instead of `num`.
Expand Down
23 changes: 13 additions & 10 deletions R/ica.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#' @param res The [fastICA::fastICA()] object is stored
#' here once this preprocessing step has be trained by
#' [prep.recipe()].
#' @param num The number of components to retain (this will be
#' deprecated in factor of `num_comp` in version 0.1.5). `num_comp`
#' will override this option.
#' @param num The number of components to retain (this will be
#' deprecated in factor of `num_comp` in version 0.1.5). `num_comp`
#' will override this option.
#' @param prefix A character string that will be the prefix to the
#' resulting new variables. See notes below.
#' @return An updated version of `recipe` with the new step
Expand Down Expand Up @@ -82,14 +82,17 @@
#' ica_trans <- step_center(rec, V1, V2)
#' ica_trans <- step_scale(ica_trans, V1, V2)
#' ica_trans <- step_ica(ica_trans, V1, V2, num_comp = 2)
#' # ica_estimates <- prep(ica_trans, training = tr)
#' # ica_data <- bake(ica_estimates, te)
#'
#' # plot(te$V1, te$V2)
#' # plot(ica_data$IC1, ica_data$IC2)
#' if (require(dimRed) & require(fastICA)) {
#' ica_estimates <- prep(ica_trans, training = tr)
#' ica_data <- bake(ica_estimates, te)
#'
#' # tidy(ica_trans, number = 3)
#' # tidy(ica_estimates, number = 3)
#' plot(te$V1, te$V2)
#' plot(ica_data$IC1, ica_data$IC2)
#'
#' tidy(ica_trans, number = 3)
#' tidy(ica_estimates, number = 3)
#' }
#' @seealso [step_pca()] [step_kpca()]
#' [step_isomap()] [recipe()] [prep.recipe()]
#' [bake.recipe()]
Expand All @@ -108,7 +111,7 @@ step_ica <-


recipes_pkg_check(c("dimRed", "fastICA"))
if (!is.null(num))
if (!is.null(num))
message("The argument `num` is deprecated in factor of `num_comp`. ",
"`num` will be removed in next version.", call. = FALSE)
add_step(
Expand Down
11 changes: 7 additions & 4 deletions R/integer.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' @param strict A logical for whether the values should be returned as
#' integers (as opposed to double).
#' @param zero_based A logical for whether the integers should start at zero and
#' new values be appended as the largest integer.
#' new values be appended as the largest integer.
#' @return An updated version of `recipe` with the new step added
#' to the sequence of existing steps (if any). For the `tidy`
#' method, a tibble with columns `terms` (the selectors or
Expand All @@ -38,7 +38,7 @@
#' argument above). Missing values propagate.
#'
#' Factor inputs are ordered by their levels. All others are
#' ordered by `sort`.
#' ordered by `sort`.
#'
#' Despite the name, the new values are returned as numeric unless
#' `strict = TRUE`, which will coerce the results to integers.
Expand Down Expand Up @@ -144,12 +144,12 @@ prep.step_integer <- function(x, training, info = NULL, ...) {
map_key_to_int <- function(dat, key, strict = FALSE, zero = FALSE) {
if (is.factor(dat))
dat <- as.character(dat)

res <- full_join(tibble(value = dat, .row = seq_along(dat)), key, by = "value")
res <- dplyr::filter(res, !is.na(.row))
res <- arrange(res, .row)
if (zero) {
res$integer[is.na(res$integer) & !is.na(res$value)] <-
res$integer[is.na(res$integer) & !is.na(res$value)] <-
max(key$integer, na.rm = TRUE) + 1
} else {
res$integer[is.na(res$integer) & !is.na(res$value)] <- 0
Expand Down Expand Up @@ -200,3 +200,6 @@ tidy.step_integer <- function(x, ...) {
res$id <- x$id
res
}

#' @importFrom stats runif
utils::globalVariables(c(".row"))
26 changes: 14 additions & 12 deletions R/isomap.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#' used.
#' @param neighbors The number of neighbors.
#' @param options A list of options to [dimRed::Isomap()].
#' @param num The number of isomap dimensions (this will be deprecated
#' in factor of `num_terms` in version 0.1.5). `num_terms` will
#' override this option.
#' @param num The number of isomap dimensions (this will be deprecated
#' in factor of `num_terms` in version 0.1.5). `num_terms` will
#' override this option.
#' @param res The [dimRed::Isomap()] object is stored
#' here once this preprocessing step has be trained by
#' [prep.recipe()].
Expand Down Expand Up @@ -85,16 +85,18 @@
#' neighbors = 100,
#' num_terms = 2)
#'
#' # im_estimates <- prep(im_trans, training = biomass_tr)
#' if (require(dimRed) & require(RSpectra)) {
#' im_estimates <- prep(im_trans, training = biomass_tr)
#'
#' # im_te <- bake(im_estimates, biomass_te)
#' im_te <- bake(im_estimates, biomass_te)
#'
#' # rng <- extendrange(c(im_te$Isomap1, im_te$Isomap2))
#' # plot(im_te$Isomap1, im_te$Isomap2,
#' # xlim = rng, ylim = rng)
#' rng <- extendrange(c(im_te$Isomap1, im_te$Isomap2))
#' plot(im_te$Isomap1, im_te$Isomap2,
#' xlim = rng, ylim = rng)
#'
#' # tidy(im_trans, number = 4)
#' # tidy(im_estimates, number = 4)
#' tidy(im_trans, number = 4)
#' tidy(im_estimates, number = 4)
#' }
#' }
#' @seealso [step_pca()] [step_kpca()]
#' [step_ica()] [recipe()] [prep.recipe()]
Expand All @@ -115,7 +117,7 @@ step_isomap <-
id = rand_id("isomap")) {

recipes_pkg_check(c("dimRed", "RSpectra", "igraph", "RANN"))
if (!is.null(num))
if (!is.null(num))
message("The argument `num` is deprecated in factor of `num_terms`. ",
"`num` will be removed in next version.", call. = FALSE)
add_step(
Expand All @@ -137,7 +139,7 @@ step_isomap <-
}

step_isomap_new <-
function(terms, role, trained, num_terms, neighbors, options, res, num,
function(terms, role, trained, num_terms, neighbors, options, res, num,
prefix, skip, id) {
step(
subclass = "isomap",
Expand Down
26 changes: 14 additions & 12 deletions R/kpca.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#' @param res An S4 [kernlab::kpca()] object is stored
#' here once this preprocessing step has be trained by
#' [prep.recipe()].
#' @param num The number of components to retain (this will be
#' deprecated in factor of `num_comp` in version 0.1.5). `num_comp`
#' will override this option.
#' @param num The number of components to retain (this will be
#' deprecated in factor of `num_comp` in version 0.1.5). `num_comp`
#' will override this option.
#' @param prefix A character string that will be the prefix to the
#' resulting new variables. See notes below.
#' @return An updated version of `recipe` with the new step
Expand Down Expand Up @@ -94,16 +94,18 @@
#' step_scale(all_predictors()) %>%
#' step_kpca(all_predictors())
#'
#' # kpca_estimates <- prep(kpca_trans, training = biomass_tr)
#' if (require(dimRed) & require(kernlab)) {
#' kpca_estimates <- prep(kpca_trans, training = biomass_tr)
#'
#' # kpca_te <- bake(kpca_estimates, biomass_te)
#' kpca_te <- bake(kpca_estimates, biomass_te)
#'
#' # rng <- extendrange(c(kpca_te$kPC1, kpca_te$kPC2))
#' # plot(kpca_te$kPC1, kpca_te$kPC2,
#' # xlim = rng, ylim = rng)
#' rng <- extendrange(c(kpca_te$kPC1, kpca_te$kPC2))
#' plot(kpca_te$kPC1, kpca_te$kPC2,
#' xlim = rng, ylim = rng)
#'
#' # tidy(kpca_trans, number = 4)
#' # tidy(kpca_estimates, number = 4)
#' tidy(kpca_trans, number = 4)
#' tidy(kpca_estimates, number = 4)
#' }
#' @seealso [step_pca()] [step_ica()]
#' [step_isomap()] [recipe()] [prep.recipe()]
#' [bake.recipe()]
Expand All @@ -123,10 +125,10 @@ step_kpca <-
id = rand_id("kpca")) {

recipes_pkg_check(c("dimRed", "kernlab"))
if (!is.null(num))
if (!is.null(num))
message("The argument `num` is deprecated in factor of `num_comp`. ",
"`num` will be removed in next version.", call. = FALSE)

add_step(
recipe,
step_kpca_new(
Expand Down
20 changes: 10 additions & 10 deletions R/nnmf.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@
#'
#' @examples
#' \donttest{
#' library(dimRed)
#' library(NMF)
#' data(biomass)
#'
#' rec <- recipe(HHV ~ ., data = biomass) %>%
#' update_role(sample, new_role = "id var") %>%
#' update_role(dataset, new_role = "split variable") %>%
#' step_nnmf(all_predictors(), num_comp = 2, seed = 473, num_run = 2) %>%
#' prep(training = biomass, retain = TRUE)
#' if (require(dimRed) & require(NMF)) {
#' rec <- recipe(HHV ~ ., data = biomass) %>%
#' update_role(sample, new_role = "id var") %>%
#' update_role(dataset, new_role = "split variable") %>%
#' step_nnmf(all_predictors(), num_comp = 2, seed = 473, num_run = 2) %>%
#' prep(training = biomass, retain = TRUE)
#'
#' # juice(rec)
#' juice(rec)
#'
#' # library(ggplot2)
#' # ggplot(juice(rec), aes(x = NNMF2, y = NNMF1, col = HHV)) + geom_point()
#' library(ggplot2)
#' ggplot(juice(rec), aes(x = NNMF2, y = NNMF1, col = HHV)) + geom_point()
#' }
#' }
#' @seealso [step_pca()], [step_ica()], [step_kpca()],
#' [step_isomap()], [recipe()], [prep.recipe()],
Expand Down
15 changes: 9 additions & 6 deletions man/step_ica.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions man/step_isomap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions man/step_kpca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions man/step_nnmf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions tests/testthat/test_isomap.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ test_that('correct Isomap values', {
skip_if_not_installed("igraph")
skip_if_not_installed("RANN")
skip_if_not_installed("dimRed")

skip_if(getRversion() <= "3.4.4")

im_rec <- rec %>%
step_isomap(x1, x2, x3, neighbors = 3, num_terms = 3, id = "")

im_trained <- prep(im_rec, training = dat1, verbose = FALSE)

im_pred <- bake(im_trained, new_data = dat2)

all.equal(as.matrix(im_pred), as.matrix(exp_res))
# unique up to sign
all.equal(abs(as.matrix(im_pred)), abs(as.matrix(exp_res)))

im_tibble <-
tibble(terms = c("x1", "x2", "x3"), id = "")
Expand All @@ -61,7 +63,8 @@ test_that('printing', {
skip_if_not_installed("igraph")
skip_if_not_installed("RANN")
skip_if_not_installed("dimRed")

skip_if(getRversion() <= "3.4.4")

im_rec <- rec %>%
step_isomap(x1, x2, x3, neighbors = 3, num_terms = 3)
expect_output(print(im_rec))
Expand Down

0 comments on commit 9003792

Please sign in to comment.