# problem with example inverse distance weighted (IDW) interpolation

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## problem with example inverse distance weighted (IDW) interpolation

 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
Open this post in threaded view
|
Report Content as Inappropriate

## Re: problem with example inverse distance weighted (IDW) interpolation

 Administrator 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 > [hidden email] > 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: [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo Roger Bivand Economic Geography Section Department of Economics Norwegian School of Economics and Business Administration Helleveien 30 N-5045 Bergen, Norway
Open this post in threaded view
|
Report Content as Inappropriate

## Re: problem with example inverse distance weighted (IDW) interpolation

 In reply to this post by Nerak Dear Roger I tried to reproduce this example, and also ended with an error: ################################################# # Error: is.ppp(X) && is.marked(X) is not TRUE ################################################# While krige is working fine: > idw.out <- krige(zinc ~ 1, meuse, meuse.grid) [inverse distance weighted interpolation] Usually I use krige function for the idw with squared distances, but recently I wanted to test linear distances in idw, without success. Now I saw this post of Nerak so maybe you can help us to solve it. Regards, Melita Percec Tadic Meteorological and Hydrological Service of Croatia. Example together with the info session: sessionInfo() #R version 2.15.0 (2012-03-30) #Platform: i386-pc-mingw32/i386 (32-bit) #locale: #[1] LC_COLLATE=Croatian_Croatia.1250  LC_CTYPE=Croatian_Croatia.1250    LC_MONETARY=Croatian_Croatia.1250 LC_NUMERIC=C                     #[5] LC_TIME=Croatian_Croatia.1250     #attached base packages: #[1] stats     graphics  grDevices utils     datasets  methods   base     #other attached packages: # [1] spatstat_1.26-1    deldir_0.0-19      mgcv_1.7-16        boot_1.3-4         RColorBrewer_1.0-5 classInt_0.1-18    e1071_1.6         # [8] class_7.3-3        gstat_1.0-10       spacetime_0.6-2    xts_0.8-6          zoo_1.7-7          rgdal_0.7-11       maptools_0.8-14   #[15] lattice_0.20-6     sp_0.9-99          foreign_0.8-49     #loaded via a namespace (and not attached): #[1] grid_2.15.0  Matrix_1.0-6 nlme_3.1-103 tools_2.15.0 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.0) ################################################# # Error: is.ppp(X) && is.marked(X) is not TRUE ################################################# spplot(idw.out, "var1.pred")
Open this post in threaded view
|
Report Content as Inappropriate

## Re: problem with example inverse distance weighted (IDW) interpolation

 On 09/13/2012 12:43 PM, melita wrote: > # Error: is.ppp(X) && is.marked(X) is not TRUE Melita, thanks for bringing this up. If you first load package gstat, and then load package spatstat, you'll see the following warning: Attaching package: ‘spatstat’ The following object(s) are masked from ‘package:gstat’:     idw At some stage package spatstat decided to provide inverse distance weighting and and also call the function to do this idw, but using other syntax and assumptions about the arguments provided than gstat does. There's no way for gstat to catch this as it is now, as gstat::idw is not called at all. A solution would be to not load spatstat, or load spatstat after gstat (in which case the conflict will point to spatstat:idw being masked by gstat). The spatstat package authors could solve this conflict if idw in spatstat was also defined as an S4 method, as gstat does, rather than as a function. Now it comes up, I'm curious why, as spatstat suggests, one would want to interpolate point pattern marks. As there are no marks where there are no points, how should the interpolated values be understood? Or is this intended to be done outside the observation window? -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.dehttp://www.52north.org/geostatistics      [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Open this post in threaded view
|
Report Content as Inappropriate