# Visualizing variability from graph

It is written in the book Applied Logistic Regression, Second Edition. By David W. Hosmer and Stanley Lemeshow , p.2 that a problem with the above graph is that the variability in CHD at all ages is large.

But I cannot visualize the variability in CHD.

Did they mean the distance between the two straight line by variability? But 0 and 1 for CHD indicate absence and presence of CHD and these two levels are just nominally defined. If I want to represent it by 1 and 1.01, then the distance will not be so large.

From what angle should I visualize the variability in CHD?

The point is presumably elementary, namely that cases with CHD occur at almost any age, but this seems banal even to non-medical people. But statistically, variability means much more than "variation exists"; it is something to be quantified.

With no context and nothing else said, the main problem with the graph seems other than stated. It's that the reader has no way of telling whether each data point is one person or numerous people and no way of comparing frequencies, because people with the same age and status on CHD will inevitably be plotted at exactly at the same point. Overplotting is the main weakness of this graph.

It's reasonable enough to record age to the nearest year but a more informative graph would show fractions with CHD at each age and absolute numbers too. Quite how best to do that depends on the numbers involved. With modest frequencies dot or stripplots showing points stacked by age might be feasible. With much larger frequencies, paired histograms would be a one possibility.

Detail: Editing the OP's post reveals a caption that the graph represents 100 subjects. I've left my comments above as first written because I think them fair on what was visibly presented to everyone except those happening to edit the post.

P.S. It's a strong convention to represent presence-absence, yes or no, etc. binary states with 0 and 1. A major reason for that is that means of 0s and 1s then represent proportion present (yes, etc.). The OP suggests that there might be coding such as 1 and 1.01 to which the answer is Yes, in principle, but there would be no reason for such coding stronger than the advantages of 0,1. In any case, graphs like this should always be drawn with a scale suitable to distinguish different states. So use of binary coding is reasonable and does not itself make the graph problematic; indeed the next step is to show fractions (proportions, probabilities) which can be done consistently with that scale.

• I've edited my question. Perhaps the book first tries to make sense why if we have dichotomous outcome then the scatterplot of outcome and independent variable makes it difficult to describe functional relationship between them. But I've not understood how have they looked the variability in CHD? I can't compute the variability by both from graph and data. Because if i use the data to find the variance , it seems no logical as I can't find the variance of nominal data. – ABC Feb 21 '15 at 13:23
• But i agree if we have make a plot of age group and frequency, then it was easy to describe the relationship. But are how authors computing variability in CHD when the data are for individual? – ABC Feb 21 '15 at 13:26
• Thanks for supplying the reference, which turns out to be a well-known text with high reputation. You are asking precisely what the authors did and what they mean; without reference to the book it's impossible to comment. It's in order here to ask for elucidation of difficult details in texts, but full context is desirable to allow good answers. – Nick Cox Feb 21 '15 at 13:39
• That apart, any idea that you can't calculate variance for "nominal" data has zero force here. Binary (dichotomous) data coded 0, 1 have variance according to the usual rules, as is shown by any discussion of Bernoulli distributions. This is utterly standard and (be reassured) not at all problematic. – Nick Cox Feb 21 '15 at 13:41
• (+1) Also note that sunflower plots can be highly effective in these situations. R, for ex., has a sunflowerplot() function, using the libraries stats and grDevices. – rolando2 Feb 21 '15 at 17:58

By "variability," the authors mean any reasonable measure of the dispersion of CHD conditional on age.

Study this by slicing the data into narrow age groups (as shown by different colors in the right hand scatterplot), computing the dispersion of the CHD values within each age group, and plotting those dispersions against age (shown in the left hand dot chart).

Because CHD is binary and encoded with zeros and ones, it is a Bernoulli variable. The CHD values within any age group $i$ are completely summarized by their count $n_i$ and the count of (say) the ones, $k_i$, which thereby has a Binomial distribution with (unknown) probability $p_i = \Pr(1)$. Although there are many ways to estimate $p_i$, we needn't be fussy; the proportion $\hat p_i = k_i / n_i$ will do nicely. An appropriate measure of the dispersion of CHD then is the estimated standard deviation $\sqrt{\hat p_i(1-\hat p_i)}$. It can vary from $0$ (when $\hat p_i$ is close to $0$ or $1$) up to $1/2$ (attained when $\hat p_i = 1/2$). The full range of possible standard deviations is shown on the horizontal axis in the left plot. Clearly all of them are in the high (rightmost) range, explaining and justifying the assessment that "the variability at all ages is large."

Hosmer and Lemeshow go on to analyze these data into eight age groups rather than the eleven shown here. The conclusion of consistently large variability begins to break down with more age groups: we can see in the right hand plot that all CHD values are constant at the very lowest and very highest ages, exhibiting no variability at all. That is to be expected: when we use many groups, some groups will have few values, resulting in high uncertainty concerning the true dispersion within each group. The authors, by limiting the number of groups, maintain fairly high counts $n_i$ within each group, thereby achieving a robust picture of the dispersion of CHD conditional on age.

A more sophisticated, but slightly more opaque, method to estimate the conditional dispersion is to smooth CHD against Age using a local estimator of the mean. This smooth can be converted to an estimator of the dispersion using the same formula as before:

I have highlighted (in red and by thickening the line) the "large" standard deviations--that is, those greater than the middle value of $1/4$.

These data are available in the file chdage.dat found at ftp://ftp.wiley.com/public/sci_tech_med/logistic/alr.zip. The R code used to create these plots is reproduced below for those who would like to experiment with them.

#
# Applied Logistic Regression, Table 1.1
#
folder <- "F:/Research/ALR/logistic/" # Location of the data file on your system
x <- read.table(paste0(folder, "chdage.dat"), col.names=c("Id", "Age", "CHD"))
#
# Specify age grouping.
#
n.groups <- 11
k <- 5           # Should be relatively prime to n.groups and near n.groups/2
colors <- rainbow(n.groups)
colors <- colors[(1:n.groups * k) %% n.groups + 1]
#
# Study dispersion ("variability") of CHD by age.
#
breaks <- quantile(x$Age, (0:n.groups)/n.groups) #$
x$AgeGroup <- cut(x$Age, breaks)
s <- aggregate(x$CHD, by=list(x$AgeGroup), function(y) sqrt(mean(y)*(1-mean(y))))
dotchart(s$x, s$Group.1, xlim=c(0, 0.525), pch=16, col=colors, cex=min(1, 10/n.groups),
xlab="Standard Deviation",
main="Variation in CHD by Age Group", cex.main=0.8)
#
# Plot the raw data.
#
names(colors) <- levels(x$AgeGroup) plot(jitter(x$Age), x$CHD, yaxp=c(0, 1, 1), ylim=c(0,1)+c(-1,1)*0.05, cex=1.25, col=colors[x$AgeGroup],
xlab="Age (years, jittered)", ylab="CHD",
main="CHD vs. Age", cex.main=0.8)
abline(v = breaks, lty=1, col="Gray")
#
# Plot the smoothed dispersion.
#
CHD.smooth <- lowess(x$Age, x$CHD)
CHD.smooth$y <- pmin(1, pmax(0, CHD.smooth$y))
CHD.sd <- sqrt(CHD.smooth$y * (1-CHD.smooth$y))
large <- CHD.sd > 1/4
plot(CHD.smooth$x, CHD.sd, type="l", lwd=2, col="Gray", xlab="Age", ylab="Standard Deviation", main="Smoothed Dispersion of CHD", cex.main=0.75, cex.lab=0.75, cex.axis=0.75) #$
lines(CHD.smooth\$x[large], CHD.sd[large], lwd=3, col="Red")

• +1 That's a cool graphing technique, but those color choices are abominable! :) Wouldn't a grayscale with max variability shaded darkest, or a saturation scale with max variability most saturated more effectively communicate the variability on the colored CHD vs. Age graph? As it is I have to keep looking back and forth, because the color choices merely serve as labels, rather than convey ordering information about the variability. Could even do a dual color graph (e.g. red/blue) with saturation to convey both variability and directionality. – Alexis Feb 21 '15 at 20:15
• @Alexis I agree with your criticism about the color choices. However, the purpose is only to illustrate the technique. Among the default palettes offered by R, this is by far the best of the bunch! The colors were permuted on purpose because they are not intended to signify ordering (it is well known that color is not very useful for that), but only to discriminate one age group from the next, which--perhaps due to their garishness--they do just fine. – whuber Feb 21 '15 at 20:19

I believe the point they are trying to make is that, for any given age bin (say 40-45) there are about equal numbers of CHD=0 and CHD=1. This indicates that CHD is not very predictable from age, so it has a high variability. If you wanted to quantify this, you could use something like binary entropy - the closer that p(CHD=0) is to 1/2, the more entropy/variability there is for that age bin.

• But you can't infer equal numbers from the graph at all! The graph remains ambiguous because of the possibility of overplotting, as discussed in my answer. Naturally it is remotely possible that there was really was at most one patient of each age and status, but that's an implausible set-up. – Nick Cox Feb 21 '15 at 11:24
• I agree that the plot is silly for many reasons (overplotting, plotting a discrete variable on a continuous axis, etc). I was trying to interpret the authors' statement that "variability in CHD at all ages is large" and suggest a more reasonable way to measure and plot this variability, assuming that we had the raw data. – Chris Feb 22 '15 at 16:19
• Plotting discrete variables on a continuous axis is fine by me. I do it just about every day. (I agreed with your main point, although don't see any need to resort to entropy here.) – Nick Cox Feb 22 '15 at 16:27