low-decarie / temperatureresponse

R package for fitting a suite equations for the unimodal response to temperature
2 stars 1 forks source link

Making these equations stand alone functions #2

Open padpadpadpad opened 6 years ago

padpadpadpad commented 6 years ago

Hi Etienne

Really liked the paper alongside which this R package was published. I recently published an R package, nls.multstart, to fit non-linear regression using minpack::nlsLM() that allows multiple start values to make sure you get the best fit across non-linear curves. This guarantees the same parameter estimates as they can be sensitive to the start values due to different local minima.

When doing this for thermal responses, I use a function for the thermal response curve. For example, for the Sharpe-Schoofield curve, I use:

schoolfield_high <- function(lnc, E, Eh, Th, temp, Tc) {
  Tc <- 273.15 + Tc
  k <- 8.62e-5
  boltzmann.term <- lnc + log(exp(E/k*(1/Tc - 1/temp)))
  inactivation.term <- log(1/(1 + exp(Eh/k*(1/Th - 1/temp))))
  return(boltzmann.term + inactivation.term)
}

And then fit it to data using:

# run nls_multstart
fit <- nls_multstart(ln.rate ~ schoolfield_high(lnc, E, Eh, Th, temp = K, Tc = 20),
                     data = d_examp,
                     iter = 500,
                     start_lower = c(lnc = -10, E = 0.1, Eh = 0.2, Th = 285),
                     start_upper = c(lnc = 10, E = 2, Eh = 5, Th = 330),
                     supp_errors = 'Y',
                     na.action = na.omit,
                     lower = c(lnc = -10, E = 0, Eh = 0, Th = 0))

I was wondering whether an approach like nlsMicrobio but for thermal response curves might work here, where the functions are standalone to use in any nls() function rather than being fit within eq4() etc.

Just wondering if you'd be open to this suggestion instead of me reinventing the wheel in a new package? If you are happy for me to do this I am happy to make in roads to this over the next couple of weeks.

Many thanks Dan

low-decarie commented 6 years ago

Dear Dan,

I should probably have alerts for github issues, didn't catch this until now.
More than happy for you to do this conversion, it would be great.

As you can see from issues (as a to-do list for myself), likely stepping away from nls towards mle or other likelihood approaches as almost by definition the error is not normally distributed (bounded by 0, often strong upper limits, or strong skews of error distribution at high and low mean values).

padpadpadpad commented 6 years ago

Hi Etienne

Thanks for getting back to me. No problem with it being so long, I have not really had the time to work on anything like this anyway. When I have time I will try get all the thermal performance curves into stand alone functions.

Really like the look of the amend_ouput() function so would definitely retain that possibility. Just need a bit of time and motivation to make it happen.

In answer to your bullet points:

I have not played much with maximum likelihood but if it allows more flexibility in specifying error distributions that seems like an excellent idea.

I have started experimenting with Bayesian non-linear regression with brms in R, it does seem to allow the fitting of non-linear mixed effects models much easier.

Cheers Dan