I want to fit a sum of n gaussians to my data of the form: Sum of Gaussians
how would I go around fitting a specified sum of gaussians with the nls() function
without specifically writing the individaul parameters like here. I am aware that overfitting becomes a problem, so I want to fit the coefficent of determination for each number of n to graphically determine the best number of n.
To make an reproducilbe example I have this code which resembles my data:
library(ggplot2)
# Define a Gaussian function
gaussian <- function(x, mean, sd, amplitude) {
amplitude * dnorm(x, mean = mean, sd = sd)
}
# Generate x values for plotting
x <- seq(-10, 10, by = 0.1)
# Define parameters for 6 Gaussian components
params <- list(
list(mean = -2, sd = 1, amplitude = 1),
list(mean = -1, sd = 0.5, amplitude = 0.8),
list(mean = 0, sd = 0.3, amplitude = 0.6),
list(mean = 1, sd = 0.8, amplitude = 0.7),
list(mean = 2, sd = 0.6, amplitude = 0.5),
list(mean = 3, sd = 0.4, amplitude = 0.3)
)
# Initialize an empty vector to store the sum
sum_curve <- rep(0, length(x))
# Sum the Gaussian components
for (param in params) {
sum_curve <- sum_curve + gaussian(x, mean = param$mean, sd = param$sd, amplitude = param$amplitude)
}
# Create a data frame for plotting
data <- data.frame(x = x, y = sum_curve)
# Create the plot for the sum of Gaussian curves
ggplot(data, aes(x, y)) +
geom_line() +
labs(title = "Sum of Gaussian Curves", x = "X", y = "Density") +
theme_minimal()
To summarize I would kindly ask for help with:
- designing a nls function consisting of a sum of n gaussians
- executing this function for different n (e.g. 1:15) and plotting the results
- plotting the correspondig coefficient of determination against n
- plotting the sum of n gaussians and the individual components.