r-spatial / gstat

Spatial and spatio-temporal geostatistical modelling, prediction and simulation
http://r-spatial.github.io/gstat/
GNU General Public License v2.0
195 stars 49 forks source link

predict is not exported #125

Closed glaroc closed 1 year ago

glaroc commented 1 year ago

If I do

library(gstat)
g.prdSph<-gstat::predict(g.dummy,Arbo_mask,nsim=9)
Error: 'predict' is not an exported object from 'namespace:gstat'

predict.gstat() is in the documentation, but doesn't appear to be exported.

edzer commented 1 year ago

If you read ?predict you'll see that predict is exported by namespace stats; gstat merely adds methods.

edzer commented 1 year ago

So, use stats::predict if you must use the :: construct.

glaroc commented 1 year ago

Thanks for the clarification. We used to be able to do unconditional Gaussian simulations on a raster by giving gstat::predict a blank raster. Is that still possible through other functions ?

edzer commented 1 year ago

Please provide a reprex.

glaroc commented 1 year ago

This used to work:

#read data frame with x,y coordinates and variables in other columns
Arbo<-read.csv(file.choose())

# Select a raster tiff file with a defined CRS
Arbo_mask<-rast(file.choose())
Arbo <- st_as_sf(Arbo, coords = c("X","Y"),crs=crs(Arbo_mask))

v <- vgm(1, "Sph", 200)
g.dummy <- gstat(formula = z~1, locations=Arbo, dummy = TRUE, beta = 0,model = v, nmax = 20)
g.prdSph<-predict(g.dummy,Arbo_mask,nsim=9)

Now this gives

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'obj' in selecting a method for function 'coordinates': no slot of name "bbox" for this object of class "SpatRaster"

I can supply the files if needed.

edzer commented 1 year ago

Definitely not a reprex, but isn't that a raster - terra issue?

glaroc commented 1 year ago

Here is a more reproducible example:

data(meuse.grid)
m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data = meuse.grid)
r <- terra::rast(m)
# r <- raster::raster(m) # this gives same error
g.dummy <- gstat(formula = z~1, dummy = TRUE, beta = 0,
                 model = vgm(1,"Exp",15), nmax = 10) 
y <- predict(g.dummy, r, nsim = 4)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'obj' in selecting a method for function 'coordinates': no slot of name "bbox" for this object of class "SpatRaster"
glaroc commented 1 year ago

Ok, it works when the raster is a stars objects. So this works:

data(meuse.grid)
m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data = meuse.grid)
r <- st_as_stars(m)
g.dummy <- gstat(formula = z~1, dummy = TRUE, beta = 0,
                 model = vgm(1,"Exp",15), nmax = 10) 
y <- predict(g.dummy, r, nsim = 4)