R:完美的平滑曲线 [英] R: perfect smoothing curve
问题描述
我试图将平滑曲线拟合到我的数据集中;是否有比我使用下面的代码产生的更好的平滑曲线:
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,$ b (x,y)
xv < - seq(min(x),max(x,y))b b1,1.7,1.6,1.4)
lo< - 黄土(x),(max(x) - min(x))/ 1000000)
lines(xv,predict(lo,xv),col ='blue',lwd = 1)
EDITS:
我不打算产生好看)
我想显示一个平滑的趋势....
我不关心相关的模型公式....我需要恢复公式
正如所提出的,这个问题几乎没有意义。有没有这样的事情作为最佳适合线,因为最好取决于你的研究目标。生成一条平滑的线以适应每个单点数据是相当简单的(例如,一个18阶多项式将完美地适合您的数据,但很可能是相当无意义的)。
也就是说,您可以通过更改 span $>来指定
loess
模型的平滑度 c $ c>参数。量程越大,曲线越平滑,量程越小,每个点越适合:
这是一个值 span = 0.25
:
x < - seq(1, (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 (黄土(y〜x,span = 0.25),newdata = xl))
<另一种方法是通过数据拟合样条曲线。样条线被限制穿过每个点(但是更平滑的例如
lowess
可能不会。)plot(x,y)
lines(predict(spl,xl))
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 <- 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)
lo <- loess(y~x)
plot(x,y)
xv <- seq(min(x),max(x), (max(x) - min(x))/1000000)
lines(xv, predict(lo,xv), col='blue', lwd=1)
EDITS:
I do not intend to produce good looking (not necessary) I want show a smoothed trend .... I am not concerned with associated model formula ....I do need to recover formula
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))
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))
这篇关于R:完美的平滑曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!