I've got some data that has this basic shape (using R):

df <- data.frame(group=sample(LETTERS, 500, T, log(2:27)), 
                 type=sample(c("x","y"), 500, T, c(.4,.6)),
                 value=sample(0:20, 500, T))

I want to investigate the ratios between x and y within each group.

One way would be to first compute the mean of x and y within each group, then use the compressed.ratio function I wrote (does it have a name? I just made it up) to map the ratio between the means from the interval [0,Inf] onto the interval [-1,1] so that it can be plotted symmetrically in x and y:

compressed.ratio <- function(x, y) (x-y)/(x+y)
df.means <- ddply(df, .(group,type), 
            function(df) data.frame(mean=mean(df$value),n=nrow(df)))
with(df.means, plot(unique(group), 
               compressed.ratio(mean[type=="x"], mean[type=="y"]), ylim=c(-1,1)))

In addition to this, I'd like to show something that gets at the amount of variation within each group, and also shows where there might be problems with very small numbers of samples in a given group.

But I haven't thought of a good way to do these - the obvious way to show uncertainty due to sample size would be to use standard-error bars, but I'm not sure how to compute the standard-error of a ratio between two groups of quantities. Would it be appropriate to compute the ratios of each x to mean(y), and then mean(x) to each y, and treat those as x+y separate measurements? Or maybe to do some kind of random simulation, doing draws from the x and y pools and taking their ratios?

Finally, does anyone know some kind of visual standard way to show both the standard-deviation and standard-error in the same graph? Maybe a thick error bar and thin whiskers?


Why not display the raw data?

df <- data.frame(group=sample(LETTERS, 500, T, log(2:27)), 
                 x=sample(0:20, 500, T), 
                 y=sample(0:20, 500, T))
df$ratio <- with(df, (x-y)/(x+y))

qplot(group, ratio, data = df) + 
  stat_summary(fun.y = mean, colour = "red", size = 2, geom = "point")

qplot(group, ratio, data = df) + 
  stat_summary(fun.data = "mean_cl_boot", colour = "red", geom = "crossbar")
  • $\begingroup$ Ack, sorry, I totally messed up the shape of my data when constructing the fake example (the real data is HIPAA-sensitive). My true observations aren't paired, there are a different number of x and y observations in each group. Maybe I should kill this question and start a new one. $\endgroup$ Jan 27 '11 at 15:11
  • $\begingroup$ I've gone back & revised the data example here to make it unpaired. $\endgroup$ Jan 27 '11 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.