2016-10-01 46 views
1

我試圖在包含數據點最大值的圖上創建平滑線。我搜索了很多東西,並且用黃土()和ksmooth()修飾過,但我還沒有完成它的工作。我的數據的平滑插值

我最好的嘗試,到目前爲止已與ksmooth(),但該行不通過的最大數據點

enter image description here

我是個化學家,不是一個統計學家,所以方法/各種平滑技術的描述經常超出我的頭腦。任何建議將非常感激。

編輯:只是想弄清楚一些事情。基本上,我所追求的是以下曲線的平滑版本,並且該曲線通過了最大y值。

enter image description here

要生成在我用下面的代碼中第一張照片的情節:

plot(ChiM~Temp, xlim=c(2,6), ylim=c(0,0.225), lwd=2, pch=16, col='red',subset=(v=='20'), main='Out-of-Phase AC Suscetability Plot', xlab='Temperature (K)', ylab=expression(chi[M]*'" (cm'^3*~'mol'^-1*')')) 
setone <- subset(DSM32ac, v=='20') #v=20 is the subset of the data I have provided 
attach(setone) 
lines(ksmooth(Temp, ChiM, 'normal', bandwidth=0.5), col='red',lwd=2) 

我希望這使得事情更清晰一點。如果你需要更多的信息來回答這個問題,請讓我知道。

編輯2:我已經刪除了數據,因爲我無法制作整潔的表格。如果它真的很重要,我會嘗試把它放回

+0

你是對的;平滑的插值是我想要的。感謝您的建議。 –

+0

如果'DF'是一個輸入數據幀,那麼'dput(DF)'將以可重複的方式輸出它,以便響應者可以輕鬆地將其複製並粘貼到其會話中。 –

+0

好吧我現在明白了。如果我有其他問題,我會確保使用dput()。 –

回答

2

試試這個:

y <- c(.07, .12, .17, .11, .04, .02, .01) 
x <- seq_along(y) 

s <- spline(x, y) 

plot(y ~ x) 
lines(s) 

捐贈:

screenshot

+0

這看起來像我之後,所以插值是我想要的。感謝您的建議。如果你不介意的話,我有幾個跟進問題。 1.是否有可變的變量使線條更平滑?我有七個數據系列,我將把它們放在一張圖上。對於我在第二個圖中使用的lines(),我可以使用subset(),但似乎沒有樣條線的選項。把所有的子集放在第一位,然後對每個子集應用樣條線是最好的選擇嗎? –

+0

您可以忽略最後的評論。我已經完成了所有工作。對於遇到此問題的其他人,可以通過使用spline()使用n = x參數來減少鋸齒線。增加用作x的數量會增加曲線的平滑度。 –

0

你可以試試這個:

n <- 10 # generate 10 data points 
d <- data.frame(x = 1:n, y = rnorm(n)) 

# with loes smoothing (span parameter controls the degree of smoothing) 
library(ggplot2) 
ggplot() + geom_point(data=d,aes(x,y), size=5) + 
    geom_smooth(data=d,aes(x,y, colour='span=0.5'), span=0.5, se=FALSE) + 
    geom_smooth(data=d,aes(x,y, colour='span=0.6'), span=0.6, se=FALSE) + 
    geom_smooth(data=d,aes(x,y, colour='span=0.7'), span=0.7, se=FALSE) + 
    geom_smooth(data=d,aes(x,y, colour='span=0.8'), span=0.8, se=FALSE) 

enter image description here

# with B-spline curves using lm (degree of polynomial fitted controls the smoothness) 
ggplot() + 
    geom_point(data=d, aes(x, y), size=5) + 
    geom_smooth(data=d, aes(x, y,col='degree=3'), method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE) + 
    geom_smooth(data=d, aes(x, y,col='degree=4'), method = "lm", formula = y ~ splines::bs(x, 4), se = FALSE) + 
    geom_smooth(data=d, aes(x, y,col='degree=5'), method = "lm", formula = y ~ splines::bs(x, 5), se = FALSE) + 
    geom_smooth(data=d, aes(x, y,col='degree=6'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE) + 
    geom_smooth(data=d, aes(x, y,col='degree=7'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE) 

enter image description here

# with smooth.spline (spar parameter control smoothness of the fitted curve) 
colors <- rainbow(100) 
plot(d$x, d$y, pch=19, xlab='x', ylab='y') 
i <- 1 
for (spar in seq(0.001,1,length=100)) { 
    lines(smooth.spline(d$x, d$y, spar=spar, all.knots=TRUE)$y, col=colors[i]) 
    i <- i + 1 
} 
points(d$x, d$y, pch=19) 

enter image description here