[R] how to calculate the mode of a continuous variable

Manuel Ramon manugen at gmail.com
Fri Aug 29 13:59:53 CEST 2008


Thanks Peter, it's a good solution. 
Finding on RSiteSearch I found a similar solution and I wrote a function to
obtain the mode. That function is as follows.

  mode <- function(data) {
  # Function for mode estimation of a continuous variable
  # Kernel density estimation by Ted Harding & Douglas Bates (found on
RSiteSearch)	

    x<-data
    lim.inf=min(x)-1; lim.sup=max(x)+1

    hist(x,freq=FALSE,breaks=seq(lim.inf,lim.sup,0.2))
    s<-density(x,from=lim.inf,to=lim.sup,bw=0.2)
    n<-length(s$y)
    v1<-s$y[1:(n-2)];
    v2<-s$y[2:(n-1)];
    v3<-s$y[3:n]
    ix<-1+which((v1<v2)&(v2>v3))
    
    lines(s$x,s$y,col="red")
    points(s$x[ix],s$y[ix],col="blue")
    
    md <- s$x[which(s$y==max(s$y))] 

    md
  }

Thanks for your help,

Manuel Ramon


Peter Dalgaard wrote:
> 
> Henrique Dallazuanna wrote:
>> Try:
>>
>> as.numeric(names(which.max(table(x))))
>>
>> On Fri, Aug 29, 2008 at 3:13 AM, Manuel Ramon <manugen at gmail.com> wrote:
>>   
> 
> You missed the word "continuous" there...
>> x <- rnorm(10)
>> table(x)
> x
>  -1.64244637710945 -0.836534097622312 -0.810292826933485
> -0.721008996586432
>                  1                  1                  1                 
> 1
> -0.679702422788255 -0.667735659553467 -0.263432175981501
> 0.0795699932826675
>                  1                  1                  1                 
> 1
>  0.387151850978792  0.761964511475389
>                  1                  1
>> as.numeric(names(which.max(table(x))))
> [1] -1.642446
> 
> 
> Instead, how about
> 
> 
>> dd <- density(x)
>> which.max(dd$y)
> [1] 227
>> dd$x[which.max(dd$y)]
> [1] -0.6938049
>> plot(dd)
>> rug(x)
>> abline(v=dd$x[which.max(dd$y)])
> 
>>   
>>> Is there any R funtion that allow the estimation of mode in a continuous
>>> variable?
>>> Thank you
>>> --
>>> View this message in context:
>>> http://www.nabble.com/how-to-calculate-the-mode-of-a-continuous-variable-tp19214243p19214243.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>     
>>
>>
>>
>>   
>> ------------------------------------------------------------------------
>>
>> ______________________________________________
>> 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.
>>   
> 
> 
> -- 
>    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-calculate-the-mode-of-a-continuous-variable-tp19214243p19218548.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list