Home > database >  Is there a way to create a list of ppp objects using spatstat in R?
Is there a way to create a list of ppp objects using spatstat in R?

Time:10-01

I am trying to create a list of landscape objects, which have been grouped according to a variable called sim. Ideally I want a list with landscape objects, that can be called i.e.


[[1]]

Planar point pattern: 100 points
window: rectangle = [0, 1000] x [0, 1000] units

[[2]]

Planar point pattern: 100 points
window: rectangle = [0, 1000] x [0, 1000] units

The code I've tried so far is this:


library(tidyr)
library(spatstat)

set.seed(23)

x<-runif(1000)*1000
y<-runif(1000)*1000
sim<-rep(1:10,100)

coordinates<-data.frame(x,y,sim)

coordinates<-coordinates[order(sim),]

emptylist<-vector(mode="list",length=10)
metriccalculation<-function(x,y){
  emptylist<-ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))
}

coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y))

This returns the error:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'coordinates' of mode 'function' was not found

EDIT 1: I tried adding emptylist as a variable in the metriccalculation function, returning the same error.

metriccalculation<-function(x,y,r){
  r<-ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))
}

coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y,emptylist))

EDIT 2: I tried adding an append to the function so that the list is filled. This also returned the same error.

emptylist<-vector(mode="list",length=10)
metriccalculation<-function(x,y,r){
  r<-append(r,(ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))))
}

coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y,emptylist))

EDIT 3:

Okay, I've checked online and there is a possible way of passing multiple variables through an apply function. This leads to:

coordinates%>%group_by(sim)%>%apply(coordinates,metriccalculation,x=coordinates$x,y=coordinates$y,r=emptylist)

And a new error,

Error in d[-MARGIN] : invalid subscript type 'list'

CodePudding user response:

If you really just need a list of 10 point patterns with 100 uniform random points you can do:

library(spatstat)
rslt <- runifpoint(100, win = square(1000), nsim = 10)
rslt # Is a list with extra spatstat class `solist` (spatial object list)
#> List of point patterns
#> 
#> Simulation 1:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 2:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 3:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 4:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 5:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 6:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 7:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 8:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 9:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> Simulation 10:
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
plot(rslt)

If you want to split your data.frame with coordinates into 10 different point patterns you can do (reusing your code):

set.seed(23)
x<-runif(1000)*1000
y<-runif(1000)*1000
sim<-rep(1:10,100)
coordinates<-data.frame(x,y,sim)
coord_list <- split(coordinates[,c("x", "y")], coordinates$sim)
rslt2 <- lapply(coord_list, as.ppp, W = square(1000))
rslt2 # Is a usual list
#> $`1`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`2`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`3`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`4`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`5`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`6`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`7`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`8`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`9`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
#> 
#> $`10`
#> Planar point pattern: 100 points
#> window: rectangle = [0, 1000] x [0, 1000] units
plot(as.solist(rslt2))

  • Related