optimCheck-package | R Documentation |
Tools for checking that the output of an optimization algorithm is indeed at a local mode of the objective function. This is accomplished graphically by calculating all one-dimensional "projection plots" of the objective function, i.e., varying each input variable one at a time with all other elements of the potential solution being fixed. The numerical values in these plots can be readily extracted for the purpose of automated and systematic unit-testing of optimization routines.
Maintainer: Martin Lysy mlysy@uwaterloo.ca
Useful links:
# example: logistic regression
ilogit <- binomial()$linkinv
# generate data
p <- sample(2:10,1) # number of parameters
n <- sample(1000:2000,1) # number of observations
X <- matrix(rnorm(n*p),n,p) # design matrix
beta0 <- rnorm(p, sd = .1) # true parameter values
y <- rbinom(n, size = 1, prob = ilogit(X %*% beta0)) # response
# fit logistic regression
bhat <- coef(glm(y ~ X - 1, family = binomial))
# check convergence
# likelihood function
loglik <- function(beta, y, X) {
sum(dbinom(y, size = 1, prob = ilogit(X %*% beta), log = TRUE))
}
# projection plots
bnames <- parse(text = paste0("beta[", 1:p, "]"))
system.time({
oproj <- optim_proj(xsol = bhat,
fun = function(beta) loglik(beta, y, X),
xnames = bnames,
xlab = "Coefficient", ylab = "Loglikelihood")
})
# numerical summary
oproj # see ?summary.optproj for more information
# elementwise differences between potential and optimal solution
diff(oproj) # same as summary(oproj)$xdiff
# refit general purpose optimizer starting from bhat
# often faster than optim_proj, but less stable
system.time({
orefit <- optim_refit(xsol = bhat,
fun = function(beta) loglik(beta, y, X))
})
orefit
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.