Hello, all; I get an unexpected result when trying to plot a probability histogram with R1.9.1 on windows xp: #with the following code:> x <- runif(100,0,1) > hist(x) > hist(x, freq=F) > h <- hist(x, freq=F) > summary(h)# Length Class Mode #breaks 11 -none- numeric #counts 10 -none- numeric #intensities 10 -none- numeric #density 10 -none- numeric #mids 10 -none- numeric #xname 1 -none- character #equidist 1 -none- logical # The help file says that <h$density> holds the values # plotted in the probability histogram. If that's the # case, I'd expect that the sum of h$density for a histogram where freq=F would equal 1.0 ... However:> sum(h$density)#returns the value : #[1] 10 I would really like to plot values in the probablility histogram that sum to 1, not 10. Is there a switch in the hist(x) command that I'm missing? Or, is there another function that can do this? Thanks in advance, jlb -- ************************************ Joseph P. LeBouton Forest Ecology PhD Candidate Department of Forestry Michigan State University East Lansing, Michigan 48824 Office phone: 517-355-7744 email: lebouton at msu.edu
Alec Stephenson Department of Statistics Macquarie University NSW 2109, Australia>>> Joseph LeBouton <lebouton at msu.edu> 08/20/04 09:56am >>>Hello, all; I get an unexpected result when trying to plot a probability histogram with R1.9.1 on windows xp: #with the following code:> x <- runif(100,0,1) > hist(x) > hist(x, freq=F) > h <- hist(x, freq=F) > summary(h)# Length Class Mode #breaks 11 -none- numeric #counts 10 -none- numeric #intensities 10 -none- numeric #density 10 -none- numeric #mids 10 -none- numeric #xname 1 -none- character #equidist 1 -none- logical # The help file says that <h$density> holds the values # plotted in the probability histogram. If that's the # case, I'd expect that the sum of h$density for a histogram where freq=F would equal 1.0 ... However:> sum(h$density)#returns the value : #[1] 10 I would really like to plot values in the probablility histogram that sum to 1, not 10. Is there a switch in the hist(x) command that I'm missing? Or, is there another function that can do this? The width of each bar is 0.1, so you need sum(h$density) * 0.1 or in general sum(h$density * diff(h$breaks)) Thanks, Alec
Alec, Thanks for your reply. I guess what I'm getting at is that I to plot the histogram such that the HEIGHT of each bar represents the proportion of that class in the sample. From your reply I gather that the AREA of each bar is currently representing the proportion. My current work-around is to not plot the histogram immediately; I set it up (with plot=F), divide h$density by 10, then plot h; x <- runif(100,0,1) h <- hist(x, freq=F, plot=F) h$density <- h$density/10 plot(h, freq=F) while this is up to my normal hacking modus operandi, it's not terribly efficient. Is there another way to do that? Or is what I'm trying to do a perceptually and/or statistically incorrect way to think about histograms? Thanks again, jlb Alec Stephenson wrote:> > Alec Stephenson > Department of Statistics > Macquarie University > NSW 2109, Australia > > >>>>Joseph LeBouton <lebouton at msu.edu> 08/20/04 09:56am >>> > > Hello, all; > > I get an unexpected result when trying to plot a probability histogram > with R1.9.1 on windows xp: > > #with the following code: > > >>x <- runif(100,0,1) >>hist(x) >>hist(x, freq=F) >>h <- hist(x, freq=F) >>summary(h) > > > # Length Class Mode > #breaks 11 -none- numeric > #counts 10 -none- numeric > #intensities 10 -none- numeric > #density 10 -none- numeric > #mids 10 -none- numeric > #xname 1 -none- character > #equidist 1 -none- logical > > # The help file says that <h$density> holds the values > # plotted in the probability histogram. If that's the > # case, I'd expect that the sum of h$density for a histogram > where freq=F would equal 1.0 ... However: > > >>sum(h$density) > > > #returns the value : > #[1] 10 > > I would really like to plot values in the probablility histogram that > sum to 1, not 10. Is there a switch in the hist(x) command that I'm > missing? Or, is there another function that can do this? > > The width of each bar is 0.1, so you need > sum(h$density) * 0.1 > or in general > sum(h$density * diff(h$breaks)) > > Thanks, > Alec > > > >-- ************************************ Joseph P. LeBouton Forest Ecology PhD Candidate Department of Forestry Michigan State University East Lansing, Michigan 48824 Office phone: 517-355-7744 email: lebouton at msu.edu
Alec Stephenson Department of Statistics Macquarie University NSW 2109, Australia>>> Joseph LeBouton <lebouton at msu.edu> 08/20/04 10:28am >>>Alec, Thanks for your reply. I guess what I'm getting at is that I to plot the histogram such that the HEIGHT of each bar represents the proportion of that class in the sample. From your reply I gather that the AREA of each bar is currently representing the proportion. My current work-around is to not plot the histogram immediately; I set it up (with plot=F), divide h$density by 10, then plot h; x <- runif(100,0,1) h <- hist(x, freq=F, plot=F) h$density <- h$density/10 plot(h, freq=F) while this is up to my normal hacking modus operandi, it's not terribly efficient. Is there another way to do that? Or is what I'm trying to do a perceptually and/or statistically incorrect way to think about histograms? Quick responses to each sentence in this paragraph: 1) Seems perfectly fine to me. 2) Not that I know of. 3) A histogram can be thought of as a density estimate. If you change the scale on the x-axis, you are no longer plotting a histogram. Yours, Alec