Last chance! 50% off unlimited learning
Sale ends in
This function implements bivariate interpolation for irregularly spaced input data. Piecewise linear (=barycentric interpolation), bilinear or bicubic spline interpolation according to Akimas method is applied.
interp(x, y = NULL, z, xo = seq(min(x), max(x), length = nx),
yo = seq(min(y), max(y), length = ny),
linear = (method == "linear"), extrap = FALSE,
duplicate = "error", dupfun = NULL,
nx = 40, ny = 40, input="points", output = "grid",
method = "linear", deltri = "shull", h=0,
kernel="gaussian", solver="QR", degree=3,
baryweight=TRUE, autodegree=FALSE, adtol=0.1,
smoothpde=FALSE, akimaweight=TRUE, nweight=25,
na.rm=FALSE)
a list with 3 components:
If output="grid"
:
vectors of xo
, or yo
, if present. Otherwise, their
default, a vector 40 points evenly spaced over the range of the
input x
and y
.
If output="points"
: vectors of xo
and yo
.
If output="grid"
:
matrix of fitted z[i,j]
is computed
at the point z
has
dimensions length(xo)
times length(yo)
.
If output="points"
: a vector with the calculated z values for
the output points as given by xo
and yo
.
If the input was a SpatialPointsDataFrame
a
SpatialPixelsDataFrame
is returned for output="grid"
and a SpatialPointsDataFrame
for output="points"
.
vector of SpatialPointsDataFrame
object (a regular gridded
SpatialPixelsDataFrame
is also allowed). In this case also
an sp data object will be returned.
Missing values are not accepted.
vector of
If left as NULL indicates that x
should be a
SpatialPointsDataFrame
and z
names the variable of
interest in this dataframe.
vector of SpatialPointsDataFrame
x
.
Missing values are not accepted by default, see parameter na.rm
.
x
, y
, and z
must be the same length
(execpt if x
is a SpatialPointsDataFrame
) and may
contain no fewer than four points. The points of x
and
y
should not be collinear if input="grid"
,
as the underlying triangulation in these cases sometimes fails.
interp
is meant for cases in which you have outer
.
If output="grid"
(which is the default): sequence of nx
points between
min(x)
and max(x)
.
If output="points"
: vector of
If output="grid"
(default): sequence of ny
points between
min(y)
and max(y)
.
If output="points"
: vector of xo
.
text, possible values are "grid"
(not yet implemented) and
"points"
(default).
This is used to distinguish between regular and irregular gridded input data.
text, possible values are "grid"
(=default) and
"points"
.
If "grid"
is choosen then xo
and yo
are
interpreted as vectors spanning a rectangular grid of points
akima::interp
works.
In the case of "points"
xo
and yo
have to be
of same length and are taken as possibly irregular spaced output
points no=length(xo)
. nx
and ny
are ignored in this
case. This case is meant as replacement for the pointwise
interpolation done by akima::interpp
. If the input x
is a SpatialPointsDataFrame
and output="points"
then
xo
has to be a SpatialPointsDataFrame
, yo
will
be ignored.
logical, only for backward compatibility with akima::interp
,
indicates if piecewise linear interpolation or Akima splines should be
used.
Please use the new method
argument instead!
text, possible methods are
"linear"
(piecewise linear interpolation within
the triangles of the Delaunay triangulation, also
referred to as barycentric interpolation based on barycentric
coordinates) and "akima"
(a reimplementation for Akimas spline
algorithms for irregular gridded data with the accuracy of a
bicubic polynomial).
method="bilinear"
is only applicable to regular grids
(input="grid"
) and in turn calls bilinear
,
see there for more details.
method="linear"
replaces the old linear
argument of
akima::interp
.
logical, indicates if extrapolation outside the convex hull is intended, this will not work for piecewise linear interpolation!
character string indicating how to handle duplicate data points. Possible values are
"error"
produces an error message,
"strip"
remove duplicate z values,
"mean"
,"median"
,"user"
calculate
mean , median or user defined function (dupfun
) of duplicate
a function, applied to duplicate points if
duplicate= "user"
.
dimension of output grid in x direction
dimension of output grid in y direction
triangulation method used, this argument may later be moved
into a control set together with others related to the spline
interpolation! Possible values are "shull"
(default, sweep
hull algorithm) and "deldir"
(uses packagedeldir
).
bandwidth for partial derivatives estimation, compare locpoly
for details
kernel for partial derivatives estimation, compare locpoly
for details
solver used in partial derivatives estimation, compare locpoly
for details
degree of local polynomial used for partial derivatives
estimation, compare locpoly
for details
calculate three partial derivatives estimators and return a barycentric weighted average.
This increases the accuracy of Akima splines but the runtime is multplied by 3!
try to reduce degree
automatically
tolerance used for autodegree
Use an averaged version of partial derivatives
estimates, by default simple average of nweight
estimates.
Currently disabled by default (FALSE), underlying code still a bit experimental.
apply Akima weighting scheme on partial derivatives estimations instead of simply averaging
size of search neighbourhood for weighting scheme, default: 25
remove points where z=NA
, defaults to FALSE
Albrecht Gebhardt <albrecht.gebhardt@aau.at>, Roger Bivand <roger.bivand@nhh.no>
Moebius, A. F. (1827) Der barymetrische Calcul. Verlag v. Johann Ambrosius Barth, Leipzig, https://books.google.at/books?id=eFPluv_UqFEC&hl=de&pg=PR1#v=onepage&q&f=false
Franke, R., (1979). A critical comparison of some methods for interpolation of scattered data. Tech. Rep. NPS-53-79-003, Dept. of Mathematics, Naval Postgraduate School, Monterey, Calif.
Akima, H. (1978). A Method of Bivariate Interpolation and Smooth Surface Fitting for Irregularly Distributed Data Points. ACM Transactions on Mathematical Software 4, 148-164.
Akima, H. (1996). Algorithm 761: scattered-data surface fitting that has the accuracy of a cubic polynomial. ACM Transactions on Mathematical Software 22, 362--371.
interpp
### Use all datasets from Franke, 1979:
data(franke)
## x-y irregular grid points:
oldseed <- set.seed(42)
ni <- 64
xi <- runif(ni,0,1)
yi <- runif(ni,0,1)
xyi <- cbind(xi,yi)
## linear interpolation
fi <- franke.fn(xi,yi,1)
IL <- interp(xi,yi,fi,nx=80,ny=80,method="linear")
## prepare breaks and colors that match for image and contour:
breaks <- pretty(seq(min(IL$z,na.rm=TRUE),max(IL$z,na.rm=TRUE),length=11))
db <- breaks[2]-breaks[1]
nb <- length(breaks)
breaks <- c(breaks[1]-db,breaks,breaks[nb]+db)
colors <- terrain.colors(length(breaks)-1)
image(IL,breaks=breaks,col=colors,main="Franke function 1",
sub=paste("linear interpolation, ", ni,"points"))
contour(IL,add=TRUE,levels=breaks)
points(xi,yi)
## spline interpolation
fi <- franke.fn(xi,yi,1)
IS <- interp(xi,yi,fi,method="akima",
kernel="gaussian",solver="QR")
## prepare breaks and colors that match for image and contour:
breaks <- pretty(seq(min(IS$z,na.rm=TRUE),max(IS$z,na.rm=TRUE),length=11))
db <- breaks[2]-breaks[1]
nb <- length(breaks)
breaks <- c(breaks[1]-db,breaks,breaks[nb]+db)
colors <- terrain.colors(length(breaks)-1)
image(IS,breaks=breaks,col=colors,main="Franke function 1",
sub=paste("spline interpolation, ", ni,"points"))
contour(IS,add=TRUE,levels=breaks)
points(xi,yi)
## regular grid:
nx <- 8; ny <- 8
xg<-seq(0,1,length=nx)
yg<-seq(0,1,length=ny)
xx <- t(matrix(rep(xg,ny),nx,ny))
yy <- matrix(rep(yg,nx),ny,nx)
xyg<-expand.grid(xg,yg)
## linear interpolation
fg <- outer(xg,yg,function(x,y)franke.fn(x,y,1))
IL <- interp(xg,yg,fg,input="grid",method="linear")
## prepare breaks and colors that match for image and contour:
breaks <- pretty(seq(min(IL$z,na.rm=TRUE),max(IL$z,na.rm=TRUE),length=11))
db <- breaks[2]-breaks[1]
nb <- length(breaks)
breaks <- c(breaks[1]-db,breaks,breaks[nb]+db)
colors <- terrain.colors(length(breaks)-1)
image(IL,breaks=breaks,col=colors,main="Franke function 1",
sub=paste("linear interpolation, ", nx,"x",ny,"points"))
contour(IL,add=TRUE,levels=breaks)
points(xx,yy)
## spline interpolation
fg <- outer(xg,yg,function(x,y)franke.fn(x,y,1))
IS <- interp(xg,yg,fg,input="grid",method="akima",
kernel="gaussian",solver="QR")
## prepare breaks and colors that match for image and contour:
breaks <- pretty(seq(min(IS$z,na.rm=TRUE),max(IS$z,na.rm=TRUE),length=11))
db <- breaks[2]-breaks[1]
nb <- length(breaks)
breaks <- c(breaks[1]-db,breaks,breaks[nb]+db)
colors <- terrain.colors(length(breaks)-1)
image(IS,breaks=breaks,col=colors,main="Franke function 1",
sub=paste("spline interpolation, ", nx,"x",ny,"points"))
contour(IS,add=TRUE,levels=breaks)
points(xx,yy)
## apply interp to sp data:
require(sp)
## convert Akima data set to a sp object
data(akima)
asp <- SpatialPointsDataFrame(list(x=akima$x,y=akima$y),
data = data.frame(z=akima$z))
spplot(asp,"z")
## linear interpolation
spli <- interp(asp, z="z", method="linear")
## the result is again a SpatialPointsDataFrame:
spplot(spli,"z")
## now with spline interpolation, slightly higher resolution
spsi <- interp(asp, z="z", method="akima", nx=120, ny=120)
spplot(spsi,"z")
## now sp grids: reuse stuff from above
spgr <- SpatialPixelsDataFrame(list(x=c(xx),y=c(yy)),
data=data.frame(z=c(fg)))
spplot(spgr)
## linear interpolation
spli <- interp(spgr, z="z", method="linear", input="grid")
## the result is again a SpatialPointsDataFrame:
spplot(spli,"z")
## now with spline interpolation, slightly higher resolution
spsi <- interp(spgr, z="z", method="akima", nx=240, ny=240)
spplot(spsi,"z")
set.seed(oldseed)
Run the code above in your browser using DataLab