[R-sig-Geo] problem with example inverse distance weighted (IDW) interpolation

Nerak nerak.t at hotmail.com
Tue Sep 11 20:48:31 CEST 2012


Hi all,

I'm new to spatial analysis in R and I'm starting to learn how to use it. 
I have daily data from 14 rain gauges and want to interpolate data between
this gauges to have a value for certain points in the area I'm working with.
I did Inverse Distance Weighted (IDW) interpolation in QGIS and extracted
the data for the specific coordinates but since I have to do it for at least
365 days (to begin with), I'm looking for a faster way. As spatial analysis
are also possible in R, I was thinking of making a kind of loop to do it.
 
I was looking for an example to try that I could understand how it works and
how I should adapt the script to my data. Therefore I was hoping that the
book 'Applied spatial analysis in R' would have a reproducible working
example, but I get errors when I try the example...

library(sp)
data(meuse)
library(gstat)
idw.out <- idw(zinc ~ 1, meuse, meuse.grid, idp = 2.5)

But I get the error:

Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "idw", for signature
"formula", "data.frame" 


Maybe there is something wrong with the class of my data? I tried with a
normal dataframe, SpatialPixelsDataFrame, SpatialPointsDataFrame, ...

Tried to define the coordinates in different ways...

data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
meuse.grid <- as(meuse.grid, "SpatialPixelsDataFrame")
coordinates(meuse.grid) = ~x+y

This is what the book gives:
> library(gstat)
> idw.out <- idw(zinc ~ 1, meuse, meuse.grid, idp = 2.5)
[inverse distance weighted interpolation]
> as.data.frame(idw.out)[1:5, ]
var1.pred var1.var x y
1 701.9621 NA 181180 333740
2 799.9616 NA 181140 333700
3 723.5780 NA 181180 333700
4 655.3131 NA 181220 333700
5 942.0218 NA 181100 333660

I'm using R version 2.14.2  Could this be a problem that the package gstat
isn't compatible with it?
I'm not familiar with this IDW function in R and still have to figure out
how I should adapt my own data to the right format but I think I first
should be able to understand the example and let it work.
Maybe somebody knows what I should do with this error to let it work? My
apologizes if I should have been able to find an existing post on the
mailinglist concerning this, I tried to search for it, but didn't found
something that could solve the error.

Kind regards,
Nerak




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/problem-with-example-inverse-distance-weighted-IDW-interpolation-tp7580941.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list