R: perfect smoothing curve

前端 未结 3 1795
Happy的楠姐
Happy的楠姐 2021-02-06 19:35

I am trying to fit smooth curve to my dataset; is there is any better smoothing curve than I produced using the following codes:

x <- seq(1, 10, 0.5)
y <-          


        
3条回答
  •  醉话见心
    2021-02-06 19:49

    I think perhaps you're looking for an interpolated smooth line, which in the case of R is probably most easily accomplished by fitting an interpolation spline? As the other answers discuss, that's not what statistical fitting is about, but there are many contexts where you want a smooth interpolated curve -- I think your terminology may have thrown people off.

    Splines are more numerically stable than polynomials.

    x <- seq(1, 10, 0.5)
    y <- c(1, 1.5, 1.6, 1.7, 2.1,
        2.2, 2.2, 2.4, 3.1, 3.3,
        3.7, 3.4, 3.2, 3.1, 2.4,
        1.8, 1.7, 1.6, 1.4)
    
    library(splines)
    
    isp <- interpSpline(x,y)
    
    xvec <- seq(min(x),max(x),length=200)  ## x values for prediction
    
    png("isp.png")
    plot(x,y)
    ## predict() produces a list with x and y components
    lines(predict(isp,xvec),col="red")
    dev.off()
    

    enter image description here

提交回复
热议问题