2012-04-19 28 views
1

我試圖將兩個高斯峯,以我的密度圖數據,使用下面的代碼:R:配件高斯峯密度圖數據使用NLS

model <- function(coeffs,x) 
{ 
    (coeffs[1] * exp(- ((x-coeffs[2])/coeffs[3])**2)) 

} 

y_axis <- data.matrix(den.PA$y) 
x_axis <- data.matrix(den.PA$x) 
peak1 <- c(1.12e-2,1075,2) # guess for peak 1 
peak2 <- c(1.15e-2,1110,2) # guess for peak 2 

peak1_fit <- model(peak1,den.PA$x) 
peak2_fit <- model(peak2,den.PA$x) 

total_peaks <- peak1_fit + peak2_fit 
err <- den.PA$y - total_peaks 

fit <- nls(y_axis~coeffs2 * exp(- ((x_axis-coeffs3)/coeffs4)**2),start=list(coeffs2=1.12e-2, coeffs3=1075, coeffs4=2)) 
fit2<- nls(y_axis~coeffs2 * exp(- ((x_axis-coeffs3)/coeffs4)**2),start=list(coeffs2=1.15e-2, coeffs3=1110, coeffs4=2)) 


fit_coeffs = coef(fit) 
fit2_coeffs = coef(fit2) 

a <- model(fit_coeffs,den.PA$x) 
b <- model(fit2_coeffs,den.PA$x) 



plot(den.PA, main="Cytochome C PA", xlab= expression(paste("Collision Cross-Section (", Å^2, ")"))) 
lines(results2,a, col="red") 
lines(results2,b, col="blue") 

這給了我下面的情節:

enter image description here

這是我的問題所在。我計算彼此獨立的擬合,並將高斯峯疊加在彼此之上。我需要將err變量提供給nls,該變量應該返回6個coeffs,然後我可以重新建模高斯峯值以適合該圖。

回答

2

只要我發佈了問題,答案就臨到我了。更改適合這樣的:

fit <- nls(y_axis~(coeffs2 * exp(- ((x_axis-coeffs3)/coeffs4)**2)) + (coeffs5 * exp(- ((x_axis-coeffs6)/coeffs7)**2)), start=list(coeffs2=1.12e-2, coeffs3=1075, coeffs4=2,coeffs5=1.15e-2, coeffs6=1110, coeffs7=2)) 

給出:

enter image description here

的不雅soloution,但它的工作。

+0

最後沒有必要通過err變量 – Harpal 2012-04-19 15:32:55

+0

如果任何人都可以想出一個更優雅的解決方案,我會很樂意接受他們的答案 – Harpal 2012-04-24 21:27:23