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.
CodePudding user response:
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.
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).
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.
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
. 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)