Quantcast

problem with example inverse distance weighted (IDW) interpolation

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

problem with example inverse distance weighted (IDW) interpolation

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

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

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

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

melita
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")
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

edzer


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.de
http://www.52north.org/geostatistics      [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

melita
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.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

If you reply to this email, your message will be added to the discussion below:
http://r-sig-geo.2731867.n2.nabble.com/problem-with-example-inverse-distance-weighted-IDW-interpolation-tp7580941p7580952.html
To unsubscribe from problem with example inverse distance weighted (IDW) interpolation, click here. NAML
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Marcelino de la Cruz
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.uni-muenster.de
>http://www.52north.org/geostatistics      [hidden email]
>
>_______________________________________________
>R-sig-Geo mailing list
>[hidden email]
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo

___________________________________________________

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.
E-28933 Móstoles (Madrid)
España
Fax: +34 91 664 7490

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Loading...