【问题标题】:Unexpected result with weights in spatstat::density.psp()spatstat::density.psp() 中权重的意外结果
【发布时间】:2022-02-08 10:57:23
【问题描述】:

我正在尝试使用内核密度平滑来绘制可能害虫从车辆交通中逃逸的强度。每条路线都被分解成直线,每条线都有一个整数属性,表示路段经过的次数。但是,当我将此属性用作核密度平滑中的权重时,似乎没有使用权重。

我在下面用两条相邻的直线创建了一个简化的代表。谁能向我解释我如何使 density.psp() 说明一个片段的属性是另一个片段的 2 倍?

非常感谢您的帮助,

乔什

    # Load packages 
    library(spatstat)
    
    # Create the data frame. Note that coordinates are projected to the
    # North America Lambert Conformal Conic system. https://epsg.io/102009
    my_lines <- data.frame(
      fx = c(1252365.22479882, 1233600.0015391),
      fy = c(510853.438463705, 626675.859171899),
      tx = c(1233600.0015391, 1218256.03484937),
      ty = c(626675.859171899, 721347.256108354),
      attrib = c(100, 50)
    ) 
    
    # Creat the observation window
    my_owin <- owin(
      xrange = c(1200000, 1270000),
      yrange = c(500000,  730000)
    ) 
    
    # Create the psp object with 'attrib' as the mark. 
    my_psp <- psp(
      x0 = my_lines$fx, y0 = my_lines$fy,
      x1 = my_lines$tx, y1 = my_lines$ty,
      window = my_owin, marks = my_lines$attrib
    )
    
    # Create the KDS image with 'attrib' as the weights
    kdi_image_weighted <-
      density.psp(
        my_psp,
        kernel = "gaussian",
        sigma = 4000,
        weights = my_psp$attrib,
        edge = FALSE
      )
    
    # Create the KDS image without weights
    kdi_image_unweighted <-
      density.psp(
        my_psp,
        kernel = "gaussian",
        sigma = 4000,
        edge = FALSE
      )
    
    # Plot the weighted and unweighted KDS images. Note that they are the same 
    # despite one being weighted. 
    plot(kdi_image_weighted) 
    plot(kdi_image_unweighted)

【问题讨论】:

  • 嗨 Josh Persi,kdi_image_weighted &lt;- density.psp(...) 代码中的 weights = my_psp$num_trips 似乎没有引用任何内容。也许,试试weights = my_psp$marks。干杯
  • 嗨@lovalery,感谢您接听!在我重命名num_trips 列以使此表示更通用之后,我的代码应该阅读weights = my_psp$attrib。但是,切换weights = my_psp$marks 可以工作并提供预期的输出!非常感谢!

标签: r spatial spatstat


【解决方案1】:

简答:

使用marks()spatstat 包中的对象中提取标记值。示例:

 Z <- density(my_psp, weights=marks(my_psp))

长答案:

此问题与函数density.psp无关。

首先对对象的名称存在误解。在您的示例中,对象my_psp 创建为psp(.... marks=my_lines$attrib)。然后后面的代码期望my_psp 包含一个元素my_psp$attrib。这不是 R 中的评估方式。

当您键入命令psp(...., marks=my_lines$attrib) 时,将计算表达式my_lines$attrib(通过从对象my_lines 中提取名为attrib 的元素)。在评估这些数据后,系统会忘记它从哪里获取数据; attrib 这个名字被忘记了。结果数据(存储在my_lines$attrib 中)将作为参数marks 传递给函数psp

其次,您使用$ 运算符从对象(在本例中为spatstat 包中的"psp" 类对象)提取数据。这对于简单的对象(例如列表)来说是可以的,但对于由包创建的对象来说这是不安全的,因为更新包时内部结构可能会发生变化。

您的示例代码期望对象my_psp 包含具有特定名称的元素,但没有理由这样做。函数psp() 创建了一个类"psp" 的对象,该对象具有一些奇怪的内部结构,在help(psp.object) 中进行了描述。

要从spatstat 包中的对象中提取标记值,目前可以使用$marks,但这可能会改变,强烈建议使用函数marks()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2014-12-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多