rh2opredictterra

Error while using predict function for h2o.deeplearning model on raster stack


I am trying to use h2o.deeplearning model to predict on raster data. It returns me the following error

Error: Not compatible with requested type: [type=character; target=double].

Here is a minimal, reproducible, self-contained example

library(terra)
library(h2o)
library(tidyverse)

h2o.init()

# create a RasterStack or RasterBrick with with a set of predictor layers
logo <- rast(system.file("external/rlogo.grd", package="raster"))
names(logo)

# known presence and absence points
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
              66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,
              22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
              99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
              37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)

# extract values for points
xy <- rbind(cbind(1, p), cbind(0, a))
v <- data.frame(cbind(pa=xy[,1], terra::extract(logo, xy[,2:3]))) %>% 
  mutate(pa = as.factor(pa))

str(v) 

#### Import data to H2O cluster
df <- as.h2o(v)

#### Split data into train, validation and test dataset
splits <- h2o.splitFrame(df, c(0.70,0.15), seed=1234)
train  <- h2o.assign(splits[[1]], "train.hex")
valid  <- h2o.assign(splits[[2]], "valid.hex")
test   <- h2o.assign(splits[[3]], "test.hex")

#### Create response and features data sets
y <- "pa"
x <- setdiff(names(train), y)

### Deep Learning Model
dl_model <- h2o.deeplearning(training_frame=train,                      
  validation_frame=valid,                    
  x=x,                                       
  y=y,                                      
  standardize=TRUE,                          
  seed=125)

dnn_pred <- function(model, data, ...) {
  predict(model, newdata=as.h2o(data), ...)
}

p <- predict(logo, model=dl_model, fun=dnn_pred)
plot(p)

Solution

  • h2o.predict returns an "H2OFrame". That is not expected as almost all predict methods return a data.frame. You transform the input data to a H2OFrame, but you also need to transform the output to a data.frame:

    dnn_pred <- function(model, data, ...) {
       predict(model, newdata=as.h2o(data), ...) |> as.data.frame()
    }
    
    p <- predict(logo, model=dl_model, fun=dnn_pred)
    plot(p, nr=1, mar=c(1,2,1,4))
    

    enter image description here