tests/testthat/test-glmnetUtils.R

context("glmnetUtils")

skip_on_cran()

skip_if_not_installed("modeltests")
library(modeltests)

skip_if_not_installed("glmnetUtils")
library(glmnetUtils)

set.seed(27)

skip_if_not_installed("modeldata")
library(modeldata)
data(hpc_data)

fit <- glmnet(formula = mpg ~ ., data = mtcars)
fit2 <- glmnet(
  formula = class ~ compounds + input_fields + iterations + num_pending,
  data = hpc_data, family = "multinomial"
)

cv_fit <- cv.glmnet(formula = mpg ~ ., data = mtcars)
cv_fit2 <- cv.glmnet(
  formula = class ~ compounds + input_fields + iterations + num_pending,
  data = hpc_data, family = "multinomial"
)


test_that("glmnet.formula tidier arguments", {
  check_arguments(tidy.glmnet)
  check_arguments(glance.glmnet)

  check_arguments(tidy.cv.glmnet)
  check_arguments(glance.cv.glmnet)
})

test_that("tidy.glmnet.formula", {
  td <- tidy(fit)
  tdz <- tidy(fit, return_zeros = TRUE)

  check_tidy_output(td)
  check_tidy_output(tdz)

  check_dims(td, expected_cols = 5)
  check_dims(tdz, expected_cols = 5)

  expect_true(all(td$estimate != 0))
  expect_true(any(tdz$estimate == 0))

  # multinomial

  td2 <- tidy(fit2)
  td2z <- tidy(fit2, return_zeros = TRUE)

  check_tidy_output(td2)
  check_tidy_output(td2z)

  expect_is(td2, "tbl_df")

  expect_equal(dim(td2), c(983L, 6L))
  expect_equal(dim(td2z), c(1240L, 6L))

  expect_true(all(td2$estimate != 0))
  expect_true(any(td2z$estimate == 0))

  # regression tests
  expect_true(is.numeric(td$step) && !any(is.na(td$step)))
  expect_true(is.numeric(td2$step) && !any(is.na(td2$step)))
})

test_that("glance.glmnet.formula", {
  gl <- glance(fit)
  gl2 <- glance(fit2)

  check_glance_outputs(gl, gl2)

  expect_is(gl, "tbl_df")
  expect_equal(dim(gl), c(1L, 3L))
})

Try the broom package in your browser

Any scripts or data that you put into this service are public.

broom documentation built on July 9, 2023, 5:28 p.m.