【问题标题】:How to do R spatial interpolation with an inverse distance weighting error [closed]如何使用反距离加权误差进行 R 空间插值 [关闭]
【发布时间】:2021-12-18 17:05:23
【问题描述】:

我正在尝试使用反距离加权来插入一些值,但我收到了这个 unhelfull 错误消息:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘idw’ for signature ‘"missing", "SpatialPointsDataFrame"’

我的数据基于英国国家电网,此代码段复制了该问题:

library(gstat)
library(sp)

set.seed(1)
x <- sample(30000, 1000) + 400000
y <- sample(30000, 1000) + 410000
z <- runif(1000)
source.spdf <- data.frame(x, y, z)
coordinates(source.spdf) <- ~x+y

x <- sample(30000, 100) + 400000
y <- sample(30000, 100) + 410000
destination.spdf <- data.frame(x, y)
coordinates(destination.spdf)<- ~x+y

idw.fit<-idw(formual=z~1, locations=source.spdf, newdata=destination.spdf, idp=2.0)

谁能帮助更好地解释错误信息?谢谢。

【问题讨论】:

  • 我认为这只是一个错字。如果您将formual=z~1 更改为formula=z~1,那么它似乎可以按预期工作。错误消息是因为 formula “丢失”
  • @AllanCameron 可能值得将其作为答案,因为错误消息似乎不在网络上的其他地方,因此它可能对其他人有用
  • 作为debugonce(idw) 的细心读者值得一试,在 gstat 中展示了广泛的类型和错误覆盖范围,但最终不会告诉您您输入了错误的“formual”。
  • Stephen - 我已经尝试用一个最小的可重现示例来解释错误是如何产生的
  • @stevec 我没有意识到它是如此晦涩难懂。我已经写了一个解释它是如何产生的。谢谢

标签: r interpolation spatial


【解决方案1】:

即使这个错误是由拼写错误引起的(formual = 而不是formula = 在对idw 的调用中),错误消息有点不透明,所以万一有人在未来,我想我会展示它的含义以及它的发生原因。

在 R 中,您可以设置一个新的泛型,允许您根据传递给函数的对象的类调用不同的方法(plot 是这里的经典示例,您可以在其中调用相同的函数绘制不同类型的对象,但根据对象类型调用完全不同的代码以生成合理的绘图)。

我们可以设置一个泛型的无聊示例。假设我们想要一个名为 boring 的通用函数,当传递两个数字时,它会简单地将它们相加,但当传递两个字符串时,会将它们粘贴到一个字符串中。

setGeneric("boring", function(x, y, ...) standardGeneric("boring"))
#> [1] "boring"

setMethod("boring", c("numeric", "numeric"), function(x, y) x + y)
setMethod("boring", c("character", "character"), function(x, y) paste(x, y))

这按预期工作:

boring(x = 1, y = 2)
#> [1] 3

boring(x = "a", y = "b")
#> [1] "a b"

因为我们已经指定了允许的参数类型,所以如果我们尝试传入一个数字和一个字符,我们会收到错误消息,告诉我们该签名没有可用的方法:

boring(x = 1, y = "b")
#> Error in (function (classes, fdef, mtable) : unable to find an inherited method 
#> for function 'boring' for signature '"numeric", "character"'

但是,如果我们在调用boring 时完全错过了x(比如由于拼写错误而意外使用z 而不是x),我们不会得到标准R 错误告诉我们@ 987654334@。相反,我们收到此问题中报告的错误消息:

boring(z = 1, y = 2)
#> Error in (function (classes, fdef, mtable) : unable to find an inherited method 
#> for function 'boring' for signature '"missing", "numeric"'

所以这个错误只是意味着你错过了或错误命名了泛型函数的参数。

reprex package (v2.0.0) 于 2021 年 11 月 4 日创建

【讨论】:

    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2017-01-28
    • 2023-03-04
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多