我会尝试重新适应interp.surface
R 包中的函数fields https://cran.r-project.org/web/packages/fields/index.html。重新改编自的示例?interp.surface
:
加载包
library(fields)
#> Warning: package 'fields' was built under R version 4.1.2
#> Loading required package: spam
#> Warning: package 'spam' was built under R version 4.1.2
#> Loading required package: dotCall64
#> Warning: package 'dotCall64' was built under R version 4.1.2
#> Loading required package: grid
#> Spam version 2.7-0 (2021-06-25) is loaded.
#> Type 'help( Spam)' or 'demo( spam)' for a short introduction
#> and overview of this package.
#> Help for individual functions is also obtained by adding the
#> suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
#>
#> Attaching package: 'spam'
#> The following objects are masked from 'package:base':
#>
#> backsolve, forwardsolve
#> Loading required package: viridis
#> Loading required package: viridisLite
#>
#> Try help(fields) to get started.
加载数据
data(lennon)
obj <- list(x = 1:20, y = 1:20, z = lennon[201:220, 201:220])
定义逼近函数
approxfun_2d <- function(x, y) {
interp.surface(obj, cbind(x, y))
}
Test it
set.seed(1)
approxfun_2d(x = runif(1, min = 1, max = 20), y = runif(1, min = 1, max = 20))
#> [1] 33.34148
绘制原始数据
image.plot(obj)
计算插值
x_interp <- runif(2e2, 1, 20)
y_interp <- runif(2e2, 1, 20)
z_interp <- approxfun_2d(x_interp, y_interp)
绘图近似
quilt.plot(x_interp, y_interp, z_interp)
Created on 2021-11-23 by the reprex package https://reprex.tidyverse.org (v2.0.1)
这并不 100% 等于 2D 版本approxfun
,但对于大多数用途来说可能已经足够了。