How to find good start values for nls function?

后端 未结 3 1607
野趣味
野趣味 2021-02-09 09:54

I don\'t understand why I can\'t have a nls function for these data. I have tried with a lot of different start values and I have always the same error.

Here is what I h

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 10:40

    Fitting a three-parameter nonlinear model to four data points is going to be moderately challenging in any case, although in this case the data are well behaved. Point #1 is that your starting value for your c parameter (-5) was way off. Drawing a picture of the curve corresponding to your starting parameters (see below) would help you understand this (so would recognizing that the curve you get will range from c at its minimum to c+a at its maximum, and the range of your data is from 0.6 to 1 ...)

    However, even with a better starting guess I found myself fussing with control parameters (i.e. control=nls.control(maxiter=200)), followed by more warnings -- nls is not known for its robustness. So I tried the SSasympOff model, which implements a self-starting version of the curve you want to fit.

    start1 <- list(a=1, b=75, c=-5)
    start2 <- list(a=0.5, b=75, c=0.5)  ## a better guess
    
    pfun <- function(params) {
      data.frame(vec_x=60:90,
                 vec_y=do.call(expFct2,c(list(x=60:90),params)))
    }
    library(ggplot2)
    ggplot(data = dt,aes(x = vec_x, y = vec_y)) +  geom_point() +
      geom_line(data=pfun(start1))+
      geom_line(data=pfun(start2),colour="red")+
      geom_smooth(data=dt, method="nls", formula=y~SSasympOff(x, a, b, c),
                  se=FALSE)
    

    My advice in general is that it's easier to figure out what's going on and fix problems if you fit nls outside of geom_smooth and construct the curve you want to add using predict.nls ...

    More generally, the way to get good starting parameters is to understand the geometry of the function you are fitting, and which parameters control which aspects of the curve. As I mentioned above, c is the minimum value of the shifted saturating-exponential curve, a is the range, and b is a scale parameter (you can see that when x=b, the curve is 1-exp(-1) or approximately 2/3 of the way from the minimum to the maximum). Either a bit of algebra and calculus (i.e. taking limits), or playing around with the curve() function, are good ways to gather this information.

提交回复
热议问题