R包Ranger产生与变量“ none”相关的错误

时间:2018-12-06 12:48:53

标签: r modeling

我正在R中使用Ranger包来创建文本分类模型。但是,当变量之一为“ none”时,出现以下错误。

Error in intI(j, n = x@Dim[2], dn[[2]], give.dn = FALSE) : 
  index larger than maximal 5002

我还注意到变量“ none”没有包含在模型中,即使它在数据集中也是如此。

我创建了一个可复制的示例

library(dplyr)
library(Matrix)
library(ranger)

Dataset <- tibble(
                  Y = sample(c(1,0), 1000, replace = T),
                  none = sample(c(1,0), 1000, replace = T), 
                  None = sample(c(1,0), 1000, replace = T),
                  NONE = sample(c(1,0), 1000, replace = T))


trainset <- sample(1:1000, 800)

Dataset2Train <- Dataset[trainset,] %>%
  as.matrix()  %>%
  Matrix(., sparse = TRUE)

Dataset2Test <- Dataset[-trainset,] %>%
  as.matrix()  %>%
  Matrix(., sparse = TRUE)

#Creates model with no messages
rf <- ranger(data = Dataset2Train,
                              dependent.variable.name = "Y", 
                              classification = TRUE)

#Creates model with no messages
rf2 <- ranger(data =  Dataset[trainset,],
                              dependent.variable.name = "Y", 
                              classification = TRUE)

#produces error message
rf3 <- ranger(data =  Dataset[trainset,] %>% setNames(c("Y", "w", "x", "z")),
                              dependent.variable.name = "Y", 
                              classification = TRUE)

#does not include the none variable
rf$forest$independent.variable.names
rf2$forest$independent.variable.names

#crashes Rstudio
predict(rf, data = Dataset[-trainset,],  type = "response", predict.all = T)

#creates a prediction
predict(rf2, data = Dataset[-trainset,],  type = "response", predict.all = T)

真正的数据集是稀疏的,并且在我预测时不会崩溃,但是会返回问题开头提到的错误消息。

“无”不是R中的保留字,为什么会这样?

1 个答案:

答案 0 :(得分:1)

固定在游侠0.10.6中。在CRAN上进行更改之前,请通过

进行安装
devtools::install_github("imbs-hl/ranger")