【问题标题】:Summary statistics in glmnetglmnet 中的汇总统计信息
【发布时间】:2015-09-22 12:30:20
【问题描述】:

我一直在研究一个数据集,并使用 glmnet 进行线性 LASSO/Ridge 回归。

为简单起见,假设我使用的模型如下:

cv.glmnet(train.features, train.response, alpha=1, nlambda=100, type.measure = "mse", nfolds = 10)

我正在为客户准备演示文稿,我需要展示 变量的 T 统计数据 和 R 平方 值。此外,我还需要根据模型的拟合值绘制残差。

在创建从头开始执行此操作的函数之前,我想问一下库中是否已经涵盖了这一点。我检查了glmnet vignette,但没有找到任何东西。

感谢您的帮助!

【问题讨论】:

  • glmnet 用于预测而非推理(尽管它确实进行了一种变量选择)。我认为仍然没有一种公认的方法来生成标准错误,我看到 CI 的唯一方法是进行引导(不包括在 glmnet 中)。对于 rsq,您可以获得观测值和预测值之间的相关性并将其平方 - 但这并不能说明模型的复杂性

标签: r statistics linear-regression cross-validation glmnet


【解决方案1】:

您的问题的部分答案: plotres 中的功能 plotmo R 包是为各种模型绘制残差 的简单方法,包括 glmnet 和 cv.glmnet 模型。 plotres vignette 包含在包中有详细信息。例如

library(glmnet)
data(longley)
mod <- glmnet(data.matrix(longley[,1:6]), longley[,7])
library(plotmo) # for plotres
plotres(mod)

给出以下情节。您可以通过将适当的参数传递给 plotres 来选择子图并修改图。

【讨论】:

    【解决方案2】:

    “yardstick”和“modelr”这两个包可以提供帮助。

    我使用插入符号通过“train()”调用 glmnet,返回的对象有一个 $resample 对象,其中包含每个交叉验证折叠的 RMSE、Rsquared 和 MAE。

    library( tictoc ) # If you don't want to install this, just take out the calls to tic() and toc()
    library( caret )
    library( tidyverse )
    
    training_folds <- createFolds( dmv, returnTrain = TRUE )
    
    ctl <- trainControl( method = "cv", number = 5, index = training_folds )
    tic()
    dmv_pp <- preProcess( dmv, method = c( "nzv", "center", "scale" ))
    toc() # This can take a while
    
    dmv_train <- predict( dmv_pp, dmv )
    # Using just a subset of the data, because otherwise I run out of memory.
    mdl <- train( duration_avg ~ ., data = dmv_train[1:1E4,], trControl = ctl,  method = "glmnet",
                  tuneGrid = expand.grid(
                    alpha = c( 0, 0.5, 1),
                    lambda = c( 0.001, 0.01 )
                  )
              )
    
    mdl$resample %>% names()
    
    mdl %>%
        listviewer::jsonedit() # This object should contain $resamples
    
    dmv_train <- dmv_train %>%
        modelr::add_predictions( mdl, var = "predicted_duration_avg" ) # I think this should work with any model that has a predict() method
    
    dmv_train %>%
      yardstick::metrics( duration_avg, predicted_duration_avg )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2016-01-28
      • 2019-03-19
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多