【问题标题】:Nearest-neighbour distances of a point pattern by year点模式的最近邻距离(按年份)
【发布时间】:2021-11-05 08:26:16
【问题描述】:

我希望使用spatstat 按年份计算点模式的最近邻距离 (NND),但出现错误,尽管我看不出与 documentation 中提供的示例有任何区别。正确的做法是什么?

library(spatstat)

#Build point pattern
pp <- structure(list(window = structure(list(type = "rectangle", xrange = c(952727.038715708, 
969663.326063713), yrange = c(1928725.27732809, 1943334.46685032
), units = structure(list(singular = "unit", plural = "units", 
    multiplier = 1), class = "unitname")), class = "owin"), n = 6L, 
    x = c(959238.044802669, 968582.344942023, 960218.207335433, 
    969663.326063713, 964665.091671559, 952727.038715708), y = c(1941763.77812852, 
    1934201.29833662, 1943334.46685032, 1939411.6354699, 1928725.27732809, 
    1931868.51667007), markformat = "vector", marks = c("1994", 
    "1994", "2005", "2005", "2005", "2005")), class = "ppp")

#Calculate NNDs
nndist(pp, by=marks(pp))

#Error in split.ppp(X %mark% idX, f = by, un = FALSE) : 
#  f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks

【问题讨论】:

    标签: r spatstat


    【解决方案1】:

    错误信息给你一个提示:

    > nndist(pp, by=marks(pp))
    Error in split.ppp(X %mark% idX, f = by, un = FALSE) :
      f must be a factor, a logical vector, a tessellation, a window, an image, or the name of a column of marks
    

    它表示marks 应该是一个因子、逻辑向量或曲面细分。

    经检查,pp$marks 是一个字符:

    > str(pp$marks)
     chr [1:6] "1994" "1994" "2005" "2005" "2005" "2005"
    

    将其转换为因子可以解决问题:

    pp$marks <- as.factor(pp$marks)
    nndist(pp, by=marks(pp))
              1994      2005
    [1,] 12021.108  1851.427
    [2,] 12021.108  5321.291
    [3,]  1851.427 10227.359
    [4,]  5321.291 10227.359
    [5,]  6732.880 11797.483
    [6,] 11845.227 12344.920
    

    【讨论】:

    • 谢谢!我确实注意到我的标记形成了一个向量,但假设我的示例属于“标记列的名称”的情况。基spatstat函数nndist没有系统地接受基函数marks的输出是不是有点奇怪?
    • @syre 我认为你误会了。 marks() 是一个 spatstat 函数。问题是,如果您希望点模式的标记代表两个不同的组,则应将它们编码为factors。这是 R 表示分组变量的方式,因此您应该遵循该约定。这有意义吗? @otto-kässi 唯一与您不同的做法是将标记覆盖为代表组的因素,而不仅仅是任意字符串。否则你的代码是完全正确的。
    • nndist 假定ppp 中的marks 元素是一个因素。将非因子元素作为 by 参数提供会导致错误。如果您认为 marks() 应该默认返回因子,您可能需要向 spatstat 开发人员提交错误报告。
    • 我同意错误信息不是超级信息!
    猜你喜欢
    • 2020-01-26
    • 2011-06-18
    • 2020-08-28
    • 2016-10-31
    • 2018-12-02
    • 2011-11-12
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多