# How to calculate skewness of data from histogram in R?

In R, the function skewness from package moments allows one to calculate the skewness of the distribution from a given sample. Does anybody know if there is a ready-to-use function to calculate the skewness of the distribution from a given histogram?

-
Not sure I understand the question... If you have an histogram object, you should normally have access to the data from which this object was constructed. This is given by the $xname attribute of the histogram object. – dominic999 Jul 12 '11 at 0:40 @dominic Frequently--especially when reading papers--a histogram is just about the most detailed presentation of the data available. This situation is not new: older statistical texts spend a lot of time on making estimates from histograms and binned data. For instance, see the MathWorld article on Sheppard's Corrections and note the dates of the references. – whuber Jul 12 '11 at 0:59 @dominic999: Well, R is not the only way to generate a histogram. Mine exists outside of R and the only reason I needed R was to calculate the skewness. – Leo Jul 12 '11 at 2:35 Ok. No need for sarcasm. Was trying to help. – dominic999 Jul 12 '11 at 18:44 add comment ## 2 Answers Sounds like you're looking at a published histogram and don't have the actual data? If that is the case you could calculate a rough skewness figure by doing something like pub_his <- c(rep(10,16),rep(15,18),rep(20,27)...), picking the mid point of each bar and reading the frequency off the graph, then you'd have the data and could use the skewness function. - This would be approximate though, but probably just as useful as an accurate skewness value.... – nzcoops Jul 12 '11 at 1:02 @nz You're right. This method is great for estimating the third moment when the skewness is small. However, because it tends to overestimate the variance, it will typically underestimate the absolute value of the skewness. The correction depends on the size of the histogram bin width relative to the standard deviation (as well as on the shape of the distribution): it becomes substantial when the bin widths exceed 1 SD (which can happen for coarse histograms of highly skewed datasets). – whuber Jul 12 '11 at 1:13 @nzcoops: Yes, I can do it this way. So, no way to calculate$\sum (x_i - \bar{x})^2$and$\sum (x_i - \bar{x})^3$knowing bar positions and heights without "re-generating" the data from the histogram? – Leo Jul 12 '11 at 2:40 Correct, without the xis you can't do any calculations that include the xis. You could substitute your 'middle of the bin' position and get an approximate result, but personally I would never do that and I wouldn't recommend it to you either. As whuber notes, there is lots at play here, bin with etc. If this is a histgram in a journal article try contacting the authors, you may be pleasantly surprised. – nzcoops Jul 12 '11 at 2:55 add comment In the case of variance (and indeed for the fourth central moment), the "taking the bin center" approach carries a bias. Such bias can be corrected, via what are known as Sheppard's correction e.g. see here, which in the case of variance would subtract$\frac{1}{12}$of the square of the bin-width from the binned estimate of the second moment. However, in the case of the third moment, no such adjustment is needed. On the other hand, the third-moment-skewness, if just calculated directly by dividing the binned third-moment by the unadjusted binned-variance to the power$\frac{3}{2}\$ will yield too large a denominator, and so the skewness will be on average slightly underestimated.

This suggests that the binned variance be adjusted by Sheppard's correction before computing the skewness. So:

1) calculate the binned moments (first, second, third) as needed (i.e. by taking every observation at its bin center and if possible using the weighted/grouped-data formulas)

2) correct the variance for the grouping bias

3) compute the skewness by dividing the grouped third moment by the corrected grouped second moment.

However, you may want to ponder the circumstances under which the corrections apply. For example, you might see the discussion of the corrections in Kendall and Stuart, or maybe consult

M. G. Kendall (1938),
The Conditions under which Sheppard's Corrections are Valid
Journal of the Royal Statistical Society,
Vol. 101, No. 3, p592-605

-