View source: R/likeli_4_optim.R
likeli_4_optim | R Documentation |
Wraps the function likeli
so you can use it with
optim
. This allows you to use other optimization methods to
find maximum likelihood estimates.
likeli_4_optim(par_2_analyze, model, par_names, var, source_data, pdf)
par_2_analyze |
Vector of initial values for those parameters that are
to be optimized. This should be a vector, NOT a list. This MUST be a
one-dimensional vector - i.e. none of the vector members can be vectors
themselves (in contrast to the rules for |
model |
Model function for which to calculate likelihood. |
par_names |
Character vector with the name for each value in
|
var |
List object with the source for all other non-parameter arguments and
data used by the model, the PDF, and any sub-functions. This is the same as
the argument that you pass to |
source_data |
Data frame containing any needed source data, including observed values. |
pdf |
Probability density function to use. If you want a log likelihood value, which is usual, the PDF must calculate the log of its result. |
This wraps the likeli
function so that it can conform to the
requirements of optim
. Setting up to use this function is
exactly like setting up to use likeli
.
Remember to set the fnscale
option in the control list for
optim
to -1 so that optim
performs a maximization rather than
the default minimization (see example for details).
A single numeric value for the likelihood. It is possible for this to be
NAN
or Inf
.
#################
## Set up for likeli
#################
## Use the included crown_rad dataset
data(crown_rad)
## Create our model function - crown radius is a linear function of DBH.
## DBH is a column of data from the crown_rad dataset; a and b are single
## parameter values.
model <- function (a, b, DBH) {a + b * DBH}
## We are planning to get maximum likelihood estimates for a and b. Create
## the list that says where all other functions and data are to be found.
## Indicate that DBH comes from the column marked "DBH" in the crown_rad dataset.
var<-list(DBH = "DBH")
## We'll use the normal probability density function dnorm - add its
## arguments to our parameter list
## "x" value in PDF is observed value
var$x <- "Radius"
## The mean is the predicted value, the outcome of the model statement. Use
## the reserved word "predicted"
var$mean <- "predicted"
var$sd <- 1.0
## Have dnorm calculate log likelihood
var$log <- TRUE
## Set up a vector with initial values for a and b
par_2_analyze <- c(0.1, 0.1)
## Set up the vector with the names of a and b, so likeli_4_optim knows
## what the values in for_optim are
par_names <- c("a", "b")
## Set your choice of optim controls - pass the other likeli_4_optim arguments
## by name so optim knows they are for likeli_4_optim
## Remember to set the fnscale option of optim to a negative value to perform
## a maximization rather than a minimization
## Not run: optim(par_2_analyze, likeli_4_optim, method = "Nelder-Mead",
control = list(fnscale = -1), model = model, par_names = par_names,
var = var, source_data = crown_rad, pdf = dnorm)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.