Question: How do I format the 'const' argument for the predict function in the Terra package?
Background and details:
I am using Terra's predict function to create spatial maps from a model. The function worked as expected when I created full rasters for my three constant values. When I saw predict has an argument 'const' for constants, I decided to use that instead. Unfortunately, the help document doesn't specify how to structure the data beyond that they should be in a dataframe.
Argument description for 'const' from ?terra::predict:
data.frame. Can be used to add a constant value as a predictor variable so that you do not need to make a SpatRaster layer for it
The argument is not used in any of the examples listed at the bottom of the document either.
What I tried:
I tried formating the dataframe as a single row with named columns for each constant value that is required in the model. The function accepted it with the warning:
Warning messages: 1: In data.frame(..., check.names = FALSE) : row names were found from a short variable and have been discarded
But this produced clearly incorrect values in the resulting outputs. Possibly it was just using the first value for all three constants, but I don't know.
I also tried formating the dataframe with two columns, one for the variable name and one for the variable values. This failed with the following error:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'writeRaster': arguments imply differing number of rows: 10000, 3
10000 may be referring to the dimenions of the raster data (10k x 10k) and the 3 is likely referring to the three rows in the constant dataframe I supplied.
I don't want to go back to writing rasters for each constant because I am producing many maps and each one would require 3 10k x 10k rasters to be generated with the constant values, which is time consuming and may lead to memory issues.
Any suggestions would be greatly appreciated!