R: perfect smoothing curve

前端 未结 3 1801
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:46

    As posed, the question is almost meaningless. There is no such thing as a "best" line of fit, since "best" depends on the objectives of your study. It is fairly trivial to generate a smoothed line to fit through every single point of data (e.g. a 18th order polynomial will fit your data perfectly, but will most likely be quite meaningless).

    That said, you can specify the amount of smoothness of a loess model by changing the span argument. The larger the value of span, the smoother the curve, the smaller the value of span, the more it will fit each point:

    Here is a plot with the value span=0.25:

    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)
    
    xl <- seq(1, 10, 0.125)
    plot(x, y)
    lines(xl, predict(loess(y~x, span=0.25), newdata=xl))
    

    enter image description here


    An alternative approach is to fit splines through your data. A spline is constrained to pass through each point (whereas a smoother such as lowess may not.)

    spl <- smooth.spline(x, y)
    plot(x, y)
    lines(predict(spl, xl))
    

    enter image description here

提交回复
热议问题