gpt4 book ai didi

r - 在 R 中的 3 维样条中插值

转载 作者:行者123 更新时间:2023-12-01 08:07:05 26 4
gpt4 key购买 nike

我想将表面拟合到某些值:

x = 1:10
y = 10:1
z = sample(1:10,10)

我想玩点像 spline_function(z ~ x + y) . R 中的实际样条函数似乎只需要 xy所以我不能有一个二维的 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)

enter image description here

关于r - 在 R 中的 3 维样条中插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com