The end of the line for error bars in R

When plotting in R, I often use the segments function to add lines representing confidence intervals. This is a very simple way to plot lines connecting pairs of x,y coordinates.

Recently I discovered that by default, segments are styled with rounded line caps, which add to their length. This means, of course, that confidence intervals are slightly wider than intended.

R provides three styles of line ending – round, butt and square – which can be specified by the lend argument. The figure here shows the outcome of using each line ending, with vertical lines indicating actual end-points of segments. Both round and square line ends overshoot these points, while butt ends represent them correctly.
par(mar=c(1, 4, 1, 1))
plot.window(xlim=c(0, 1), ylim=c(0.5, 3.5))
axis(2, 1:3, c('round', 'butt', 'square'), las=1)
segments(0.1, 1, 0.9, 1, lwd=20, lend='round')
segments(0.1, 2, 0.9, 2, lwd=20, lend='butt')
segments(0.1, 3, 0.9, 3, lwd=20, lend='square')
abline(v=c(0.1, 0.9))
Line end styles applied to segments plotted in R. Only 'butt' accurately represents end points.

Line end styles applied to segments plotted in R. Only ‘butt’ accurately represents end points.

The effect is slight, and is emphasized when line width is large. Regardless, it’s a good idea to routinely add lend='butt' (or lend=1) to your segments function calls.

A secondary benefit is that lines will appear crisper than when plotted with the default round caps.


I, Rbot: Tweeting from R

Model status update. A Tweet from the Rbot.

Model status update. A Tweet from the Rbot.

Over the past few weeks I’ve been running batches of JAGS simulations from R. Although these models typically converged within an hour or so, more complex models can take days, or even weeks to converge. Because we, as humans, are required to bathe, feed, socialise and attend weddings, there will inevitably come a time when you are required to leave the safety of your modelling den while your simulation is still chugging away. It would be nice, however, to keep in touch with your models’ progress, to be made instantly aware of any errors that occur in your absence, and to be advised upon successful task completion. This would be decidedly more satisfying than returning from a week-long holiday to find that your model broke on Day 1, just after you closed the door on your way out.

Enter Twidge, a command line Twitter client that humbly fills this neglected niche by allowing you to send a short Tweet to yourself from R via the system() function. The tweet can, of course, be a message pasted together from R objects, which permits dynamic tweet content and means your R-Tweeting power is really only limited by your imagination (and the 140 character message cap). Continue reading

OpenBUGS error messages. Something went right.

Although my relationship with OpenBUGS is still in the early stages, one glaringly obvious and quite infuriating shortcoming has been the lack of detail in its error messages. The initiated amongst you are no doubt familiar with the phrase “Sorry, something went wrong in procedure [blah]”. These error messages had me chasing my tail for hours in vain attempts to find offending segments of code. Surely, I thought, there must be a way to display more detailed information about the cause of an error.

Default OpenBUGS error messages

A default OpenBUGS error message

As it happens, there is. Continue reading