R:使用nls将高斯峰拟合到密度图数据
我正在尝试使用以下代码将两个高斯峰拟合到密度图数据中:R:使用nls将高斯峰拟合到密度图数据,r,R,我正在尝试使用以下代码将两个高斯峰拟合到密度图数据中: 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 <
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")
model我一贴出问题就得到了答案。更改此项的适用性:
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))
fit不需要在末尾传递err变量如果有人能想出一个更优雅的解决方案,我将非常乐意接受他们的答案