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 
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://rsiggeo.2731867.n2.nabble.com/problemwithexampleinversedistanceweightedIDWinterpolationtp7580941.html > Sent from the Rsiggeo mailing list archive at Nabble.com. > > _______________________________________________ > RsigGeo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/rsiggeo >  Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 email: [hidden email] _______________________________________________ RsigGeo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/rsiggeo
Roger Bivand
Department of Economics Norwegian School of Economics Helleveien 30 N5045 Bergen, Norway 
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 (20120330) #Platform: i386pcmingw32/i386 (32bit) #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.261 deldir_0.019 mgcv_1.716 boot_1.34 RColorBrewer_1.05 classInt_0.118 e1071_1.6 # [8] class_7.33 gstat_1.010 spacetime_0.62 xts_0.86 zoo_1.77 rgdal_0.711 maptools_0.814 #[15] lattice_0.206 sp_0.999 foreign_0.849 #loaded via a namespace (and not attached): #[1] grid_2.15.0 Matrix_1.06 nlme_3.1103 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") 
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.unimuenster.de http://www.52north.org/geostatistics [hidden email] _______________________________________________ RsigGeo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/rsiggeo 
Thanks, Edzer.
I had a feeling this is the case, that idw is called from spatstat instead from gstat in my session. So what I did is detach and call the gstat again, so it can overrides spatstat idw function. detach(package:gstat) library(gstat) PS: I appreciate the live streaming of the lectures from GEOSTAT 2012 in Muenster last week. Sincerely, Melita. At 13:03 13.9.2012, you wrote: 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.unimuenster.dehttp://www.52north.org/geostatistics [hidden email] _______________________________________________ RsigGeo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/rsiggeo 
In reply to this post by edzer
At 13:02 13/09/2012, Edzer Pebesma wrote:
[...] 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? If you deal with multivariate point patterns maybe you could find useful considering the effects of the marks of one type of points over points of the other types... Cheers, Marcelino > >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.unimuenster.de >http://www.52north.org/geostatistics [hidden email] > >_______________________________________________ >RsigGeo mailing list >[hidden email] >https://stat.ethz.ch/mailman/listinfo/rsiggeo ___________________________________________________ Marcelino de la Cruz Rot Departamento de Biología y Geología Escuela Superior de Ciencias Experimentales y Tecnología Universidad Rey Juan Carlos c/ Tulipán, s/n. E28933 Móstoles (Madrid) España Fax: +34 91 664 7490 _______________________________________________ RsigGeo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/rsiggeo 
Free forum by Nabble  Edit this page 