gbm包和分位数回归

gbm package and quantile regression

有人可以指出 gbm 包中分位数分布选项的正确用法吗?这个:

library(datasets)
library(gbm)
library(caret)

set.seed(42)
rm(list = ls())

model <- gbm(Petal.Width ~ Petal.Length

                        , distribution = list(name = "quantile", alpha = 0.4)
                        , data = iris
                        , n.trees = number_of_trees
                        , interaction.depth = 3
                        , shrinkage = 0.01,
                        , n.minobsinnode = 10
    )
model

无效。我得到:

Error in if (!is.element(distribution$name, getAvailableDistributions())) { : 
  argument is of length zero
Error: object 'model' not found

谢谢!

这是 gbm 中的错误,如这些 GitHub 问题中所报告:#29, #27. It was fixed in this commit。在他们在 CRAN 上获得新版本之前,您可以使用 GitHub 开发版本进行分位数回归:

devtools::install_github("gbm-developers/gbm")
#> Downloading GitHub repo gbm-developers/gbm@master
#> from URL https://api.github.com/repos/gbm-developers/gbm/zipball/master
#> Installing gbm
#> '/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore  \
#>   --quiet CMD INSTALL  \
#>   '/tmp/Rtmp4acgli/devtools55756447fca5/gbm-developers-gbm-0e07a6b'  \
#>   --library='/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5' --install-tests
#> 
#> Reloading installed gbm
#> Loaded gbm 2.1.4.9000
library(datasets)
library(gbm)
# library(caret) # this package isn't used

set.seed(42)
rm(list = ls())

model <- gbm(Petal.Width ~ Petal.Length

             , distribution = list(name = "quantile", alpha = 0.4)
             , data = iris
             , n.trees = 3 # number_of_trees -- this variable isn't given by OP
             , interaction.depth = 3
             , shrinkage = 0.01,
             , n.minobsinnode = 10
)
model
#> gbm(formula = Petal.Width ~ Petal.Length, distribution = list(name = "quantile", 
#>     alpha = 0.4), data = iris, n.trees = 3, interaction.depth = 3, 
#>     n.minobsinnode = 10, shrinkage = 0.01)
#> A gradient boosted model with quantile loss function.
#> 3 iterations were performed.
#> There were 1 predictors of which 1 had non-zero influence.

但不是 CRAN 版本:

install.packages("gbm")
#> Installing package into '/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5'
#> (as 'lib' is unspecified)
library(datasets)
library(gbm)
#> Loaded gbm 2.1.4
# library(caret) # this package isn't used

set.seed(42)
rm(list = ls())

model <- gbm(Petal.Width ~ Petal.Length

             , distribution = list(name = "quantile", alpha = 0.4)
             , data = iris
             , n.trees = 3 # number_of_trees -- this variable isn't given by OP
             , interaction.depth = 3
             , shrinkage = 0.01,
             , n.minobsinnode = 10
)
#> Error in if (!is.element(distribution$name, getAvailableDistributions())) {: argument is of length zero
model
#> Error in eval(expr, envir, enclos): object 'model' not found

问题是由这段代码引起的:

distribution <- if (missing(distribution)) {
  if (missing(distribution)) {
    y <- data[, all.vars(formula)[1L], drop = TRUE]
    guessDist(y) 
  } else if (is.character(distribution)) { 
    distribution <- list(name = distribution) 
  } 
}

您会注意到他们有时忘记处理用户传递命名列表的情况,就像文档中说的那样。但是,现在那段代码是固定的:

if (missing(distribution)) {
  y <- data[, all.vars(formula)[1L], drop = TRUE]
  distribution <- guessDist(y) 
}

if (is.character(distribution)) { 
  distribution <- list(name = distribution) 
}

这样,如果 distribution 已经是一个列表,那么它现在不受干扰。