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)
Run the code above in your browser using DataLab