获取R编程中插值得到的点列表——spline()和splinefun()函数
在 R 编程中, spline()
和splinefun()
函数用于创建通过插值获得的点列表。它执行给定数据点的三次样条插值。
Syntax:
spline(x, y, method)
and
splinefun(x, y, method)
Parameters:
x, y: represents vectors giving the points for interpolation
method: represents the type of spline interpolation to be used
要了解这两个函数的更多可选参数,请在控制台中使用以下命令:
help("spline")
示例 1:
# Coordinates
n <- 100
x <- 1:n
y <- rnorm(n)
# Output to be present as PNG file
png(file = "splineGFG.png")
# Spline() function
plot(x, y, main = "spline() function")
lines(spline(x, y))
# Saving the file
dev.off()
输出:
示例 2:
# Coordinates
n <- 100
x <- 1:n
y <- sin((x-0.5)*pi)
# Output to be present as PNG file
png(file = "splinefunGFG.png")
f <- splinefun(x, y)
curve(f(x))
# Saving the file
dev.off()
输出: