```
# Load necessary libraries
library(minpack.lm)
```

`Warning: il pacchetto 'minpack.lm' è stato creato con R versione 4.3.3`

`library(ggplot2)`

`Warning: il pacchetto 'ggplot2' è stato creato con R versione 4.3.3`

```
# Function to generate a Gaussian curve
gaussian <- function(x, amp, cen, wid) {
amp * exp(-((x - cen)^2) / (2 * wid^2))
}
# Generate sample data (a curve with a single peak)
set.seed(123)
x <- seq(0, 10, length.out = 100)
y <- gaussian(x, 1, 5, 1) + rnorm(100, sd = 0.05)
data <- data.frame(x = x, y = y)
# Function to create a model with n Gaussians
create_n_gaussian_model <- function(n) {
components <- paste0("gaussian(x, amp", 1:n, ", cen", 1:n, ", wid", 1:n, ")", collapse = " + ")
as.formula(paste("y ~", components))
}
# Function to fit n Gaussians
fit_n_gaussian <- function(data, n) {
model <- create_n_gaussian_model(n)
start_list <- as.list(rep(c(0.5, 5, 1), n))
names(start_list) <- c(rbind(paste0("amp", 1:n), paste0("cen", 1:n), paste0("wid", 1:n)))
fit <- nlsLM(model, data = data, start = start_list)
return(fit)
}
# Fit with varying number of Gaussians
fits <- lapply(1:5, function(n) fit_n_gaussian(data, n))
```

`Warning in nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : lmdif: info = -1. Number of iterations has reached `maxiter' == 50.`

```
Warning in nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : lmdif: info = -1. Number of iterations has reached `maxiter' == 50.
Warning in nls.lm(par = start, fn = FCT, jac = jac, control = control, lower = lower, : lmdif: info = -1. Number of iterations has reached `maxiter' == 50.
```

```
# Calculate AIC for each fit
aics <- sapply(fits, AIC)
# Create predictions and components for each fit
for (i in 1:length(fits)) {
data[paste0("fit", i)] <- predict(fits[[i]], newdata = data)
params <- coef(fits[[i]])
for (j in 1:i) {
data[paste0("comp", j, "_", i)] <- gaussian(data$x, params[paste0("amp", j)],
params[paste0("cen", j)],
params[paste0("wid", j)])
}
}
# Plot the results
ggplot(data, aes(x = x)) +
geom_point(aes(y = y), alpha = 0.5) +
geom_line(aes(y = fit1, color = "1 Gaussian")) +
geom_line(aes(y = fit2, color = "2 Gaussians")) +
geom_line(aes(y = fit3, color = "3 Gaussians")) +
geom_line(aes(y = fit4, color = "4 Gaussians")) +
geom_line(aes(y = fit5, color = "5 Gaussians")) +
scale_color_manual(values = c("red", "blue", "green", "purple", "orange")) +
labs(title = "Curve Deconvolution with Varying Number of Gaussians",
subtitle = paste("AIC values:", paste(round(aics, 2), collapse = ", ")),
x = "X", y = "Y", color = "Fit") +
theme_minimal()
```

```
# Plot individual components for the 3-Gaussian fit
ggplot(data, aes(x = x)) +
geom_point(aes(y = y), alpha = 0.5) +
geom_line(aes(y = fit3, color = "Total Fit")) +
geom_line(aes(y = comp1_3, color = "Component 1")) +
geom_line(aes(y = comp2_3, color = "Component 2")) +
geom_line(aes(y = comp3_3, color = "Component 3")) +
scale_color_manual(values = c("black", "red", "blue", "green")) +
labs(title = "Deconvolution into 3 Gaussians",
x = "X", y = "Y", color = "Component") +
theme_minimal()
```