eloch216 / PhotoGEA

https://eloch216.github.io/PhotoGEA/
Other
1 stars 4 forks source link

Update fit_c3_aci.R #57

Closed eloch216 closed 1 year ago

eloch216 commented 1 year ago

Previously, fit_c3_aci performed a check to ensure that the initial guess returned by initial_guess_fun lies within the lower and upper bounds supplied by the user (lower and upper, respectively):

initial_guess <- pmax(initial_guess, lower)
initial_guess <- pmin(initial_guess, upper)

However, the default_optimizer uses the dfoptim::nmkb function, which requires that the initial guess lies within (but not on) the bounds. If the initial guess lies on the bounds, no error is produced, but the optimizer's behavior becomes undefined and it may begin making guesses of NA for some parameter values.

This can lead to some weird behavior. E.g. if there is a strange looking A-Ci curve, initial_guess_c3_aci may produce a negative estimate for Rd, which then gets reset by fit_c3_aci to the default lower bound of 0, which then causes an issue with the optimizer. Ultimately, an error will occur when a value of Rd = NA is passed to calculate_c3_assimilation. This whole process is very difficult to debug because the (uninformative) error message does not indicate that the real issue is related to the initial guess being on the bounds.

This PR addresses this problem by modifying fit_c3_aci. Now, the initial_guess is checked (and possibly modified) to ensure that it lies within (but not on) the user-supplied bounds:

lower_temp <- lower + 0.01 * (upper - lower)
upper_temp <- upper - 0.01 * (upper - lower)

initial_guess <- pmax(initial_guess, lower_temp)
initial_guess <- pmin(initial_guess, upper_temp)

This is an important bug fix, so it justifies a new version of the package.