对于“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