When using different predict functions we get different results.
We have saved initial raster and a model. Then we decided to compare the results coming out of the predict function of different packages: randomForest:::predict.randomForest
, stats::predict
, and raster::predict
. Of course for the first two, we need to convert the input raster to a matrix and for that we used raster::as.matrix()
function.
Here are the results of our evaluation. Note: That a factor needs to be transposed since it differs in how a vector is built (one uses row by row, while the other one column by column).
pred1 <- randomForest:::predict.randomForest(object = models[[1]], newdata = ma_rast, type = "response")
res1 <- t(matrix(as.numeric(predv1), ncol(rast[[1]]), nrow(rast[[1]])))
res1[!is.na(res1)]
12221111111112111111111112111111111111111222121222111111111112222222112222211111111211112222222222222222111111111211111112122222222221221111111111212211211111221122222222222212111111112222222222212221⋯22222222222222222211222222222222222222222222222222112222222222222222222222222211222222222222222222222112222222222222222222222222222222222222222211222222222221222221121222222222222211222222222222222222
pred2 <- stats::predict(object = models[[1]], newdata = ma_rast, type = "response")**
res2 <- t(matrix(as.numeric(pred2), ncol(rast[[1]]), nrow(rast[[1]])))
res2[!is.na(res2)]
12221111111112111111111112111111111111111222121222111111111112222222112222211111111211112222222222222222111111111211111112122222222221221111111111212211211111221122222222222212111111112222222222212221⋯22222222222222222211222222222222222222222222222222112222222222222222222222222211222222222222222222222112222222222222222222222222222222222222222211222222222221222221121222222222222211222222222222222222 `
prev3 <- raster::predict(object = rast, model = models[[1]], type = "response")**
res3 <- raster::as.matrix(pred3)
res3[!is.na(res3)]
22221111111112211111111112221111111111111222121222111111111112222222112222211111211211112222222222222222111111111221111112122222222221221111111111212211211111221122222222222211111111112222222222222221⋯22222222222222222211222222222222222222222222222222112222222222222222222222222211222222222222222222222112222222222222222222222222222222222222222211222222222221222221111222222222222211222222222222222222
As we can see, the results between raster::predict
and the randomForest::predict
slightly differ. @carstenn any idea why?
In case it is difficult to see, many this photo helps you.