Learn R Programming

nlsr (version 2021.8.19)

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

model2rjfun(modelformula, pvec, data = NULL, jacobian = TRUE, testresult = TRUE, ...)
model2ssgrfun(modelformula, pvec, data = NULL, gradient = TRUE, 
	      testresult = TRUE, ...)
modelexpr(fun)

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.

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.

...

Dot arguments, that is, arguments that may be supplied by name = value to supply information needed to compute specific quantities in the model.

Author

John Nash and Duncan Murdoch

Details

If pvec does not have names, the parameters will have names generated in the form p_<n>, e.g. p_1, p_2. 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.

See Also

Examples

Run this code

  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)

Run the code above in your browser using DataLab