Skip to contents

Useful for converting a regular function into a function amenable to optimization via stats::optim, which requires all free parameters be passed as a single vector par.

Usage

collect_fn_params(fn)

Arguments

fn

function A non-primitive function to refactor such that the first argument becomes the second argument and all other parameters must be passed as a vector to the first argument of the new function via the par parameter.

Value

function A new non-primitive function where the first argument is par, which takes a vector of parameters being optimized, and the second argument is the old first argument to fn (usually x since this is the independent variable to optimize the function over).

Details

Takes a function of the form f(x, ...), where ... is any number of additional function parameters (bot not literal ...!) and parses it to a function of the form f(par, x) where par is a vector of values for ... in the same order as the arguments appear in fn.