# [R] Re:how to compute a "mode" of a distribution

John Wilkinson wilks at dial.pipex.com
Thu Aug 26 17:13:04 CEST 2004

```you are quite right  ,I forgot to insert \$x,

I should have written

median(density(my.vector)\$x)

that gives a median result but it is still  not the proper result.

density(my.vector)\$y)

gives the probabilites y of each x value
The correst answer would be the value of x corresponding to

y=max(density(my.vector)\$y)

see
summary(density(my.vector)

of course a

plot(density(my.vector))

would reveal the mode easily by inspection.

John

----- Original Message -----
From: "Kjetil Brinchmann Halvorsen" <kjetil at acelerate.com>
To: "John Wilkinson" <wilks at dial.pipex.com>
Cc: "Paolo Tommasini" <paolo at directwave.com.br>; <r-help at stat.math.ethz.ch>
Sent: Thursday, August 26, 2004 1:49 PM
Subject: Re: [R] Re:how to compute a "mode" of a distribution

> John Wilkinson wrote:
>
> >On 08/24/04 13:50, Paolo Tommasini wrote:
> >
> >
> >
> >>Hi my name is Paolo Tommasini does anyone know how to compute a "mode"
> >>( most frequent element ) for a distribution ?
> >>
> >>
> >
> >try
> >
> >median(density(my.vector))
> >
> >
> >
> That cannot be right. Try:
>
>  > test <- rnorm(1000)
>  > d <- density(test)
>  > median(d)
> Error in median(d) : need numeric data
>  > str(d)
> List of 7
>  \$ x        : num [1:512] -3.64 -3.63 -3.62 -3.60 -3.59 ...
>  \$ y        : num [1:512] 2.16e-05 2.60e-05 3.11e-05 3.71e-05 4.43e-05 ...
>  \$ bw       : num 0.221
>  \$ n        : int 1000
>  \$ call     : language density(x = test)
>  \$ data.name: chr "test"
>  \$ has.na   : logi FALSE
>  - attr(*, "class")= chr "density"
>  > i <- which.max(d\$y)
>  > d\$x[i]
> [1] 0.05625893
>
>
> Kjetil halvorsen
>
>
>
> >John
> >
> >John Wilknson
> >
> >[wilks at dial.pipex.com]
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
> >
> >
> >
> >
>
>
>

```

More information about the R-help mailing list