curve-fittingnls

non-linear least squares fitting: parameters without starting value


I am working on rate measurements (y) over a range of temperatures (x), and i would like to curve-fit the scatterplot using the nls command. However, the fitting does not initialize, since the algorithm is apparently missing some starting values. Although i provide starting values for the model parameters, it throws an error saying that it wants also starting values for the x and y data, which i think should be calculated from the model parameters.. ? I am a bit lost here, and a bit afraid that the mistake i am making is stupid and obvious...

Here is a reproducible example:

#make up some data
d<-data.frame(x=c(0,2,4,6,8,10,12),
              y=c(0.05, 0.1, 0.2, 0.26, 0.28,0.23, 0.12))

#plot the data
plot(d$x, d$y)

# define the Blanchard equation
blanchard <- function(temp, rmax, tmax, topt,beta) {
  return(  (rmax*((tmax-temp)/(tmax-topt))^beta)   * exp( -beta*((topt-temp)/(tmax-topt)         )     )
        )}

#test proper functionality of the equation:
#I give some fantasy parameters and use x data to predict y:
d$prediction<-blanchard(temp = d$x, rmax = 0.3, topt = 8, tmax =12.1,beta = 1.5)

#visualize:
lines(d$x, d$prediction, col="red")

#The model apparently works

#Now obtain some sensible starting values for the curve fitting
rmax_start <- max(d$y, na.rm = TRUE)
tmax_start <- max(d$x, na.rm = TRUE)
topt_start <- mean(d$x[d$y == rmax_start])
beta_start = 1.5
  
#Now it should be possible to fit the data, but an error occurs...
blanchardfit <- nls(rate~blanchard(temp = temperature, rmax, tmax, topt,beta),
                     data = d,
                     start=list(rmax=rmax_start, tmax=tmax_start, topt=topt_start),
                     supp_errors = 'Y')

#Here i tried with an explicit formula-writing and also insulating the model with I():
blanchardfit <- nls(rate~I((rmax*((tmax-temp)/(tmax-topt))^beta)   * exp( -beta*((topt-temp)/(tmax-topt)))),
                    data = d,
                    start= list(rmax=rmax_start, tmax=tmax_start, topt=topt_start),
                    supp_errors = 'Y')

Hope you can help me with this! Best wishes, Sebastian

I tried to give different names to modelparameters (temp) and column names (temperature), so these don´t get mixed up.

Tried to copy another, structurally similar command that works, and replaced it term by term, but I replace the starting values, itr still asks for starting values of x and y..


Solution

  • The "guessed" values of the parameters (rmax, topt, tmax, beta) are requiered to start the iterative process. It seems that several of your values rmax = 0.3, topt = 8, tmax =12.1, beta = 1.5 are far from the optimal ones. This might cause trouble. I found the values below (LMSRE criteria).

    enter image description here