2016-12-08 3 views
1

vol_coarseという名前のテーブルで2次元補間を実行しようとしています。`interp2 {pluma}`で双線形補間を実行するとエラーが発生します。 2D補間のためのよりよい方法はありますか?

install.packages("install.load") 
install.load::load_package("pracma", "data.table") 

vol_coarse <- data.table(V1 = c(3/8, 1/2, 3/4, 1, 1 + 1/2, 2, 3, 6), 
V2 = c(0.50, 0.59, 0.66, 0.71, 0.75, 0.78, 0.82, 0.87), 
V3 = c(0.48, 0.57, 0.64, 0.69, 0.73, 0.76, 0.80, 0.85), 
V4 = c(0.44, 0.53, 0.60, 0.65, 0.69, 0.72, 0.76, 0.81)) 
setnames(vol_coarse, c("Maximum size of aggregate (in)", "2.40", "2.60", "2.80")) 

x <- vol_coarse[, 2][[1]] 

y <- as.numeric(colnames(vol_coarse[, 2:ncol(vol_coarse)])) 

z <- meshgrid(x, y) 

xp <- 3/4 

yp <- 2.70 

interp2(x = x, y = y, Z = z, xp = xp, yp = yp, method = "linear") 

これは返されるエラーメッセージです:

length(x) = nrow(Z) = 8 and length(y) = ncol(Z) = 3 must be satisfied. 

がどのように私は私がなるように3で8である行列を作成することができます。私はその?interp2に読ん

Error: is.numeric(Z) is not TRUE

interp2を使用できますか?

また、このタイプの補間を実行するより良い方法がありますか?

ありがとうございます。

答えて

1

私はあなたが間違って取得しない場合は、希望:

x <- c(3/8, 1/2, 3/4, 1, 1 + 1/2, 2, 3, 6) ## V1 
y <- c(2.4, 2.6, 2.8) ## column names 
Z <- cbind(c(0.50, 0.59, 0.66, 0.71, 0.75, 0.78, 0.82, 0.87), ## V2 
      c(0.48, 0.57, 0.64, 0.69, 0.73, 0.76, 0.80, 0.85), ## V3 
      c(0.44, 0.53, 0.60, 0.65, 0.69, 0.72, 0.76, 0.81)) ## V4 
xp <- 3/4 
yp <- 2.70 

あなたはすでにグリッド上で明確に定義された行列を持っています。たとえば、あなたがして、3Dデータを調べることができます:interp2機能にバグがあるよう

persp(x, y, Z) 
image(x, y, Z) 
contour(x, y, Z) 

私は、pracmaをお勧めしません。グリッド上の補間用のfieldsパッケージのinterp.surface機能を提案します。 pracmaから

library(fields) 
## the list MUST has name `x`, `y`, `x`! 
## i.e., unnamed list `list(x, y, Z)` does not work! 
interp.surface(list(x = x, y = y, z = Z), cbind(xp, yp)) 
# [1] 0.62 

interp2矛盾しています。マニュアルにはZ行列があり、length(x)length(y)ですが、実際にはZlength(y)で、length(x)である必要があります。だから、

## from manual 

    Z: numeric ‘length(x)’-by-‘length(y)’ matrix. 

## from source code of `interp2` 

    lx <- length(x) 
    ly <- length(y) 
    if (ncol(Z) != lx || nrow(Z) != ly) 
     stop("Required: 'length(x) = ncol(Z)' and 'length(y) = nrow(Z)'.") 

interp2作品を作るために、あなたはZの転置を渡す必要があります:

interp2(x, y, t(Z), xp, yp) 
# [1] 0.62 

またはxyリバース(あまりにも、とxpypを!!):

これは実際にと一緒に作業する方法と矛盾します,contourおよびpersp

+0

詳細な説明をいただきありがとうございます。この回答は非常に役に立ちます。 – iembry

+0

あなたがリクエストしたとおりにしました。あなたは良い研究をしています。 – iembry

関連する問題