# Create functions to calculate the residual vector or the sum of squares, possibly with derivatives.

### Description

Create functions to calculate the residual vector or the sum of squares, possibly with derivatives.

### Usage

1 2 3 |

### Arguments

`modelformula` |
A formula describing a nonlinear regression model. |

`pvec` |
A vector of parameters. |

`data` |
A dataframe, list or environment holding data used in the calculation. |

`jacobian` |
Whether to compute the Jacobian matrix. |

`gradient` |
Whether to compute the Jacobian matrix. |

`testresult` |
Whether to test the function by evaluating it at |

`...` |
Additional data needed to compute the residuals and Jacobian. |

### Details

If `pvec`

does not have names, the parameters will have names
generated in the form p<n>, e.g. `p1, p2`

. Names that appear in
`pvec`

will be taken to be parameters of the model.

The `data`

argument may be a dataframe, list or environment, or `NULL`

.
If it is not an environment, one will be constructed using the components
of `data`

with parent environment set to be
the environment of `modelformula`

.

The search order for variables in the formula will be as follows:
Variables specified in the `...`

argument will always take highest precedence.
If `data`

is given and is an environment, the search continues only in that environment and its
parent, grandparent, etc. Otherwise `data`

is searched
next, followed by the environment of `modelformula`

(and its parents, grandparents, etc.)

### Value

`model2rjfunx`

returns a function with header `function(prm)`

, which
evaluates the residuals (and if `jacobian`

is `TRUE`

the
Jacobian matrix) of the model at `prm`

. The residuals are defined to be
the right hand side of `modelformula`

minus the left hand side.

`model2ssgrfunx`

returns a function with header `function(prm)`

, which
evaluates the sum of squared residuals (and if `gradient`

is `TRUE`

the
gradient vector) of the model at `prm`

.

### Author(s)

John Nash and Duncan Murdoch

### See Also

`nls`

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ```
y <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 38.558,
50.156, 62.948, 75.995, 91.972)
tt <- seq_along(y) # for testing
mydata <- data.frame(y = y, tt = tt)
f <- y ~ b1/(1 + b2 * exp(-1 * b3 * tt)) + shift
p <- c(b1 = 1, b2 = 1, b3 = 1)
# This fails
first<-try(rjfn <- model2rjfunx(f, p, data = mydata, shift=100))
if (class(first) != "try-error") {
rjfn(p)
myexp <- modelexpr(rjfn)
cat("myexp:")
print(myexp)
} else { cat("rjfn try-error\n") }
# ssgrfn <- model2ssgrfun(f, p, data = mydata, shift=100)
# ssgrfn(p)
# ssgrfn2 <- model2ssgrfun(f, p, data = mydata, shift=50)
# ssgrfn2(p)
``` |