1
votes

I have a randomForest model that I am trying to predict back over the study area. I am able to run the predict without errors, and I can see that it produces a raster, but when I try to run writeRaster() or plot(), I get the error Error in .local(.object, ...) without any more information. Here is the code (note that envStack is the raster stack of environmental predictors and rf.full is the randomForest model object):

library(here)
library(raster)
library(randomForest)
setwd(here("species", model_species, "outputs", "grids"))
fn <- paste0(model_run_name, "_raw_", Sys.Date(), ".tif")

outRas <- predict(object = envStack,
                  model = rf.full,
                  type = "prob",
                  index = 2, 
                  na.rm = TRUE,
                  filename = fn,
                  format = "GTiff",
                  overwrite = TRUE,
                  progress = "text")
writeRaster(outRas, filename = fn, format = "GTiff", overwrite = TRUE)

And, here is the error:

Error in .local(.Object, ...) :

The same thing happens if I try to plot the output raster:

plot(outRas)

Error in .local(.Object, ...) :

I'm including a screenshot of the envStack and rf.full objects as well as the outRas raster product from the predict. To check if the issue was a file size issue, I used object.size(outRas), which returned 12832 bytes. I also checked the available space on the drive where I want to store the raster, and there are over 60 GB of free space: screenshot of raster stack, rf model, and output prediction raster objects

I'm not sure why the min and max values are unknown for a lot of the rasters in the stack, but just to make sure the individual rasters weren't corrupt, I tried to plot one with missing min/max values (plot(envStack[[1]])), and it plotted just fine: plot of first raster in envStack

The model object (rf.full) was used successfully to compute various validation metrics, so as far as I know, there are no issues with it, and the folder where I want to save the output raster exists.

enter image description here

When I check the temporary file location with rasterTmpFile(), it returns a file that doesn't exist (although the containing folder does):

rasterTmpFile()
[1] "C:/Users/username/AppData/Local/Temp/Rtmpkxfn1t/raster/r_tmp_2020-02-17_152958_23480_67648.grd"

empty temp folder

I feel like I have encountered this issue before, but I can't remember how I resolved it (or if I resolved it). The related questions I have looked at on here point to file size and file location issues, which don't seem to by me problem. Some hint at issues with format type, but I have saved other rasters as GeoTiffs as recently as last week in earlier steps of this process using writeRaster() with format = "GTiff". Also, the fact that I get the same error when just trying to plot outRas within R makes it seem like there is an issue with the raster itself. Any help is appreciated.

UPDATE: The problem turned out to be that the writeRaster() statement was unnecessary. Taking it out solved the issue. See my answer below for more details.

1

1 Answers

0
votes

I figured out what I was doing wrong. I had that code bit embedded in an if/else try statement, so it was trying to run the predict and write the raster as part of the same run. However, the writeRaster() line is unnecessary since the predict() function includes a filename= argument to save the file. By telling it to write the raster right after that confused R somehow and caused it to throw that error. Thus, I removed the writeRaster() statement, and it solved the problem.