问题描述
我正在尝试在 R
中绘制平滑曲线.我有以下简单的玩具数据:
I'm trying to draw a smooth curve in R
. I have the following simple toy data:
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y
[1] 2 4 6 8 7 12 14 16 18 20
现在,当我使用标准命令绘制它时,它看起来很颠簸和前卫,当然:
Now when I plot it with a standard command it looks bumpy and edgy, of course:
> plot(x,y, type='l', lwd=2, col='red')
如何使曲线平滑,以便使用估计值使 3 条边变圆?我知道有很多方法可以拟合平滑曲线,但我不确定哪种方法最适合这种类型的曲线,以及如何在 R
中编写它.
How can I make the curve smooth so that the 3 edges are rounded using estimated values? I know there are many methods to fit a smooth curve but I'm not sure which one would be most appropriate for this type of curve and how you would write it in R
.
推荐答案
我非常喜欢 loess()
用于平滑:
I like loess()
a lot for smoothing:
x <- 1:10
y <- c(2,4,6,8,7,12,14,16,18,20)
lo <- loess(y~x)
plot(x,y)
lines(predict(lo), col='red', lwd=2)
Venables 和 Ripley 的 MASS 书有一整节关于平滑,也涵盖了样条和多项式——但 loess()
几乎是每个人的最爱.
Venables and Ripley's MASS book has an entire section on smoothing that also covers splines and polynomials -- but loess()
is just about everybody's favourite.
这篇关于如何在 R 中为我的数据拟合平滑曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!