2012-04-03 206 views
3

我有這樣的數據。有什麼方法可以平滑我的情節?如何平滑曲線

cr <- colorRampPalette(col=c("red", "red", "red", "red"), bias=1) 
linecols <- cr(3) 
x<-c(-1000.000000,-900.000000,-800.000000,-700.000000,-600.000000,-500.000000,-400.000000,-300.000000,-200.000000,-100.000000,0.000000,100.000000,200.000000,300.000000,400.000000,500.000000,600.000000,700.000000,800.000000,900.000000,1000.000000) 
y<-c(0.809524,1.000000,1.333333,1.333333,3.285714,7.761905,13.619048,7.571429,14.809524,3.904762,1.857143,2.285714,4.857143,8.571429,2.000000,1.523810,2.714286,0.857143,1.285714,0.857143,1.380952) 
plot(x, y,type="l",main="Average",ylab="Average Profile",col=linecols[1],ylim=c(0.809524,14.809524),xaxt="s",yaxt="s",lwd=2) 

回答

9
lines(x, smooth(y)) 

?smooth

lines(supsmu(x, y)) 

請參閱'?supsmu'。

小心,平滑是惡魔的業務。

+0

看來Excel的平滑是ggplot或R平滑不同的微調。 Excel不會更改任何形狀,但是R/ggplot可以。 – bogu 2012-04-09 03:33:23

+2

Excel使用什麼算法?你可能會爲此做什麼真的沒有盡頭。你應該提供一個例子,如果你認爲這是一個問題。 – mdsumner 2012-04-09 04:40:21

6

我會約平滑(互聯網搜索的‘壞平滑數據’返回大量網頁的),但我會提供了另一種解決方案第二@mdsumner的叮嚀:

plot(lowess(x,y,f=1/3),type="l",col="red") 

更多見?lowess信息。

enter image description here

3

許多平整器是可用的。

這裏是一個平滑函數:

trace.smooth<-function(trace, type="Savitsky-Golay", width=10){ 

    if(type=="lowess"){ 
    smooth.trace<-with(clean.trace, lowess(x=1:length(trace), 
            y=trace, 
            f=width/length(trace), 
            delta=width/2))$y 
    } 

    if(type=="moving-average"){ 
     moving_average<-function(width=10){ 
     moving.average<-rep(1,width)/width 
     return(moving.average) 
     } 

     moving.average<-moving_average(width) 

     smooth.trace<-filter(trace, moving.average) 

    } 

    if(type=="Savitsky-Golay"){ 
     # Savitsky-Golay smoothing function 
    savistsky_golay<-function(width=10){ 
     x<-1:width-width/2 
     y<-max(x^2)-x^2 
     sg<-y/sum(y) 
     return(sg) 
    } 

    sg<-savistsky_golay(width) 

     smooth.trace<-filter(trace, sg) 
    } 

    return(smooth.trace) 
} 

使用ggplot2

library(ggplot2) 

df<-data.frame(x=x, y=y) 

qplot(data=df, 
     x=x, 
     y=y, 
     geom=c("line", "point"))+ 
     geom_smooth(se=F) 

plot with smooth

可以將方法參數添加到geom_smooth的溶液(方法= 「黃土」)

方法:smo無法使用的方法(功能),例如。 LM,GLM,GAM,黃土,RLM

您可以使用stat_smooth

+0

此代碼無法運行。 clean.trace沒有定義 – Paul 2015-04-24 16:29:41