glmnet - variable importance?

前端 未结 3 1538
旧巷少年郎
旧巷少年郎 2021-02-09 15:35

I´m using the glmnet package to perform a LASSO regression. Is there a way to get the importance of the individual variables that were selected? I thought about ranking the coef

3条回答
  •  北海茫月
    2021-02-09 16:02

    This is how it is done in caret package.

    To summarize, you can take the absolute value of the final coefficients and rank them. The ranked coefficients are your variable importance.

    To view the source code, you can type

    caret::getModelInfo("glmnet")$glmnet$varImp
    

    If you don't want to use caret package, you can run the following lines from the package, and it should work.

    varImp <- function(object, lambda = NULL, ...) {
    
      ## skipping a few lines
    
      beta <- predict(object, s = lambda, type = "coef")
      if(is.list(beta)) {
        out <- do.call("cbind", lapply(beta, function(x) x[,1]))
        out <- as.data.frame(out, stringsAsFactors = TRUE)
      } else out <- data.frame(Overall = beta[,1])
      out <- abs(out[rownames(out) != "(Intercept)",,drop = FALSE])
      out
    }
    

    Finally, call the function with your fit.

    varImp(cvfit, lambda = cvfit$lambda.min)
    

提交回复
热议问题