gbm软件包和分位数回归

时间:2018-10-16 10:42:28

标签: r gbm

有人可以指出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

谢谢!

1 个答案:

答案 0 :(得分:3)

这是gbm中的一个错误,如以下GitHub问题中所述:#29#27。它已在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.

reprex package(v0.2.1)于2018-10-20创建

但不是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

reprex package(v0.2.1)于2018-10-20创建

此问题是由以下代码引起的:

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已经是列表,则现在不受干扰。