Publications by is.R()

Visually-weighted regression plots, with Zelig

25.09.2012

As a follow-up to yesterday’s post on producing visually-weighted regression plots, here is some code which illustrates the production of similar plots, but using Zelig’s convenient modeling and simulation functions. This code was produced to assist a colleague, which just goes to show that the “Ask us anything” page really works! https:/...

754 sym 2 img

Modifying select off-diagonal items in a matrix

25.09.2012

This is something I have had the occasion to do, and never remember how, so this is legitimately a reminder to my future self of how to do things with off-diagonal elements of a matrix. Select rows and columns are easy: mat[1:10, ] or mat[, -c(5)], for example, as is the diagonal: diag(mat) However, access to select off-diagonal slices is not ob...

927 sym

Simplest possible heatmap with ggplot2

27.09.2012

Featuring the lovely “spectral” palette from Colorbrewer. This really just serves as a reminder of how to do four things I frequently want to do: Make a heatmap of some kind of matrix, often a square correlation matrix Reorder a factor variable, as displayed along the axis of a plot Define my own color palette with colorRampPalette() Use RCol...

820 sym 2 img

Optimal seriation for your matrices

28.09.2012

In our previous post, we used a quick-and-dirty method for ordering the axes on our heatmap. It has been pointed out to me that There is a Package for That (which is my nominee for a new slogan for R — not that it needs a slogan). seriation offers many methods for optimally ordering dist() objects, matrices, and arrays, and the Gist below offer...

1076 sym 2 img

Making random, equally-sized partitions

01.10.2012

Sometimes, as with cross-validation, one needs to generate k partitions, each with an equal number of observations. There are probably an infinite number of ways this could be done in R, but the Gist below illustrates one way to do it in four lines, while ensuring that the partition counts are as equal as possible. https://gist.github.com/3695362...

738 sym 2 img

A replacement for theme_blank()

02.10.2012

ggplot2 has just hit 0.9.2, and with the change comes a new theme system. Previous versions of ggplot2 offered a theme_blank(), which was a stripped-down, essentially blank plotting canvas, but it is now deprecated. github user jrnold has produced a series of nice themes to work with the new system, but my most pressing need was for a replacement...

920 sym 2 img

Transforming a color scale

03.10.2012

In developing plots, I often use color (or “colour” in ggplot2 parlance) to reflect values of a third, non-X/Y, variable. Depending on the distribution of this Z variable, however, the effective color range can be narrow, making it difficult to discriminate between Z values, as in this plot: As you can see, the bulk of the points are in the ...

1206 sym 4 img

Log odds ratios and an indicator matrix from categorical data

04.10.2012

A long title, but there are a couple of handy things in this Gist. The first, and more obscure, is the conversion of a data.frame of categorical variables into a matrix of dummy/binary/indicator variables, one for each category of each original variable. It is non-obvious (to me, at least) how to best do this, so the solution comes from “Gavin ...

1057 sym 2 img

Calculating distances (across matrices)

05.10.2012

This Gist is mostly for my future self, as a reminder of how to find distances between each row in two different matrices. To create a distance matrix from a single matrix, the function dist(), from the stats package is sufficient. There are times, however, when I want to see how close each row of a matrix is to another set of observations, and t...

1409 sym 2 img

Functions for plotting and getting Greek in labels

08.10.2012

The problem: We often want to plot data and assign plot attributes based on characteristics of the data. For example, if we have a group of students with the following IQs, we might want to indicate who is an outlier in the statistical sense. I like using the IQ measure as an example for this, even though I think the measure itself is imperfe...

3397 sym 4 img