【问题标题】:Creating ppp objects with lapply (package spatstat)使用 lapply 创建 ppp 对象(包 spatstat)
【发布时间】:2016-06-30 18:17:55
【问题描述】:

我的最终目标是使用包 spatstat 计算数据帧列表的 Clark-Evans-Index (clarkevans.test)。

所以我有一个我的点数据列表:

points.li <- list(structure(list(x.n = c(-1.37977544977995, 0.0787053126116266, 
-6.50583075192879, -9.17021249875416, -19.4146851390704, -22.7380870106472, 
-20.3267566111816, -15.7328116296655, -8.74750043303314, -11.8963575795747
), y.n = c(13.1276911114957, 2.22311850078447, 9.48873515598742, 
2.7986686485412, 2.56632386092958, -0.757078010647191, 6.88269379207495, 
11.5304629645448, 19.131978467755, 28.8757897612883)), row.names = 1098:1107, class = "data.frame", .Names = c("x.n", 
"y.n")), structure(list(x.n = c(0.104714438623701, 1.93357872460516, 
1.51117985822383, 4.47756948027361, 0.710996014054978, -0.727469791776916, 
0.694499984379773, 2.88088318987335, -5.90066975026119, -11.3699018974284
), y.n = c(-5.99908617093835, -9.09677268682439, -12.3075722803524, 
-16.7105167948009, -16.2844860117843, -13.8809505330886, -19.88787745768, 
-20.4985490229505, -14.9797228445106, -17.1780479345837)), row.names = 108:117, class = "data.frame", .Names = c("x.n", 
"y.n")))

> points.li
[[1]]
              x.n       y.n
1098  -1.37977545 13.127691
1099   0.07870531  2.223119
1100  -6.50583075  9.488735
1101  -9.17021250  2.798669
1102 -19.41468514  2.566324
1103 -22.73808701 -0.757078
1104 -20.32675661  6.882694
1105 -15.73281163 11.530463
1106  -8.74750043 19.131978
1107 -11.89635758 28.875790

[[2]]
            x.n        y.n
108   0.1047144  -5.999086
109   1.9335787  -9.096773
110   1.5111799 -12.307572
111   4.4775695 -16.710517
112   0.7109960 -16.284486
113  -0.7274698 -13.880951
114   0.6945000 -19.887877
115   2.8808832 -20.498549
116  -5.9006698 -14.979723
117 -11.3699019 -17.178048

以及绘图坐标列表:

ref.li <- list(structure(list(x.ref = c(-51.957519, -44.640527, 24.976003, 
17.659011), y.ref = c(39.756418, -29.860112, -22.54312, 47.07341
)), class = "data.frame", row.names = c(NA, -4L), .Names = c("x.ref", 
"y.ref")), structure(list(x.ref = c(15.613798, -52.306902, -35.372372, 
32.548328), y.ref = c(40.306747, 23.372217, -44.548483, -27.613953
)), class = "data.frame", row.names = c(NA, -4L), .Names = c("x.ref", 
"y.ref")))

> ref.li
[[1]]
      x.ref     y.ref
1 -51.95752  39.75642
2 -44.64053 -29.86011
3  24.97600 -22.54312
4  17.65901  47.07341

[[2]]
      x.ref     y.ref
1  15.61380  40.30675
2 -52.30690  23.37222
3 -35.37237 -44.54848
4  32.54833 -27.61395

我创建了一个owin 对象列表:

library(spatstat)
bound.li <- lapply(ref.li, function(x) {owin(poly = list(x = x$x.ref, y = x$y.ref))})

> bound.li
[[1]]
window: polygonal boundary
enclosing rectangle: [-51.95752, 24.976] x [-29.86011, 47.07341] units

[[2]]
window: polygonal boundary
enclosing rectangle: [-52.3069, 32.54833] x [-44.54848, 40.30675] units

现在我想创建ppp 对象:

pattern.li <- lapply(points.li, function(x) {ppp(x$x.n, x$y.n, window=bound.li)})

导致:

Error in verifyclass(window, "owin") : 
  argument ‘window’ is not of class ‘owin’

我不知道问题是使用了 owin 对象列表还是我错误地使用了 ppp 函数的 lapply,因为我需要在这里引用两个列表但不知道如何。任何提示如何解决这个问题?

(edit) 我也试过了

mapply(function(x, y) {ppp(x$x.n, x$y.n, window=y)}, x=points.li, y=bound.li)

但这不会返回 ppp 对象列表..

【问题讨论】:

  • 使用以下解决方案之一后,您可能希望将 spatstat 类 solist(空间对象列表)添加到结果中(假设您将列表命名为 pattern.li):pattern.li &lt;- as.solist(pattern.li)。例如,当您绘制整个点模式列表时,它会自动为您制作布局:plot(pattern.li).
  • @EgeRubak 谢谢,这非常有趣和有用!

标签: r lapply spatstat


【解决方案1】:

您将整个owin 对象列表传递给ppp 中的window 参数。您可以使用 Map 同时迭代两个列表。当您尝试mapply 时,它会自动简化结果。您也可以使用SIMPLIFY=FALSE 参数来表示mapply

Map(function(x, y) {ppp(x$x.n, x$y.n, window=y)}, x=points.li, y=bound.li)
#[[1]]
#Planar point pattern: 10 points
#window: polygonal boundary
#enclosing rectangle: [-51.95752, 24.976] x [-29.86011, 47.07341] units
#
#[[2]]
#Planar point pattern: 10 points
#window: polygonal boundary
#enclosing rectangle: [-52.3069, 32.54833] x [-44.54848, 40.30675] units

【讨论】:

    【解决方案2】:

    您的最后一个建议几乎奏效了。你只需要参数SIMPLIFY = FALSE:

    mapply(function(x, y) {ppp(x$x.n, x$y.n, window=y)}, x=points.li, y=bound.li, SIMPLIFY = FALSE)
    

    或者,您可以调用函数as.ppp,将X 作为点坐标的data.frame,将W 作为owin 对象:

    mapply(as.ppp, X = points.li, W = bound.li, SIMPLIFY = FALSE)
    

    【讨论】:

    • @pierre-lafortune 使用Map 的方法在函数式编程的精神上似乎更现代,所以我也赞成这一点。实际上,R 中的 Map 似乎只是 mapply 的包装,避免了简化参数,所以你真的走在正确的轨道上。
    猜你喜欢
    • 2021-11-22
    • 2019-02-14
    • 2020-10-04
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2020-10-03
    • 2021-07-22
    • 2021-12-08
    相关资源
    最近更新 更多