Hello R users, I have numerical data sampled on two grids, each one shifted by 0.5 from the other. For example: grid1 = expand.grid(x=0:3, y=0.5:2.5) grid2 = expand.grid(x=0.5:2.5, y=0:3) gridFinal = expand.grid(x=0.5:2.5, y=0.5:2.5) plot(gridFinal, xlim=c(0,3), ylim=c(0,3), col="black", pch=19) points(grid1, xlim=c(0,3), ylim=c(0,3), col="red", pch=19) points(grid2, xlim=c(0,3), ylim=c(0,3), col="blue", pch=19) I would like to interpolate the quantities on grid1 (red) and grid2 (blue) on the same grid (black). This scenario is very common in geophysical data and models. I only found: - functions in package akima which are designed for irregular grids - krigging in package fields, which also requires irregular spaced data - approx or spline which works in 1D and which I could apply line by line and column by column and use a mean of both estimates I am sure there are plenty of functions already available to do this but searching R-help and the packages site did not help. Pointer to a function/package would be highly appreciated. Eventually, the same scenario will occur in 3D so if the function is 3D capable it would be a plus (but I am sure the solution to this is generic enough to work in nD) Thank you in advance. JiHO --- http://jo.irisson.free.fr/
2/3d interpolation from a regular grid to another regular grid
5 messages · Scionforbai, jiho
- krigging in package fields, which also requires irregular spaced data
That kriging requires irregularly spaced data sounds new to me ;) It cannot be, you misread something (I feel free to say that even if I never used that package). It can be tricky doing kriging, though, if you're not comfortable with a little bit of geostatistics. You have to infer a variogram model for each data set; you possibly run into non-stationarity or anisotropy, which are indeed very well treated (maybe at best) by kriging in one of its forms, but ... it takes more than this list to help you then; basically kriging requires modelling, so it is often very difficult to set up an automatic procedure. I can reccomend kriging if the spatial variability of your data (compared to grid refinement) is quite important. In other simple cases, a wheighted mean using the (squared) inverse of the distance as wheight and a spherical neighbourhood could be the simpliest way to perform the interpolation.
On 2007-December-04 , at 21:38 , Scionforbai wrote:
- krigging in package fields, which also requires irregular spaced data
That kriging requires irregularly spaced data sounds new to me ;) It cannot be, you misread something (I feel free to say that even if I never used that package).
Of Krigging I only know the name and general intent so I gladly line up to your opinion. I just read the description in ?Krig in the package fields which says: " Fits a surface to irregularly spaced data. " But there are probably other Krigging methods I overlooked.
It can be tricky doing kriging, though, if you're not comfortable with a little bit of geostatistics. You have to infer a variogram model for each data set; you possibly run into non-stationarity or anisotropy, which are indeed very well treated (maybe at best) by kriging in one of its forms, but ... it takes more than this list to help you then; basically kriging requires modelling, so it is often very difficult to set up an automatic procedure. I can reccomend kriging if the spatial variability of your data (compared to grid refinement) is quite important.
This was the impression I had too: that Krigging is an art in itself and that it requires you to know much about your data. My problem is simpler: the variability is not very large between grid points (it is oceanic current velocity data so it is highly auto-correlated spatially) and I can get grids fine enough for variability to be low anyway. So it is really purely numerical.
In other simple cases, a wheighted mean using the (squared) inverse of the distance as wheight and a spherical neighbourhood could be the simpliest way to perform the interpolation.
Yes, that would be largely enough for me. I had C routines for 2D polynomial interpolation of a similar cases and low order polynomes gave good results. I just hoped that R had that already coded somewhere in an handy and generic function rather than having to recode it myself in a probably highly specialized and not reusable manner. Thank you very much for you answer and if someone knows a function doing what is described above, that would be terrific. JiHO --- http://jo.irisson.free.fr/
I just read the description in ?Krig in the package fields which says: " Fits a surface to irregularly spaced data. "
Yes, that is the most general case. Regular data location is a subset of irregular. Anyway, kriging, just one g, after the name of Danie Krige, the south african statistician who first applied such method for minig survey.
My problem is simpler
...
So it is really purely numerical.
...
I just hoped that R had that already coded ...
Of course R has ... ;) If your grids are really as simple as the
example you posted above, and you have a really little variability,
all you need is a "moving average", the arithmetic mean of the two
nearest points belonging to grid1 and grid2 respectively. I assume
that your regularly shaped grids are values stored in matrix objects.
The functions comes from the "diff.default" code (downloading the R
source code, I assure, is worth):
my.interp <- function(x, lag = 1)
{
r <- unclass(x) # don't want class-specific subset methods
i1 <- -1:-lag
r <- (r[i1] + r[-length(r):-(length(r)-lag+1)])/2
class(r) <- oldClass(x)
return(r)
}
Finally,
g1 <- apply(grid1val,1,my.interp)
g2 <- apply(grid2val,2,my.interp)
give the interpolations on gridFinal, provided that all gridFinal
points are within the grid1 and grid2 ones.
If you want the mean from 4 points, you apply once more with lag=3,
cbind/rbind to the result columns/rows o NAs, and you calculate the
mean of the points of the two matrixes.
This is the simplest (and quickest) moving average that you can do.
For more complicated examples, and for 3d, you have to go a little
further, but the principle holds.
ScionForbai
On 2007-December-05 , at 16:47 , Scionforbai wrote:
I just read the description in ?Krig in the package fields which says: " Fits a surface to irregularly spaced data. "
Yes, that is the most general case. Regular data location is a subset of irregular. Anyway, kriging, just one g, after the name of Danie Krige, the south african statistician who first applied such method for minig survey.
ooops. sorry about the typo.
My problem is simpler
...
So it is really purely numerical.
...
I just hoped that R had that already coded ...
Of course R has ... ;) If your grids are really as simple as the example you posted above, and you have a really little variability, all you need is a "moving average", the arithmetic mean of the two nearest points belonging to grid1 and grid2 respectively. I assume that your regularly shaped grids are values stored in matrix objects. The functions comes from the "diff.default" code (downloading the R source code, I assure, is worth):
I can imagine it is indeed. I use the source of packages functions very often.
my.interp <- function(x, lag = 1)
{
r <- unclass(x) # don't want class-specific subset methods
i1 <- -1:-lag
r <- (r[i1] + r[-length(r):-(length(r)-lag+1)])/2
class(r) <- oldClass(x)
return(r)
}
Finally,
g1 <- apply(grid1val,1,my.interp)
g2 <- apply(grid2val,2,my.interp)
give the interpolations on gridFinal, provided that all gridFinal
points are within the grid1 and grid2 ones.
If you want the mean from 4 points, you apply once more with lag=3,
cbind/rbind to the result columns/rows o NAs, and you calculate the
mean of the points of the two matrixes.
This is the simplest (and quickest) moving average that you can do.
For more complicated examples, and for 3d, you have to go a little
further, but the principle holds.
Thanks very much. I'll test this soon (and it looks like the vector operation might even be directly translatable in Fortran which is nice since I'll need to do it in Fortran too). Thanks again. JiHO --- http://jo.irisson.free.fr/