Learn R Programming

akima (version 0.6-3.6)

interp: Gridded Bivariate Interpolation for Irregular Data

Description

These functions implement bivariate interpolation onto a grid for irregularly spaced input data. Bilinear or bicubic spline interpolation is applied using different versions of algorithms from Akima.

Usage

interp(x, y=NULL, z, xo=seq(min(x), max(x), length = nx),
       yo=seq(min(y), max(y), length = ny),
       linear = TRUE, extrap=FALSE, duplicate = "error", dupfun = NULL,
       nx = 40, ny = 40,
       jitter = 10^-12, jitter.iter = 6, jitter.random = FALSE,
       remove = !linear)

Value

list with 3 components:

x,y

vectors of x- and y- coordinates of output grid, the same as the input argument xo, or yo, if present. Otherwise, their default, a vector 40 points evenly spaced over the range of the input x.

z

matrix of fitted z-values. The value z[i,j] is computed at the x,y point xo[i], yo[j]. z has dimensions length(xo) times length(yo).

If input is a SpatialPointsDataFrame a

SpatialPixelssDataFrame is returned.

Arguments

x

vector of x-coordinates of data points or a SpatialPointsDataFrame object. Missing values are not accepted.

y

vector of y-coordinates of data points. Missing values are not accepted.

If left as NULL indicates that x should be a SpatialPointsDataFrame and z names the variable of interest in this dataframe.

z

vector of z-coordinates of data points or a character variable naming the variable of interest in the SpatialPointsDataFrame x.

Missing values are not accepted.

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, i.e, they should not fall on the same line (two vectors x and y such that y = ax + b for some a, b will not produce menaningful results). Some heuristics is built in to avoid this case by adding small jitter to x and y when the number of NA values in the result exceeds 10%.

interp is meant for cases in which you have x, y values scattered over a plane and a z value for each. If, instead, you are trying to evaluate a mathematical function, or get a graphical interpretation of relationships that can be described by a polynomial, try outer().

xo

vector of x-coordinates of output grid. The default is 40 points evenly spaced over the range of x. If extrapolation is not being used (extrap=FALSE, the default), xo should have a range that is close to or inside of the range of x for the results to be meaningful.

yo

vector of y-coordinates of output grid; analogous to xo, see above.

linear

logical -- indicating wether linear or spline interpolation should be used.

extrap

logical flag: should extrapolation be used outside of the convex hull determined by the data points?

duplicate

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 z values.

dupfun

a function, applied to duplicate points if duplicate= "user".

nx

dimension of output grid in x direction

ny

dimension of output grid in y direction

jitter

Jitter of amount of diff(range(XX))*jitter (XX=x or y) will be added to coordinates if collinear points are detected. Afterwards interpolation will be tried once again.

Note that the jitter is not generated randomly unless jitter.random is set to TRUE. This ensures reproducable result. tri.mesh of package tripack uses the same jitter mechanism. That means you can plot the triangulation on top of the interpolation and see the same triangulation as used for interpolation, see examples below.

jitter.iter

number of iterations to retry with jitter, amount will be multiplied in each iteration by iter^1.5

jitter.random

logical, see jitter, defaults to FALSE

remove

logical, indicates whether Akimas removal of thin triangles along the border of the convex hull should be performed, experimental setting! defaults to !linear, so it will be left out for linear interpolation by default. For some point configurations it is the only available option to skip this removal step.

Details

If linear is TRUE (default), linear interpolation is used in the triangles bounded by data points. Cubic interpolation is done if linear is set to FALSE. If extrap is FALSE, z-values for points outside the convex hull are returned as NA. No extrapolation can be performed for the linear case.

The interp function handles duplicate (x,y) points in different ways. As default it will stop with an error message. But it can give duplicate points an unique z value according to the parameter duplicate (mean,median or any other user defined function).

The triangulation scheme used by interp works well if x and y have similar scales but will appear stretched if they have very different scales. The spreads of x and y must be within four orders of magnitude of each other for interp to work.

References

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.

R. J. Renka (1996). Algorithm 751: TRIPACK: a constrained two-dimensional Delaunay triangulation package. ACM Transactions on Mathematical Software. 22, 1-8.

R. J. Renka and Ron Brown (1998). Remark on algorithm 761. ACM Transactions on Mathematical Software. 24, 383-385.

See Also

contour, image, approx, spline, aspline, outer, expand.grid, link{franke.data}.