Oriol Prat Oriol Prat - 3 months ago 9
R Question

rare error when running my function in R

I have two function. One to train a classifier and one to predict test data. If I run the predict function step by step it works fine, however If I call the predict function I get an error. I can't know what is happening due to the code of the function has no errors compiled manually. I've upload the two functions and the data on Github.you can access here

modelFit=mdp(Class = dades[,1],data=dades[,-1],lambda = 1,info.pred = T)
predict.mdp(modelFit, dades[1:5,-1])
Error in D[row, i] : subscript out of bounds


Thank you for anything you can do to help

Answer

The reason I could see was that in D[row, i] , variable row was overshooting the number of rows in D. row itself is derived from vec.new : for(row in vec.new)

This piece is the culprit :

start=dim(D)[1]
vec.new=(start+1):(start+dim(newdata)[1])

vec.new starts from nrow(D)+1 , so even the first element is actually just beyond the size of D .

You can insert cat(row) in code and see.

I guess you will have to think about what start should be.

Comments