簡體   English   中英

如何使用逆距離加權誤差進行 R 空間插值

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

我正在嘗試使用反距離權重來插入一些值,但我收到了以下無用的錯誤消息:

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)

任何人都可以幫助更好地解釋錯誤消息嗎? 謝謝。

盡管此錯誤是由拼寫錯誤引起的(在對idw的調用中是formual =而不是formula = ),但錯誤消息有點不透明,所以如果將來有人遇到類似的錯誤,我想我會展示它意味着什么以及它為什么會出現。

在 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 錯誤,告訴我們x is missing with no default value 相反,我們得到了這個問題中報告的錯誤消息:

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 包( v2.0.0 ) 於 2021 年 11 月 4 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM