简体   繁体   English

如何使用逆距离加权误差进行 R 空间插值

[英]How to do R spatial interpolation with an inverse distance weighting error

I am trying to use inverse distance weighting to interpolate some values, but I get this unhelfull error message:我正在尝试使用反距离权重来插入一些值,但我收到了以下无用的错误消息:

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

My data is based on the British National Grid, and this code segment replicates the issue:我的数据基于英国国家电网,这个代码段复制了这个问题:

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)

Can anyone help better interpret the error message?任何人都可以帮助更好地解释错误消息吗? Thanks.谢谢。

Even though this error was caused by a typo, ( formual = instead of formula = in the call to idw ), the error message is a little opaque, so in case anyone comes across a similar error in the future, I thought I would show what it means and why it comes about.尽管此错误是由拼写错误引起的(在对idw的调用中是formual =而不是formula = ),但错误消息有点不透明,所以如果将来有人遇到类似的错误,我想我会展示它意味着什么以及它为什么会出现。

In R you can set up a new generic that allows you to call different methods according to the class of the object that is passed to a function ( plot is the classic example here, where you can call on the same function to plot different types of object, but completely different code is called depending on the object type in order to generate a sensible plot).在 R 中,您可以设置一个新的泛型,允许您根据传递给函数的对象的类来调用不同的方法( plot是这里的经典示例,您可以调用相同的函数来绘制不同类型的对象,但根据对象类型调用完全不同的代码以生成合理的图)。

We can set up a boring example of a generic.我们可以建立一个乏味的泛型示例。 Suppose we want to have a generic function called boring that, when passed two numbers, will simply add them, but when passed two character strings, will paste them together into a single string.假设我们想要一个名为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))

This works as expected:这按预期工作:

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

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

Because we have specified the type of arguments that are allowed, if we try to pass in one number and one character, we get an error telling us that there is no method available for that signature:因为我们已经指定了允许的参数类型,如果我们尝试传入一个数字和一个字符,我们会得到一个错误,告诉我们该签名没有可用的方法:

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

However, if we completely miss out x when calling boring (say by accidentally using z instead of x due to a typo), we don't get the standard R error telling us that x is missing with no default value .然而,如果我们在调用boring时完全错过了x (比如由于打字错误而意外使用了z而不是x ),我们不会得到标准的 R 错误,告诉我们x is missing with no default value Instead, we get the error message reported in this question:相反,我们得到了这个问题中报告的错误消息:

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

So the error just means you have missed out or misnamed a parameter to a generic function.因此该错误仅意味着您遗漏或错误命名了泛型函数的参数。

Created on 2021-11-04 by the reprex package (v2.0.0)reprex 包( v2.0.0 ) 于 2021 年 11 月 4 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM