`nls` fitting error: always reach maximum number of iterations regardless starting values

后端 未结 1 1531
孤街浪徒
孤街浪徒 2021-01-15 05:44

Using this parametrization for a growth curve logistic model

I created some points with: K =0.7 ; y0=0.01 ; r =0.3

df = data.frame(x= seq(1,         


        
相关标签:
1条回答
  • 2021-01-15 06:46

    Do not use ‘nls’ on artificial "zero-residual" data., as documented in ?nls.

    set.seed(0)
    x <- seq(1, 50, by = 5)
    y <- 0.7 / (1 + ((0.7 - 0.01) / 0.01) * exp(-0.3 * x))
    y <- y + rnorm(length(x), sd = 0.05)  ## add Gaussian error!!
    dat <- data.frame(x = x, y = y); rm(x, y)
    with(dat, plot(x, y))
    
    fit <- nls(y ~ K / (1 + ((K - y0) / y0) * exp(-r * x)), data = dat,
               start = list(K = 0.7, y0 = 0.01, r = 0.3))
    
    #Nonlinear regression model
    #  model: y ~ K/(1 + ((K - y0)/y0) * exp(-r * x))
    #   data: dat
    #      K      y0       r 
    #0.70013 0.01841 0.27950 
    # residual sum-of-squares: 0.02851
    # 
    #Number of iterations to convergence: 12 
    #Achieved convergence tolerance: 4.145e-06
    

    Also, avoid using $ in model formula, otherwise you will get into trouble when using predict later.

    0 讨论(0)
提交回复
热议问题