gridInt | R Documentation |
***************************************************************************** Grid interpolation in arbitrary dimension.
gridInt(
X,
Y,
Xout,
interpFun = function(x, y, xout) approx(x = x, y = y, xout = xout)$y,
intOrder = NULL,
useC = TRUE,
trace = 1L,
out_of_bounds = stop,
...
)
X |
An object that can be coerced into |
Y |
Response to be interpolated. It must be a vector with
length |
Xout |
Interpolation locations. Can be a vector or a
matrix. In the first case, the length of the vector must be equal
to the spatial dimension |
interpFun |
The function to interpolate. This function must
have as its first 3 formals 'x', 'y', and 'xout', as does
|
intOrder |
Order of the one-dimensional interpolations. Must
be a permutation of |
useC |
Logical. If |
trace |
Level of verbosity. |
out_of_bounds |
Function to handle Xout outside x (default is stop). Then Xout will be bounded by x range. |
... |
Further arguments to be passed to |
The grid interpolation is performed by looping over dimensions. For each dimension, a one-dimensional interpolation is carried out, leading to a collection of interpolation problems each with a dimension reduced by one.
A single interpolated value when Xout
is either a
vector or a row matrix. If Xout
is a matrix with several
rows, the result is a vector of interpolated values, in the order
of the rows of Xout
.
A future multivariate version to come will allow the
simultaneous interpolation of several responses. Most
probably, this possibility will be used by using a different rule
for the object Y
(matrix or list).
When X
is a Grid
object and Y
is a
vector, the user must ensure that the order of nodes is the
same for inputs and output. If so, the order of the dimensions in
X
can be changed using the generalised transposition
aperm
. This will change the order of the univariate
interpolations with possible effects on the computation time
and on the result when the univariate interpolation method is
not linear (w.r.t. the response).
When X
is a data frame or a matrix and Y
is a
vector, the values in Y
are matched to the rows of X
is the same as the order. This situation typically arises when
X
and Y
are columns extracted from a same data
frame.
Yves Deville
set.seed(12345)
##========================================================================
## Select Interpolation Function. This function must have its first 3
## formals 'x', 'y', and 'xout', as does 'approx'. It must also return
## the vector of interpolated values as DOES NOT 'approx'. So a wrapper
## must be xritten.
##=======================================================================
myInterp <- function(x, y, xout) approx(x = x, y = y, xout = xout)$y
##=======================================================================
## ONE interpolation, d = 2. 'Xout' is a vector.
##=======================================================================
myFun1 <- function(x) exp(-x[1]^2 - 3 * x[2]^2)
myGD1 <- Grid(nlevels = c("X" = 8, "Y" = 12))
Y1 <- apply_Grid(myGD1, myFun1)
Xout1 <- runif(2)
GI1 <- gridInt(X = myGD1, Y = Y1, Xout = Xout1, interpFun = myInterp)
c(true = myFun1(Xout1), interp = GI1)
##=======================================================================
## ONE interpolation, d = 7. 'Xout' is a vector.
##=======================================================================
d <- 7; a <- runif(d); myFun2 <- function(x) exp(-crossprod(a, x^2))
myGD2 <- Grid(nlevels = rep(4L, time = d))
Y2 <- apply_Grid(myGD2, myFun2)
Xout2 <- runif(d)
GI2 <- gridInt(X = myGD2, Y = Y2, Xout = Xout2, interpFun = myInterp)
c(true = myFun2(Xout2), interp = GI2)
##=======================================================================
## n interpolations, d = 7. 'Xout' is a matrix. Same grid data and
## response as before
##=======================================================================
n <- 30
Xout3 <- matrix(runif(n * d), ncol = d)
GI3 <- gridInt(X = myGD2, Y = Y2, Xout = Xout3, interpFun = myInterp)
cbind(true = apply(Xout3, 1, myFun2), interp = GI3)
##======================================================================
## n interpolation, d = 5. 'Xout' is a matrix. Test the effect of the
## order of interpolation.
##=======================================================================
d <- 5; a <- runif(d); myFun4 <- function(x) exp(-crossprod(a, x^2))
myGD4 <- Grid(nlevels = c(3, 4, 5, 2, 6))
Y4 <- apply_Grid(myGD4, myFun4)
n <- 100
Xout4 <- matrix(runif(n * d), ncol = d)
t4a <- system.time(GI4a <- gridInt(X = myGD4, Y = Y4, Xout = Xout4,
interpFun = myInterp))
t4b <- system.time(GI4b <- gridInt(X = myGD4, Y = Y4, Xout = Xout4,
interpFun = myInterp,
intOrder = 1L:5L))
cbind(true = apply(Xout4, 1, myFun4), inta = GI4a, intb = GI4b)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.