【问题标题】:Generate smoothing splines for one feature variable in R为 R 中的一个特征变量生成平滑样条曲线
【发布时间】:2019-07-19 03:43:27
【问题描述】:

在阅读了有关平滑样条的资料后,我想使用以下R代码为特征变量x生成平滑样条。

这是我为获得特征变量 x 的平滑样条曲线的基所做的:

x = sort(rnorm(30)) # x is the feature variable 
px = stats::poly(x, degree = 3) # orthogonal polynomial basis

smooth_spline_basis1 = smooth.spline(x, px[,1],df=3, all.knots = TRUE)$y 
smooth_spline_basis2 = smooth.spline(x, px[,2],df=3, all.knots = TRUE)$y 
smooth_spline_basis3 = smooth.spline(x, px[,3],df=3, all.knots = TRUE)$y 

par(mfrow=c(2,2))
plot(px[,1],smooth_spline_basis1, main = "smoothing_spline_basis1 VS polynomial_spline_basis1")
plot(px[,2],smooth_spline_basis2, main = "smoothing_spline_basis2 VS polynomial_spline_basis2")
plot(px[,3],smooth_spline_basis3, main = "smoothing_spline_basis3 VS polynomial_spline_basis3")
par(mfrow=c(1,1))

思考过程是否正确?还是我错过了什么?

【问题讨论】:

    标签: r spline smoothing


    【解决方案1】:

    mgcv 包通过函数gam() 为您提供了一个很好的样条平滑器,用于广义加法模型。下面是样条曲线拟合到正弦曲线的示例:

    library(mgcv)
    
    x <- seq(0, 2 * pi, length.out = 100)
    y <- sin(x)
    
    mod <- gam(y ~ s(x))
    summary(mod)
    
    plot(x, y)
    lines(x, fitted(mod), col = "green", lwd = 2)
    

    【讨论】:

    • 这不是 OP 要求的。
    • @eastclintw00d 感谢您的快速回复。包 mgcv 很好,但这张海报更多的是寻找拟合特征变量的平滑样条表示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多