0
我有一個(x,y,z)值的網格,並且我想要一個函數,當給定(x,y)點位於網格之外時,它可以近似z值。超出數據網格的外推
我試圖解決使用Akima包(代碼塊3)的問題,但我似乎無法得到INTERP函數與線性= FALSE選項,需要外插網格外。
數據:
# Grid data
x <- seq(0,1,length.out=6)
y <- seq(0,1,length.out=6)
z <- outer(x,y,function(x,y){sqrt(x^2+y^3)})
可視化數據(問題並不重要):
## Visualize the data - Not important for question ##
jet.colors <- colorRampPalette(c("Royal Blue", "Lime Green"))
nbcol <- 100
color <- jet.colors(nbcol)
nrz <- nrow(z)
ncz <- ncol(z)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
pmat <- persp(x, y, z, d = 1,r = 4,
ticktype="detailed",
col = color[facetcol],
main = "Title",
xlab="x value",
ylab = "y value",
zlab= "z value",
scale=FALSE,
expand=0.6,
theta=-40,
phi=25)
## End visualization ##
我在使用阿克瑪包
library(akima)
# Vectorize the grid:
zz <- as.vector(z)
# create all combinations of x and y
xy <- expand.grid(x,y)
# What we want:
sqrt(0.7^2 + 0.7^3) # c(0.7, 0.7) = 0.9126883
sqrt(0.7^2 + 1.2^3) # c(0.7, 1.2) = 1.489295
# We get a result for the first point inside the grid,
# but not for the second one outside the grid.
# This is expected behaviour when linear=TRUE:
interp(xy[,1], xy[,2], zz, xo = c(0.7), yo= c(0.7, 1.2), linear=TRUE)
# = (0.929506, NA)
# When LINEAR = FALSE we get z= 0, 0!!
interp(xy[,1], xy[,2], zz, xo = c(0.7), yo= c(0.7, 1.2), linear=FALSE, extrap = TRUE)
# = (0, 0)
# Dropping extrap=TRUE we see that both are actually NA in this case
interp(xy[,1], xy[,2], zz, xo = c(0.7), yo= c(0.7, 1.2), linear=FALSE)
# = (NA, NA)
# What is going on?
[R採用3.1.3解決問題的嘗試和akima_0.5-11。
1.2超出x和y範圍。 – 2015-03-31 09:05:20
jep,這就是爲什麼我想外推。從我對Akima人的理解中,使用extrap = TRUE,應該可以獲得超出網格的值? – zaphoid 2015-03-31 09:14:04
從手冊:extrap - 邏輯標誌:應該外推用於由數據點確定的凸包外嗎? – zaphoid 2015-03-31 09:15:53