Skip to content

as.im in spatstat::ppm not working as before

5 messages · ASANTOS, Roger Bivand, Roozbeh Valavi +1 more

#
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.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:

  
    
#
as.im() was in maptools, which was retired last year. If you need to recreate old work, install maptools from source from the CRAN package archive
https://cran.r-project.org/src/contrib/Archive/maptools/.

For non-archival work, update the workflow. See also https://github.com/r-spatial/evolution/issues/8.

Hope this clarifies,

Roger

---
Roger Bivand
Emeritus Professor
Department of Economics
Norwegian School of Economics, Bergen, Norway

________________________________
Fra: R-sig-Geo <r-sig-geo-bounces at r-project.org> p? vegne av Edzer Pebesma <edzer.pebesma at uni-muenster.de>
Sendt: onsdag, mai 8, 2024 9:28:54 a.m.
Til: r-sig-geo at r-project.org <r-sig-geo at r-project.org>
Emne: Re: [R-sig-Geo] as.im in spatstat::ppm not working as before

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:
--
Edzer Pebesma (he/him)
Universit?t M?nster, Institute for Geoinformatics
Heisenbergstrasse 2, 48149 M?nster, Germany
Phone: +49 251 8333081

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7CRoger.Bivand%40nhh.no%7Cd2a2b64634a7462f860708dc6f3082d5%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C638507501339856786%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=6H%2F9HQ%2BV1nyf2AdWabq0eoG3b8zqCQEC3ZC%2BFoRYvUI%3D&reserved=0<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
2 days later
#
This might also help:

library(spatstat)
library(terra)

.raster_to_im <- function(x){
        r <- as.data.frame(x, xy = TRUE)
         im <- spatstat.geom::as.im(r) return(im)
}

 # to generalise the spatstat.geom::as.im to SpatRaster and RasterLayer
as.im.SpatRaster <- function(x){ .raster_to_im(x) } as.im.RasterLayer <-
function(x){ .raster_to_im(x) }


I haven?t used it in a while so there might be a direct support in the
spatstat.geom now!


Roozbeh Valavi,
Biodiversity Modeller & Spatial Ecologist
Senior Research Scientist | CSIRO Environment, VIC, Australia
*Twitter*: @ValaviRoozbeh | ResearchGate
<https://www.researchgate.net/profile/Roozbeh-Valavi> | Google Scholar
<https://scholar.google.co.uk/citations?user=3m0jCHwAAAAJ&hl=en&oi=ao>
On Wed, 8 May 2024 at 7:41?PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:

            

  
  
#
On 11/05/2024 10:21, Roozbeh Valavi wrote:
the solution below will not raise the error if x has geographic 
(unprojected) coordinates, a case for which you should not use spatstat.