My first guess would be that your code is using an incorrect value for i, and also your predict() function is returning a 2D matrix, instead of a vector.