fitplotR
This ggplot2-based function allows quickly assessing paramter recovery. It shows the point estimates, the corresponding confidence/credibility intervals and the true value. The deviation of the point estimates is highlighted. Different models (e.g. using different estimation algorithms or different prior specifications) can be compared.
The function is a straightforward call of ggplot2:
library(ggplot2)
fitplotr = function(est, lw, up, true) {
if (is.null(dim(est))) {
nvar = length(est)
if (is.null(names(est))) {
varnames = paste0("v", 1:nvar)
} else {
varnames = names(est)
}
nmodel = 1
model = factor(1)
} else {
nvar = nrow(est)
if (is.null(rownames(est))) {
varnames = paste0("v", 1:nvar)
} else {
varnames = rownames(est)
}
nmodel = ncol(est)
if (is.null(colnames(est))) {
model = factor(1:nmodel)
} else {
model = colnames(est)
}
}
d = data.frame(x = rep(varnames, nmodel + 1), model = c(rep(model, each = nvar),
rep("true", nvar)), y = c(as.vector(est), true), Lo = c(as.vector(lw),
rep(NA, nvar)), Hi = c(as.vector(up), rep(NA, nvar)))
# computing the y-coordinates for the geom_segment part
starts <- ends <- drop(matrix(d$y[d$model == "true"], 1, sum(d$model !=
"true")))
foo0 = d$y[d$model == "true"] > d$y[d$model != "true"]
starts[foo0] = (d$y[d$model != "true"])[foo0]
ends[!foo0] = (d$y[d$model != "true"])[!foo0]
d$Lo2[d$model != "true"] = starts
d$Hi2[d$model != "true"] = ends
# plot the main part
p = ggplot(d[d$model != "true", ], aes(x, y, color = model)) + geom_point(size = 4,
position = position_dodge(width = 0.3)) + geom_errorbar(aes(ymin = Lo,
ymax = Hi), width = 0.15, position = position_dodge(width = 0.3)) +
geom_linerange(aes(ymin = Lo2, ymax = Hi2), size = 2, position = position_dodge(width = 0.3))
# plot the true value line
p + geom_crossbar(aes(ymin = y, ymax = y), data = d[d$model == "true", ])
}
Example using simulated data:
estimates = cbind(c(3, 4, 7), c(3, 5, 7) + 1)
true = c(4, 5, 6)
lowbound = estimates - runif(6) * 4
upbound = estimates + runif(6) * 4
fitplotr(estimates, lowbound, upbound, true)
## ymax not defined: adjusting position using y instead