简体   繁体   English

二次样条

[英]Quadratic spline

Is it there a way to adjust a quadratic spline (instead of a cubic one) to some data? 有没有办法将二次样条(而不是立方样条)调整为某些数据?

I have this data and I don't seem to find the appropiate function in R to do this. 我有这些数据,我似乎没有在R中找到适当的功能来执行此操作。

Expanding just a bit on the comments above, you can use a B-spline basis (implemented in function splines::bs() ), setting degree=2 rather than the default degree=3 : 在上面的注释中稍微扩展一下,你可以使用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")

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM