Laura Teresa Corredor Bohórquez
2015-Jul-16 12:21 UTC
[R] nls singular gradient matrix - fit parameters in integral's upper limits
-------The las post rejected two files I had attached, so I modified it.--------------- Hi. I am trying to make a nls fit for a little bit complicated expression that includes two integrals with two of the fit parameters in their upper limits. I got the error "Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates". First of all, I have searched already in the previous answers, but didn?t help. The parameters initialization seems to be ok, I have tried to change the parameters but no one works. If my function has just one integral everything works very nicely, but when adding a second integral term just got the error. I don?t believe the function is over-parametrized, as I have performed other fits with much more parameters and they worked. I did try to enclose the data but the attachment was rejected. The minimal example is the following: # read the data from a csv file dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE) x = 0*(1:97) y = 0*(1:97) for(i in 1:97){ x[i] = dados[i,1] y[i] = dados[i,2] } integrand <- function(X) { return(X^4/(2*sinh(X/2))^2) } fitting = function(T1, T2, N, D, x){ int1 = integrate(integrand, lower=0, upper = T1)$value int2 = integrate(integrand, lower=0, upper = T2)$value return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2 )+(448.956*(x/T1)^3*int1)+(299.304*(x/T2)^3*int2)) } fit = nls(y ~ fitting(T1, T2, N, D, x), start=list(T1=400,T2=200,N=0.01,D=2)) ------>For reference, the fit that worked is the following: # read the data from a csv file dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE) x = 0*(1:97) y = 0*(1:97) for(i in 1:97){ x[i] = dados[i,1] y[i] = dados[i,2] } integrand <- function(X) { return(X^4/(2*sinh(X/2))^2) } fitting = function(T1, N, D, x){ int = integrate(integrand, lower=0, upper = T1)$value return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2 )+(748.26)*(x/T1)^3*int) } fit = nls(y ~ fitting(T1 , N, D, x), start=list(T1=400,N=0.01,D=2)) I cannot figure out what happen. I need to perform this fit for three integral components, but even for two I have this problem. I appreciate so much your help. Thank you. [[alternative HTML version deleted]]
ProfJCNash
2015-Jul-16 14:36 UTC
[R] nls singular gradient matrix - fit parameters in integral's upper limits
The list rejects almost all attachments. You could dput the data and put it in your posting. You may also want to try a Marquardt solver. In R from my nlmrt or compiled in Kate Mullen's minpack.lm. They are slightly different in flavour and the call is a bit different from nls. JN On 15-07-16 08:21 AM, Laura Teresa Corredor Boh?rquez wrote:> -------The las post rejected two files I had attached, so I modified > it.--------------- > > > Hi. I am trying to make a nls fit for a little bit complicated expression that > includes two integrals with two of the fit parameters in their upper limits. > > I got the error "Error in nlsModel(formula, mf, start, wts) : > singular gradient > matrix at initial parameter estimates". First of all, I have searched > already in the previous answers, but didn?t help. The parameters initialization > seems to be ok, I have tried to change the parameters but no one works. If > my function has just one integral everything works very nicely, but when adding > a second integral term just got the error. I don?t believe the function is > over-parametrized, as I have performed other fits with much more parameters > and they worked. I did try to enclose the data but the attachment was > rejected. > > The minimal example is the following: > > # read the data from a csv file > dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE) > x = 0*(1:97) > y = 0*(1:97) > for(i in 1:97){ > x[i] = dados[i,1] > y[i] = dados[i,2] > } > integrand <- function(X) { > return(X^4/(2*sinh(X/2))^2) > } > fitting = function(T1, T2, N, D, x){ > int1 = integrate(integrand, lower=0, upper = T1)$value > int2 = integrate(integrand, lower=0, upper = T2)$value > return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2 > )+(448.956*(x/T1)^3*int1)+(299.304*(x/T2)^3*int2)) > } > fit = nls(y ~ fitting(T1, T2, N, D, x), > start=list(T1=400,T2=200,N=0.01,D=2)) > > ------>For reference, the fit that worked is the following: > > # read the data from a csv file > dados = read.csv("file.csv", header=FALSE, stringsAsFactors=FALSE) > x = 0*(1:97) > y = 0*(1:97) > for(i in 1:97){ > x[i] = dados[i,1] > y[i] = dados[i,2] > } > integrand <- function(X) { > return(X^4/(2*sinh(X/2))^2) > } > fitting = function(T1, N, D, x){ > int = integrate(integrand, lower=0, upper = T1)$value > return(N*(D/x)^2*(exp(D/x)/(1+exp(D/x))^2 )+(748.26)*(x/T1)^3*int) > } > fit = nls(y ~ fitting(T1 , N, D, x), start=list(T1=400,N=0.01,D=2)) > > > I cannot figure out what happen. I need to perform this fit for three > integral components, but even for two I have this problem. I appreciate so > much your help. Thank you. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >