fitplotR - quickly assessing model recovery



fitplotR

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


hview - quick preview of html output in the rstudio viewer pane

Sometimes, you want to quickly check what the html output of xtable or similar function will look like in your markdown code, but you don't want to open up a new markdown document. Right from the console, you can create a temp html file, put the output in and view it in the viewer pane. This way you can quickly look at model output in that pane, as opposed to on the console.
require(xtable)
## Loading required package: xtable
hview = function(htmllines) {
    htmlFile <- tempfile(fileext = ".html")
    writeLines(htmllines, htmlFile)
    rstudio::viewer(htmlFile)
}

data(tli)
fm2 <- lm(tlimth ~ sex * ethnicty, data = tli)
fm2.table <- xtable(fm2)
print(fm2.table, type = "html")
Estimate Std. Error t value Pr(&gt |t|)
(Intercept) 73.6364 4.2502 17.33 0.0000
sexM -1.6364 5.8842 -0.28 0.7816
ethnictyHISPANIC -9.7614 6.5501 -1.49 0.1395
ethnictyOTHER 15.8636 10.8360 1.46 0.1466
ethnictyWHITE 4.7970 4.9687 0.97 0.3368
sexM:ethnictyHISPANIC 10.6780 8.7190 1.22 0.2238
sexM:ethnictyWHITE 5.1230 7.0140 0.73 0.4670
hview(print(fm2.table, type = "html"))```