[R] How is the relation between Frequency and Counts in hist/density defined?

Bert Gunter gunter.berton at gene.com
Mon May 23 17:46:48 CEST 2011


Check the Help archives, google, search cran, as this has been
requested many times. IIRC, Greg Snow's gtools package has a function
that does this.

-- Bert

On Mon, May 23, 2011 at 8:39 AM, Tal Galili <tal.galili at gmail.com> wrote:
> Thanks Bery and Richard.
> So how would you suggest to create a histogram function with an added
> density plot on top of it?
> Here is what I've came up so far (ANY suggestion for improvement will be
> welcomed!):
>
> #-------------------------------------------------------------
> # function
> hist.plus.density <- function(X,..., density_col = "blue", density_lwd = 5,
> main)
> {
> if(missing(main))
> {
> xname <- paste(deparse(substitute(X), 500), collapse = "\n")
> main <- paste("Histogram of", xname)
> }
> a <- hist(X, main  = main , ...)
> y <- density(X)$y  * max(a$counts / a$density, na.rm = T)
> x <- density(X)$x
> lines(x = x, y = y, col = density_col, lwd = density_lwd)
> return(invisible(a))
> }
>
> # example of use:
>  set.seed(242)
>  z = rnorm(30)
>  hist.plus.density(z)
> #-------------------------------------------------------------
>
>
> Thanks,
> Tal
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
>
>
>
> On Mon, May 23, 2011 at 6:16 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>>
>> ... Which makes the cumulative (discrete) distribution 1, as it should be.
>>
>> -- Bert
>>
>> On Mon, May 23, 2011 at 7:59 AM, Tal Galili <tal.galili at gmail.com> wrote:
>> > Thanks Richard!
>> >
>> > ----------------Contact
>> > Details:-------------------------------------------------------
>> > Contact me: Tal.Galili at gmail.com |  972-52-7275845
>> > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> > www.r-statistics.com (English)
>> >
>> > ----------------------------------------------------------------------------------------------
>> >
>> >
>> >
>> >
>> > On Mon, May 23, 2011 at 5:56 PM, <Richard.Cotton at hsl.gov.uk> wrote:
>> >
>> >>
>> >> > In order to do this I can use the relation between count and density,
>> >> > but
>> >> I
>> >> > would like to know if there is a way for me to predict it upfront.
>> >>
>> >> In the code for hist.default, you'll see the line
>> >>
>> >> dens <- counts/(n * diff(breaks))
>> >>
>> >> > Here is an example:
>> >> >
>> >> > set.seed(242)
>> >> > z = rnorm(30)
>> >> > hist_z <- hist(z)
>> >> > hist_z$counts / hist_z$density # the relation is 15
>> >>
>> >> In your example, n is 30, and the breaks are evenly spaced every 0.5.
>> >>
>> >> Regards,
>> >> Richie.
>> >>
>> >> Mathematical Sciences Unit*
>> >> **HSL* <http://www.hsl.gov.uk/contact-us.htm>*
>> >> **4D Pie Charts* <http://4dpiecharts.com/>
>> >>
>> >> ------------------------------
>> >> *ATTENTION:*
>> >>
>> >> This message contains privileged and confidential information intended
>> >> for
>> >> the addressee(s) only. If this message was sent to you in error, you
>> >> must
>> >> not disseminate, copy or take any action in reliance on it and we
>> >> request
>> >> that you notify the sender immediately by return email.
>> >>
>> >> Opinions expressed in this message and any attachments are not
>> >> necessarily
>> >> those held by the *Health and Safety Laboratory
>> >> <http://www.hsl.gov.uk/>*or any person connected with the organisation, save
>> >> those by whom the
>> >> opinions were expressed.
>> >>
>> >> Please note that any messages sent or received by the *Health and
>> >> Safety
>> >> Laboratory <http://www.hsl.gov.uk/>* email system may be monitored and
>> >> stored in an information retrieval system.
>> >>
>> >> ------------------------------
>> >> Think before you print - do you really need to print this email?
>> >> ------------------------------
>> >>
>> >>   ------------------------------
>> >>
>> >> Scanned by *MailMarshal* - Marshal's comprehensive email content
>> >> security
>> >> solution. Download a free evaluation of MailMarshal at www.marshal.com
>> >>
>> >> **
>> >> ------------------------------
>> >>
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>
>>
>> --
>> "Men by nature long to get on to the ultimate truths, and will often
>> be impatient with elementary studies or fight shy of them. If it were
>> possible to reach the ultimate truths without the elementary studies
>> usually prefixed to them, these would not be preparatory studies but
>> superfluous diversions."
>>
>> -- Maimonides (1135-1204)
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list