【问题标题】:Using ppx and nndist to find nearest neighbour of each type使用 ppx 和 nndist 查找每种类型的最近邻
【发布时间】:2020-08-07 01:21:11
【问题描述】:

对于具有三个协变量和一个治疗指标的数据集,我试图找到每个人最近的邻居。特别是,我想在每个治疗组中找到最近的邻居。

# Generate a treatment indicator factor
treatment <- factor(data_train[,"a"], levels = c("0", "1"), labels = c("Untreated", "Treated"))

# Put the covariate data into 'points' format
pointpattern <- ppx(data = data.frame(data_train[, c("Z1", "Z2", "Z3")], "Treatment" = treatment), coord.type = c("s", "s", "s", "m"))

# Find the nearest neighbour of each type
dists <- nndist(X = pointpattern, by = marks(pointpattern))

但是对象“dists”只是一个向量,它似乎是完全忽略治疗组的最近邻居。

我花了将近一整天的时间试图找出我做错了什么 - 请帮助!

【问题讨论】:

    标签: r spatial nearest-neighbor spatstat


    【解决方案1】:

    您在此示例中使用的函数 nndist.ppx 无法识别参数 by

    这是关于R 中的“类和方法”。函数nndist 是通用的;当您在类"ppx" 的对象上调用nndist 时,系统会调用函数nndist.ppx,这是该类的“方法”。

    您可以通过查看nndist.ppx 的帮助文件来检查其功能;它不支持参数by

    还有其他 nndist 方法可以识别参数 by,例如 nndist.ppp,我猜您正在查看相关文档。

    我们将更新spatstat 中的代码,以便nndist.ppx 也可以使用此功能。

    同时,您可以使用函数nncross.ppx 查找一组点到另一组点的最近距离。以下是获得所需结果的方法:

    Y <- split(pointpattern) # divide into groups
    m <- length(Y) # number of groups
    n <- npoints(pointpattern)
    result <- matrix(, n, m) # final results will go here
    partresults <- list() # collect results for each group here
    for(i in 1:m) {
      Yi <- Y[[i]]
      ni <- npoints(Yi)
      a <- matrix(, ni, m)
      a[,i] <- nndist(Yi)
      for(j in (1:m)[-i]) 
         a[,j] <- nncross(Yi, Y[[j]], what="d")
      partresults[[i]] <- a
    }
    split(result, marks(pointpattern)) <- partresults
    

    那么result 就是你想要的距离矩阵。

    【讨论】:

    • 更新:nndist(X, by) 现在适用于 ppx 对象,在 spatstat 的开发版本(版本 1.63-3.037 及更高版本)中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多