以下代码将绘制点和样条曲线(通过样条函数获得):
x <- 1:7
y <- c(2,1,4,2,5,1,2)
# plot the points
plot(x, y)
# plot the spline
lines(spline(x, y, n = 100, method = "natural"), col = 2)
我需要的是spline(x, y, n = 100, method = "natural")
获得的函数本身,因此我可以获得任何y
的{{1}}值。我怎么能这样做?
我尝试了下面的代码,但它没有工作
x
答案 0 :(得分:2)
使用?splinefun
帮助页面上引用的?spline
。
f <- splinefun(x, y, method = "natural")
f(1)
#[1] 2
f(2)
#[1] 1
f(3.5)
#[1] 2.901923