See the Likelihood Calculation page for details on how
likelihood is calculated. anneal uses the same parameters and
is set up in the same way.
The model function is the scientific model, which generally takes as
arguments the parameters for which to estimate maximum likelihood. It
returns a predicted value of the dependent variable for each record in the
source_data dataset, which is compared to the actual (observed) value
when likelihood is calculated. Write model so that it returns a
vector of numeric values, one for each record in the dataset.
The probability density function calculates the likelihood using the
predicted and observed values of the dependent variable. You can provide
your own function, but R has many built-in functions that you can use. You
can read more about R's probability density functions in the help file
“An Introduction to R”, but here is a brief list: dbeta
(beta), dexp (exponential), dgamma (gamma),
dlnorm (lognormal), dnbinom (negative binomial),
dnorm (normal), and dpois (poisson). These all
take a log argument which you should set to TRUE in var in
order to calculate the log likelihood. If you write your own probability
density function, it should return a vector of values, one for each record
in the dataset.
If you wish, some of the arguments passed to model or pdf by
likeli can be the results of other functions.
likeli handles all function calls and data. You tell likeli
how to use your functions and data using par and var.
Use par to give likeli the list of parameters for the model.
Use var to tell likeli where all other functions and data come
from. var and var are lists, and each component's name matches
the function argument it should be used for. For example, if the
model function takes an argument called “a”, there
should be a par$a or a var$a with the value of a. For
par, all values must be numeric vectors. For var, the values
can be of any data type that makes sense to the function. To indicate that
the source of a function argument is a column of data from a dataset, set
that value of var to the name of the data frame's column, as a
character string (for example, var$dbh<-"DBH"). Case matters! You
will get the best results if all function arguments and column names are
unique, so that there is no ambiguity. You are also free to reference values directly from the global environment in your functions if you prefer.
The difference between par and var is important to
anneal but not to likeli.
The reserved character string “predicted”, used in var, means
the predicted value of the dependent variable, as calculated by model.
If you want likeli to pass the results of another function as an
argument to the model or pdf functions, define the function
and then set the appropriate argument in var to the name of the
function. Then provide all arguments to the sub-function in var as
well. For instance, if your model function takes an argument called
x, and you wish x to be the result of function fun1,
then set var$x <- fun1, and add any arguments to fun1 to
var. likeli will ensure that all functions are evaluated in
the proper order.