1maxLik <- function(logLik, grad=NULL, hess=NULL, start,
2                   method,
3                   constraints=NULL,
4                   ...) {
5   ## Maximum Likelihood estimation.
6   ##
7   ## Newton-Raphson maximisation
8   ## Parameters:
9   ## logLik     log-likelihood function.  First argument must be the vector of parameters.
10   ## grad       gradient of log-likelihood.  If NULL, numeric gradient is used.  Must return either
11   ##               * vector, length=nParam
12   ##               * matrix, dim=c(nObs, 1).  Treated as vector
13   ##               * matrix, dim=c(nObs, nParam).  In this case the rows are simply
14   ##                 summed (useful for maxBHHH).
15   ## hess       Hessian function (numeric used if NULL)
16   ## start      initial vector of parameters (eventually w/names)
17   ## method     maximisation method (Newton-Raphson)
18   ## constraints  constrained optimization: a list (see below)
19   ## ...        additional arguments for the maximisation routine
20   ##
21   ## RESULTS:
22   ## list of class c("maxLik", "maxim").  This is in fact equal to class "maxim", just the
23   ## methods are different.
24   ## maximum     function value at maximum
25   ## estimate    the parameter value at maximum
26   ## gradient        gradient
27   ## hessian         Hessian
28   ## code        integer code of success, depends on the optimization
29   ##             method
30   ## message     character message describing the code
31   ## type        character, type of optimization
32   ##
33   ##             there may be more components, depending on the choice of
34   ##             the algorith.
35   ##
36   argNames <-  c( "logLik", "grad", "hess", "start", "method",
37                  "constraints" )
38   checkFuncArgs( logLik, argNames, "logLik", "maxLik" )
39   if( !is.null( grad ) ) {
40      checkFuncArgs( grad, argNames, "grad", "maxLik" )
41   }
42   if( !is.null( hess ) ) {
43      checkFuncArgs( hess, argNames, "hess", "maxLik" )
44   }
45   ## Constrained optimization.  We can two possibilities:
46   ## * linear equality constraints
47   ## * linear inequality constraints
48   ##
49   if(missing(method)) {
50      if(is.null(constraints)) {
51         method <- "nr"
52      }
53      else if(identical(names(constraints), c("ineqA", "ineqB"))) {
54         if(is.null(grad))
55             method <- "Nelder-Mead"
56         else
57             method <- "BFGS"
58      }
59      else
60          method <- "nr"
61   }
62   maxRoutine <- switch(tolower(method),
63                        "newton-raphson" =,
64                        "nr" = maxNR,
65                        "bfgs" = maxBFGS,
66                        "bfgsr" =, "bfgs-r" = maxBFGSR,
67                        "bhhh" = maxBHHH,
68                        "conjugate-gradient" =,
69                        "cg" = maxCG,
70                        "nelder-mead" =,
71                        "nm" = maxNM,
72                        "sann" = maxSANN,
73                        stop( "Maxlik: unknown maximisation method ", method )
74                        )
75   result <- maxRoutine(fn=logLik, grad=grad, hess=hess, start=start,
76                        constraints=constraints,
77                        ...)
78   class(result) <- c("maxLik", class(result))
79   result
80}
81