回复 第1楼 的 smurf:#需要说明的是当数据量小于1000时如不指定默认用loess法,
#而当数据量大于等于1000时默认用gam法
<br />
qplot(carat,price,data=dsmall,geom=c('point','smooth'))</p>
<p>qplot(carat,price,data=diamonds,geom=c('point','smooth'))</p>
<p>
</p>
#用了smooth后会有置信区间显示在图形上,若要关闭可以使用se=F关闭
<br />
qplot(carat,price,data=dsmall,geom=c('point','smooth'),se=F)<br />
#对于拟合方法除了默认,也可以手动指定,有loess,lm,gam(需要mgcv包),rlm(需要MASS包)</p>
#曲线的弯曲程度可由span指定,从1到0越来越弯曲,仅当method=loess时有效
<br />
qplot(carat,price,data=dsmall,geom=c('point','smooth'),span=0.2)</p>
<p>qplot(carat,price,data=dsmall,geom=c('point','smooth'),span=1)</p>
<p>
#其中当载入mgcv包时,可以使用gam方法:</p>
#1.当数据量小于1000时,使用formula=y~s(x)
#2.当数据量大于等于1000时,使用formula=y~s(x,bs="cs")
<br />
qplot(carat,price,data=dsmall,geom=c('point','smooth'),<br />
method='gam',formula=y~s(x))</p>
<p>qplot(carat,price,data=dsmall,geom=c('point','smooth'),<br />
method='gam',formula=y~s(x,bs='cs'))<br />
#lm方法不多介绍,其中当载入了splines包后,可以拟合natural spline,</p>
#使用formula=y~ns(x,2),其中第二个参数是自由度,自由度越高,曲线越弯曲
<br />
qplot(carat,price,data=dsmall,geom=c('point','smooth'),<br />
method='lm')</p>
<p>library(splines)</p>
<p>qplot(carat,price,data=dsmall,geom=c('point','smooth'),<br />
method='lm',formula=y~ns(x,5))</p>
<p>
#rlm方法更稳健,不受离群点影响</p>
另外并没有报错。只是善意的提醒。