View source: R/confidence_intervals.R
conf_intervals | R Documentation |
Calculates confidence intervals for individual parameters.
conf_intervals(
object,
which_pars = NULL,
init = NULL,
conf = 95,
mult = 1.5,
num = 10,
type = c("vertical", "cholesky", "spectral", "none"),
profile = TRUE,
...
)
object |
An object of class |
which_pars |
A vector specifying the (unfixed) parameters for which
confidence intervals are required. Can be either a numeric vector,
specifying indices of the components of the full parameter
vector, or a character vector of parameter names, which must be a subset
of those supplied in
If missing, all parameters are included. |
init |
A numeric vector of initial estimates of the values of the
parameters that are not fixed and are not in |
conf |
A numeric scalar in (0, 100). Confidence level for the intervals. |
mult |
A numeric vector of length 1 or the same length as
|
num |
A numeric scalar. The number of values at which to evaluate the
profile loglikelihood either side of the MLE. Increasing |
type |
A character scalar. The argument |
profile |
A logical scalar. If |
... |
Further arguments to be passed to |
Calculates (profile, if necessary) likelihood-based confidence
intervals for individual parameters, and also provides symmetric intervals
based on a normal approximation to the sampling distribution of the
estimator. See also the S3 confint method
confint.chandwich
.
An object of class "confint", a list with components
conf |
The argument |
cutoff |
A numeric scalar. For values inside the
confidence interval the profile loglikelihood lies above
|
parameter_vals, prof_loglik_vals |
|
sym_CI, prof_CI |
|
If a value in
prof_CI
is NA
then this means that the search for the
confidence limit did no extend far enough. A remedy is to increase
the value of mult
, or the relevant component of mult
,
and perhaps also increase num
.
max_loglik |
The value of the adjusted loglikelihood
at its maximum, stored in |
type |
The argument |
which_pars |
The argument |
name |
A character scalar. The name of the model,
stored in |
p_current |
The number of free parameters in the current model. |
fixed_pars, fixed_at |
|
confint.chandwich
S3 confint method for objects
of class "chandwich"
returned from adjust_loglik
.
adjust_loglik
to adjust a user-supplied
loglikelihood function.
summary.chandwich
for maximum likelihood estimates
and unadjusted and adjusted standard errors.
plot.chandwich
for plots of one-dimensional adjusted
loglikelihoods.
conf_region
for a confidence region for
a pair of parameters.
compare_models
to compare nested models using an
(adjusted) likelihood ratio test.
plot.confint
, print.confint
.
# ------------------------- Binomial model, rats data ----------------------
# Contributions to the independence loglikelihood
binom_loglik <- function(prob, data) {
if (prob < 0 || prob > 1) {
return(-Inf)
}
return(dbinom(data[, "y"], data[, "n"], prob, log = TRUE))
}
rat_res <- adjust_loglik(loglik = binom_loglik, data = rats, par_names = "p")
# 95% likelihood-based confidence intervals, vertically adjusted
ci <- conf_intervals(rat_res)
plot(ci)
# Unadjusted
conf_intervals(rat_res, type = "none")
# -------------------------- GEV model, owtemps data -----------------------
# ------------ following Section 5.2 of Chandler and Bate (2007) -----------
gev_loglik <- function(pars, data) {
o_pars <- pars[c(1, 3, 5)] + pars[c(2, 4, 6)]
w_pars <- pars[c(1, 3, 5)] - pars[c(2, 4, 6)]
if (isTRUE(o_pars[2] <= 0 | w_pars[2] <= 0)) return(-Inf)
o_data <- data[, "Oxford"]
w_data <- data[, "Worthing"]
check <- 1 + o_pars[3] * (o_data - o_pars[1]) / o_pars[2]
if (isTRUE(any(check <= 0))) return(-Inf)
check <- 1 + w_pars[3] * (w_data - w_pars[1]) / w_pars[2]
if (isTRUE(any(check <= 0))) return(-Inf)
o_loglik <- log_gev(o_data, o_pars[1], o_pars[2], o_pars[3])
w_loglik <- log_gev(w_data, w_pars[1], w_pars[2], w_pars[3])
return(o_loglik + w_loglik)
}
# Initial estimates (method of moments for the Gumbel case)
sigma <- as.numeric(sqrt(6 * diag(var(owtemps))) / pi)
mu <- as.numeric(colMeans(owtemps) - 0.57722 * sigma)
init <- c(mean(mu), -diff(mu) / 2, mean(sigma), -diff(sigma) / 2, 0, 0)
# Log-likelihood adjustment of the full model
par_names <- c("mu[0]", "mu[1]", "sigma[0]", "sigma[1]", "xi[0]", "xi[1]")
large <- adjust_loglik(gev_loglik, data = owtemps, init = init,
par_names = par_names)
# 95% likelihood-based confidence intervals, vertically adjusted
large_v <- conf_intervals(large, which_pars = c("xi[0]", "xi[1]"))
large_v
plot(large_v)
plot(large_v, which_par = "xi[1]")
# Unadjusted
large_none <- conf_intervals(large, which_pars = c("xi[0]", "xi[1]"),
type = "none")
large_none
plot(large_v, large_none)
plot(large_v, large_none, which_par = "xi[1]")
# --------- Misspecified Poisson model for negative binomial data ----------
# ... following Section 5.1 of the "Object-Oriented Computation of Sandwich
# Estimators" vignette of the sandwich package
# https://cran.r-project.org/web/packages/sandwich/vignettes/sandwich-OOP.pdf
# Simulate data
set.seed(123)
x <- rnorm(250)
y <- rnbinom(250, mu = exp(1 + x), size = 1)
# Fit misspecified Poisson model
fm_pois <- glm(y ~ x + I(x^2), family = poisson)
summary(fm_pois)$coefficients
# Contributions to the independence loglikelihood
pois_glm_loglik <- function(pars, y, x) {
log_mu <- pars[1] + pars[2] * x + pars[3] * x ^ 2
return(dpois(y, lambda = exp(log_mu), log = TRUE))
}
pars <- c("alpha", "beta", "gamma")
pois_quad <- adjust_loglik(pois_glm_loglik, y = y, x = x, par_names = pars)
conf_intervals(pois_quad)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.