補間

• こちらは近似・補間に関するPDF(159ページ)
• interpolation
• 内挿とも言う
• 既知の数値列に対して、データ列の各区間の間を埋める
• Rで??interpolationと尋ねると、いくつかの方法が見える
• 平滑な３次元空間の曲面作成のための補間
• akimaパッケージなど
• いろいろな(時系列)補間
• pracmaパッケージなど
• たとえば１次の補間
• 方法
• "constant", "linear", "nearest", "spline", "cubic"
```library(pracma)
x<-sort(runif(10))
y<-x^2+rnorm(length(x))*0.1
xi<-seq(from=range(x)[1],to=range(x)[2],length=100)
yc<-interp1(x,y,xi,method="constant")
yl<-interp1(x,y,xi,method="linear")
yn<-interp1(x,y,xi,method="nearest")
ys<-interp1(x,y,xi,method="spline")
yc<-interp1(x,y,xi,method="cubic")
plot(x,y)
grid()
lines(xi,yc,col=2)
lines(xi,yl,col=3)
lines(xi,yn,col=4)
lines(xi,ys,col=5)
lines(xi,yc,col=6)
```

• Lagrange補間(１次)
```barycentricExample <- function(fun, a, b, n, m)
{
xi <- seq(a, b, len=n)
yi <- fun(xi)
x  <- seq(a, b, len=m)

y <- barylag(xi, yi, x)
plot(xi, yi, col="red", xlab="x", ylab="y",
main="Example of barycentric interpolation")

lines(x, fun(x), col="yellow", lwd=2)
lines(x, y, col="darkred")

grid()
}

barycentricExample(sin, -pi, pi, 11, 101)  # good interpolation
barycentricExample(runge, -1, 1, 21, 101)  # bad interpolation
```
• Lagrange補間(２次)
```##  Example from R-help
xn <- c(4.05, 4.10, 4.15, 4.20, 4.25, 4.30, 4.35)
yn <- c(60.0, 67.5, 75.0, 82.5, 90.0)
foo <- matrix(c(
-137.8379, -158.8240, -165.4389, -166.4026, -166.2593,
-152.1720, -167.3145, -171.1368, -170.9200, -170.4605,
-162.2264, -172.5862, -174.1460, -172.9923, -172.2861,
-168.7746, -175.2218, -174.9667, -173.0803, -172.1853,
-172.4453, -175.7163, -174.0223, -171.5739, -170.5384,
-173.7736, -174.4891, -171.6713, -168.8025, -167.6662,
-173.2124, -171.8940, -168.2149, -165.0431, -163.8390),
nrow = 7, ncol = 5, byrow = TRUE)
xf <- c(4.075, 4.1)
yf <- c(63.75, 67.25)
barylag2d(foo, xn, yn, xf, yf)
#  -156.7964 -163.1753
#  -161.7495 -167.0424

# Find the minimum of the underlying function
bar <- function(xy) barylag2d(foo, xn, yn, xy[1], xy[2])
# \$par
# 4.230547 68.522747
# \$value
# -175.7959

## Not run:
# Image and contour plots
image(xn, yn, foo)
contour(xn, yn, foo, col="white", add = TRUE)
xs <- seq(4.05, 4.35, length.out = 51)
ys <- seq(60.0, 90.0, length.out = 51)
zz <- barylag2d(foo, xn, yn, xs, ys)
contour(xs, ys, zz, nlevels = 20, add = TRUE)
contour(xs, ys, zz, levels=c(-175, -175.5), add = TRUE)
points(4.23, 68.52)
## End(Not run)
```
```??interpolation
```
```[Top]
The search string was "interpolation"
Help pages:
akima::akima 		Waveform Distortion Data for Bivariate Interpolation
akima::aspline 		Univariate Akima interpolation
akima::interp 		Gridded Bivariate Interpolation for Irregular Data
akima::interpp 		Pointwise Bivariate Interpolation for Irregular Data
cba::lminter 		Interpolating Logical Matrices
fields::interp.surface 		Fast bilinear interpolator from a grid.
fields::splint 		Cubic spline interpolation
hyperSpec::spc.loess 		loess smoothing interpolation for spectra Spectra can be smoothed and interpolated on a new wavelength axis using 'loess'.
pracma::barylag 		Barycentric Lagrange Interpolation
pracma::barylag2d 		2-D Barycentric Lagrange Interpolation
pracma::interp1 		One-dimensional Interpolation
pracma::interp2 		Two-dimensional Data Interpolation
pracma::newtonInterp 		Lagrange and Newtons Interpolation
pracma::pchip 		Hermitean Interpolation Polynomials
pracma::ratinterp 		Rational Interpolation
pracma::spinterp 		Monotone (Shape-Preserving) Interpolation
rgl::par3dinterp 		Interpolator for par3d parameters
zoo::na.approx 		Replace NA by Interpolation
grDevices::colorRamp 		Color interpolation
splines::interpSpline 		Create an Interpolation Spline
splines::periodicSpline 		Create a Periodic Interpolation Spline
stats::approx 		Interpolation Functions
stats::NLSstClosestX 		Inverse Interpolation
stats::spline 		Interpolating Splines```