Laura Teresa Corredor Bohórquez
2015-Jul-15 14:26 UTC
[R] nls singular gradient matrix with an integrand
Hi. I am trying to make a nls fit for a little bit complicated expression that includes two integrals (please find enclosed the equations). 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 am enclosing the data too (file.csv). And 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.
Very few attachment types are permitted on the R mailing lists... apparently whatever file you attached did not qualify. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On July 15, 2015 7:26:59 AM PDT, "Laura Teresa Corredor Boh?rquez" <ltcorredorb at gmail.com> wrote:>Hi. I am trying to make a nls fit for a little bit complicated >expression that >includes two integrals (please find enclosed the equations). > >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 am enclosing the data too (file.csv). > >And 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. >______________________________________________ >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.