作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将表面拟合到某些值:
x = 1:10
y = 10:1
z = sample(1:10,10)
spline_function(z ~ x + y)
. R 中的实际样条函数似乎只需要
x
和
y
所以我不能有一个二维的 x 坐标。在 R 中这样做的方法是什么?我知道
loess
对于局部多项式等,但样条曲线确实是我正在寻找的。
最佳答案
一个不错的选择是 mgcv 所有版本的 R 附带的包。它通过 s()
具有两个或多个变量的各向同性惩罚回归样条曲线和通过张量积和 te()
对两个或多个变量进行各向异性惩罚回归样条.
如果你不想要惩罚回归样条,你可以使用参数 fx = TRUE
修复已知的自由度样条。
这是来自 ?te
的示例
# following shows how tensor pruduct deals nicely with
# 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 with s() smooths
b1 <- gam(y ~ s(x,z))
persp(xs, zs, truth)
title("truth")
vis.gam(b1)
title("t.p.r.s")
## model 2 with 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)
关于r - 在 R 中的 3 维样条中插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554690/
我是一名优秀的程序员,十分优秀!