[R-sig-Geo] as.im in spatstat::ppm not working as before

Edzer Pebesma edzer@pebe@m@ @end|ng |rom un|-muen@ter@de
Wed May 8 09:21:31 CEST 2024


r.grid.world.SGDF.F[[1]] is a numerical vector, not even a matrix, so 
clearly as.im.default doesn't know what it should do with it.

Maybe there was once a method as.im for r.grid.world.SGDF.F, which is of 
class SpatialGridDataFrame, but no longer - software evolves.

A way you could approach this is converting that object to a stars object:

 > library(stars)
Loading required package: abind
Loading required package: sf
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
 > as.im(st_as_stars(r.grid.world.SGDF.F))
Error: Only projected coordinates may be converted to spatstat class objects

and that points you to another issue you have: spatstat works with data 
in Cartesian coordiantes (R2), not with geodetic coordinates (S2). So 
that's a helpful error message.


On 08/05/2024 01:49, Alexandre Santos via R-sig-Geo wrote:
> Dear Members,
> 
> I'll calculate some old codes for a paper review, and I'm astonished that as.im() for fitting the Poisson model is not working as before. In my example:
> 
> library(raster)
> library(spatstat)
> 
> 
> # Elevation
> r.grid.world <- raster('https://raw.githubusercontent.com/Leprechault/trash/main/wc2.1_10m_elev.tif')
> proj4string(r.grid.world) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
> r.grid.world.SGDF.F <- as(r.grid.world, "SpatialGridDataFrame")
> 
> 
> #Fitting Poisson model
> fit.c <- ppm(nztrees, ~ elev,
> covariates=list(elev=as.im(r.grid.world.SGDF.F[[1]])))
> #
> # Error in as.im.default(r.grid.world.SGDF.F[[1]]) :
> #   Can't convert X to a pixel image
> 
> I tried as matrix and rotate, and the final real-valued pixel image didn't work as a covariate in the model.
> 
> Please, any help with it?
> 
> Thanks in advance
> 
> Alexandre
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma (he/him)
Universität Münster, Institute for Geoinformatics
Heisenbergstrasse 2, 48149 Münster, Germany
Phone: +49 251 8333081



More information about the R-sig-Geo mailing list