二次样条插值

6

有没有一种方法可以将二次样条(而不是三次样条)调整到某些数据上?

我有这些数据,但似乎找不到适当的R函数来实现此操作。


4
bs() (http://stat.ethz.ch/R-manual/R-patched/library/splines/html/bs.html) 也许是你需要的吗? - RoyalTS
3
更具体地说,library(splines); fit <- lm(y~bs(x,degree=4,df=?),...)的意思是使用splines库中的bs函数对自变量x进行4阶B样条拟合,并用线性模型lm()对因变量y进行拟合。其中的参数df表示拟合中自由度的数量,需要根据具体情况进行指定。 - Ben Bolker
1
@BenBolker 为什么 degree=4 会给出一个二次样条?它不应该是四次的吗? - Glen_b
1个回答

11

稍微扩展一下上面的评论,您可以使用B样条基函数(在函数splines :: bs()中实现),将degree = 2设置为默认值degree = 3

library(splines)

## Some example data
set.seed(1)
x <- 1:10
y <- rnorm(10)

## Fit a couple of quadratic splines with different degrees of freedom
f1 <- lm(y ~ bs(x, degree = 2))  # Defaults to 2 - 1 = 1 degree of freedom
f9 <- lm(y ~ bs(x, degree = 2, df=9))

## Plot the splines
x0 <- seq(1, 10, by = 0.1)
plot(x, y, pch = 16)
lines(x0, predict(f1, data.frame(x = x0)), col = "blue")
lines(x0, predict(f9, data.frame(x = x0)), col = "red")

在这里输入图片描述


1
一个小注释:在这里使用的二次B样条方法与使用二次(插值)样条会产生不同的结果。 - Lars Lau Raket

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接