如何在R中将平滑曲线拟合到我的数据? [英] How to fit a smooth curve to my data in R?

查看:450
本文介绍了如何在R中将平滑曲线拟合到我的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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

现在,当我使用标准命令对其进行绘制时,它当然看起来很坎and和前卫:

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中将平滑曲线拟合到我的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆