Hi all, I think this question could be quite trivial, but I can?t find out the solution... How can you compute the statistic "mode" of a sample, in case it exists (as mode() returns the mode of an object)? I tried help.search("mode") but I couldn't find a clue... Any help would be much appreciated. Regards, Aurora
The problem is that 'the statistic "mode" of a sample' has no clear definition. If the distribution is highly discrete, then the following will do the job: > set.seed(1) > X <- rpois(11,1) > (nX <- table(X)) X 0 1 2 3 4 4 2 1 > names(nX)[nX==max(nX)] [1] "0" "1" However, if the data are continuous with no 2 numbers exactly equal, then the "mode" depends on the procedure, e.g., the specific selection of breakpoints for a histogram. If you insist on finding something, you can try "www.r-project.org" -> search -> "R site search" for something like ""nonparametric density estimation" and / or "kernel density estimator". hope this helps. spencer graves p.s. This has been discussed recently on this list, but I could not easily find it in the archives. Aurora Torrente wrote:> Hi all, > I think this question could be quite trivial, but I can?t find out the > solution... How can you compute the statistic "mode" of a sample, in > case it exists (as mode() returns the mode of an object)? I tried > help.search("mode") but I couldn't find a clue... > Any help would be much appreciated. Regards, > > Aurora > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Aurora Torrente wrote:> Hi all, > I think this question could be quite trivial, but I can?t find out the > solution... How can you compute the statistic "mode" of a sample, in > case it exists (as mode() returns the mode of an object)? I tried > help.search("mode") but I couldn't find a clue... > Any help would be much appreciated. Regards,There are several possibilities, e.g for *discrete* data: x <- floor(runif(100, min=10, max=20)) # some discrete data Half a year ago it was proposed to use e.g.: x[rev(order(table(x)))[1]] another possibility is: f <- table(x) as.numeric(names(f[max(f)==f])) # extracts mode(s) from vector names For *continuous data* you can use class frequencies (from hist) together with an interpolation formula. Another approximative solution uses kernel density estimates (the density function): x <- rnorm(100, mean=5, sd=1) # generate some data hist(x, prob=TRUE) dens <- density(x) lines(dens) dens$x[dens$y == max(dens$y)] # gives the mode The precision depends on the parameters of the density() function. If the distribution is multi-modal, I use a small function peaks() to extract several maxima (a generalized version of the one in R-news 2003/3 p. 9). Thomas P. -- Thomas Petzoldt Dresden University of Technology Institute of Hydrobiology petzoldt at rcs.urz.tu-dresden.de 01062 Dresden http://www.tu-dresden.de/fghhihb/
I remember Prof. Ripley suggesting the "taut springs" approach to estimating the modes, sometime ago in a posting to this group. I would be interested in knowing whether there is any R implementation of this approach (developed by Davies (1995)), for both non-parametric regression and density estimation. Ravi. ----- Original Message ----- From: Spencer Graves <spencer.graves at pdf.com> Date: Tuesday, February 24, 2004 7:12 am Subject: Re: [R] Computing the mode> The problem is that 'the statistic "mode" of a sample' has > no > clear definition. If the distribution is highly discrete, then > the > following will do the job: > > > set.seed(1) > > X <- rpois(11,1) > > (nX <- table(X)) > X > 0 1 2 3 > 4 4 2 1 > > names(nX)[nX==max(nX)] > [1] "0" "1" > > However, if the data are continuous with no 2 numbers > exactly > equal, then the "mode" depends on the procedure, e.g., the > specific > selection of breakpoints for a histogram. If you insist on > finding > something, you can try "www.r-project.org" -> search -> "R site > search" > for something like ""nonparametric density estimation" and / or > "kernel > density estimator". > > hope this helps. > spencer graves > p.s. This has been discussed recently on this list, but I > could > not easily find it in the archives. > > Aurora Torrente wrote: > > > Hi all, > > I think this question could be quite trivial, but I can?t find > out the > > solution... How can you compute the statistic "mode" of a > sample, in > > case it exists (as mode() returns the mode of an object)? I > tried > > help.search("mode") but I couldn't find a clue... > > Any help would be much appreciated. Regards, > > > > Aurora > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html
Forgive me for not following the "posting guidelines" and posting before doing my homework! I checked CRAN website and found that there is a package developed by Davies and Kovac, called "ftnonpar" that implements the "taut spring" approach that I mentioned in my previous posting. # For example: library(ftnonpar) plot(dclaw(seq(-3,3,len=1000)),type="l") xx <- rclaw(500) pmden(xx,verbose=T) Best, Ravi. ----- Original Message ----- From: Ravi Varadhan <rvaradha at jhsph.edu> Date: Tuesday, February 24, 2004 4:23 pm Subject: Re: [R] Computing the mode> I remember Prof. Ripley suggesting the "taut springs" approach to > estimating the modes, sometime ago in a posting to this group. I > would > be interested in knowing whether there is any R implementation of > this > approach (developed by Davies (1995)), for both non-parametric > regression and density estimation. > > Ravi. > > ----- Original Message ----- > From: Spencer Graves <spencer.graves at pdf.com> > Date: Tuesday, February 24, 2004 7:12 am > Subject: Re: [R] Computing the mode > > > The problem is that 'the statistic "mode" of a sample' has > > no > > clear definition. If the distribution is highly discrete, then > > the > > following will do the job: > > > > > set.seed(1) > > > X <- rpois(11,1) > > > (nX <- table(X)) > > X > > 0 1 2 3 > > 4 4 2 1 > > > names(nX)[nX==max(nX)] > > [1] "0" "1" > > > > However, if the data are continuous with no 2 numbers > > exactly > > equal, then the "mode" depends on the procedure, e.g., the > > specific > > selection of breakpoints for a histogram. If you insist on > > finding > > something, you can try "www.r-project.org" -> search -> "R site > > search" > > for something like ""nonparametric density estimation" and / or > > "kernel > > density estimator". > > > > hope this helps. > > spencer graves > > p.s. This has been discussed recently on this list, but I > > could > > not easily find it in the archives. > > > > Aurora Torrente wrote: > > > > > Hi all, > > > I think this question could be quite trivial, but I can?t find > > out the > > > solution... How can you compute the statistic "mode" of a > > sample, in > > > case it exists (as mode() returns the mode of an object)? I > > tried > > > help.search("mode") but I couldn't find a clue... > > > Any help would be much appreciated. Regards, > > > > > > Aurora > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide! > > > http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! http://www.R- > project.org/posting- > > guide.html >