# 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 ## 1 Answer 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