Using the interp function (Akima package), it is possible to draw the surface corresponding to the bivariate interpolation of a data set, see example below (from interp documentation):
library(rgl)
data(akima)
# data visualisation
rgl.spheres(akima$x,akima$z , akima$y,0.5,color="red")
rgl.bbox()
# bivariate linear interpolation
# interp:
akima.li <- interp(akima$x, akima$y, akima$z,
xo=seq(min(akima$x), max(akima$x), length = 100),
yo=seq(min(akima$y), max(akima$y), length = 100))
# interp surface:
rgl.surface(akima.li$x,akima.li$y,akima.li$z,color="green",alpha=c(0.5))
However, the output is only a list describing a set of points, not a general function.
Question: is there any method to obtain a function z = f(x,y) that matches the previously obtained surface ? I know that it works using interp(akima$x, akima$y, akima$z, xo=A, yo=B), but it is very slow.
In two dimensions, the approxfun() function would do the job, but I could not find the equivalent for multiple parameters interpolation.
If you want a linear interpolation so that the surface cross all points, you will not be able to interpolate with a function
z = f(x,y)
, except if the dataset has been simulated through this kind of function.If you are looking for a function
z=f(x,y)
that matches your point set, you will have to build a model with GLM or GAM for instance. However, this induces that the surface will not cross all points data and there will be some residuals.As I use to work with spatial datasets, which means x and y coordinates with a z observation, I will give you some clues in this way.
First, I prepare a dataset for interpolation:
Then, I use your akima interpolation function:
Using rasters
From now, if you want to get interpolated information on some specific points, you can re-use
interp
function or decide to work with a rasterized image. Using rasters, you are then able to increase resolution, and get any spatial position information data.Spatial interpolation (inverse distance interpolation or kriging)
When thinking in spatial for interpolation, I first think about kriging. This will smooth your surface, thus it will not cross every data points.
Interpolate using gam or glm
However, if you want to find a formula like
z = f(x,y)
, you should use GLM or GAM with high degrees of freedom depending on the smooth you hope to see. Another advantage is that you can add other covariates, not only x and y. The model needs to be fitted with a x/y interaction.Here an example with a simple GAM smooth:
Does this answer goes in the right direction for you ?