wants <- c("numDeriv")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
Half the normal density is to the left of the mean
[1] 0.5
[1] 0.5
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.
pGauss <- function(x, mu=0, sigma=1) {
integrate(dGauss, lower=-Inf, upper=x, mu=mu, sigma=sigma)$value
}
Check custom CDF against R implementation
[1] 0.8413448
[1] 0.8413447
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
Maximum likelihood parameter estimate in Poisson regression
N <- 100
X <- rnorm(N, 0, 2)
mu <- exp(1 + 0.5*X)
Y <- rpois(N, mu)
glmFit <- glm(Y ~ X, family=poisson(link="log"))
(bML <- cbind(coef(glmFit)))
[,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
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
R markdown - markdown - R code - all posts