Integration and Differentiation

Install required packages

numDeriv

Integration

Example

Half the normal density is to the left of the mean

[1] 0.5
[1] 0.5

Re-implement normal cumulative distribution function

Case study for implementing a cumulative distribution function which may not be available in closed form whereas the corresponding density function is.

Naive implementation of normal density - numerically unstable, only works within narrow parameter range.

Implement normal cumulative distribution function by integrating density function.

Check custom CDF against R implementation

[1] 0.8413448
[1] 0.8413447

Differentiation

Examples

Density function = derivative of cumulative distribution function. Check with normal distribution.

[1] 0.05399097 0.24197072 0.39894228 0.24197072 0.05399097
[1] 0.05399097 0.24197072 0.39894228 0.24197072 0.05399097

Derivative of exponential function is exponential function.

[1] 0.1353353 0.3678794 1.0000000 2.7182818 7.3890561
[1] 0.1353353 0.3678794 1.0000000 2.7182818 7.3890561

Delta method for standard error of function of parameter estimates

Maximum likelihood parameter estimate in Poisson regression

                 [,1]
(Intercept) 1.0960995
X           0.4842425

Function of the first two parameter estimates -> Evaluate function at ML estimate.

[1] 2.263534

Delta method -> approximate standard error of function of parameter estimates -> approximate Wald confidence interval

[1]  2.065081 -4.674382
          [,1]
[1,] 0.2109704
      lo       up 
1.850040 2.677029 

Standard errors from Hessian matrix

Poisson negative log-likelihood

Inverse of Hessian at ML estimate = variance-covariance matrix of parameter estimates.

              [,1]          [,2]
[1,]  0.0040165026 -0.0009308869
[2,] -0.0009308869  0.0004305852

Compare to vcov()

              (Intercept)             X
(Intercept)  0.0040165025 -0.0009308869
X           -0.0009308869  0.0004305852

Further resources

Useful packages

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts