0

I've been experiencing an issue when trying to estimate a model using the 'nls' function. I am trying to estimate the parameters (i.e. the aj's for j=0,1,...,9) in the following equation:

log(y) = log(a0 + a1*x1 + a2*x2 + a3*x3 + a4*(x1)^2 + a5*(x2)^2 + 
             a6*(x3)^2 + a7*(x1*x2) + a8*(x1*x3) + a9*(x2*x3)) + error

In order to avoid negative values inside the log function, I set the starting values for the parameters in the non-linear least squares model according to the following code:

ols.model <- lm(y ~ x1 + x2 + x3 + (x1)^2 + (x2)^2 + (x3)^2 + x1*x2 + 
                x1*x3 + x2*x3)
ols.coefficients <- ols.model$coefficients
fitted <- fitted(ols.model)
fitted <- fitted-ols.coefficients[1]
min.fitted <- min(fitted)
b0.start <- -min.fitted + 0.1

The above ensured that none of the starting fitted values will be negative inside the log function. My call of the nls regression looked like this:

nls.model <- nls(log(y) ~ log(b0 + b1*x1 + b2*x2 + b3*x3 + b4*(x1)^2 + 
                 b5*(x2)^2 + b6*(x3)^2 + b7*(x1*x2) + b8*(x1*x3) + 
                 b9*(x2*x3)),
             start=list(b0=b0.start, b1=ols.coefficients[2], 
                        b2=ols.coefficients[3], b3=ols.coefficients[4],
                        b4=ols.coefficients[5],b5=ols.coefficients[6],
                        b6=ols.coefficients[7], b7=ols.coefficients[8], 
                        b8=ols.coefficients[9], b9=ols.coefficients[10]),
             trace=TRUE)

Despite these carefully selected starting parameter values, I keep on getting an error message that reads:

Error in numericDeriv(form[[3L]], names(ind), env) : 
Missing value or an infinity produced when evaluating the model
In addition: Warning message:
In log(b0 + b1 * x1 + b2 * x2 + b3 * x3 + b4 * x4 +  :
NaNs produced

Does anyone have any idea how I can resolve this issue and estimate the non-linear model without getting an error message? My dataset does not contain any missing or zero values so that is definitely not the problem.

Larry
  • 45
  • 5
  • By the way, upvotes would be appreciated so that I can get my rep up to 15 and begin upvoting all of the kind souls who provide answers to my questions. As far as I understand, I can't upvote answers until my rep reaches 15. – Larry Oct 20 '14 at 21:11
  • 2
    If you do not provide reproducible problems, I see no reason for any upvotes. – IRTFM Oct 20 '14 at 21:25
  • In fact that's actually a good reason to downvote – Rich Scriven Oct 21 '14 at 01:17
  • If you apply exp to both sides, then the ols.model would not have a log(y) term, but just y. I would start with one term, calculate y, jitter it, and fit ols.model, and use that for the start value in the nls.model. Repeat for 2, 3, terms. Also, download the source for nls, and use debug to step in, or add print statements to see why nan are produced. –  Oct 21 '14 at 02:19
  • It may be helpful to use [wolfram](http://www.wolframalpha.com/input/?i=derivative%28log%28a*x%2Bb*x*y%2Bc*x*x%29%2Cx%29) to calculate some dervatives –  Oct 21 '14 at 02:25
  • I wouldn't say these starting values are carefully chosen. As John points out, this OLS model will not generate starting coefficients that are close to the right ones. The LHS variable should be `y`, not `log(y)`. `b0=1` and everything else 0 would probably work just as well or perhaps better. – farnsy Oct 21 '14 at 04:10
  • @BondedDust My apologies for not expressing the problem in the correct format. I suppose I was looking for some general advice about what might be going wrong rather than a line-by-line solution. I'll be sure to avoid this mistake next time I post a question. – Larry Oct 22 '14 at 02:41
  • CrossValidated.com is the place to post questions for which a specific coding response may not be possible. – IRTFM Oct 22 '14 at 02:50
  • Thanks @John and @farnsy for the suggestions. The `log(y)` in the `ols.model` line should have simply been `y`. The associated starting values still gave me an error message though. Anyway, I believe that I've now found a solution to the problem, so thanks to you both for your very helpful input. – Larry Oct 22 '14 at 02:53
  • @BondedDust Duly noted. Sorry again for the mixup. – Larry Oct 22 '14 at 02:54
  • I want to point out that if ols.model is solved for y = ..., then the next model with log of both sides should have the same solution. Also, frequently, if the gradient is going na, then there is a denominator that is going to zero, or the dependency on one of the inputs is removed because a coefficient is zero. –  Oct 22 '14 at 04:10

0 Answers0