R中的插值曲面

时间:2016-05-26 15:01:59

标签: r interpolation

我试图插入形式

的多项式

z = Ax^2 + By^2 + Cxy + Dx + Ey + F

R中,大写字母是常数系数。

以下数据

我的横轴是:KSo<-c(0.90,0.95,1.00,1.05,1.10)

我的纵轴是:T<-c(1/12,3/12,6/12,1,2,5)

KSo X T映射的数据是:

14.2 13.0 12.0 13.1 14.5

14.0 13.0 12.0 13.1 14.2

14.1 13.3 12.5 13.4 14.3

14.7 14.0 13.5 14.0 14.8

15.0 14.4 14.0 14.5 15.1

14.8 14.6 14.4 14.7 15.0

换句话说,(1.00,6/12)的观察数据是12.5

如何插入(0.98,11/12)的预测数据?

编辑:我找到了一个包含akima函数的精美软件包bicubic,它使用样条函数。我还是想看看人们的建议

1 个答案:

答案 0 :(得分:1)

以下是两种可能的建议解决方案 原始数据:

KSo<-c(0.90,0.95,1.00,1.05,1.10)
T<-c(1/12,3/12,6/12,1,2,5)
mapping<-c(14.2, 13.0, 12.0, 13.1, 14.5,
           14.0, 13.0, 12.0, 13.1, 14.2,
           14.1, 13.3, 12.5, 13.4, 14.3,
           14.7, 14.0, 13.5, 14.0, 14.8,
           15.0, 14.4, 14.0, 14.5, 15.1,
           14.8, 14.6, 14.4, 14.7, 15.0)
mapped<-matrix(mapping, ncol=5, byrow=TRUE)

这是线性插值解决方案:

#predict
x<-0.98
y<-11/12

#Perform 2D interpolation
#find index along x and y axis
ki<-as.integer(cut(x, KSo, right=FALSE))
Ti<-as.integer(cut(y, T, right=FALSE))

#dx = (x-x1)/(x2-x1)  where x is the point to interpolate to.
# dx will vary from 0 to <1, (or worded differently the % distance between the 2 grid points)
dx<-(x-KSo[ki])/(KSo[ki+1]-KSo[ki])
dy<-(y-T[Ti])/(T[Ti+1]-T[Ti])

#find values and weighed sums of neighboring points
#    equations as per Wikipedia
f00<-mapped[Ti, ki]*(1-dx)*(1-dy)  
#    f(0,0) -weight each corner contributes to the final results
f01<-mapped[Ti+1, ki]*(1-dx)*dy
f10<-mapped[Ti, ki+1]*dx*(1-dy)
f11<-mapped[Ti+1, ki+1]*dx*dy
sum(f00, f10, f01, f11)

与上述相同的分析但具有R的功能

ki<-as.integer(cut(x, KSo, right=FALSE))
Ti<-as.integer(cut(y, T, right=FALSE))
ilower<-approx(T, mapped[,ki], y)$y
iupper<-approx(T, mapped[,(ki+1)], y)$y
approx(KSo[ki:(ki+1)], c(ilower, iupper), x)

以下是使用整个数据集拟合的回归模型。由于数据不是完美拟合,因此网格点处的估计值与原始指定值不同。

#establish data frame with raw data
df<-data.frame(expand.grid(KSo, T), mapping)
names(df)<-c("KSo", "T", "z")

#Perform regression
model<-lm(z~I(KSo^2)+I(T^2)+KSo*T, data=df)

#Predict results at desired coordinates
dfnew<-data.frame(KSo=c(1, 0.98), T=c(0.5, 0.9166667))
predict(model, dfnew)

希望这有帮助。