Search code examples
rcurve-fittingr-mosaic

problems finding the right parameter value to fit a curve to observations using fitModel


I want to find the value which makes the following function predict the observations contained in the following dataset.

require(mosaic)
d=data.frame(t=c(41.5,44,42.77,47), y=c(230,76.4/60,3.4,1))
fitModel(y~(-(47-t)*I/(35-t)*(1+exp((42.77-t)*I))),
     data=d,start=list(I=3.1,t=47))

It seems I am replicating exactly what the example in the function's help says, but yet it reports a rather obscure error message.

Error in qr(.swts * gr) : dims [product 2] does not match the length of object [4]

Any suggestions?


Solution

  • t is your independent variable, it should not be included in the “start” list.

    Try:

    require(mosaic)
    d=data.frame(t=c(41.5, 44, 42.77, 47), y=c(230, 76.4/60, 3.4, 1))
    
    fitModel(y~(-(47-t)*I/(35-t)*(1+exp((42.77-t)*I))),
         data=d, start=list(I=3.1))
    

    Using nls() as alternative to fitModel():

    nls(y~(-(47-t)*I/(35-t)*(1+exp((42.77-t)*I))),
              data=d, start=list(I=3.1))
    
    Nonlinear regression model
      model: y ~ (-(47 - t) * I/(35 - t) * (1 + exp((42.77 - t) * I)))
       data: d
        I 
    3.432 
     residual sum-of-squares: 1.126
    
    Number of iterations to convergence: 4 
    Achieved convergence tolerance: 4.092e-07