0

I have some trouble with adding another fitting parameter to my formula. I'm using nlsLM for fitting functions and plyr package fitting in groups. You can see the code below.

As well I understood from other questions there are many suggestions on when you obtain singular gradient matrix at initial parameter estimates you can vary the starting values or try to simplify your model by looking for redundant parameters which usually cause troubles.

So, I understand that starting parameter is important for not to get singular gradient matrix at initial parameter estimates error. why-is-nls-giving-me-singular-gradient-matrix-at-initial-parameter-estimates, sing-r-to-fit-a-curve-to-a-dataset-using-a-specific-equation

Then I started to fit my data by starting with two-term;

set.seed(12345)
set =rep(rep(c("1","2","3","4"),each=21),times=1)
time=rep(c(10,seq(100,900,100),seq(1000,10000,1000),20000),times=1)
value <- replicate(1,c(replicate(4,sort(10^runif(21,-6,-3),decreasing=FALSE))))

data_prep <- data.frame(time, value,set) ## this is example data set

> head(data_prep)
#    time        value set
#1     10 1.007882e-06   1
#2    100 1.269423e-06   1
#3    200 2.864973e-06   1
#4    300 3.155843e-06   1
#5    400 3.442633e-06   1
#6    500 9.446831e-06   1


sigma=17
d_step <- 1
Ps <- 0.5
f <- 1e9

formula = value~Ps*(1-exp(-2*f*time*exp(-d)))*1/(sqrt(2*pi*sigma))*exp(-(d-d_ave)^2/(2*sigma))*d_step

enter image description here its a probability distribution function probability distribution function. ps. I took sigma^2 as sigma. So no problem there.

library(minpack.lm)# load this packed
library(plyr)    # load this package for fitting

get.coefs <- function(data_prep) {
  fit <- nlsLM(formula ,
               data=data_prep,start=c(d_ave=43,d=42),trace=T,control = nls.lm.control(maxiter=100))
}

fit <- dlply(data_prep, c("set"), .fun = get.coefs)   # Fit data grouped by "set"

#    > fit
#    $`1`
#    Nonlinear regression model
#      model: value ~ Ps * (1 - exp(-2 * f * time * exp(-d))) * (1/(sqrt(2 #*   pi * sigma) * exp(-(d - d_ave)^2/(2 * sigma))) * d_step)
#       data: data_prep
#    d_ave     d 
#    55.71 41.34 
#     residual sum-of-squares: 1.249e-07

this is the result with fitting line

enter image description here

Ok when I do this process with d_ave and d I can do the fitting. However, when I wanted to add sigma parameter as a fitting parameter;

 get.coefs <- function(data_prep) {
      fit <- nlsLM(formula ,
                   data=data_prep,start=c(d_ave=43,d=42,sigma=17),trace=T,control = nls.lm.control(maxiter=100))
    }

fit <- dlply(data_prep, c("set"), .fun = get.coefs)   # Fit data grouped by "set"

I am getting two error and the first one is,

Error in nlsModel(formula, mf, start, wts) : 
  singular gradient matrix at initial parameter estimates

and the second one,

In addition: Warning messages:
1: In sqrt(2 * pi * sigma) : NaNs produced
2: In sqrt(2 * pi * sigma) : NaNs produced
3: In sqrt(2 * pi * sigma) : NaNs produced
4: In sqrt(2 * pi * sigma) : NaNs produced

I want to add even Ps value for fitting parameter later on to see how it is converges through the fitting. But even for three-term fitting does not converges and giving errors.

Any advice or answer will be appreciated. Can anyone point out what I'm doing wrong? Thanks for your help

Community
  • 1
  • 1
Alexander
  • 4,527
  • 5
  • 51
  • 98
  • That's a pretty complex model and some pretty limited data you have there. I would have been surprised if you had gotten a successful fit. The model seems to be a peak superposed with a saturation curve and these features would have to be super-obvious in your data to make a fit easy. – Roland Apr 28 '16 at 09:03
  • @Roland Can you elobrate the issue bit more. I couldn't catch your point:) – Alexander Apr 28 '16 at 09:14
  • 1
    The point is that complex models are difficult to fit. The more so, the less the data supports the model until it become impossible. You might be able to fit this, if you had extremely good starting values. – Roland Apr 28 '16 at 09:19
  • @Roland your suggestion is making model less complex? Is that what you mean. Actually the model is `probability distribution function`. I added picture of the model to my question. – Alexander Apr 28 '16 at 09:42
  • Yes, I recognized the normal distribution. But I don't understand "I'm looking for this distribution at each ...". – Roland Apr 28 '16 at 10:26
  • 1
    Try using nlxb from the nlmrt package instead of nlsLM. – G. Grothendieck Apr 28 '16 at 11:55
  • @Roland dear Roland I finally fixed my model to fit and following @G. Grothendieck I used `nlxb` from `nlmrt`. I moved my question to next stage. Please check it out from here [link](http://stackoverflow.com/questions/37110555/how-to-do-prediction-with-nlxb-from-nlmrt-package) – Alexander May 09 '16 at 08:18
  • @G.Grothendieck can you check my new question related to this post [link] http://stackoverflow.com/questions/37110555/how-to-do-prediction-with-nlxb-from-nlmrt-package – Alexander May 09 '16 at 08:19

0 Answers0