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

Description

These functions create functions to evaluate residuals or sums of squares at particular parameter locations.

Usage

1
2
3
4
model2rjfun(modelformula, pvec, data = NULL, jacobian = TRUE, testresult = TRUE)
model2ssgrfun(modelformula, pvec, data = NULL, gradient = TRUE, 
	      testresult = TRUE)
modelexpr(fun)

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 gradient vector.

testresult

Whether to test the function by evaluating it at pvec.

fun

A function produced by one of model2rjfun or model2ssgrfun.

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.

Value

model2rjfun 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.

model2ssgrfun 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.

modelexpr returns the expression used to calculate the vector of residuals (and possibly the Jacobian) used in the previous functions.

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
  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))
  p <- c(b1 = 1, b2 = 1, b3 = 1)
  rjfn <- model2rjfun(f, p, data = mydata)
  rjfn(p)
  myexp <- modelexpr(rjfn)
  cat("myexp:")
  print(myexp)


  ssgrfn <- model2ssgrfun(f, p, data = mydata)
  ssgrfn(p)