简体   繁体   中英

Debugging Code that uses predict() in R

When running this code:

predict(rfm, x[split.idx[[i]], sig_otu], type="prob")

I get this error message:

Error in x[, vname, drop = FALSE] : subscript out of bounds

Does this error message arise from incorrect bracketing? I've tried re-arranging brackets, adding parentheses, etc. However, I will still get this error message.

split.idx is a list of 8 str(split.idx)

List of 8

 $ 1: Named int [1:10] 2 3 13 17 34 40 72 70 30 27
  ..- attr(*, "names")= chr [1:10] "01" "09" "017" "025" ...
 $ 2: Named int [1:10] 21 32 67 52 12 55 36 37 24 31
  ..- attr(*, "names")= chr [1:10] "02" "010" "018" "026" ...
 $ 3: Named int [1:10] 49 58 64 8 54 62 1 41 39 35
  ..- attr(*, "names")= chr [1:10] "03" "011" "019" "027" ...
 $ 4: Named int [1:10] 46 59 20 43 47 4 57 76 33 29
  ..- attr(*, "names")= chr [1:10] "04" "012" "020" "028" ...
 $ 5: Named int [1:10] 42 45 69 71 44 38 7 10 9 78
  ..- attr(*, "names")= chr [1:10] "05" "013" "021" "029" ...
 $ 6: Named int [1:10] 15 5 73 61 63 25 28 48 53 14
  ..- attr(*, "names")= chr [1:10] "06" "014" "022" "030" ...
 $ 7: Named int [1:10] 68 6 23 11 65 16 66 26 22 56
  ..- attr(*, "names")= chr [1:10] "07" "015" "023" "031" ...
 $ 8: Named int [1:9] 51 60 74 75 50 19 77 18 79
  ..- attr(*, "names")= chr [1:9] "08" "016" "024" "032" ...

This is the type of variable sig_otu is:

str(sig_otu)
Named int [1:56] 12 26 44 50 79 131 144 178 240 253 ...
   - attr(*, "names")= chr [1:56] "" "" "" "" ...

This is the value assigned to rfm:

rfm <- randomForest(x[-split.idx[[i]]], sig_otu], y[-split.idx[[i]]])

X is a large matrix with dim [1] 79, 1924.

Y is discrete factor with length 79 and 2 levels.

I hope that my question is clear enough to answer.

i refers to this line that I ran earlier in the code: for(i in 1:k) and I set k=8.

> dim(x)[2] [1] 1924

> max(sig_otu) [1] 1921

Somthing that worked for me was:

  1. Editing the predict.randomForest function. To do so type randomForest:::predict.randomForest (I assume the randomForest package is loaded).
  2. Renaming the function. For example, myrandomForestpredictfun=prediccion.rf=function (object, newdata, type = "response", norm.votes = TRUE, predict.all = FALSE, proximity = FALSE, nodes = FALSE, cutoff, ...)
  3. Commenting the line that contains "x <- x[, vname, drop = FALSE]"
  4. Using your new function to get predictions on the training set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM