[R] Test model for singular gradient matrix

Ben Bolker bolker at ufl.edu
Fri Oct 16 19:08:20 CEST 2009




dmhultst wrote:
> 
> 
> Hello,
> 
> I am working with a real-time hydrologic modeling system, and I am using R
> (R batch script on Linux) to create a non-linear relationship
> (exponential)
> between observed data.  I want to extract the non-linear coefficients (“b”
> and “m”) if the relationship can be created, if the relationship cannot be
> created I will use default “b” and “m” coefficients.  I keep getting an
> error of singular gradient matrix (see below).  I want to test whether I
> can create the relationship (because if I cannot the script crashes) and
> use the model extracted coefficients or use default coefficients.
> 
> Model in R batch script:
> fit.nls <-  nls(P~(b*exp(m*Z)), start=list(m=0.015,b=0.017),
> control=list(maxiter=200))
> Error in nlsModel(formula, mf, start, wts) : 
> singular gradient matrix at initial parameter estimates
> 
> ** the initial parameter values are also the default
> 
> Question:
> 1) Is there a way to test fit.nls (or the data) prior to see if this error
> occurs.
> 2) Would this test be set up as an if statement?
> 	if (fit is good) {proceed model coefficients} else {use default
> coefficients}?
> 
> 


The standard R approach to this is try(), i.e. something like

result <- it.nls <-  try(nls(P~(b*exp(m*Z)), start=list(m=0.015,b=0.017),
control=list(maxiter=200)))
if (inherits(result,"try-error"))) {
     values <- default
} else {
   values <- coef(it.nls)
}

-- 
View this message in context: http://www.nabble.com/Test-model-for-singular-gradient-matrix-tp25927673p25928893.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list