Description Usage Arguments Details Value Author(s) Examples
Grid linear interpolation in arbitrary dimension through repeated one-dimensional interpolations.
1 2 3 4 5 6 |
X |
An object that can be coerced into |
Y |
Response to be interpolated. It must be a vector of
length n equal to the number of nodes. When |
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 d as given by |
interpFun1d |
The function to interpolate in 1d. This
function must have as its first 3 formals 'x', 'y', and 'xout', as
does |
cardinalBasis1d |
Function evaluating an interpolation
Cardinal Basis. This function must have as its first 2 formals
'x', and 'xout'. It must return a matrix with |
intOrder |
Order of the one-dimensional interpolations. Used
only when |
useC |
Logical. Used only when |
trace |
Level of verbosity. |
... |
Further arguments to be passed to |
The one-dimensional interpolation is performed using an
interpolation function given in interpFun1d
or a Cardinal
Basis function given in cardinalBasis1d
. In the second
case, it is required here that the one-dimensional interpolation
method is linear w.r.t. the vector of interpolated values.
For each dimension, a one-dimensional interpolation is carried
out, leading to a collection of interpolation problems each with a
dimension reduced by one.
When a cardinal basis is used, the same basis is used for
all interpolations w.r.t. the same variable, so the number of call
to the cardinalBasis1d
function is equal to the interpolation
dimension, making this method very fast compared to the
general grid interpolation, see the Examples section.
By default, i.e. when both interpFun1d
and cardinalBasis1d
are NULL
, a Lagrange cardinal basis interpolation is used. So
the 1d interpolation is the broken line interpolation.
A single interpolated value if Xout
is 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
.
Yves Deville
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 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.
##=======================================================================
myInterpFun <- function(x, y, xout) approx(x = x, y = y, xout = xout)$y
##=======================================================================
## Example 1
## 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 <- interp_Grid(X = myGD1, Y = Y1, Xout = Xout1,
interpFun1d = myInterpFun)
c(true = myFun1(Xout1), interp = GI1)
##=======================================================================
## Example 2
## 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 <- interp_Grid(X = myGD2, Y = Y2, Xout = Xout2,
interpFun1d = myInterpFun)
c(true = myFun2(Xout2), interp = GI2)
##=======================================================================
## Example 3
## '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 <- interp_Grid(X = myGD2, Y = Y2, Xout = Xout3,
interpFun1d = myInterpFun)
cbind(true = apply(Xout3, 1, myFun2), interp = GI3)
##======================================================================
## Example 4
## '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 <- interp_Grid(X = myGD4, Y = Y4, Xout = Xout4,
interpFun1d = myInterpFun))
t4b <- system.time(GI4b <- interp_Grid(X = myGD4, Y = Y4, Xout = Xout4,
interpFun1d = myInterpFun,
intOrder = 1L:5L))
cbind(true = apply(Xout4, 1, myFun4), inta = GI4a, intb = GI4b)
##======================================================================
## Example 5
## 'n' interpolation, d = 5 using a GIVEN CARDINAL BASIS.
## 'Xout' is a matrix.
##=======================================================================
## Natural spline for use through Cardinal Basis in 'gridIntCB'
myInterpCB5 <- function(x, xout) cardinalBasis_natSpline(x = x, xout = xout)$CB
## Natural spline for use through an interpolation function
myInterpFun5 <- function(x, y, xout) {
spline(x = x, y = y, n = 3 * length(x), method = "natural", xout = xout)$y
}
n <- 10
## Since we use a natural spline, there must be at least 3 levels for
## each dimension.
myGD5 <- Grid(nlevels = c(3, 4, 5, 4, 6))
Y5 <- apply_Grid(myGD5, myFun4)
Xout5 <- matrix(runif(n * d), ncol = d)
t5a <- system.time(GI5a <- interp_Grid(X = myGD5, Y = Y5, Xout = Xout5,
interpFun1d = myInterpFun5))
t5b <- system.time(GI5b <- interp_Grid(X = myGD5, Y = Y5, Xout = Xout5,
interpFun1d = myInterpFun5,
useC = FALSE))
t5c <- system.time(GI5c <- interp_Grid(X = myGD5, Y = Y5, Xout = Xout5,
cardinalBasis1d = myInterpCB5))
df <- data.frame(true = apply(Xout5, 1, myFun4),
gridInt_C = GI5a, gridInt_R = GI5b, gridIntCB = GI5c)
head(df)
rbind(gridInt_C = t5a, gridInt_R = t5b, gridIntCB = t5c)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.