r quantregForest() error: NA's produced by integer overflow lead to an invalid argument in the rep() function

r quantregForest() error: NA's produced by integer overflow lead to an invalid argument in the rep() function

我正在尝试使用 quantregForest 包中的 quantregForest() 函数(它建立在 randomForest 包上。)

我尝试使用以下方法训练模型:

qrf_model <- quantregForest(x=Xtrain, y=Ytrain, importance=TRUE, ntree=10)

我收到以下错误消息(即使将树的数量从 100 减少到 10):

Error in rep(0, nobs * nobs * npred) : invalid 'times' argument

加上一个警告:

In nobs * nobs * npred : NAs produced by integer overflow

数据框Xtrain有38个数值变量,看起来像这样:

> str(Xtrain)
'data.frame':   31132 obs. of  38 variables:
 $ X1 : num  301306 6431 2293 1264 32477 ...
 $ X2 : num  173.2 143.5 43.4 180.6 1006.2 ...
 $ X3 : num  0.1598 0.1615 0.1336 0.0953 0.1988 ...
 $ X4 : num  0.662 0.25 0.71 0.709 0.671 ...
 $ X5 : num  0.05873 0.0142 0 0.00154 0.09517 ...
 $ X6 : num  0.01598 0 0.0023 0.00154 0.01634 ...
 $ X7 : num  0.07984 0.03001 0.00845 0.04304 0.09326 ...
 $ X8 : num  0.92 0.97 0.992 0.957 0.907 ...
 $ X9 : num  105208 1842 830 504 11553 ...
 $ X10: num  69974 1212 611 352 7080 ...
 $ X11: num  0.505 0.422 0.55 0.553 0.474 ...
 $ X12: num  0.488 0.401 0.536 0.541 0.45 ...
 $ X13: num  0.333 0.419 0.257 0.282 0.359 ...
 $ X14: num  0.187 0.234 0.172 0.207 0.234 ...
 $ X15: num  0.369 0.216 0.483 0.412 0.357 ...
 $ X16: num  0.0765 0.1205 0.0262 0.054 0.0624 ...
 $ X17: num  2954 77 12 10 739 ...
 $ X18: num  2770 43 9 21 433 119 177 122 20 17 ...
 $ X19: num  3167 72 49 25 622 ...
 $ X20: num  3541 57 14 24 656 ...
 $ X21: num  3361 82 0 33 514 ...
 $ X22: num  3929 27 10 48 682 ...
 $ X23: num  3695 73 61 15 643 ...
 $ X24: num  4781 52 5 14 680 ...
 $ X25: num  3679 103 5 23 404 ...
 $ X26: num  7716 120 55 40 895 ...
 $ X27: num  11043 195 72 48 1280 ...
 $ X28: num  16080 332 160 83 1684 ...
 $ X29: num  12312 125 124 62 1015 ...
 $ X30: num  8218 99 36 22 577 ...
 $ X31: num  9957 223 146 26 532 ...
 $ X32: num  0.751 0.444 0.621 0.527 0.682 ...
 $ X33: num  0.01873 0 0 0.00317 0.02112 ...
 $ X34: num  0.563 0.372 0.571 0.626 0.323 ...
 $ X35: num  0.366 0.39 0.156 0.248 0.549 ...
 $ X36: num  0.435 0.643 0.374 0.505 0.36 ...
 $ X37: num  0.526 0.31 0.577 0.441 0.591 ...
 $ X38: num  0.00163 0 0 0 0.00155 0.00103 0 0 0 0 ...

响应变量 Ytrain 如下所示:

> str(Ytrain)
  num [1:31132] 2605 56 8 16 214 ...

我检查过 XtrainYtrain 都不包含任何 NA,方法是:

 > sum(is.na(Xtrain))
  [1] 0
 > sum(is.na(Ytrain))
   [1] 0

我假设 rep(0, nobs * nobs * npred)) 函数的无效 "times" 参数的错误消息来自分配给产品 nobs * nobs * npredNA 值,原因是整数溢出。

我不明白的是整数溢出是从哪里来的。 None 我的变量是整数 class 那么我错过了什么?

我检查了 source code for the quantregForest() function and the source code 中由 quantregForest() 函数调用的方法 predict.imp

我发现nobs代表观察次数。在上面的例子中 nobs =length(Ytrain) = 31132 。变量 npred 代表预测变量的数量。它由 npred = ncol(Xtrain)=38 给出。 nprednobs都是class整数,

npred*npred*nobs = 31132*31132*38 = 36829654112.

这就是错误的根本原因,因为:

npred*npred*nobs = 36829654112 > 2147483647,

其中 2147483647 是 R 中的最大整数值。因此出现整数溢出警告并将乘积 npred*npred*nobs 替换为 NA。

最重要的是,为了避免出现错误消息,我在训练模型时必须使用相当少的观察值或在 quantregForest() 函数参数中设置 importance=FALSE。查找变量重要性所需的计算是 非常 内存密集型,即使使用少于 10000 个观察值也是如此。