interpolation - interpolate in a 3 dimensional spline in R -
i fit surface values:
x = 1:10 y = 10:1 z = sample(1:10,10)
i fun spline_function(z ~ x + y)
. actual spline functions in r seem take x
, y
cannot have 2 dimensional x coordinate. way in r? aware of loess
local polynomials etc. splines looking for.
one option mgcv package comes versions of r. has isotropic penalised regression splines of 2 or more variables via s()
, anisotropic penalised regression splines of 2 or more variables via tensor products , te()
.
if don't want penalised regression splines, can use argument fx = true
fix known degree of freedom splines.
here example ?te
# following shows how tensor pruduct deals nicely # badly scaled covariates (range of x 5% of range of z ) require(mgcv) test1 <- function(x, z ,sx=0.3, sz=0.4) { x <- x*20 (pi ** sx * sz) * (1.2 * exp(-(x - 0.2)^2 / sx^2 - ( z - 0.3)^2 / sz^2) + 0.8 * exp(-(x - 0.7)^2 / sx^2 -(z - 0.8)^2 / sz^2)) } n <- 500 old.par<-par(mfrow=c(2,2)) x <- runif(n) / 20 z<-runif(n) xs <- seq(0, 1, length=30) / 20 zs <- seq(0, 1, length=30) pr <- data.frame(x=rep(xs, 30), z=rep(zs, rep(30, 30))) truth <- matrix(test1(pr$x, pr$z), 30, 30) f <- test1(x, z) y <- f + rnorm(n) * 0.2 ## model 1 s() smooths b1 <- gam(y ~ s(x,z)) persp(xs, zs, truth) title("truth") vis.gam(b1) title("t.p.r.s") ## model 2 te() smooths b2 <- gam(y ~ te(x, z)) vis.gam(b2) title("tensor product") ## model 3 te() smooths specifying margin bases b3 <- gam(y ~ te(x, z, bs=c("tp", "tp"))) vis.gam(b3) title("tensor product") par(old.par)
Comments
Post a Comment