Do any clever people out there know how to code a quantForestError() prediction on a raster stack instead of a matrix or data.frame? For example...
library(ranger)
library(tidyverse)
library(raster)
library(forestError)
data(iris)
glimpse(iris)
mod<-ranger(Sepal.Length~. -Species,iris,keep.inbag =TRUE,seed=4)#random forest model
#trying to get prediction, and upper and lower prediction interval
#using quantForestError(model,x-training data, new data, y-training data, what I want)$which thing as output
quantForestError(mod,iris[,2:4],iris[10,2:4],iris[,1],what=c("interval"))$pred
#4.823236
quantForestError(mod,iris[,2:4],iris[10,2:4],iris[,1],what=c("interval"))$upper_0.05
#5.091271
quantForestError(mod,iris[,2:4],iris[10,2:4],iris[,1],what=c("interval"))$lower_0.05
#4.293341
#generate raster data
sw <- raster(ncol=10, nrow=10)
set.seed(4)
values(sw) <- runif(ncell(sw),min=iris$Sepal.Width,max=iris$Sepal.Width)
pl <- raster(ncol=10, nrow=10)
set.seed(4)
values(pl) <- runif(ncell(pl),min=iris$Petal.Length,max=iris$Petal.Length)
pw <- raster(ncol=10, nrow=10)
set.seed(4)
values(pw) <- runif(ncell(pw),min=iris$Petal.Width,max=iris$Petal.Width)
all<-stack(sw,pl,pw)
names(all)<-c("Sepal.Width","Petal.Length","Petal.Width")
plot(all)
##try on raster --> error
quantForestError(mod,iris[,2:4],all,iris[,1],what=c("interval"))$pred
#Error in checkXtrainXtest(X.train, X.test) :
#'X.test' must be a matrix or data.frame of dimension 2
#for what it's worth this is how I would get prediction on raster normally with ranger
plot(predict(all, mod, type='se', seed=4, fun = function(model, ...) predict(model, ...)$predictions))
plot(predict(all, mod, type='se', seed=4, fun = function(model, ...) predict(model, ...)$se))
I do understand I could use the se predictions from ranger prediction to get confidence intervals but see the problem here. I think what I need to do is convert the raster to a matrix, make predictions, and convert back to raster, but I'm unsure how to do this. Thanks for any input you have.
I show how to do that with the "terra" package that has replaced "raster".
You can use
terra::predict. You need to name the additional arguments and you need to supply an argument tofunas you want to usequantForestErrorinstead of thepredictmethod from "ranger". To only get "pred" you need to supply a wrapper aroundquantForestError.Example data
Model and predictions