如何在R中获得样条公式?

时间:2015-05-07 01:10:57

标签: r

以下代码将绘制点和样条曲线(通过样条函数获得):

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

1 个答案:

答案 0 :(得分:2)

使用?splinefun帮助页面上引用的?spline

f <- splinefun(x, y, method = "natural")
f(1)
#[1] 2
f(2)
#[1] 1
f(3.5)
#[1] 2.901923