R中的二维线性插值
2D linear interpolation in R
我想在 R 中做一个双线性插值,但我不知道如何用 akima
interp
函数来做,因为我得到一个矩阵一半用数字填充,一半用 NA
.这是我的问题的一个例子:
x = rep(c(1,2,3,6,9,12)/12,5)
y = seq(2709,3820,length = 30)
z = seq(9.2,16.5,length = 30)
inter_lin = interp(x,y,z,xo = seq(min(x),max(x),length=100),
yo = seq(min(y), max(y), length=100), linear=T)
我也尝试使用 interp.surface.grid
形式的函数 fields
但 z
必须是一个矩阵,其中包含所有 x
和 y
使用的值我没有所以它不起作用。
编辑
这是我实际拥有的 y
和 z
向量:
y = c(2931.076,2901.935,2868.635,2804.006,2760.297,2709.114,2983.466,2969.436,2954.808,2928.802,2915.815,2903.867,
3043.365,3051.140,3057.960,3079.230,3103.015,3127.245,3118.090,3156.223,3194.574,3291.021,3380.687,3472.676,
3195.631,3260.866,3331.776,3502.477,3658.052,3829.511)
z = c(10.280984,9.733925, 10.176117, 10.644877, 10.950297, 11.737252, 10.442495, 10.170472, 10.590579, 11.153778 ,11.501962,
12.066833,11.000000, 11.025000, 11.450000, 12.125000, 12.550000, 12.875000, 12.142495, 11.870472, 12.390579, 13.053778,
13.501962,14.066833,13.005984, 12.458925, 13.051117, 13.694877, 14.150297, 14.937252)
设置linear = FALSE
和extrap = TRUE
。
x = rep(c(1,2,3,6,9,12)/12,5)
y = seq(2709,3820,length = 30)
z = seq(9.2,16.5,length = 30)
inter_lin = interp(x,y,z,xo = seq(min(x),max(x),length=100),
yo = seq(min(y), max(y), length=100), linear=FALSE, extrap = TRUE)
我认为问题是在边界上插值,这会从边缘向内波及。通过允许外推——线性插值不可能——可以避免这个问题。
编辑
如果插值域被限制在 有 数据的区域,结果就很好。
library(akima)
library(spatialkernel)
library(fields)
# Create grid
grd <- expand.grid(x = seq(min(x),max(x),length=100), y = seq(min(y), max(y), length=100))
# Find points in convex hull
mask <- pinpoly(cbind(x, y)[chull(x, y),], as.matrix(grd))
# Crop grid to convex hull
grd <- grd[mask == 2,]
# Interpolate to points in convex hull
res <- interpp(x, y, z, xo = grd$x, yo = grd$y)
# Plot results
quilt.plot(res$x, res$y, res$z)
我想在 R 中做一个双线性插值,但我不知道如何用 akima
interp
函数来做,因为我得到一个矩阵一半用数字填充,一半用 NA
.这是我的问题的一个例子:
x = rep(c(1,2,3,6,9,12)/12,5)
y = seq(2709,3820,length = 30)
z = seq(9.2,16.5,length = 30)
inter_lin = interp(x,y,z,xo = seq(min(x),max(x),length=100),
yo = seq(min(y), max(y), length=100), linear=T)
我也尝试使用 interp.surface.grid
形式的函数 fields
但 z
必须是一个矩阵,其中包含所有 x
和 y
使用的值我没有所以它不起作用。
编辑
这是我实际拥有的 y
和 z
向量:
y = c(2931.076,2901.935,2868.635,2804.006,2760.297,2709.114,2983.466,2969.436,2954.808,2928.802,2915.815,2903.867,
3043.365,3051.140,3057.960,3079.230,3103.015,3127.245,3118.090,3156.223,3194.574,3291.021,3380.687,3472.676,
3195.631,3260.866,3331.776,3502.477,3658.052,3829.511)
z = c(10.280984,9.733925, 10.176117, 10.644877, 10.950297, 11.737252, 10.442495, 10.170472, 10.590579, 11.153778 ,11.501962,
12.066833,11.000000, 11.025000, 11.450000, 12.125000, 12.550000, 12.875000, 12.142495, 11.870472, 12.390579, 13.053778,
13.501962,14.066833,13.005984, 12.458925, 13.051117, 13.694877, 14.150297, 14.937252)
设置linear = FALSE
和extrap = TRUE
。
x = rep(c(1,2,3,6,9,12)/12,5)
y = seq(2709,3820,length = 30)
z = seq(9.2,16.5,length = 30)
inter_lin = interp(x,y,z,xo = seq(min(x),max(x),length=100),
yo = seq(min(y), max(y), length=100), linear=FALSE, extrap = TRUE)
我认为问题是在边界上插值,这会从边缘向内波及。通过允许外推——线性插值不可能——可以避免这个问题。
编辑
如果插值域被限制在 有 数据的区域,结果就很好。
library(akima)
library(spatialkernel)
library(fields)
# Create grid
grd <- expand.grid(x = seq(min(x),max(x),length=100), y = seq(min(y), max(y), length=100))
# Find points in convex hull
mask <- pinpoly(cbind(x, y)[chull(x, y),], as.matrix(grd))
# Crop grid to convex hull
grd <- grd[mask == 2,]
# Interpolate to points in convex hull
res <- interpp(x, y, z, xo = grd$x, yo = grd$y)
# Plot results
quilt.plot(res$x, res$y, res$z)