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

Roger Bivand Roger.Bivand at nhh.no
Tue Sep 11 21:01:49 CEST 2012


On Tue, 11 Sep 2012, Nerak wrote:

> 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"

Not surprisingly, as both are still data.frame objects, and you haven't 
loaded one of them. This works:

library(gstat)
data(meuse)
coordinates(meuse) <- c("x", "y")
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
idw.out <- idw(zinc ~ 1, meuse, meuse.grid, idp = 2.5)
spplot(idw.out, "var1.pred")

You were omitting important lines in the script.

Roger

>
>
> 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.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list