I am trying to fit a nonlinear model to empirical data using the minpack.lm::nlsLM
function. The initial conditions for the model work, but I am getting this warning message:
In log((dayOfYear_num - tau_2_A)/delta_2_L) : NaNs produced.
I am unable to resolve this problem. I tried to impose conditions (for example, parameters > 0) like this, but it doesn't work:
lower = c(tau_1_A = 0.0001, tau_2_A = 0.0001, h_1_L = 0.0001, h_2_L = 0.0001, delta_1_L = 0.0001, delta_2_L = 0.0001,
alpha_1_L = 0.0001, alpha_2_L = 0.0001)
Here is the data:
z <- structure(list(dayOfYear = structure(1:33, levels = c("128",
"136", "137", "138", "142", "143", "144", "157", "162", "163",
"164", "166", "167", "173", "184", "185", "186", "187", "189",
"197", "198", "199", "200", "201", "232", "233", "235", "236",
"253", "255", "256", "299", "308"), class = "factor"), density = c(0,
0, 0, 0, 0.0153846153846154, 0.0974358974358974, 0.0745192307692308,
0.130769230769231, 0.0307692307692308, 0.0259615384615385, 0.158974358974359,
0.207692307692308, 0.0375, 0.0510989010989011, 0.00769230769230769,
0, 0, 0.00384615384615385, 0, 0.00192307692307692, 0.0201923076923077,
0.00341880341880342, 0.00538461538461539, 0, 1.11923076923077,
0.39957264957265, 1.32735042735043, 0.564102564102564, 0.562820512820513,
0.486410256410256, 0.343956043956044, 0, 0), dayOfYear_num = c(128,
136, 137, 138, 142, 143, 144, 157, 162, 163, 164, 166, 167, 173,
184, 185, 186, 187, 189, 197, 198, 199, 200, 201, 232, 233, 235,
236, 253, 255, 256, 299, 308)), row.names = 54:86, class = "data.frame")
Here is the code:
mod <- minpack.lm::nlsLM(density ~ ifelse(dayOfYear_num < tau_1_A, 0,
ifelse(dayOfYear_num < tau_2_A,
h_1_L*exp(-1/2*((((log((dayOfYear_num - tau_1_A)/delta_1_L))^2)/alpha_1_L)^2)),
h_2_L*exp(-1/2*((((log((dayOfYear_num - tau_2_A)/delta_2_L))^2)/alpha_2_L)^2)))),
data=z,
start=list(tau_1_A = 100,
tau_2_A = 150,
h_1_L = 0.5,
h_2_L = 1,
delta_1_L = 50,
delta_2_L = 50,
alpha_1_L = 0.1,
alpha_2_L = 0.1), algorithm="port")