问题
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
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
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
来源:https://stackoverflow.com/questions/36909690/trouble-when-adding-3rd-fitting-parameter-in-nls