Re: [R] density() returns a density function that does not add up to 1

About this list Date view Thread view Subject view Author view Attachment view

From: A.J. Rossini (rossini@blindglobe.net)
Date: Sat 31 Aug 2002 - 06:05:08 EST


Message-id: <87r8ggi03v.fsf@jeeves.blindglobe.net>


>>>>> "jonathan" == Jonathan Li <jonqli@labs.agilent.com> writes:

    jonathan> Dear R users,
    jonathan> I ran into this curious problem:

>> d <- rnorm(100)
>> d.density <- density(d)
>> sum( d.density$x * d.density$y)
    jonathan> [1] 2.517502

I get 6.5

    jonathan> Admittedly the method of computing the mass under the density curve at
    jonathan> line 3 is crude.

It's actually wrong. You aren't computing an integral, you need the
the approximate area under d.density$y[i], NOT the value of the X
axis.

    jonathan> But 2.5 is pretty far from 1, the value it should be.

nope.

    jonathan> I tried a few other dataset and got similar result. Am I missing
    jonathan> something obvious?
    jonathan> Or is the return of density() not supposed to be normalized?

You just need to integrate properly. You are attempting Riemann
integration, but need to change things a bit.

best,
-tony

-- 
A.J. Rossini				Rsrch. Asst. Prof. of Biostatistics
U. of Washington Biostatistics		rossini@u.washington.edu	
FHCRC/SCHARP/HIV Vaccine Trials Net	rossini@scharp.org
-------------- http://software.biostat.washington.edu/ ----------------
FHCRC: M: 206-667-7025 (fax=4812)|Voicemail is pretty sketchy/use Email
UW:   Th: 206-543-1044 (fax=3286)|Change last 4 digits of phone to FAX
(my tuesday/wednesday/friday locations are completely unpredictable.)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jun 2004 - 09:31:11 EST