【问题标题】:Is there a way to create a list of ppp objects using spatstat in R?有没有办法在 R 中使用 spatstat 创建 ppp 对象列表?
【发布时间】:2021-11-22 04:56:54
【问题描述】:

我正在尝试创建一个景观对象列表,这些对象已根据名为 sim 的变量进行分组。理想情况下,我想要一个包含景观对象的列表,可以称为即


[[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

到目前为止我尝试过的代码是这样的:


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))

这会返回错误:

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

编辑 1:我尝试在 metriccalculation 函数中添加空列表作为变量,返回相同的错误。

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))

编辑 2:我尝试在函数中添加一个追加,以便填充列表。这也返回了同样的错误。

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))

编辑 3:

好的,我在网上查了一下,有一种方法可以通过 apply 函数传递多个变量。这导致:

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

还有一个新错误,

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

【问题讨论】:

  • 按变量 sim 分组的列表是什么意思?什么是分组列表?如果需要,您可以创建一个嵌套列表,使其有 10 个条目(对应于 sim 1 - 10),然后每个条目都有一个点模式列表。那是你要的吗?你目前所拥有的似乎很复杂。并不是说coordinates 是长度为 3 的 data.frame(因此是列表),并且您对 lapply 的调用然后迭代这 3 列(x、y、sim),这很可能不是您想要的。清楚地说明您想要什么作为最终结果,我们或许可以提供帮助。
  • "您可以根据需要制作一个嵌套列表,使其有 10 个条目(对应于 sim 1 - 10),然后每个条目都有一个点模式列表。这就是您想?”是的,这正是我想要的。 “并不是说坐标是长度为 3 的 data.frame(因此是列表),并且您对 lapply 的调用然后迭代这 3 列(x、y、sim),这很可能不是您想要的。”我不看看它不是一个data.frame,因为我创建了一个``` 坐标
  • data.frame 只是一个特殊的list,其中所有元素的长度相同。所以coordinates 既是data.frame 又是list。在下面的答案中,我没有创建一个嵌套列表,而只是一个简单的列表,其中每个元素都是一个单点模式。我认为这就是您想要的,但如果每个 sim 值都有多个点模式,则可能不是。

标签: r function tidyr spatstat


【解决方案1】:

如果你真的只需要一个包含 100 个统一随机数的 10 个点模式的列表 你可以做的点:

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)

如果您想将带有坐标的 data.frame 拆分为 10 个不同的 你可以做的点模式(重用你的代码):

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))

【讨论】:

  • 非常感谢您,我非常感谢您的贡献。此外,我学到了很多东西。
  • 很高兴帮助@opensauce。请按答案旁边的复选标记,将其标记为已接受的答案,供以后的读者阅读。
猜你喜欢
  • 2016-06-30
  • 2017-03-23
  • 2013-08-04
  • 2021-05-01
  • 1970-01-01
  • 2019-04-24
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
相关资源
最近更新 更多