File: test_glmnet_varImp.R

package info (click to toggle)
r-cran-caret 7.0-1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,036 kB
  • sloc: ansic: 210; sh: 10; makefile: 2
file content (24 lines) | stat: -rw-r--r-- 694 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

library(caret)

context('Testing varImp')

test_that('glmnet varImp returns non-negative values', {
  skip_on_cran()
  skip_if_not_installed('glmnet')
  set.seed(1)
  dat <- SLC14_1(200)

  reg <- train(y ~ ., data = dat,
               method = "glmnet",
               tuneGrid = data.frame(lambda = .1, alpha = .5),
               trControl = trainControl(method = "none"))

  # this checks that some coefficients are negative
  coefs <- predict(reg$finalModel, s=0.1, type="coef")
  expect_lt(0, sum(0 > coefs))
  # now check that all elements of varImp are nonnegative,
  # in spite of negative coefficients
  vis <- varImp(reg, s=0.1, scale=F)$importance
  expect_true(all(vis >= 0))
})