# \donttest{
# use of \donttest{} because execution time exceeds 5 seconds
if(!requireNamespace("lattice", quietly = TRUE)){
install.packages("lattice", repos = "https://cloud.r-project.org/")
requireNamespace("lattice", quietly = TRUE)
}
data(sim_wrc_hcc)
# define number of cores for parallel computations
if(interactive()) ncpu <- parallel::detectCores() - 1L else ncpu <- 1L
# estimate parameters for 3 samples simultaneously by ...
# ... unconstrained, global optimisation algorithm NLOPT_GN_MLSL (default)
rfit_uglob <- fit_wrc_hcc(
wrc_formula = wc ~ head | id, hcc_formula = hc ~ head | id,
data = sim_wrc_hcc,
control = control_fit_wrc_hcc(param_bound = param_boundf(
alpha = c(0.00001, 50), n = c(1.0001, 7), tau = c(-1, 5)
), pcmp = control_pcmp(ncores = ncpu)))
# ... unconstrained, local optimisation algorithm NLOPT_LN_BOBYQA,
rfit_uloc <- update(
rfit_uglob,
param = as.matrix(coef(rfit_uglob, what = "nonlinear")),
control = control_fit_wrc_hcc(
settings = "ulocal", param_tf = param_transf(
alpha = "identity", n = "identity", tau = "identity"
), param_bound = param_boundf(
alpha = c(0.00001, 50), n = c(1.0001, 7), tau = c(-1, 5)
), pcmp = control_pcmp(ncores = ncpu)))
# extract estimated parameters for sample id == "1"
coef_id_1 <- unlist(coef(rfit_uloc, gof = TRUE, se = TRUE, subset = "1"))
# compute loglikelihood profile of parameter alpha for sample id == "1"
rprfloglik_alpha <- prfloglik_sample(
rfit_uloc, values = data.frame(alpha = seq(1.5, 3.0, length = 40L)),
soil_sample = "1", ncores = ncpu)
# plot loglikelihood profile along with 95% confidence intervals
plot(loglik ~ alpha, rprfloglik_alpha, type = "l")
abline(v = coef_id_1["alpha"])
# 95% confidence intervall based on likelihood ratio test
abline(h = -coef_id_1["obj"] - 0.5 * qchisq(0.95, 1), lty = "dashed")
# 95% confidence intervall based on asymptotic normal distribution
segments(
x0 = coef_id_1["alpha"] + qnorm(0.025) * coef_id_1["se.alpha"],
x1 = coef_id_1["alpha"] + qnorm(0.975) * coef_id_1["se.alpha"],
y0 = min(rprfloglik_alpha$loglik)
)
# compute loglikelihood profile of parameter n for sample id == "1"
rprfloglik_n <- prfloglik_sample(
rfit_uloc, values = data.frame(n = seq(1.7, 2.25, length = 40L)),
soil_sample = "1", ncores = ncpu
)
# plot loglikelihood profile along with 95% confidence intervals
plot(loglik ~ n, rprfloglik_n, type = "l")
abline(v = coef_id_1["n"])
# 95% confidence intervall based on likelihood ratio test
abline(h = -coef_id_1["obj"] - 0.5 * qchisq(0.95, 1), lty = "dashed")
# 95% confidence intervall based on asymptotic normal distribution
segments(
x0 = coef_id_1["n"] + qnorm(0.025) * coef_id_1["se.n"],
x1 = coef_id_1["n"] + qnorm(0.975) * coef_id_1["se.n"],
y0 = min(rprfloglik_n$loglik)
)
# compute loglikelihood profile of parameters alpha and n for sample id == "1"
rprfloglik_alpha_n <- prfloglik_sample(
rfit_uloc, values = expand.grid(
alpha = seq(1.5, 3.0, length = 40L), n = seq(1.7, 2.25, length = 40L)),
soil_sample = "1", ncores = ncpu
)
# joint confidence region of alpha and n based on likelihood ratio test
lattice::levelplot(loglik ~ alpha + n, rprfloglik_alpha_n,
panel = function(x, y, z, subscripts, ...){
lattice::panel.levelplot(x, y, z, subscripts, ...)
lattice::panel.levelplot(x, y, z, subscripts, region = FALSE, contour = TRUE,
at = -coef_id_1["obj"] - 0.5 * qchisq(0.95, 2),
lty = "solid"
)
lattice::panel.levelplot(x, y, z, subscripts, region = FALSE, contour = TRUE,
at = -coef_id_1["obj"] - 0.5 * qchisq(0.9, 2),
lty = "dashed"
)
lattice::panel.levelplot(x, y, z, subscripts, region = FALSE, contour = TRUE,
at = -coef_id_1["obj"] - 0.5 * qchisq(0.8, 2),
lty = "dotted"
)
lattice::panel.points(coef_id_1["alpha"], coef_id_1["n"], pch = 16)
lattice::panel.lines(
x = rprfloglik_alpha[, c("alpha", "n")], col = "blue"
)
lattice::panel.lines(
x = rprfloglik_n[, c("alpha", "n")], col = "magenta"
)
}, key = list(
corner = c(1, 0.97),
lines = list(
lty = c(rep("solid", 3), "dashed", "dotted"),
col = c("blue", "magenta", rep("black", 3))
),
text = list(c(
"estimate of n as a function of fixed alpha",
"estimate of alpha as a function of fixed n",
"95% joint confidence region",
"90% joint confidence region",
"80% joint confidence region"
))
))
# compute 95%-confidence interval
(ci.alpha <- confint_prfloglik_sample(
rfit_uloc, parm = "alpha", soil_sample = "1"
))
# use limits to draw loglikelihood profile for alpha
rprfloglik_alpha <- prfloglik_sample(
rfit_uloc, values = data.frame(
alpha = seq(0.9 * ci.alpha[1], 1.1 * ci.alpha[2], length = 40L)),
soil_sample = "1", ncores = ncpu)
plot(loglik ~ alpha, rprfloglik_alpha, type = "l")
lines(
ci.alpha,
rep(diff(unlist(attr(ci.alpha, "prfloglik")[c("qtest", "loglik")])) , 2)
)
abline(v = attr(ci.alpha, "prfloglik")["estimate"], lty = "dashed")
# 95%-confidence intervals of all nonlinear parameters based for all
# soil samples asymptotic normal distribution of maximum likelihood estimates
confint(rfit_uloc, type = "normal")
# 95%-confidence intervals of all nonlinear parameters based for all
# soil samples based on likelihood ratio test
confint(rfit_uloc, ncores = ncpu)
# }Run the code above in your browser using DataLab