# [R] nls model definition help

Prof J C Nash (U30A) nashjc at uottawa.ca
Wed Oct 23 14:38:13 CEST 2013

```Because you have y on both sides, and at different times, I think you
are going to have to bite the bullet and write down a residual function.

Suggestion: write it as

res[t+1] = (th1*x1 + R1*x2) * exp(a1*x3) + (1-th1*x1 + R1*x2)*y(t) - y[t+1]

(cleaning up the indices -- they are surely needed for the x's too).
This way you can compute derivatives without sign issue if you decide to
do so rather than relying on numeric Jacobian.

nlmrt or minpack.lm will give more stable computations that nls(). Both
have either formula or functional versions of the NLLS minimizer. nlfb()
from nlmrt does bounds constraints and also fixed parameters (masks).

JN

On 13-10-23 06:00 AM, r-help-request at r-project.org wrote:
> Message: 37
> Date: Tue, 22 Oct 2013 17:52:28 +0000
> From: <Wayne.W.Jones at shell.com>
> To: <R-help at r-project.org>
> Subject: [R]  nls model definition help
> Message-ID:
>
> Content-Type: text/plain
>
> Hi fellow R users,
>
> I'm trying to fit a model using nls with the following model definition:
>
> y(t+1)=(th1*x1 + R1*x2) * exp(a1*x3) + (1-th1*x1 + R1*x2)*y(t)
>
> y is the dependent variable (note on both sides of eq) and the x's represent the regressors.
> th1, R1 and a1 are parameters to be estimated. The problem is non- linear and hence why I'm trying to fit using the well used "nls" function.
>
> To fit the model I would like to be able to use the formula interface rather than build my own ugly function definition.
> Any ideas if this is achievable and if not any ideas on how to fit this model?
>
>
> Many thanks,
>
> Wayne

```