在防风草模型上使用VIP包计算重要性度量

时间:2020-05-05 05:28:38

标签: r machine-learning tidymodels

我正在尝试在防风草制成的逻辑回归模型上使用vi_firm()计算特征重要性。对于正则表达式,我将使用虹膜数据集并尝试预测观察结果是否为setosa。

iris1 <- iris %>%
  mutate(class  = case_when(Species == 'setosa' ~ 'setosa',
                            TRUE ~ 'other'))
iris1$class = as.factor(iris1$class)

#set up logistic regression model
iris.lr = logistic_reg(
  mode="classification",
  penalty=NULL,
  mixture=NULL
) %>%
  set_engine("glmnet")

iris.fit = iris.lr %>%
  fit(class ~. , data = iris1)

library(vip)
vip::vi_firm(iris.fit, feature_names = features, train = iris1, type = 'classification')

这给

错误:您是要使用new_data而不是newdata吗?

我还试图使用相关pdp包中的partial来生成偏相关图。我收到同样的错误。

2 个答案:

答案 0 :(得分:2)

对于像那些适合glmnet的正则化模型,您可能需要坚持特定于模型的重要性评分(​​默认值为vi())。另外,请注意两件事:

  • 您需要指定要计算变量重要性的lambda值(在此示例中,我只是在此处随机选择了一个值)
  • 适合的glmnet对象位于iris_fit$fit中,位于防风草对象内部
library(tidymodels)
#> ── Attaching packages ────────────────────────────────────────── tidymodels 0.1.0 ──
#> ✓ broom     0.5.6      ✓ recipes   0.1.12
#> ✓ dials     0.0.6      ✓ rsample   0.0.6 
#> ✓ dplyr     0.8.5      ✓ tibble    3.0.1 
#> ✓ ggplot2   3.3.0      ✓ tune      0.1.0 
#> ✓ infer     0.5.1      ✓ workflows 0.1.1 
#> ✓ parsnip   0.1.1      ✓ yardstick 0.0.6 
#> ✓ purrr     0.3.4
#> ── Conflicts ───────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard()  masks scales::discard()
#> x dplyr::filter()   masks stats::filter()
#> x dplyr::lag()      masks stats::lag()
#> x ggplot2::margin() masks dials::margin()
#> x recipes::step()   masks stats::step()

iris1 <- iris %>%
  mutate(class  = case_when(Species == 'setosa' ~ 'setosa',
                            TRUE ~ 'other'),
         class = factor(class)) %>%
  select(-Species)


iris_mod <- logistic_reg(
  penalty = NULL,
  mixture = NULL
) %>%
  set_engine("glmnet")

iris_fit <- iris_mod %>%
  fit(class ~ ., data = iris1)

library(vip)
#> 
#> Attaching package: 'vip'
#> The following object is masked from 'package:utils':
#> 
#>     vi

vi(iris_fit$fit,
   lambda = iris_fit$fit$lambda[10])
#> # A tibble: 4 x 3
#>   Variable     Importance Sign 
#>   <chr>             <dbl> <chr>
#> 1 Sepal.Width        3.35 POS  
#> 2 Sepal.Length       0    NEG  
#> 3 Petal.Width       -2.97 NEG  
#> 4 Petal.Length      -3.98 NEG

reprex package(v0.3.0)于2020-05-14创建

答案 1 :(得分:1)

对于“ glmnet”对象,为了与s保持一致,正确的参数应该为lambda,而不是coef.glmnet(但是,当前使用vi()进行调用由于与scale参数部分匹配而导致的错误---我将在本周末推送修复程序; https://github.com/koalaverse/vip/issues/103)。另外,从0.2.2版开始,vi_model应该直接与model_fit对象一起使用。因此,此处的正确呼叫应该是:

> vi_model(iris_fit, s = iris_fit$fit$lambda[10]). #
# A tibble: 4 x 3
  Variable     Importance Sign 
  <chr>             <dbl> <chr>
1 Sepal.Length      0     NEG  
2 Sepal.Width       0     NEG  
3 Petal.Length     -0.721 NEG  
4 Petal.Width       0     NEG 

vi_firm()pdp::partial()而言,最简单的方法是创建自己的预测包装器。每个函数的文档中应该有很多细节,而我们即将发表的论文(https://github.com/koalaverse/vip/blob/master/rjournal/RJwrapper.pdf)中还有更多示例,但这是一个基本示例:

> # Data matrix (features only)
> X <- data.matrix(subset(iris1, select = -class))
> 
> # Prediction wrapper for partial dependence
> pfun <- function(object, newdata) {
+   # Return averaged prediciton for class of interest
+   mean(predict(object, newx = newdata, s = iris_fit$fit$lambda[10], 
+        type = "link")[, 1L])
+ }
> 
> # PDP-based VI
> features <- setdiff(names(iris1), "class")
> vip::vi_firm(
+   object = iris_fit$fit, 
+   feature_names = features, 
+   train = X, 
+   pred.fun = pfun
+ )
# A tibble: 4 x 2
  Variable     Importance
  <chr>             <dbl>
1 Sepal.Length       0   
2 Sepal.Width        0   
3 Petal.Length       1.27
4 Petal.Width        0   
> 
> # PDP
> pd <- pdp::partial(iris_fit$fit, "Petal.Length", pred.fun = pfun, 
+                    train = X)
> head(pd)
  Petal.Length      yhat
1     1.000000 1.0644756
2     1.140476 0.9632228
3     1.280952 0.8619700
4     1.421429 0.7607172
5     1.561905 0.6594644
6     1.702381 0.5582116