I am trying to fit several models to the same data, in this example it’s a double exponential approach. I have tried to eyeball the parameters, as well as use a brute force approach (see below.
In this case it seems that the manual setting of the parameters (black line) is better than the brute force approach (red line). I don’t really understand why this is the case, because the parameter space set in the starting values of the brute force method includes the parameters that are set for the manual approach.
Could someone explain why the brute force method isn’t converging on the same parameters, or at least have a better fit?
Thanks!
# loading packages
library(nls2)
library(tidyverse)
# example dataset
df = data.frame(
time = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 18, 21, 23, 32, 33),
proportion = c(
1.00000000, 0.89583333, 0.77083333, 0.58333333, 0.54166667,
0.43750000, 0.35416667, 0.31250000, 0.25000000, 0.22916667,
0.16666667, 0.14583333, 0.12500000, 0.10416667, 0.08333333,
0.06250000, 0.04166667, 0.02083333, 0.00000000
)
)
# Fitting model using manual approach
manual_fit <- nls2::nls2(proportion ~ a * exp(b * time) + c * exp(d * time),
data = df,
start = list(a = 1, b = -0.1, c = 1, d = -0.5))
# setting starting parameter space for brute approach
start_values <- list(a = seq(-.5, 1.5, length.out = 10),
b = seq(-.5, 2, length.out = 10),
c = seq(-.5, 2, length.out = 10),
d = seq(-1, 2, length.out = 10))
# fitting model using brute approach
brute_fit <- nls2::nls2(proportion ~ a * exp(b * time) + c * exp(d * time),
data = df,
start = expand.grid(start_values),
control = nls.control(maxiter = 2000), algorithm = "brute-force")
# plotting to see both fits
ggplot(df) +
geom_point(aes(x = time, y = proportion), size = 3.5) +
geom_line(aes(x = time, y = predict(manual_fit)), linewidth = 1) +
geom_line(aes(x = time, y = predict(brute_fit)), linewidth = 1, colour = "red") +
ylab("Proportion > Time") +
xlab("Time (days)")
Created on 2024-07-01 with reprex v2.1.0