gradient.RdExtract the gradients of the log-likelihood function evaluated
at each observation (‘Empirical Estimating Function’,
see estfun).
# S3 method for class 'maxLik'
estfun(x, ...)
# S3 method for class 'maxim'
gradient(x, ...)gradientvector, objective function gradient at estimated maximum (or the last calculated value if the estimation did not converge.)
estfunmatrix, observation-wise log-likelihood gradients at the estimated parameter value evaluated at each observation. Observations in rows, parameters in columns.
The sandwich package must be loaded in order to use estfun.
estfun only works if the observaton-specific gradient information
was available for the estimation. This is the case of the
observation-specific gradient was supplied (see the grad
argument for maxLik), or the log-likelihood function
returns a vector of observation-specific values.
## ML estimation of exponential duration model:
t <- rexp(10, 2)
loglik <- function(theta) log(theta) - theta*t
## Estimate with numeric gradient and hessian
a <- maxLik(loglik, start=1 )
gradient(a)
#> [1] 1.04361e-08
# Extract the gradients evaluated at each observation
library( sandwich )
estfun( a )
#> [,1]
#> [1,] 0.1673213
#> [2,] -0.1980411
#> [3,] 0.3334365
#> [4,] -0.2093178
#> [5,] -0.2763933
#> [6,] -0.2160405
#> [7,] 0.2114339
#> [8,] 0.2603026
#> [9,] 0.1273366
#> [10,] -0.2000382
## Estimate with analytic gradient.
## Note: it returns a vector
gradlik <- function(theta) 1/theta - t
b <- maxLik(loglik, gradlik, start=1)
gradient(a)
#> [1] 1.04361e-08
estfun( b )
#> [,1]
#> [1,] 0.1673213
#> [2,] -0.1980411
#> [3,] 0.3334365
#> [4,] -0.2093178
#> [5,] -0.2763933
#> [6,] -0.2160405
#> [7,] 0.2114339
#> [8,] 0.2603026
#> [9,] 0.1273366
#> [10,] -0.2000382