modeest/0000755000176200001440000000000013564534375011731 5ustar liggesusersmodeest/NAMESPACE0000644000176200001440000000410013564525601013133 0ustar liggesusers# Generated by roxygen2: do not edit by hand S3method(mlv,character) S3method(mlv,default) S3method(mlv,factor) S3method(mlv,integer) S3method(mlv,lientz) S3method(mlv,logical) S3method(plot,lientz) S3method(print,lientz) export(asselin) export(bernMode) export(betaMode) export(binomMode) export(cauchyMode) export(chisqMode) export(dagumMode) export(distrMode) export(expMode) export(fMode) export(fiskMode) export(frechetMode) export(gammaMode) export(geomMode) export(gevMode) export(ghMode) export(ghtMode) export(gldMode) export(gompertzMode) export(gpdMode) export(grenander) export(gumbelMode) export(hrm) export(hsm) export(hypMode) export(hyperMode) export(koenkerMode) export(kumarMode) export(laplaceMode) export(lientz) export(lnormMode) export(logisMode) export(lomaxMode) export(maxwellMode) export(meanshift) export(mfv) export(mfv1) export(mlv) export(mlv1) export(mvnormMode) export(naive) export(nakaMode) export(nbinomMode) export(nigMode) export(normMode) export(paralogisticMode) export(paretoMode) export(parzen) export(poisMode) export(rayleighMode) export(shorth) export(skewness) export(stableMode) export(stableMode2) export(tMode) export(tsybakov) export(unifMode) export(venter) export(vieu) export(weibullMode) export(yulesMode) importFrom(fBasics,ghMode) importFrom(fBasics,ghtMode) importFrom(fBasics,gldMode) importFrom(fBasics,hypMode) importFrom(fBasics,nigMode) importFrom(graphics,legend) importFrom(graphics,locator) importFrom(graphics,plot) importFrom(graphics,points) importFrom(stable,stable.mode) importFrom(stabledist,dstable) importFrom(statip,.kernelsList) importFrom(statip,bandwidth) importFrom(statip,kernelfun) importFrom(statip,mfv) importFrom(statip,mfv1) importFrom(statip,name2distr) importFrom(stats,bw.SJ) importFrom(stats,bw.bcv) importFrom(stats,bw.nrd) importFrom(stats,bw.nrd0) importFrom(stats,bw.ucv) importFrom(stats,median) importFrom(stats,optim) importFrom(stats,optimize) importFrom(stats,quantile) importFrom(stats,sd) importFrom(stats,uniroot) modeest/README.md0000644000176200001440000000106513564525534013207 0ustar liggesusers# modeest: Estimation of the mode The package 'modeest' provides estimators of the mode of univariate unimodal (and sometimes multimodal) data and values of the modes of usual probability distributions. ## Installation You can install the *release* version of `modeest` from the [CRAN](https://CRAN.R-project.org/package=modeest) with: ```R install.packages("modeest") ``` You can install the *development* version of `modeest` from GitHub with: ```R # install.packages("devtools") devtools::install_github("paulponcet/modeest") ``` modeest/man/0000755000176200001440000000000013442241424012465 5ustar liggesusersmodeest/man/lientz.Rd0000644000176200001440000000676213564343270014303 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/lientz.R \name{lientz} \alias{lientz} \alias{Lientz} \alias{plot.lientz} \alias{print.lientz} \alias{mlv.lientz} \title{The empirical Lientz function and the Lientz mode estimator} \usage{ lientz(x, bw = NULL) \method{plot}{lientz}(x, zoom = FALSE, ...) \method{print}{lientz}(x, digits = NULL, ...) \method{mlv}{lientz}(x, bw = NULL, abc = FALSE, par = shorth(x), optim.method = "BFGS", ...) } \arguments{ \item{x}{numeric (vector of observations) or an object of class \code{"lientz"}.} \item{bw}{numeric. The smoothing bandwidth to be used. Should belong to (0, 1). Parameter 'beta' in Lientz (1970) function.} \item{zoom}{logical. If \code{TRUE}, one can zoom on the graph created.} \item{...}{if \code{abc = FALSE}, further arguments to be passed to \code{\link[stats]{optim}}, or further arguments to be passed to \code{\link[graphics]{plot}}.} \item{digits}{numeric. Number of digits to be printed.} \item{abc}{logical. If \code{FALSE} (the default), the Lientz empirical function is minimised using \code{\link[stats]{optim}}.} \item{par}{numeric. The initial value used in \code{\link[stats]{optim}}.} \item{optim.method}{character. If \code{abc = FALSE}, the method used in \code{\link[stats]{optim}}.} } \value{ \code{lientz} returns an object of class \code{c("lientz", "function")}; this is a function with additional attributes: \itemize{ \item{x}{ the \code{x} argument} \item{bw}{ the \code{bw} argument } \item{call}{ the call which produced the result } } \code{mlv.lientz} returns a numeric value, the mode estimate. If \code{abc = TRUE}, the \code{x} value minimizing the Lientz empirical function is returned. Otherwise, the \code{\link[stats]{optim}} method is used to perform minimization, and the attributes: 'value', 'counts', 'convergence' and 'message', coming from the \code{\link[stats]{optim}} method, are added to the result. } \description{ The Lientz mode estimator is nothing but the value minimizing the empirical Lientz function. A 'plot' and a 'print' methods are provided. } \details{ The Lientz function is the smallest non-negative quantity \eqn{S(x,\beta)}{S(x,b)}, where \eqn{\beta}{b} = \code{bw}, such that \deqn{F(x+S(x,\beta)) - F(x-S(x,\beta)) \geq \beta.}{F(x+S(x,b)) - F(x-S(x,b)) >= b.} Lientz (1970) provided a way to estimate \eqn{S(x,\beta)}{S(x,b)}; this estimate is what we call the empirical Lientz function. } \note{ The user may call \code{mlv.lientz} through \code{mlv(x, method = "lientz", ...)}. } \examples{ # Unimodal distribution x <- rbeta(1000,23,4) ## True mode betaMode(23, 4) ## Lientz object f <- lientz(x, 0.2) print(f) plot(f) ## Estimate of the mode mlv(f) # optim(shorth(x), fn = f) mlv(f, abc = TRUE) # x[which.min(f(x))] mlv(x, method = "lientz", bw = 0.2) # Bimodal distribution x <- c(rnorm(1000,5,1), rnorm(1500, 22, 3)) f <- lientz(x, 0.1) plot(f) } \references{ \itemize{ \item Lientz B.P. (1969). On estimating points of local maxima and minima of density functions. \emph{Nonparametric Techniques in Statistical Inference (ed. M.L. Puri, Cambridge University Press}, p.275-282. \item Lientz B.P. (1970). Results on nonparametric modal intervals. \emph{SIAM J. Appl. Math.}, \bold{19}:356-366. \item Lientz B.P. (1972). Properties of modal intervals. \emph{SIAM J. Appl. Math.}, \bold{23}:1-5. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation; \code{\link[modeest]{shorth}} for the shorth estimate of the mode } modeest/man/hsm.Rd0000644000176200001440000000450013564343270013551 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/hsm.R \name{hsm} \alias{hsm} \alias{HSM} \title{Half sample mode estimator} \usage{ hsm(x, bw = NULL, k, tie.action = "mean", tie.limit = 0.05, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric or function. The bandwidth to be used. Should belong to (0, 1].} \item{k}{numeric. See 'Details'.} \item{tie.action}{character. The action to take if a tie is encountered.} \item{tie.limit}{numeric. A limit deciding whether or not a warning is given when a tie is encountered.} \item{...}{Additional arguments.} } \value{ A numeric value is returned, the mode estimate. } \description{ This function computes the Robertson-Cryer mode estimator described in Robertson and Cryer (1974), also called half sample mode (if \code{bw = 1/2}) or fraction sample mode (for some other \code{bw}) by Bickel (2006). } \details{ The modal interval, i.e. the shortest interval among intervals containing \code{k+1} observations, is computed iteratively, until only one value is found, the mode estimate. At each step \eqn{i}{i}, one takes \code{k = ceiling(bw*n) - 1}, where \code{n} is the length of the modal interval computed at step \eqn{i-}{i-}\code{1}. If \code{bw} is of class \code{"function"}, then \code{k = ceiling(bw(n)) - 1} instead. } \note{ The user may call \code{hsm} through \code{mlv(x, method = "hsm", ...)}. } \examples{ # Unimodal distribution x <- rweibull(10000, shape = 3, scale = 0.9) ## True mode weibullMode(shape = 3, scale = 0.9) ## Estimate of the mode bandwidth <- function(n, alpha) {1/n^alpha} hsm(x, bw = bandwidth, alpha = 2) mlv(x, method = "hsm", bw = bandwidth, alpha = 2) } \references{ \itemize{ \item Robertson T. and Cryer J.D. (1974). An iterative procedure for estimating the mode. \emph{J. Amer. Statist. Assoc.}, \bold{69}(348):1012-1016. \item Bickel D.R. and Fruehwirth R. (2006). On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation; \code{\link[modeest]{venter}} for the Venter mode estimate. } \author{ D.R. Bickel for the original code, P. Poncet for the slight modifications introduced. } modeest/man/mlv.Rd0000644000176200001440000001146413564344605013572 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/mlv.R \name{mlv} \alias{mlv} \alias{mlv.character} \alias{mlv.factor} \alias{mlv.logical} \alias{mlv.integer} \alias{mlv.default} \alias{mlv1} \title{Estimation of the Mode(s) or Most Likely Value(s)} \usage{ mlv(x, ...) \method{mlv}{character}(x, na.rm = FALSE, ...) \method{mlv}{factor}(x, na.rm = FALSE, ...) \method{mlv}{logical}(x, na.rm = FALSE, ...) \method{mlv}{integer}(x, na.rm = FALSE, ...) \method{mlv}{default}(x, bw = NULL, method, na.rm = FALSE, ...) mlv1(x, ...) } \arguments{ \item{x}{numeric (vector of observations), or an object of class \code{"factor"}, \code{"integer"}, etc.} \item{...}{Further arguments to be passed to the function called for computation.} \item{na.rm}{logical. Should missing values be removed?} \item{bw}{numeric. The bandwidth to be used. This may have different meanings regarding the \code{method} used.} \item{method}{character. One of the methods available for computing the mode estimate. See 'Details'.} } \value{ A vector of the same type as \code{x}. Be aware that the length of this vector can be \code{> 1}. } \description{ \code{mlv} is a generic function for estimating the mode of a univariate distribution. Different estimates (or methods) are provided: \itemize{ \item \code{\link{mfv}}, which returns the most frequent value(s) in a given numerical vector, \item the \code{\link{Lientz}} mode estimator, which is the value minimizing the Lientz function estimate, \item the Chernoff mode estimator, also called \code{\link{naive}} mode estimator, which is defined as the center of the interval of given length containing the most observations, \item the \code{\link{Venter}} mode estimator, including the \code{\link{shorth}}, i.e. the midpoint of the modal interval, \item the \code{\link{Grenander}} mode estimator, \item the half sample mode (\code{\link{HSM}}) and the half range mode (\code{\link{HRM}}), which are iterative versions of the Venter mode estimator, \item \code{\link{Parzen}}'s kernel mode estimator, which is the value maximizing the kernel density estimate, \item the \code{\link{Tsybakov}} mode estimator, based on a gradient-like recursive algorithm, \item the \code{\link{Asselin}} de Beauville mode estimator, based on a algorithm detecting chains and holes in the sample, \item the \code{\link{Vieu}} mode estimator, \item the \code{\link{meanshift}} mode estimator. } \code{mlv} can also be used to compute the mode of a given distribution, with \code{mlv.character}. } \details{ For the default method of \code{mlv}, available methods are \code{"lientz"}, \code{"naive"}, \code{"venter"}, \code{"grenander"}, \code{"hsm"}, \code{"parzen"}, \code{"tsybakov"}, \code{"asselin"}, and \code{"meanshift"}. See the description above and the associated links. If \code{x} is of class \code{"character"} (with length > 1), \code{"factor"}, or \code{"integer"}, then the most frequent value found in \code{x} is returned using \code{\link[statip]{mfv}} from package \pkg{statip}. If \code{x} is of class \code{"character"} (with length 1), \code{x} should be one of \code{"beta"}, \code{"cauchy"}, \code{"gev"}, etc. i.e. a character for which a function \code{*Mode} exists (for instance \code{betaMode}, \code{cauchyMode}, etc.). See \code{\link[modeest]{distrMode}} for the available functions. The mode of the corresponding distribution is returned. If \code{x} is of class \code{mlv.lientz}, see \code{\link[modeest]{Lientz}} for more details. } \examples{ # Unimodal distribution x <- rbeta(1000,23,4) ## True mode betaMode(23, 4) # or mlv("beta", shape1 = 23, shape2 = 4) ## Be aware of this behaviour: mlv("norm") # returns 0, the mode of the standard normal distribution mlv("normal") # returns 0 again, since "normal" is matched with "norm" mlv("abnormal") # returns "abnormal", since the input vector "abrnormal" # is not recognized as a distribution name, hence is taken as a character # vector from which the most frequent value is requested. ## Estimate of the mode mlv(x, method = "lientz", bw = 0.2) mlv(x, method = "naive", bw = 1/3) mlv(x, method = "venter", type = "shorth") mlv(x, method = "grenander", p = 4) mlv(x, method = "hsm") mlv(x, method = "parzen", kernel = "gaussian") mlv(x, method = "tsybakov", kernel = "gaussian") mlv(x, method = "asselin", bw = 2/3) mlv(x, method = "vieu") mlv(x, method = "meanshift") } \references{ See the references on mode estimation on the \code{\link[modeest]{modeest-package}}'s page. } \seealso{ \code{\link[statip]{mfv}}, \code{\link[modeest]{parzen}}, \code{\link[modeest]{venter}}, \code{\link[modeest]{meanshift}}, \code{\link[modeest]{grenander}}, \code{\link[modeest]{hsm}}, \code{\link[modeest]{lientz}}, \code{\link[modeest]{naive}}, \code{\link[modeest]{tsybakov}}, \code{\link[modeest]{skewness}} } modeest/man/modeest-package.Rd0000644000176200001440000002154313330156527016017 0ustar liggesusers\name{modeest} \alias{modeest-package} \alias{modeest} \docType{package} \title{ Mode Estimation } \description{ This package provides estimators of the mode of univariate unimodal (and sometimes multimodal) data, and values of the modes of usual probability distributions. %It also includes computation of the density function, distribution function, and quantile %function of the Chernoff distribution, which is the limiting distribution of the %Chernoff mode estimator. For a complete list of functions, use \code{library(help = "modeest")} or \code{help.start()}. } \references{ \itemize{ \item Parzen E. (1962). On estimation of a probability density function and mode. \emph{Ann. Math. Stat.}, \bold{33}(3):1065-1076. \item Chernoff H. (1964). Estimation of the mode. \emph{Ann. Inst. Statist. Math.}, \bold{16}:31-41. \item Huber P.J. (1964). Robust estimation of a location parameter. \emph{Ann. Math. Statist.}, \bold{35}:73-101. \item Dalenius T. (1965). The Mode - A Negleted Statistical Parameter. \emph{J. Royal Statist. Soc. A}, \bold{128}:110-117. \item Grenander U. (1965). Some direct estimates of the mode. \emph{Ann. Math. Statist.}, \bold{36}:131-138. \item Venter J.H. (1967). On estimation of the mode. \emph{Ann. Math. Statist.}, \bold{38}(5):1446-1455. \item Lientz B.P. (1969). On estimating points of local maxima and minima of density functions. \emph{Nonparametric Techniques in Statistical Inference (ed. M.L. Puri, Cambridge University Press)}, p.275-282. \item Lientz B.P. (1970). Results on nonparametric modal intervals. \emph{SIAM J. Appl. Math.}, \bold{19}:356-366. \item Wegman E.J. (1971).% a revoir !! A note on the estimation of the mode. \emph{Ann. Math. Statist.}, \bold{42}(6):1909-1915. \item Yamato H. (1971). % a revoir !! Sequential estimation of a continuous probability density function and mode. \emph{Bull. Math. Statist.}, \bold{14}:1-12. \item Ekblom H. (1972). A Monte Carlo investigation of mode estimators in small samples. \emph{Applied Statistics}, \bold{21}:177-184. \item Lientz B.P. (1972). Properties of modal intervals. \emph{SIAM J. Appl. Math.}, \bold{23}:1-5. \item Konakov V.D. (1973). On the asymptotic normality of the mode of multidimensional distributions. \emph{Theory Probab. Appl.}, \bold{18}:794-803. \item Robertson T. and Cryer J.D. (1974). An iterative procedure for estimating the mode. \emph{J. Amer. Statist. Assoc.}, \bold{69}(348):1012-1016. \item Kim B.K. and Van Ryzin J. (1975). Uniform consistency of a histogram density estimator and modal estimation. \emph{Commun. Statist.}, \bold{4}:303-315. \item Sager T.W. (1975).% a revoir !! Consistency in nonparametric estimation of the mode. \emph{Ann. Statist.}, \bold{3}(3):698-706. \item Stone C.J. (1975). Adaptive maximum likelihood estimators of a location parameter. \emph{Ann. Statist.}, \bold{3}:267-284. \item Mizoguchi R. and Shimura M. (1976). Nonparametric Learning Without a Teacher Based on Mode Estimation. \emph{IEEE Transactions on Computers}, \bold{C25}(11):1109-1117. \item Adriano K.N., Gentle J.E. and Sposito V.A. (1977). On the asymptotic bias of Grenander's mode estimator. \emph{Commun. Statist.-Theor. Meth. A}, \bold{6}:773-776. \item Asselin de Beauville J.-P. (1978). Estimation non parametrique de la densite et du mode, exemple de la distribution Gamma. \emph{Revue de Statistique Appliquee}, \bold{26}(3):47-70. \item Sager T.W. (1978).% a revoir !! Estimation of a multivariate mode. \emph{Ann. Statist.}, \bold{6}:802-812. \item Devroye L. (1979).% a revoir !! Recursive estimation of the mode of a multivariate density. \emph{Canadian J. Statist.}, \bold{7}(2):159-167. \item Sager T.W. (1979).% a revoir !! An iterative procedure for estimating a multivariate mode and isopleth. \emph{J. Amer. Statist. Assoc.}, \bold{74}(366):329-339. \item Eddy W.F. (1980). Optimum kernel estimators of the mode. \emph{Ann. Statist.}, \bold{8}(4):870-882. \item Eddy W.F. (1982). The Asymptotic Distributions of Kernel Estimators of the Mode. \emph{Z. Wahrsch. Verw. Gebiete}, \bold{59}:279-290. \item Hall P. (1982). Asymptotic Theory of Grenander's Mode Estimator. \emph{Z. Wahrsch. Verw. Gebiete}, \bold{60}:315-334. \item Sager T.W. (1983). Estimating modes and isopleths. \emph{Commun. Statist.-Theor. Meth.}, \bold{12}(5):529-557. \item Hartigan J.A. and Hartigan P.M. (1985). The Dip Test of Unimodality. \emph{Ann. Statist.}, \bold{13}:70-84. \item Hartigan P.M. (1985). Computation of the Dip Statistic to Test for Unimodality. \emph{Appl. Statist. (JRSS C)}, \bold{34}:320-325. \item Romano J.P. (1988). On weak convergence and optimality of kernel density estimates of the mode. \emph{Ann. Statist.}, \bold{16}(2):629-647. \item Tsybakov A. (1990). Recursive estimation of the mode of a multivariate distribution. \emph{Probl. Inf. Transm.}, \bold{26}:31-37. \item Hyndman R.J. (1996). Computing and graphing highest density regions. \emph{Amer. Statist.}, \bold{50}(2):120-126. \item Vieu P. (1996). A note on density mode estimation. \emph{Statistics \& Probability Letters}, \bold{26}:297--307. \item Leclerc J. (1997). Comportement limite fort de deux estimateurs du mode : le shorth et l'estimateur naif. \emph{C. R. Acad. Sci. Paris, Serie I}, \bold{325}(11):1207-1210. %\item Minnotte M. C. (1997). %Nonparametric testing of the existence of modes. %\emph{Ann. Statist.}, \bold{25}(4):1646-1660. % %\item Futschik A. (1999). %A new estimate of the mode based on the quantile density. %\emph{Statistics and Probability Letters}, \bold{43}:145-152. % \item Leclerc J. (2000). Strong limiting behavior of two estimates of the mode: the shorth and the naive estimator. \emph{Statistics and Decisions}, \bold{18}(4). %\item Groeneboom P.and Wellner J.A. (2001). %Computing Chernoff's distribution. %\emph{J. Comput. Graph. Statist.}, \bold{10}:388-400. % \item Shoung J.M. and Zhang C.H. (2001). Least squares estimators of the mode of a unimodal regression function. \emph{Ann. Statist.}, \bold{29}(3):648-665. \item Bickel D.R. (2002). Robust estimators of the mode and skewness of continuous data. \emph{Computational Statistics and Data Analysis}, \bold{39}:153-163. \item Abraham C., Biau G. and Cadre B. (2003). Simple Estimation of the Mode of a Multivariate Density. \emph{Canad. J. Statist.}, \bold{31}(1):23-34. \item Bickel D.R. (2003). Robust and efficient estimation of the mode of continuous data: The mode as a viable measure of central tendency. \emph{J. Statist. Comput. Simul.}, \bold{73}:899-912. \item Djeddour K., Mokkadem A. et Pelletier M. (2003). Sur l'estimation recursive du mode et de la valeur modale d'une densite de probabilite. \emph{Technical report 105}. \item Djeddour K., Mokkadem A. et Pelletier M. (2003). Application du principe de moyennisation a l'estimation recursive du mode et de la valeur modale d'une densite de probabilite. \emph{Technical report 106}. \item Hedges S.B. and Shah P. (2003). Comparison of mode estimation methods and application in molecular clock analysis. \emph{BMC Bioinformatics}, \bold{4}:31-41. %\item Ziegler K. (2003). %On the asymptotic normality of kernel regression estimators of the mode in the nonparametric random design model. %\emph{Journal of Statistical Planning and Inference}, \bold{115}:123-144. % \item Herrmann E. and Ziegler K. (2004). Rates of consistency for nonparametric estimation of the mode in absence of smoothness assumptions. \emph{Statistics and Probability Letters}, \bold{68}:359-368. \item Abraham C., Biau G. and Cadre B. (2004). On the Asymptotic Properties of a Simple Estimate of the Mode. \emph{ESAIM Probab. Stat.}, \bold{8}:1-11. \item Mokkadem A. and Pelletier M. (2005). Adaptive Estimation of the Mode of a Multivariate Density. \emph{J. Nonparametr. Statist.}, \bold{17}(1):83-105. \item Bickel D.R. and Fruehwirth R. (2006). On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. } } \keyword{ distribution } \keyword{ univar } \keyword{ robust } \keyword{ nonparametric } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation. } modeest/man/hrm.Rd0000644000176200001440000000457013564344636013566 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/hrm.R \name{hrm} \alias{hrm} \alias{HRM} \title{Bickel's half-range mode estimator} \usage{ hrm(x, bw = NULL, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The bandwidth to be used. Should belong to (0, 1]. This gives the fraction of the observations to consider at each step of the iterative algorithm.} \item{...}{Additional arguments.} } \value{ A numeric value is returned, the mode estimate. } \description{ SINCE THIS FUNCTION USED TO DEPEND ON THE BIOCONDUCTOR PACKAGE 'GENEFILTER', IT IS CURRENTLY DEFUNCT. This function computes Bickel's half range mode estimator described in Bickel (2002). It is a wrapper around the function \code{half.range.mode} from package \pkg{genefilter}. } \details{ The mode estimator is computed by iteratively identifying densest half ranges. A densest half range is an interval whose width equals half the current range, and which contains the maximal number of observations. The subset of observations falling in the selected densest half range is then used to compute a new range, and the procedure is iterated. } \note{ The user may call \code{hrm} through \code{mlv(x, method = "hrm", bw, ...)}. } \examples{ \dontrun{ # Unimodal distribution x <- rgamma(1000, shape = 31.9) ## True mode gammaMode(shape = 31.9) ## Estimate of the mode hrm(x, bw = 0.4) mlv(x, method = "hrm", bw = 0.4) } } \references{ \itemize{ \item Bickel D.R. (2002). Robust estimators of the mode and skewness of continuous data. \emph{Computational Statistics and Data Analysis}, \bold{39}:153-163. \item Hedges S.B. and Shah P. (2003). Comparison of mode estimation methods and application in molecular clock analysis. \emph{BMC Bioinformatics}, \bold{4}:31-41. \item Bickel D.R. and Fruehwirth R. (2006). On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. } } \seealso{ \code{\link[modeest]{mlv}()} for general mode estimation; \code{\link[modeest]{hsm}()} for the half sample mode; \code{\link[modeest]{venter}()} for the Venter mode estimate. } \author{ The C and R code are due to Richard Bourgon \email{bourgon@stat.berkeley.edu}, see package \pkg{genefilter}. The algorithm is described in Bickel (2002). } modeest/man/skewness.Rd0000644000176200001440000000414613564343270014632 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/skewness.R \name{skewness} \alias{skewness} \title{Skewness} \usage{ skewness(x, na.rm = FALSE, method = c("moment", "fisher", "bickel"), M, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{na.rm}{logical. Should missing values be removed?} \item{method}{character. Specifies the method of computation. These are either \code{"moment"}, \code{"fisher"} or \code{"bickel"}. The \code{"moment"} method is based on the definition of skewness for distributions; this form should be used when resampling (bootstrap or jackknife). The \code{"fisher"} method corresponds to the usual "unbiased" definition of sample variance, although in the case of skewness exact unbiasedness is not possible.} \item{M}{numeric. (An estimate of) the mode of the observations \code{x}. Default value is \code{\link[modeest]{shorth}(x)}.} \item{...}{Additional arguments.} } \value{ \code{skewness} returns a numeric value. An attribute reports the method used. } \description{ This function encodes different methods to calculate the skewness from a vector of observations. } \examples{ ## Skewness = 0 x <- rnorm(1000) skewness(x, method = "bickel", M = shorth(x)) ## Skewness > 0 (left skewed case) x <- rbeta(1000, 2, 5) skewness(x, method = "bickel", M = betaMode(2, 5)) ## Skewness < 0 (right skewed case) x <- rbeta(1000, 7, 2) skewness(x, method = "bickel", M = hsm(x, bw = 1/3)) } \references{ \itemize{ \item Bickel D.R. (2002). Robust estimators of the mode and skewness of continuous data. \emph{Computational Statistics and Data Analysis}, \bold{39}:153-163. \item Bickel D.R. et Fruehwirth R. (2006). On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation; \code{\link[modeest]{shorth}} for the shorth estimate of the mode } \author{ Diethelm Wuertz and contributors for the original \code{skewness} function from package \pkg{fBasics}. } modeest/man/grenander.Rd0000644000176200001440000000532113564343270014731 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/grenander.R \name{grenander} \alias{grenander} \alias{Grenander} \title{The Grenander mode estimator} \usage{ grenander(x, bw = NULL, k, p, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The bandwidth to be used. Should belong to (0, 1].} \item{k}{numeric. Paramater 'k' in Grenander's mode estimate, see below.} \item{p}{numeric. Paramater 'p' in Grenander's mode estimate, see below. If \code{p = Inf}, the function \code{\link[modeest]{venter}} is used.} \item{...}{Additional arguments to be passed to \code{\link[modeest]{venter}}.} } \value{ A numeric value is returned, the mode estimate. If \code{p = Inf}, the \code{\link[modeest]{venter}} mode estimator is returned. } \description{ This function computes the Grenander mode estimator. } \details{ The Grenander estimate is defined by \deqn{ \frac{ \sum_{j=1}^{n-k} \frac{(x_{j+k} + x_{j})}{2(x_{j+k} - x_{j})^p} } { \sum_{j=1}^{n-k} \frac{1}{(x_{j+k} - x_{j})^p} } }{ ( sum_{j=1}^{n-k} (x_{j+k} + x_{j})/(2(x_{j+k} - x_{j})^p) ) / ( sum_{j=1}^{n-k} 1/((x_{j+k} - x_{j})^p) ) } If \eqn{p}{p} tends to infinity, this estimate tends to the Venter mode estimate; this justifies to call \code{\link[modeest]{venter}} if \code{p = Inf}. The user should either give the bandwidth \code{bw} or the argument \code{k}, \code{k} being taken equal to \code{ceiling(bw*n) - 1} if missing. } \note{ The user may call \code{grenander} through \code{mlv(x, method = "grenander", bw, k, p, ...)}. } \examples{ # Unimodal distribution x <- rnorm(1000, mean = 23, sd = 0.5) ## True mode normMode(mean = 23, sd = 0.5) # (!) ## Parameter 'k' k <- 5 ## Many values of parameter 'p' ps <- seq(0.1, 4, 0.01) ## Estimate of the mode with these parameters M <- sapply(ps, function(p) grenander(x, p = p, k = k)) ## Distribution obtained plot(density(M), xlim = c(22.5, 23.5)) } \references{ \itemize{ \item Grenander U. (1965). Some direct estimates of the mode. \emph{Ann. Math. Statist.}, \bold{36}:131-138. \item Dalenius T. (1965). The Mode - A Negleted Statistical Parameter. \emph{J. Royal Statist. Soc. A}, \emph{128}:110-117. \item Adriano K.N., Gentle J.E. and Sposito V.A. (1977). On the asymptotic bias of Grenander's mode estimator. \emph{Commun. Statist.-Theor. Meth. A}, \bold{6}:773-776. \item Hall P. (1982). Asymptotic Theory of Grenander's Mode Estimator. \emph{Z. Wahrsch. Verw. Gebiete}, \bold{60}:315-334. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation; \code{\link[modeest]{venter}} for the Venter mode estimate. } \author{ D.R. Bickel for the original code, P. Poncet for the slight modifications introduced. } modeest/man/asselin.Rd0000644000176200001440000000227513564343270014427 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/asselin.R \name{asselin} \alias{asselin} \alias{Asselin} \title{The Asselin de Beauville mode estimator} \usage{ asselin(x, bw = NULL, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. A number in \code{(0, 1]}. If \code{bw = 1}, the selected 'modal chain' may be too long.} \item{...}{further arguments to be passed to the \code{\link[stats]{quantile}} function.} } \value{ A numeric value is returned, the mode estimate. } \description{ This mode estimator is based on the algorithm described in Asselin de Beauville (1978). } \note{ The user may call \code{asselin} through \code{mlv(x, method = "asselin", ...)}. } \examples{ x <- rbeta(1000, shape1 = 2, shape2 = 5) ## True mode: betaMode(shape1 = 2, shape2 = 5) ## Estimation: asselin(x, bw = 1) asselin(x, bw = 1/2) mlv(x, method = "asselin") } \references{ \itemize{ \item Asselin de Beauville J.-P. (1978). Estimation non parametrique de la densite et du mode, exemple de la distribution Gamma. \emph{Revue de Statistique Appliquee}, \bold{26}(3):47-70. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation. } modeest/man/tsybakov.Rd0000644000176200001440000000572513564343270014636 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/tsybakov.R \name{tsybakov} \alias{tsybakov} \alias{Tsybakov} \title{The Tsybakov mode estimator} \usage{ tsybakov( x, bw = NULL, a, alpha = 0.9, kernel = "triangular", dmp = TRUE, par = shorth(x) ) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. Vector of length \code{length(x)} giving the sequence of smoothing bandwidths to be used.} \item{a}{numeric. Vector of length \code{length(x)} used in the gradient algorithm} \item{alpha}{numeric. An alternative way of specifying \code{a}. See 'Details'.} \item{kernel}{character. The kernel to be used. Available kernels are \code{"biweight"}, \code{"cosine"}, \code{"eddy"}, \code{"epanechnikov"}, \code{"gaussian"}, \code{"optcosine"}, \code{"rectangular"}, \code{"triangular"}, \code{"uniform"}. See \code{\link[stats]{density}} for more details on some of these kernels.} \item{dmp}{logical. If \code{TRUE}, Djeddour et al. version of the estimate is used.} \item{par}{numeric. Initial value in the gradient algorithm. Default value is \code{\link[modeest]{shorth}(x)}.} } \value{ A numeric value is returned, the mode estimate. } \description{ This mode estimator is based on a gradient-like recursive algorithm, more adapted for online estimation. It includes the Mizoguchi-Shimura (1976) mode estimator, based on the window training procedure. } \details{ If \code{bw} or \code{a} is missing, a default value advised by Djeddour et al (2003) is used: \code{bw = (1:length(x))^(-1/7)} and \code{a = (1:length(x))^(-alpha)}. (with \code{alpha = 0.9} if \code{alpha} is missing). } \note{ The user may call \code{tsybakov} through \code{mlv(x, method = "tsybakov", ...)}. } \section{Warning}{ The Tsybakov mode estimate as it is presently computed does not work very well. The reasons of this inefficiency should be further investigated. } \examples{ x <- rbeta(1000, shape1 = 2, shape2 = 5) ## True mode: betaMode(shape1 = 2, shape2 = 5) ## Estimation: tsybakov(x, kernel = "triangular") tsybakov(x, kernel = "gaussian", alpha = 0.99) mlv(x, method = "tsybakov", kernel = "gaussian", alpha = 0.99) } \references{ \itemize{ \item Mizoguchi R. and Shimura M. (1976). Nonparametric Learning Without a Teacher Based on Mode Estimation. \emph{IEEE Transactions on Computers}, \bold{C25}(11):1109-1117. \item Tsybakov A. (1990). Recursive estimation of the mode of a multivariate distribution. \emph{Probl. Inf. Transm.}, \bold{26}:31-37. \item Djeddour K., Mokkadem A. et Pelletier M. (2003). Sur l'estimation recursive du mode et de la valeur modale d'une densite de probabilite. \emph{Technical report 105}. \item Djeddour K., Mokkadem A. et Pelletier M. (2003). Application du principe de moyennisation a l'estimation recursive du mode et de la valeur modale d'une densite de probabilite. \emph{Technical report 106}. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation. } modeest/man/reexports.Rd0000644000176200001440000000066013564525601015020 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/reexports.R \docType{import} \name{reexports} \alias{reexports} \alias{mfv} \alias{mfv1} \title{Objects exported from other packages} \keyword{internal} \description{ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ \item{statip}{\code{\link[statip]{mfv}}, \code{\link[statip]{mfv1}}} }} modeest/man/naive.Rd0000644000176200001440000000301013564343270014057 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/naive.R \name{naive} \alias{naive} \alias{Chernoff} \alias{chernoff} \title{The Chernoff or 'naive' mode estimator} \usage{ naive(x, bw = 1/2) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The smoothing bandwidth to be used. Should belong to (0, 1). See below.} } \value{ A numeric vector is returned, the mode estimate, which is the center of the interval of length \code{2*bw} containing the most observations. } \description{ This estimator, also called the *naive* mode estimator, is defined as the center of the interval of given length containing the most observations. It is identical to Parzen's kernel mode estimator, when the kernel is chosen to be the uniform kernel. } \note{ The user may call \code{naive} through \code{mlv(x, method = "naive", bw)}. } \examples{ # Unimodal distribution x <- rf(10000, df1 = 40, df2 = 30) ## True mode fMode(df1 = 40, df2 = 30) ## Estimate of the mode mean(naive(x, bw = 1/4)) mlv(x, method = "naive", bw = 1/4) } \references{ \itemize{ \item Chernoff H. (1964). Estimation of the mode. \emph{Ann. Inst. Statist. Math.}, \bold{16}:31-41. \item Leclerc J. (1997). Comportement limite fort de deux estimateurs du mode : le shorth et l'estimateur naif. \emph{C. R. Acad. Sci. Paris, Serie I}, \bold{325}(11):1207-1210. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation; \code{\link[modeest]{parzen}} for Parzen's kernel mode estimation. } modeest/man/meanshift.Rd0000644000176200001440000000344213564343270014744 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/meanshift.R \name{meanshift} \alias{meanshift} \title{The Meanshift mode estimator} \usage{ meanshift( x, bw = NULL, kernel = "gaussian", par = shorth(x), iter = 1000, tolerance = sqrt(.Machine$double.eps) ) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The smoothing bandwidth to be used.} \item{kernel}{character. The kernel to be used. Available kernels are \code{"biweight"}, \code{"cosine"}, \code{"eddy"}, \code{"epanechnikov"}, \code{"gaussian"}, \code{"optcosine"}, \code{"rectangular"}, \code{"triangular"}, \code{"uniform"}. See \code{\link[stats]{density}} for more details on some of these kernels.} \item{par}{numeric. The initial value used in the meanshift algorithm.} \item{iter}{numeric. Maximal number of iterations.} \item{tolerance}{numeric. Stopping criteria.} } \value{ \code{meanshift} returns a numeric value, the mode estimate, with an attribute \code{"iterations"}. The number of iterations can be less than \code{iter} if the stopping criteria specified by \code{eps} is reached. } \description{ The Meanshift mode estimator. } \note{ The user should preferentially call \code{meanshift} through \code{mlv(x, method = "meanshift", ...)}. } \examples{ # Unimodal distribution x <- rweibull(100, shape = 12, scale = 0.8) ## True mode weibullMode(shape = 12, scale = 0.8) ## Estimate of the mode mlv(x, method = "meanshift", par = mean(x)) } \references{ \itemize{ \item Fukunaga, K. and Hostetler, L. (1975). The estimation of the gradient of a density function, with applications in pattern recognition. \emph{IEEE Transactions on Information Theory}, \bold{21}(1):32--40. } } \seealso{ \code{\link[modeest]{mlv}}, \code{\link[modeest]{tsybakov}}. } modeest/man/vieu.Rd0000644000176200001440000000341013564343270013731 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/vieu.R \name{vieu} \alias{vieu} \alias{Vieu} \title{Vieu's mode estimator} \usage{ vieu(x, bw = NULL, kernel = "gaussian", abc = FALSE, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The smoothing bandwidth to be used.} \item{kernel}{character. The kernel to be used. Available kernels are \code{"biweight"}, \code{"cosine"}, \code{"eddy"}, \code{"epanechnikov"}, \code{"gaussian"}, \code{"optcosine"}, \code{"rectangular"}, \code{"triangular"}, \code{"uniform"}. See \code{\link[stats]{density}} for more details on some of these kernels.} \item{abc}{logical. If \code{FALSE} (the default), the root of the density derivate estimate is searched with \code{\link[stats]{uniroot}}.} \item{...}{If \code{abc = FALSE}, further arguments to be passed to \code{\link[stats]{uniroot}}.} } \value{ \code{vieu} returns a numeric value, the mode estimate. If \code{abc = TRUE}, the \code{x} value at which the density derivative estimate is null is returned. Otherwise, the \code{\link[stats]{uniroot}} method is used. } \description{ Vieu's mode estimator is the value at which the kernel density derivative estimate is null. } \note{ The user may call \code{vieu} through \code{mlv(x, method = "vieu", ...)}. Presently, \code{vieu} is quite slow. } \examples{ # Unimodal distribution x <- rlnorm(10000, meanlog = 3.4, sdlog = 0.2) ## True mode lnormMode(meanlog = 3.4, sdlog = 0.2) ## Estimate of the mode mlv(x, method = "vieu", kernel = "gaussian") } \references{ \itemize{ \item Vieu P. (1996). A note on density mode estimation. \emph{Statistics \& Probability Letters}, \bold{26}:297--307. } } \seealso{ \code{\link[modeest]{mlv}}, \code{\link[modeest]{parzen}}. } modeest/man/parzen.Rd0000644000176200001440000000562313564343270014270 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/parzen.R \name{parzen} \alias{parzen} \alias{Parzen} \title{Parzen's Kernel mode estimator} \usage{ parzen( x, bw = NULL, kernel = "gaussian", abc = FALSE, tolerance = .Machine$double.eps^0.25, ... ) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The smoothing bandwidth to be used.} \item{kernel}{character. The kernel to be used. For available kernels see \code{\link[statip]{densityfun}} in package \pkg{statip}.} \item{abc}{logical. If \code{FALSE} (the default), the kernel density estimate is maximised using \code{\link[stats]{optim}}.} \item{tolerance}{numeric. Desired accuracy in the \code{\link[stats]{optimize}} function.} \item{...}{If \code{abc = FALSE}, further arguments to be passed to \code{\link[stats]{optim}}.} } \value{ \code{parzen} returns a numeric value, the mode estimate. If \code{abc = TRUE}, the \code{x} value maximizing the density estimate is returned. Otherwise, the \code{\link[stats]{optim}} method is used to perform maximization, and the attributes: 'value', 'counts', 'convergence' and 'message', coming from the \code{\link[stats]{optim}} method, are added to the result. } \description{ Parzen's kernel mode estimator is the value maximizing the kernel density estimate. } \details{ If \code{kernel = "uniform"}, the \code{\link[modeest]{naive}} mode estimate is returned. } \note{ The user may call \code{parzen} through \code{mlv(x, method = "kernel", ...)} or \code{mlv(x, method = "parzen", ...)}. Presently, \code{parzen} is quite slow. } \examples{ # Unimodal distribution x <- rlnorm(10000, meanlog = 3.4, sdlog = 0.2) ## True mode lnormMode(meanlog = 3.4, sdlog = 0.2) ## Estimate of the mode mlv(x, method = "kernel", kernel = "gaussian", bw = 0.3, par = shorth(x)) } \references{ \itemize{ \item Parzen E. (1962). On estimation of a probability density function and mode. \emph{Ann. Math. Stat.}, \bold{33}(3):1065--1076. \item Konakov V.D. (1973). On the asymptotic normality of the mode of multidimensional distributions. \emph{Theory Probab. Appl.}, \bold{18}:794-803. \item Eddy W.F. (1980). Optimum kernel estimators of the mode. \emph{Ann. Statist.}, \bold{8}(4):870-882. \item Eddy W.F. (1982). The Asymptotic Distributions of Kernel Estimators of the Mode. \emph{Z. Wahrsch. Verw. Gebiete}, \bold{59}:279-290. \item Romano J.P. (1988). On weak convergence and optimality of kernel density estimates of the mode. \emph{Ann. Statist.}, \bold{16}(2):629-647. \item Abraham C., Biau G. and Cadre B. (2003). Simple Estimation of the Mode of a Multivariate Density. \emph{Canad. J. Statist.}, \bold{31}(1):23-34. \item Abraham C., Biau G. and Cadre B. (2004). On the Asymptotic Properties of a Simple Estimate of the Mode. \emph{ESAIM Probab. Stat.}, \bold{8}:1-11. } } \seealso{ \code{\link[modeest]{mlv}}, \code{\link[modeest]{naive}} } modeest/man/distrMode.Rd0000644000176200001440000003000213564343270014710 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/distrMode.R \name{distrMode} \alias{distrMode} \alias{betaMode} \alias{cauchyMode} \alias{chisqMode} \alias{dagumMode} \alias{expMode} \alias{fMode} \alias{fiskMode} \alias{frechetMode} \alias{gammaMode} \alias{normMode} \alias{gevMode} \alias{ghMode} \alias{ghtMode} \alias{gldMode} \alias{gompertzMode} \alias{gpdMode} \alias{gumbelMode} \alias{hypMode} \alias{koenkerMode} \alias{kumarMode} \alias{laplaceMode} \alias{logisMode} \alias{lnormMode} \alias{lomaxMode} \alias{maxwellMode} \alias{mvnormMode} \alias{nakaMode} \alias{nigMode} \alias{paralogisticMode} \alias{paretoMode} \alias{rayleighMode} \alias{stableMode} \alias{stableMode2} \alias{tMode} \alias{unifMode} \alias{weibullMode} \alias{yulesMode} \alias{bernMode} \alias{binomMode} \alias{geomMode} \alias{hyperMode} \alias{nbinomMode} \alias{poisMode} \title{Mode of some continuous and discrete distributions} \usage{ distrMode(x, ...) betaMode(shape1, shape2, ncp = 0) cauchyMode(location = 0, ...) chisqMode(df, ncp = 0) dagumMode(scale = 1, shape1.a, shape2.p) expMode(...) fMode(df1, df2) fiskMode(scale = 1, shape1.a) frechetMode(location = 0, scale = 1, shape = 1, ...) gammaMode(shape, rate = 1, scale = 1/rate) normMode(mean = 0, ...) gevMode(location = 0, scale = 1, shape = 0, ...) ghMode(alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2) ghtMode(beta = 0.1, delta = 1, mu = 0, nu = 10) gldMode(lambda1 = 0, lambda2 = -1, lambda3 = -1/8, lambda4 = -1/8) gompertzMode(scale = 1, shape) gpdMode(location = 0, scale = 1, shape = 0) gumbelMode(location = 0, ...) hypMode(alpha = 1, beta = 0, delta = 1, mu = 0, pm = c(1, 2, 3, 4)) koenkerMode(location = 0, ...) kumarMode(shape1, shape2) laplaceMode(location = 0, ...) logisMode(location = 0, ...) lnormMode(meanlog = 0, sdlog = 1) lomaxMode(...) maxwellMode(rate) mvnormMode(mean, ...) nakaMode(scale = 1, shape) nigMode(alpha = 1, beta = 0, delta = 1, mu = 0) paralogisticMode(scale = 1, shape1.a) paretoMode(scale = 1, ...) rayleighMode(scale = 1) stableMode(alpha, beta, gamma = 1, delta = 0, pm = 0, ...) stableMode2(loc, disp, skew, tail) tMode(df, ncp) unifMode(min = 0, max = 1) weibullMode(shape, scale = 1) yulesMode(...) bernMode(prob) binomMode(size, prob) geomMode(...) hyperMode(m, n, k, ...) nbinomMode(size, prob, mu) poisMode(lambda) } \arguments{ \item{x}{character. The name of the distribution to consider.} \item{...}{Additional parameters.} \item{shape1}{non-negative parameters of the Beta distribution.} \item{shape2}{non-negative parameters of the Beta distribution.} \item{ncp}{non-centrality parameter.} \item{location}{location and scale parameters.} \item{df}{degrees of freedom (non-negative, but can be non-integer).} \item{scale}{location and scale parameters.} \item{shape1.a}{shape parameters.} \item{shape2.p}{shape parameters.} \item{df1}{degrees of freedom. \code{Inf} is allowed.} \item{df2}{degrees of freedom. \code{Inf} is allowed.} \item{shape}{the location parameter \eqn{a}, scale parameter \eqn{b}, and shape parameter \eqn{s}.} \item{rate}{vector of rates.} \item{mean}{vector of means.} \item{alpha}{ shape parameter \code{alpha}; skewness parameter \code{beta}, \code{abs(beta)} is in the range (0, alpha); scale parameter \code{delta}, \code{delta} must be zero or positive; location parameter \code{mu}, by default 0. These is the meaning of the parameters in the first parameterization \code{pm=1} which is the default parameterization selection. In the second parameterization, \code{pm=2} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{zeta} and \code{rho}. In the third parameterization, \code{pm=3} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{xi} and \code{chi}. In the fourth parameterization, \code{pm=4} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{a.bar} and \code{b.bar}. } \item{beta}{ shape parameter \code{alpha}; skewness parameter \code{beta}, \code{abs(beta)} is in the range (0, alpha); scale parameter \code{delta}, \code{delta} must be zero or positive; location parameter \code{mu}, by default 0. These is the meaning of the parameters in the first parameterization \code{pm=1} which is the default parameterization selection. In the second parameterization, \code{pm=2} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{zeta} and \code{rho}. In the third parameterization, \code{pm=3} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{xi} and \code{chi}. In the fourth parameterization, \code{pm=4} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{a.bar} and \code{b.bar}. } \item{delta}{ shape parameter \code{alpha}; skewness parameter \code{beta}, \code{abs(beta)} is in the range (0, alpha); scale parameter \code{delta}, \code{delta} must be zero or positive; location parameter \code{mu}, by default 0. These is the meaning of the parameters in the first parameterization \code{pm=1} which is the default parameterization selection. In the second parameterization, \code{pm=2} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{zeta} and \code{rho}. In the third parameterization, \code{pm=3} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{xi} and \code{chi}. In the fourth parameterization, \code{pm=4} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{a.bar} and \code{b.bar}. } \item{mu}{ shape parameter \code{alpha}; skewness parameter \code{beta}, \code{abs(beta)} is in the range (0, alpha); scale parameter \code{delta}, \code{delta} must be zero or positive; location parameter \code{mu}, by default 0. These is the meaning of the parameters in the first parameterization \code{pm=1} which is the default parameterization selection. In the second parameterization, \code{pm=2} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{zeta} and \code{rho}. In the third parameterization, \code{pm=3} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{xi} and \code{chi}. In the fourth parameterization, \code{pm=4} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{a.bar} and \code{b.bar}. } \item{lambda}{ shape parameter \code{alpha}; skewness parameter \code{beta}, \code{abs(beta)} is in the range (0, alpha); scale parameter \code{delta}, \code{delta} must be zero or positive; location parameter \code{mu}, by default 0. These is the meaning of the parameters in the first parameterization \code{pm=1} which is the default parameterization selection. In the second parameterization, \code{pm=2} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{zeta} and \code{rho}. In the third parameterization, \code{pm=3} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{xi} and \code{chi}. In the fourth parameterization, \code{pm=4} \code{alpha} and \code{beta} take the meaning of the shape parameters (usually named) \code{a.bar} and \code{b.bar}. } \item{nu}{ a numeric value, the number of degrees of freedom. Note, \code{alpha} takes the limit of \code{abs(beta)}, and \code{lambda=-nu/2}. } \item{lambda1}{ are numeric values where \code{lambda1} is the location parameter, \code{lambda2} is the location parameter, \code{lambda3} is the first shape parameter, and \code{lambda4} is the second shape parameter. } \item{lambda2}{ are numeric values where \code{lambda1} is the location parameter, \code{lambda2} is the location parameter, \code{lambda3} is the first shape parameter, and \code{lambda4} is the second shape parameter. } \item{lambda3}{ are numeric values where \code{lambda1} is the location parameter, \code{lambda2} is the location parameter, \code{lambda3} is the first shape parameter, and \code{lambda4} is the second shape parameter. } \item{lambda4}{ are numeric values where \code{lambda1} is the location parameter, \code{lambda2} is the location parameter, \code{lambda3} is the first shape parameter, and \code{lambda4} is the second shape parameter. } \item{pm}{ an integer value between \code{1} and \code{4} for the selection of the parameterization. The default takes the first parameterization. } \item{meanlog}{mean and standard deviation of the distribution on the log scale with default values of \code{0} and \code{1} respectively.} \item{sdlog}{mean and standard deviation of the distribution on the log scale with default values of \code{0} and \code{1} respectively.} \item{gamma}{ value of the index parameter \code{alpha} in the interval= \eqn{(0, 2]}; skewness parameter \code{beta}, in the range \eqn{[-1, 1]}; scale parameter \code{gamma}; and location (or \sQuote{shift}) parameter \code{delta}. } \item{loc}{vector of (real) location parameters.} \item{disp}{vector of (positive) dispersion parameters.} \item{skew}{vector of skewness parameters (in [-1,1]).} \item{tail}{vector of parameters (in [1,2]) related to the tail thickness.} \item{min}{lower and upper limits of the distribution. Must be finite.} \item{max}{lower and upper limits of the distribution. Must be finite.} \item{prob}{Probability of success on each trial.} \item{size}{number of trials (zero or more).} \item{m}{the number of white balls in the urn.} \item{n}{number of observations. If \code{length(n) > 1}, the length is taken to be the number required.} \item{k}{the number of balls drawn from the urn.} } \value{ A numeric value is returned, the (true) mode of the distribution. } \description{ These functions return the mode of the main probability distributions implemented in R. } \note{ Some functions like \code{normMode} or \code{cauchyMode}, which relate to symmetric distributions, are trivial, but are implemented for the sake of exhaustivity. } \examples{ ## Beta distribution curve(dbeta(x, shape1 = 2, shape2 = 3.1), xlim = c(0,1), ylab = "Beta density") M <- betaMode(shape1 = 2, shape2 = 3.1) abline(v = M, col = 2) mlv("beta", shape1 = 2, shape2 = 3.1) ## Lognormal distribution curve(stats::dlnorm(x, meanlog = 3, sdlog = 1.1), xlim = c(0, 10), ylab = "Lognormal density") M <- lnormMode(meanlog = 3, sdlog = 1.1) abline(v = M, col = 2) mlv("lnorm", meanlog = 3, sdlog = 1.1) curve(VGAM::dpareto(x, scale = 1, shape = 1), xlim = c(0, 10)) abline(v = paretoMode(scale = 1), col = 2) ## Poisson distribution poisMode(lambda = 6) poisMode(lambda = 6.1) mlv("poisson", lambda = 6.1) } \seealso{ \code{\link[modeest]{mlv}} for the estimation of the mode; the documentation of the related distributions \code{\link[stats]{Beta}}, \code{\link[stats]{GammaDist}}, etc. } \author{ \code{\link[fBasics]{ghMode}} and \code{\link[fBasics]{ghtMode}} are from package \pkg{fBasics}; \code{\link[fBasics]{hypMode}} was written by David Scott; \code{\link[fBasics]{gldMode}}, \code{\link[fBasics]{nigMode}} and \code{\link[stabledist]{stableMode}} were written by Diethelm Wuertz. } modeest/man/venter.Rd0000644000176200001440000000626613564343270014300 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/venter.R \name{venter} \alias{venter} \alias{Venter} \alias{shorth} \title{The Venter / Dalenius / LMS mode estimator} \usage{ venter( x, bw = NULL, k, iter = 1, type = 1, tie.action = "mean", tie.limit = 0.05, warn = FALSE ) shorth(x, ...) } \arguments{ \item{x}{numeric. Vector of observations.} \item{bw}{numeric. The bandwidth to be used. Should belong to (0, 1]. See 'Details'.} \item{k}{numeric. See 'Details'.} \item{iter}{numeric. Number of iterations.} \item{type}{numeric or character. The type of Venter estimate to be computed. See 'Details'.} \item{tie.action}{character. The action to take if a tie is encountered.} \item{tie.limit}{numeric. A limit deciding whether or not a warning is given when a tie is encountered.} \item{warn}{logical. If \code{TRUE}, a warning is thrown when a tie is encountered.} \item{...}{Further arguments.} } \value{ A numeric value is returned, the mode estimate. } \description{ This function computes the Venter mode estimator, also called the Dalenius, or LMS (Least Median Square) mode estimator. } \details{ The modal interval, i.e. the shortest interval among intervals containing \code{k+1} observations, is first computed. (In dimension > 1, this question is known as a 'k-enclosing problem'.) The user should either give the bandwidth \code{bw} or the argument \code{k}, \code{k} being taken equal to \code{ceiling(bw*n) - 1} if missing, so \code{bw} can be seen as the fraction of the observations to be considered for the shortest interval. If \code{type = 1}, the midpoint of the modal interval is returned. If \code{type = 2}, the \code{floor((k+1)/2)}th element of the modal interval is returned. If \code{type = 3} or \code{type = "dalenius"}, the median of the modal interval is returned. If \code{type = 4} or \code{type = "shorth"}, the mean of the modal interval is returned. If \code{type = 5} or \code{type = "ekblom"}, Ekblom's \eqn{L_{-\infty}}{L_{-infinity}} estimate is returned, see Ekblom (1972). If \code{type = 6} or \code{type = "hsm"}, the half sample mode (hsm) is computed, see \code{\link{hsm}}. } \note{ The user may call \code{venter} through \code{mlv(x, method = "venter", ...)}. } \examples{ library(evd) # Unimodal distribution x <- rgev(1000, loc = 23, scale = 1.5, shape = 0) ## True mode gevMode(loc = 23, scale = 1.5, shape = 0) ## Estimate of the mode venter(x, bw = 1/3) mlv(x, method = "venter", bw = 1/3) } \references{ \itemize{ \item Dalenius T. (1965). The Mode - A Negleted Statistical Parameter. \emph{J. Royal Statist. Soc. A}, \emph{128}:110-117. \item Venter J.H. (1967). On estimation of the mode. \emph{Ann. Math. Statist.}, \bold{38}(5):1446-1455. \item Ekblom H. (1972). A Monte Carlo investigation of mode estimators in small samples. \emph{Applied Statistics}, \bold{21}:177-184. \item Leclerc J. (1997). Comportement limite fort de deux estimateurs du mode : le shorth et l'estimateur naif. \emph{C. R. Acad. Sci. Paris, Serie I}, \bold{325}(11):1207-1210. } } \seealso{ \code{\link[modeest]{mlv}} for general mode estimation, \code{\link[modeest]{hsm}} for the half sample mode. } modeest/DESCRIPTION0000644000176200001440000000133113564534375013435 0ustar liggesusersPackage: modeest Type: Package Title: Mode Estimation Version: 2.4.0 Authors@R: person("Paul", "Poncet", , "paulponcet@yahoo.fr", role = c("aut", "cre")) Description: Provides estimators of the mode of univariate data or univariate distributions. License: GPL-3 LazyData: TRUE Depends: R (>= 3.2) Imports: fBasics, stable, stabledist, stats, statip (>= 0.2.3) Suggests: evd, knitr, mvtnorm, testthat, VGAM URL: https://github.com/paulponcet/modeest BugReports: https://github.com/paulponcet/modeest/issues RoxygenNote: 7.0.0 NeedsCompilation: no Packaged: 2019-11-18 14:32:35 UTC; YL1101 Author: Paul Poncet [aut, cre] Maintainer: Paul Poncet Repository: CRAN Date/Publication: 2019-11-18 15:30:05 UTC modeest/R/0000755000176200001440000000000013564340224012116 5ustar liggesusersmodeest/R/distributionsList.R0000644000176200001440000000121013330156527015773 0ustar liggesusers .distributionsList <- function() { c("beta", "cauchy", "chisq", "dagum", "exp", "f", "fisk", "frechet", "gamma", "norm", "gev", "gh", "ght", "gld", "gompertz", "gpd", "gumbel", "hyp", "koenker", "kumar", "laplace", "logis", "lnorm", "lomax", "maxwell", "mvnorm", "naka", "nig", "paralogistic", "pareto", "rayleigh", "stable", #"symstb", "t", "unif", "weibull", "yules", "bern", "binom", "geom", "hyper", "nbinom", "pois") } modeest/R/parzen.R0000644000176200001440000000722213442240441013536 0ustar liggesusers#' @title #' Parzen's Kernel mode estimator #' #' @description #' Parzen's kernel mode estimator is the value #' maximizing the kernel density estimate. #' #' @details #' If \code{kernel = "uniform"}, the \code{\link[modeest]{naive}} mode estimate is returned. #' #' @note #' The user may call \code{parzen} through #' \code{mlv(x, method = "kernel", ...)} or \code{mlv(x, method = "parzen", ...)}. #' #' Presently, \code{parzen} is quite slow. #' #' @references #' \itemize{ #' \item Parzen E. (1962). #' On estimation of a probability density function and mode. #' \emph{Ann. Math. Stat.}, \bold{33}(3):1065--1076. #' #' \item Konakov V.D. (1973). #' On the asymptotic normality of the mode of multidimensional distributions. #' \emph{Theory Probab. Appl.}, \bold{18}:794-803. #' #' \item Eddy W.F. (1980). #' Optimum kernel estimators of the mode. #' \emph{Ann. Statist.}, \bold{8}(4):870-882. #' #' \item Eddy W.F. (1982). #' The Asymptotic Distributions of Kernel Estimators of the Mode. #' \emph{Z. Wahrsch. Verw. Gebiete}, \bold{59}:279-290. #' #' \item Romano J.P. (1988). #' On weak convergence and optimality of kernel density estimates of the mode. #' \emph{Ann. Statist.}, \bold{16}(2):629-647. #' #' \item Abraham C., Biau G. and Cadre B. (2003). #' Simple Estimation of the Mode of a Multivariate Density. #' \emph{Canad. J. Statist.}, \bold{31}(1):23-34. #' #' \item Abraham C., Biau G. and Cadre B. (2004). #' On the Asymptotic Properties of a Simple Estimate of the Mode. #' \emph{ESAIM Probab. Stat.}, \bold{8}:1-11. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The smoothing bandwidth to be used. #' #' @param kernel #' character. The kernel to be used. For available kernels see #' \code{\link[statip]{densityfun}} in package \pkg{statip}. #' #' @param abc #' logical. If \code{FALSE} (the default), the kernel density estimate #' is maximised using \code{\link[stats]{optim}}. #' #' @param tolerance #' numeric. Desired accuracy in the \code{\link[stats]{optimize}} function. #' #' @param ... #' If \code{abc = FALSE}, further arguments to be passed to \code{\link[stats]{optim}}. #' #' @return #' \code{parzen} returns a numeric value, the mode estimate. #' If \code{abc = TRUE}, the \code{x} value maximizing the density #' estimate is returned. Otherwise, the \code{\link[stats]{optim}} #' method is used to perform maximization, and the attributes: #' 'value', 'counts', 'convergence' and 'message', coming from #' the \code{\link[stats]{optim}} method, are added to the result. #' #' @seealso #' \code{\link[modeest]{mlv}}, \code{\link[modeest]{naive}} #' #' @importFrom stats optimize bw.SJ bw.nrd0 bw.nrd bw.ucv bw.bcv #' @importFrom statip kernelfun #' @importFrom statip .kernelsList #' @export #' @aliases Parzen #' #' @examples #' # Unimodal distribution #' x <- rlnorm(10000, meanlog = 3.4, sdlog = 0.2) #' #' ## True mode #' lnormMode(meanlog = 3.4, sdlog = 0.2) #' #' ## Estimate of the mode #' mlv(x, method = "kernel", kernel = "gaussian", bw = 0.3, par = shorth(x)) #' parzen <- function(x, bw = NULL, kernel = "gaussian", abc = FALSE, tolerance = .Machine$double.eps^0.25, ...) { if (pmatch(tolower(kernel), "normal", nomatch = 0)) { kernel <- "gaussian" } if (is.null(bw)) bw <- "nrd0" f <- statip::densityfun(x, bw = bw, kernel = kernel, ...) if (!abc) { M <- stats::optimize(f, range(x), maximum = TRUE, tol = tolerance)$maximum } else { f <- f(x) M <- x[f == max(f)] } M } modeest/R/asselin.R0000644000176200001440000001006213564337722013707 0ustar liggesusers#' @title #' The Asselin de Beauville mode estimator #' #' @description #' This mode estimator is based on the algorithm #' described in Asselin de Beauville (1978). #' #' @note #' The user may call \code{asselin} through #' \code{mlv(x, method = "asselin", ...)}. #' #' @references #' \itemize{ #' \item Asselin de Beauville J.-P. (1978). #' Estimation non parametrique de la densite et du mode, #' exemple de la distribution Gamma. #' \emph{Revue de Statistique Appliquee}, \bold{26}(3):47-70. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. A number in \code{(0, 1]}. #' If \code{bw = 1}, the selected 'modal chain' may be too long. #' #' @param ... #' further arguments to be passed to the \code{\link[stats]{quantile}} function. #' #' @return #' A numeric value is returned, the mode estimate. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation. #' #' @importFrom stats median quantile #' @export #' @aliases Asselin #' #' @examples #' x <- rbeta(1000, shape1 = 2, shape2 = 5) #' #' ## True mode: #' betaMode(shape1 = 2, shape2 = 5) #' #' ## Estimation: #' asselin(x, bw = 1) #' asselin(x, bw = 1/2) #' mlv(x, method = "asselin") #' asselin <- function(x, bw = NULL, # bw = 1 donne une chaine modale longue, bw < 1 est plus severe ...) { # TODO: look at 'na.contiguous' if (is.null(bw)) bw <- 1 nx <- length(x) kmax <- floor(ifelse(nx < 30, 10, 15)*log(nx)) y <- sort(x) ok1 <- FALSE while (!ok1) { ny <- length(y) if (ny==1) return(y) qy <- stats::quantile(y, probs = c(0.1, 0.25, 0.5, 0.75, 0.9), names = FALSE, ...) delta <- min(qy[5] - qy[4], qy[2] - qy[1]) a <- qy[1] - 3*delta b <- qy[5] + 3*delta yab <- y[y>=a & y <= b] k <- kmax ok2 <- FALSE while (!ok2) { #hy <- hist(yab, breaks = k, plot = FALSE);b <- hy$breaks;n <- c(hy$counts, 0) b <- seq(from = min(yab), to = max(yab), length = k+1) n <- c(tabulate(findInterval(yab, b[-(k+1)])), 0) N <- sum(n) v <- as.numeric(n >= N/k) ## Beginning of the first chain w <- which.max(v) v2 <- v[w:(k + 1)] ## End of the first chain w2 <- which.min(v2) + w - 1 v3 <- v[w2:(k + 1)] ## Length of the first chain nc <- sum(n[w:(w2 - 1)]) ## There exists another chain, and the first chain has only one element if (any(v3 == 1) && nc == 1) { if (k > 3) { k <- k-1 } else if (k == 3) { if (n[3] > 1) { w <- 3 w2 <- 4 } ok2 <- TRUE } else { stop("k < 3", call. = FALSE) } ## There exists another chain, and the first chain has more than one element } else if (any(v3 == 1) && nc > 1) { if (k > 3) { k <- k-1 ### In this case, w = 1 necessarily } else if (k == 3) { if (n[3] > 1) { p1 <- (1/n[1])*prod(diff(yab[yab >= b[w] & yab <= b[w2]])) # here, n[1] = length(first chain) p2 <- (1/n[3])*prod(diff(yab[yab >= b[3] & yab <= b[4]])) # and n[3] = length(second chain) if (p1 > p2) { w <- 3 w2 <- 4 } } ok2 <- TRUE } else { stop("k < 3", call. = FALSE) } ## There is no other chain: the modal chain is found! } else if (!any(v3 == 1)) { ok2 <- TRUE } } ## Update 'nc' nc <- sum(n[w:(w2-1)]) #cat("Modal chain length = ", nc, "\n") d <- abs((qy[4] + qy[2] - 2*qy[3])/(qy[4] - qy[2])) nc2 <- ny*(1-d) #cat("d = ", d, "\n") y <- yab[yab >= b[w] & yab <= b[w2]] if (nc == ny) { ok1 <- TRUE } else if (nc <= ifelse(nx < 30, nx/3, bw*nc2)) { ok1 <- TRUE } else { ok1 <- FALSE } } stats::median(y) } modeest/R/hsm.R0000644000176200001440000001110613564340421013026 0ustar liggesusers#' @title #' Half sample mode estimator #' #' @description #' This function computes the Robertson-Cryer mode estimator #' described in Robertson and Cryer (1974), #' also called half sample mode (if \code{bw = 1/2}) #' or fraction sample mode (for some other \code{bw}) by Bickel (2006). #' #' @details #' The modal interval, i.e. the shortest interval among #' intervals containing \code{k+1} observations, is computed #' iteratively, until only one value is found, the mode estimate. #' At each step \eqn{i}{i}, one takes \code{k = ceiling(bw*n) - 1}, #' where \code{n} is the length of the modal interval computed #' at step \eqn{i-}{i-}\code{1}. #' If \code{bw} is of class \code{"function"}, #' then \code{k = ceiling(bw(n)) - 1} instead. #' #' @note #' The user may call \code{hsm} through #' \code{mlv(x, method = "hsm", ...)}. #' #' @references #' \itemize{ #' \item Robertson T. and Cryer J.D. (1974). #' An iterative procedure for estimating the mode. #' \emph{J. Amer. Statist. Assoc.}, \bold{69}(348):1012-1016. #' #' \item Bickel D.R. and Fruehwirth R. (2006). #' On a Fast, Robust Estimator of the Mode: Comparisons to #' Other Robust Estimators with Applications. #' \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric or function. #' The bandwidth to be used. Should belong to (0, 1]. #' #' @param k #' numeric. See 'Details'. #' #' @param tie.action #' character. The action to take if a tie is encountered. #' #' @param tie.limit #' numeric. A limit deciding whether or not a warning #' is given when a tie is encountered. #' #' @param ... #' Additional arguments. #' #' @return #' A numeric value is returned, the mode estimate. #' #' @author #' D.R. Bickel for the original code, #' P. Poncet for the slight modifications introduced. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation; #' \code{\link[modeest]{venter}} for the Venter mode estimate. #' #' @export #' @aliases HSM #' #' @examples #' # Unimodal distribution #' x <- rweibull(10000, shape = 3, scale = 0.9) #' #' ## True mode #' weibullMode(shape = 3, scale = 0.9) #' #' ## Estimate of the mode #' bandwidth <- function(n, alpha) {1/n^alpha} #' hsm(x, bw = bandwidth, alpha = 2) #' mlv(x, method = "hsm", bw = bandwidth, alpha = 2) #' hsm <- function(x, bw = NULL, k, tie.action = "mean", tie.limit = 0.05, ...) { if (!missing(k) && is.null(bw)) { bw <- (k + 1)/length(x) } else if (missing(k) && is.null(bw)) { bw <- 1/2 } if (is.numeric(bw)) { if (bw <= 0 || bw > 1) { stop("argument 'bw' must belong to (0, 1]", call. = FALSE) } } y <- sort(x) while (length(y) >= 4) { ny <- length(y) if (is.function(bw)) { k <- ceiling(bw(ny, ...)*ny) - 1 } else { k <- ceiling(bw*ny) - 1 } inf <- y[1:(ny-k)] sup <- y[(k+1):ny] diffs <- sup - inf i <- which(diffs==min(diffs)) ## Ties? if(length(i) > 1) i <- .deal.ties(ny, i, tie.action, tie.limit) if (diffs[i]==0) { y <- y[i] } else { y <- y[i:(i+k)] } #y <- ifelse(diffs[i]==0, y[i], y[i:(i+k)]) } if (length(y) == 3) { z <- 2*y[2] - y[1] - y[3] M <- switch(as.character(sign(z)), "-1" = mean(y[1:2]), "1" = mean(y[2:3]), "0" = y[2]) } else { M <- mean(y) } M } #! Recursive estimator #mlv.hsm.rec <- #function(x, # sample # bw, # fraction of the observations to consider # tie.action = "mean", # tie.limit = 0.05) #{ # if (missing(bw)) bw <- 1/2 # if (bw <= 0 || bw > 1) stop("Argument 'bw' must belong to (0, 1].") # aux <- # function(y, ny, k) # { # if (ny == 3) { # z <- 2*y[2] - y[1] - y[3] # return(switch(as.character(sign(z)), "-1" = mean(y[1:2]), "1" = mean(y[2:3]), "0" = y[2])) # } else { # if (ny < 3) { # return(mean(y)) # } else { # diffs <- y[(k+1):ny] - y[1:(ny-k)] # i <- which(diffs==min(diffs)) # if(length(i) > 1) i <- .deal.ties(ny, i, tie.action, tie.limit) # y <- ifelse(diffs[i]==0, y[i], y[i:(i+k)]) # ny <- length(y) # Recall(y, ny, ceiling(ny*bw)-1) # } # } # } ## Output # nx <- length(x) # return(aux(sort(x), nx, ceiling(bw*nx)-1)) #} modeest/R/meanshift.R0000644000176200001440000000614413564340313014223 0ustar liggesusers#' @title #' The Meanshift mode estimator #' #' @description #' The Meanshift mode estimator. #' #' @note #' The user should preferentially call \code{meanshift} through #' \code{mlv(x, method = "meanshift", ...)}. #' #' @references #' \itemize{ #' \item Fukunaga, K. and Hostetler, L. (1975). #' The estimation of the gradient of a density function, #' with applications in pattern recognition. #' \emph{IEEE Transactions on Information Theory}, \bold{21}(1):32--40. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The smoothing bandwidth to be used. #' #' @param kernel #' character. The kernel to be used. Available kernels are #' \code{"biweight"}, \code{"cosine"}, \code{"eddy"}, #' \code{"epanechnikov"}, \code{"gaussian"}, \code{"optcosine"}, #' \code{"rectangular"}, \code{"triangular"}, \code{"uniform"}. #' See \code{\link[stats]{density}} for more details on some of these kernels. #' #' @param par #' numeric. The initial value used in the meanshift algorithm. #' #' @param iter #' numeric. Maximal number of iterations. #' #' @param tolerance #' numeric. Stopping criteria. #' #' @return #' \code{meanshift} returns a numeric value, the mode estimate, #' with an attribute \code{"iterations"}. #' The number of iterations can be less than \code{iter} #' if the stopping criteria specified by \code{eps} is reached. #' #' @seealso #' \code{\link[modeest]{mlv}}, \code{\link[modeest]{tsybakov}}. #' #' @importFrom stats bw.SJ bw.nrd0 bw.nrd bw.ucv bw.bcv #' @importFrom statip kernelfun #' @export #' #' @examples #' # Unimodal distribution #' x <- rweibull(100, shape = 12, scale = 0.8) #' #' ## True mode #' weibullMode(shape = 12, scale = 0.8) #' #' ## Estimate of the mode #' mlv(x, method = "meanshift", par = mean(x)) #' meanshift <- function(x, bw = NULL, kernel = "gaussian", par = shorth(x), iter = 1000, tolerance = sqrt(.Machine$double.eps)) { if (is.null(bw)) bw <- "nrd0" if (is.character(bw)) { if (length(x) < 2L) stop("need at least 2 points to select a bandwidth automatically", call. = FALSE) bw <- switch(tolower(bw), nrd0 = stats::bw.nrd0(x), nrd = stats::bw.nrd(x), ucv = stats::bw.ucv(x), bcv = stats::bw.bcv(x), sj = , `sj-ste` = stats::bw.SJ(x, method = "ste"), `sj-dpi` = stats::bw.SJ(x, method = "dpi"), stop("unknown bandwidth rule", call. = FALSE)) } s <- 0 for (j in seq_len(iter)) { z <- (x - par)/bw k <- statip::kernelfun(kernel)(z) M <- crossprod(x, k)/sum(k) if (is.nan(M)) stop("sum(k) is zero in the meanshift function. Change the bandwidth 'bw' or the initial value 'par'.", call. = FALSE) th <- abs(M/par-1) if (th < tolerance) { s <- j break } par <- as.vector(M) } attr(par, "iterations") <- s par } modeest/R/hrm.R0000644000176200001440000000620613564344533013042 0ustar liggesusers#' @title #' Bickel's half-range mode estimator #' #' @description #' SINCE THIS FUNCTION USED TO DEPEND ON THE BIOCONDUCTOR PACKAGE 'GENEFILTER', #' IT IS CURRENTLY DEFUNCT. #' #' This function computes Bickel's half range mode estimator #' described in Bickel (2002). It is a wrapper around the function #' \code{half.range.mode} from package \pkg{genefilter}. #' #' @details #' The mode estimator is computed by iteratively identifying #' densest half ranges. A densest half range is an interval #' whose width equals half the current range, and which #' contains the maximal number of observations. #' The subset of observations falling in the selected #' densest half range is then used to compute a new range, #' and the procedure is iterated. #' #' @note #' The user may call \code{hrm} through #' \code{mlv(x, method = "hrm", bw, ...)}. #' #' @references #' \itemize{ #' \item Bickel D.R. (2002). #' Robust estimators of the mode and skewness of continuous data. #' \emph{Computational Statistics and Data Analysis}, \bold{39}:153-163. #' #' \item Hedges S.B. and Shah P. (2003). #' Comparison of mode estimation methods and application in molecular clock analysis. #' \emph{BMC Bioinformatics}, \bold{4}:31-41. #' #' \item Bickel D.R. and Fruehwirth R. (2006). #' On a Fast, Robust Estimator of the Mode: #' Comparisons to Other Robust Estimators with Applications. #' \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The bandwidth to be used. Should belong to (0, 1]. #' This gives the fraction of the observations to consider at #' each step of the iterative algorithm. #' #' @param ... #' Additional arguments. #' #' @return #' A numeric value is returned, the mode estimate. #' #' @author The C and R code are due to Richard Bourgon \email{bourgon@stat.berkeley.edu}, #' see package \pkg{genefilter}. The algorithm is described in Bickel (2002). #' #' @seealso #' \code{\link[modeest]{mlv}()} for general mode estimation; #' \code{\link[modeest]{hsm}()} for the half sample mode; #' \code{\link[modeest]{venter}()} for the Venter mode estimate. #' # #' @importFrom genefilter half.range.mode #' @export #' @aliases HRM #' #' @examples #' \dontrun{ #' # Unimodal distribution #' x <- rgamma(1000, shape = 31.9) #' ## True mode #' gammaMode(shape = 31.9) #' #' ## Estimate of the mode #' hrm(x, bw = 0.4) #' mlv(x, method = "hrm", bw = 0.4) #' } #' hrm <- function(x, bw = NULL, ...) # TODO: introduce a 'k' argument? { .Defunct("genefilter::half.range.mode", msg = paste0("currently 'hrm()' is due to difficulties ", "in installing automatically BioConductor's ", "'genefilter' dependency")) if (is.null(bw)) stop("argument 'bw' is missing", call. = FALSE) if (bw <= 0 || bw > 1) { stop("argument 'bw' must belong to (0, 1]", call. = FALSE) } y <- sort(x) #genefilter::half.range.mode(y, beta = bw) } modeest/R/venter.R0000644000176200001440000001244513564340114013550 0ustar liggesusers#' @title #' The Venter / Dalenius / LMS mode estimator #' #' @description #' This function computes the Venter mode estimator, also called the Dalenius, #' or LMS (Least Median Square) mode estimator. #' #' @details #' The modal interval, i.e. the shortest interval among intervals containing #' \code{k+1} observations, is first computed. (In dimension > 1, this question #' is known as a 'k-enclosing problem'.) #' The user should either give the bandwidth \code{bw} or the argument \code{k}, #' \code{k} being taken equal to \code{ceiling(bw*n) - 1} if missing, so #' \code{bw} can be seen as the fraction of the observations to be considered #' for the shortest interval. #' #' If \code{type = 1}, the midpoint of the modal interval is returned. #' If \code{type = 2}, the \code{floor((k+1)/2)}th element of the modal #' interval is returned. #' If \code{type = 3} or \code{type = "dalenius"}, the median of the modal #' interval is returned. #' If \code{type = 4} or \code{type = "shorth"}, the mean of the modal interval #' is returned. #' If \code{type = 5} or \code{type = "ekblom"}, Ekblom's #' \eqn{L_{-\infty}}{L_{-infinity}} estimate is returned, see Ekblom (1972). #' If \code{type = 6} or \code{type = "hsm"}, the half sample mode (hsm) is #' computed, see \code{\link{hsm}}. #' #' @note #' The user may call \code{venter} through #' \code{mlv(x, method = "venter", ...)}. #' #' @references #' \itemize{ #' \item Dalenius T. (1965). #' The Mode - A Negleted Statistical Parameter. #' \emph{J. Royal Statist. Soc. A}, \emph{128}:110-117. #' #' \item Venter J.H. (1967). #' On estimation of the mode. #' \emph{Ann. Math. Statist.}, \bold{38}(5):1446-1455. #' #' \item Ekblom H. (1972). #' A Monte Carlo investigation of mode estimators in small samples. #' \emph{Applied Statistics}, \bold{21}:177-184. # #\item Rousseeuw and Leroy, 1987 #(ou bien Andrews ?) #' #' \item Leclerc J. (1997). #' Comportement limite fort de deux estimateurs du mode : le shorth et l'estimateur naif. #' \emph{C. R. Acad. Sci. Paris, Serie I}, \bold{325}(11):1207-1210. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The bandwidth to be used. Should belong to (0, 1]. See 'Details'. #' #' @param k #' numeric. See 'Details'. #' #' @param iter #' numeric. Number of iterations. #' #' @param type #' numeric or character. The type of Venter estimate to be computed. See 'Details'. #' #' @param tie.action #' character. The action to take if a tie is encountered. #' #' @param tie.limit #' numeric. A limit deciding whether or not a warning is given when a tie is #' encountered. #' #' @param warn #' logical. If \code{TRUE}, a warning is thrown when a tie is encountered. #' #' @param ... #' Further arguments. #' #' @return #' A numeric value is returned, the mode estimate. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation, #' \code{\link[modeest]{hsm}} for the half sample mode. #' #' @importFrom stats median #' @export #' @aliases Venter #' #' @examples #' library(evd) #' #' # Unimodal distribution #' x <- rgev(1000, loc = 23, scale = 1.5, shape = 0) #' #' ## True mode #' gevMode(loc = 23, scale = 1.5, shape = 0) #' #' ## Estimate of the mode #' venter(x, bw = 1/3) #' mlv(x, method = "venter", bw = 1/3) #' venter <- function(x, bw = NULL, k, iter = 1, type = 1, tie.action = "mean", tie.limit = 0.05, warn = FALSE) { ny <- length(x) ## Initialization type <- match.arg(tolower(as.character(type)), c("-inf", "1", "2", "3", "dalenius", "4", "shorth", "5", "ekblom", "6", "hsm")) if (type == "3") type <- "dalenius" if (type == "4") type <- "shorth" if (type == "-Inf" || type == "5") type <- "ekblom" if (type == "6") type <- "hsm" if (type == "hsm") { return(hsm(x = x, bw = bw, k = k, tie.action = tie.action, tie.limit = tie.limit)) } if (missing(k) && !is.null(bw)) { if (bw <= 0 || bw > 1) stop("argument 'bw' must belong to (0, 1]", call. = FALSE) k <- ceiling(bw * ny) - 1 } else if (missing(k) & is.null(bw)) { if (type == "ekblom") { k <- 1 } else { k <- ceiling(ny/2) - 1 } } if (k < 0 || k >= ny) stop("argument 'k' must belong to [0, length('x'))", call. = FALSE) y <- sort(x) inf <- y[1:(ny-k)] sup <- y[(k+1):ny] diffs <- sup - inf i <- which(diffs == min(diffs)) ## Ties? if (length(i) > 1) i <- .deal.ties(ny, i, tie.action, tie.limit, warn = warn) ## Output M <- switch(type, "1" = (y[i] + y[i+k])/2, "2" = y[i+floor((k+1)/2)], "dalenius" = stats::median(y[i:(i+k)]), "shorth" = mean(y[i:(i+k)]), "ekblom" = ifelse(y[i+2]-y[i+1]>y[i]-y[i-1], y[i], y[i+1])) if (iter > 1) { M <- Recall(x = y[i:(i+k)], bw = (k + 1)/ny, iter = iter-1, type = type, tie.action = tie.action, tie.limit = tie.limit) } #attr(M, "inf") <- y[i] #attr(M, "sup") <- y[i+k] M } #' @export #' @rdname venter #' shorth <- function(x, ...) venter(x, type = "shorth", ...) modeest/R/deal.ties.R0000644000176200001440000000175613564340722014125 0ustar liggesusers # #' @author # #' Adapted from the function written by Wolfgang Huber and Ligia Pedroso Bras # #' in package \pkg{genefilter}. # #' # #' @importFrom stats median .deal.ties <- function(ny, # length of the data i, # index tie.action, # action to be taken tie.limit, # limit warn = FALSE) { ## Deal with ties maxi <- max(i) mini <- min(i) if (maxi-mini > tie.limit * ny) { warning(paste("encountered a tie, and the difference between minimal and maximal value is > length('x') * 'tie.limit'", "the distribution could be multimodal", sep="\n"), call. = FALSE) } ## Take the action specified in "tie.action" switch(tie.action, mean = mean(i), median = stats::median(i), max = maxi, min = mini, stop(sprintf("invalid value '%s' for argument 'tie.action'", tie.action), call. = FALSE)) } modeest/R/lientz.R0000644000176200001440000001633413564340366013564 0ustar liggesusers#' @title #' The empirical Lientz function and the Lientz mode estimator #' #' @description #' The Lientz mode estimator is nothing but the value minimizing the empirical #' Lientz function. A 'plot' and a 'print' methods are provided. #' #' @references #' \itemize{ #' \item Lientz B.P. (1969). #' On estimating points of local maxima and minima of density functions. #' \emph{Nonparametric Techniques in Statistical Inference (ed. M.L. Puri, Cambridge University Press}, p.275-282. #' #' \item Lientz B.P. (1970). #' Results on nonparametric modal intervals. #' \emph{SIAM J. Appl. Math.}, \bold{19}:356-366. #' #' \item Lientz B.P. (1972). #' Properties of modal intervals. #' \emph{SIAM J. Appl. Math.}, \bold{23}:1-5. #' } #' #' @details #' The Lientz function is the smallest non-negative quantity \eqn{S(x,\beta)}{S(x,b)}, #' where \eqn{\beta}{b} = \code{bw}, such that #' \deqn{F(x+S(x,\beta)) - F(x-S(x,\beta)) \geq \beta.}{F(x+S(x,b)) - F(x-S(x,b)) >= b.} #' Lientz (1970) provided a way to estimate \eqn{S(x,\beta)}{S(x,b)}; this estimate #' is what we call the empirical Lientz function. #' #' @note #' The user may call \code{mlv.lientz} through #' \code{mlv(x, method = "lientz", ...)}. #' #' @param x #' numeric (vector of observations) or an object of class \code{"lientz"}. #' #' @param bw #' numeric. The smoothing bandwidth to be used. #' Should belong to (0, 1). Parameter 'beta' in Lientz (1970) function. #' #' @param abc #' logical. If \code{FALSE} (the default), the Lientz empirical function #' is minimised using \code{\link[stats]{optim}}. #' #' @param par #' numeric. The initial value used in \code{\link[stats]{optim}}. #' #' @param optim.method #' character. If \code{abc = FALSE}, the method used in #' \code{\link[stats]{optim}}. #' #' @param zoom #' logical. If \code{TRUE}, one can zoom on the graph created. #' #' @param digits #' numeric. Number of digits to be printed. #' #' @param ... #' if \code{abc = FALSE}, further arguments to be passed to #' \code{\link[stats]{optim}}, or further arguments to be passed to #' \code{\link[graphics]{plot}}. #' #' @return #' \code{lientz} returns an object of class \code{c("lientz", "function")}; #' this is a function with additional attributes: #' \itemize{ #' \item{x}{ the \code{x} argument} #' \item{bw}{ the \code{bw} argument } #' \item{call}{ the call which produced the result } #' } #' #' \code{mlv.lientz} returns a numeric value, the mode estimate. #' If \code{abc = TRUE}, the \code{x} value minimizing the Lientz empirical #' function is returned. Otherwise, the \code{\link[stats]{optim}} method is #' used to perform minimization, and the attributes: 'value', 'counts', #' 'convergence' and 'message', coming from the \code{\link[stats]{optim}} #' method, are added to the result. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation; #' \code{\link[modeest]{shorth}} for the shorth estimate of the mode #' #' @export #' @aliases Lientz #' #' @examples #' # Unimodal distribution #' x <- rbeta(1000,23,4) #' #' ## True mode #' betaMode(23, 4) #' #' ## Lientz object #' f <- lientz(x, 0.2) #' print(f) #' plot(f) #' #' ## Estimate of the mode #' mlv(f) # optim(shorth(x), fn = f) #' mlv(f, abc = TRUE) # x[which.min(f(x))] #' mlv(x, method = "lientz", bw = 0.2) #' #' # Bimodal distribution #' x <- c(rnorm(1000,5,1), rnorm(1500, 22, 3)) #' f <- lientz(x, 0.1) #' plot(f) #' lientz <- function(x, bw = NULL) { if (bw <= 0 || bw >= 1) { stop("argument 'bw' must belong to (0, 1)", call. = FALSE) } y <- sort(x) ny <- length(y) k <- ceiling(bw*ny) - 1 if (k==0) { f <- function(z) { yy <- (y + c(y[-1],Inf))/2 i <- sapply(z, FUN = function(zz) min(which(zz <= yy))) return(abs(y[i] - z)) } } else if (k>0) { f <- function(z) { yy1 <- c(y[(k+1):ny],rep(Inf,k)) # k+1 lags yy2 <- c(y[(k+2):ny],rep(Inf,k+1)) # k+2 lags yy <- sort(c((y+yy1)/2,(y+yy2)/2)) i <- sapply(z, FUN = function(zz) min(which(zz <= yy))) j <- i%%2 yy <- y[j*k + (i+j)/2] return(ifelse(j==1, yy-z, z-yy)) } } class(f) <- c("lientz", class(f)) attr(f, "call") <- sys.call() attr(f, "x") <- x attr(f, "bw") <- bw attr(f, "source") <- NULL f } #' @importFrom graphics plot points legend locator #' @export #' @rdname lientz #' plot.lientz <- function(x, # an object of class 'lientz' zoom = FALSE, # if TRUE, one can zoom on the graph created ...) { if (!inherits(x, "lientz")) { stop("argument 'x' must inherit from class 'lientz'") } arg <- list(...) ylim <- arg$ylim main <- arg$main xlab <- arg$xlab ylab <- arg$ylab xx <- attr(x, "x") #bw <- attr(x, "bw") inf <- min(xx) sup <- max(xx) z <- seq(inf, sup, (sup - inf)/1024) lz <- x(z) if (is.null(ylim)) ylim <- range(lz) if (is.null(main)) main <- "Empirical Lientz's function" if (is.null(xlab)) xlab <- "x" if (is.null(ylab)) ylab <- "Sn(x)" graphics::plot(z, lz, main = main, xlab = xlab, ylab = ylab, ylim = ylim, ...) graphics::points(xx, rep(ylim[1],length(xx)), pch = "'", col = 4) graphics::legend("topleft",legend = c("Regular grid", "x"), col = c(1,4), pch = 19, bg = "white") if (zoom) { cat("you can zoom on the graph (press 'Esc' to escape)\n") lc <- graphics::locator(2) while (!is.null(lc)) { xlim <- sort(c(lc$x[1], lc$x[2])) ylim <- sort(c(lc$y[1], lc$y[2])) plot.lientz(x, zoom = FALSE, main = main, xlab = xlab, ylab = ylab, xlim = xlim, ylim = ylim, ...) lc <- graphics::locator(2) } } invisible(NULL) } #' @export #' @rdname lientz #' @method print lientz #' print.lientz <- function(x, # an object of class 'lientz' digits = NULL, ...) { if (!inherits(x, "lientz")) { stop("argument 'x' must inherit from class 'lientz'", call. = FALSE) } bw <- attr(x, "bw") call <- attr(x, "call") #xx <- attr(x, "x") cat("Empirical Lientz function\n") cat("Call:",deparse(call),"\n") cat("bw =", format(bw, digits = digits), "\n") } #' @importFrom stats optim #' @export #' @rdname lientz #' mlv.lientz <- function(x, # sample (the data) or object of class 'lientz' bw = NULL, # bandwidth abc = FALSE, # if FALSE, 'optim' is used par = shorth(x), # initial value used in 'optim' optim.method = "BFGS", # method used in 'optim' ...) { ## Initialization if (!inherits(x, "lientz")) { Sn <- lientz(x, bw) } else { Sn <- x x <- attr(Sn, "x") } if (!abc) { mini <- stats::optim(par, fn = Sn, method = optim.method, control=list(fnscale=1),...) M <- mini$par attr(M, "value") <- mini$value attr(M, "counts") <- mini$counts attr(M, "convergence") <- mini$convergence attr(M, "message") <- mini$message } else { Sn <- Sn(x) M <- mean(x[Sn == min(Sn)]) } M } modeest/R/grenander.R0000644000176200001440000001003513564340501014203 0ustar liggesusers#' @title #' The Grenander mode estimator #' #' @description #' This function computes the Grenander mode estimator. #' #' @details #' The Grenander estimate is defined by #' \deqn{ \frac{ \sum_{j=1}^{n-k} \frac{(x_{j+k} + x_{j})}{2(x_{j+k} - x_{j})^p} } #' { \sum_{j=1}^{n-k} \frac{1}{(x_{j+k} - x_{j})^p} } }{ ( sum_{j=1}^{n-k} (x_{j+k} + x_{j})/(2(x_{j+k} - x_{j})^p) ) / ( sum_{j=1}^{n-k} 1/((x_{j+k} - x_{j})^p) ) } #' #' If \eqn{p}{p} tends to infinity, this estimate tends to the Venter mode estimate; #' this justifies to call \code{\link[modeest]{venter}} if \code{p = Inf}. #' #' The user should either give the bandwidth \code{bw} or the argument \code{k}, #' \code{k} being taken equal to \code{ceiling(bw*n) - 1} if missing. #' #' @references #' \itemize{ #' \item Grenander U. (1965). #' Some direct estimates of the mode. #' \emph{Ann. Math. Statist.}, \bold{36}:131-138. #' #' \item Dalenius T. (1965). #' The Mode - A Negleted Statistical Parameter. #' \emph{J. Royal Statist. Soc. A}, \emph{128}:110-117. #' #' \item Adriano K.N., Gentle J.E. and Sposito V.A. (1977). #' On the asymptotic bias of Grenander's mode estimator. #' \emph{Commun. Statist.-Theor. Meth. A}, \bold{6}:773-776. #' #' \item Hall P. (1982). #' Asymptotic Theory of Grenander's Mode Estimator. #' \emph{Z. Wahrsch. Verw. Gebiete}, \bold{60}:315-334. #' } #' #' @note #' The user may call \code{grenander} through #' \code{mlv(x, method = "grenander", bw, k, p, ...)}. #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The bandwidth to be used. Should belong to (0, 1]. #' #' @param k #' numeric. Paramater 'k' in Grenander's mode estimate, see below. #' #' @param p #' numeric. Paramater 'p' in Grenander's mode estimate, see below. #' If \code{p = Inf}, the function \code{\link[modeest]{venter}} is used. #' #' @param ... #' Additional arguments to be passed to \code{\link[modeest]{venter}}. #' #' @return #' A numeric value is returned, the mode estimate. #' If \code{p = Inf}, the \code{\link[modeest]{venter}} mode estimator is returned. #' #' @author D.R. Bickel for the original code, #' P. Poncet for the slight modifications introduced. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation; #' \code{\link[modeest]{venter}} for the Venter mode estimate. #' #' @export #' @aliases Grenander #' #' @examples #' # Unimodal distribution #' x <- rnorm(1000, mean = 23, sd = 0.5) #' #' ## True mode #' normMode(mean = 23, sd = 0.5) # (!) #' #' ## Parameter 'k' #' k <- 5 #' #' ## Many values of parameter 'p' #' ps <- seq(0.1, 4, 0.01) #' #' ## Estimate of the mode with these parameters #' M <- sapply(ps, function(p) grenander(x, p = p, k = k)) #' #' ## Distribution obtained #' plot(density(M), xlim = c(22.5, 23.5)) #' grenander <- function(x, bw = NULL, k, p, ...) { if (p == Inf) { message("argument 'p' is infinite. Venter's mode estimator is used") return(venter(x = x, bw = bw, k = k, ...)) } ny <- length(x) if (missing(k) && !is.null(bw)) { if (bw <= 0 || bw > 1) { stop("argument 'bw' must belong to (0, 1]", call. = FALSE) } k <- ceiling(bw*ny) - 1 } else if (missing(k) && is.null(bw)) { k <- ceiling(ny/2) - 1 } if (k < 0 || k >= ny) { stop("argument 'k' must belong to [0, length('x'))", call. = FALSE) } y <- sort(x) inf <- y[1:(ny-k)] sup <- y[(k+1):ny] diff <- sup - inf tot <- inf + sup if (any(diff==0)) { warning("limiting value of Grenander mode used", call. = FALSE) # TODO: be more specific M <- mean(ifelse(diff==0, tot, NA), na.rm = TRUE)/2 } else { b <- sum(tot/diff^p)/2 a <- sum(1/diff^p) if (is.finite(b/a)) { M <- b/a } else { stop("function 'grenander' failed. Argument 'p' may be too large", call. = FALSE) } } M } modeest/R/tsybakov.R0000644000176200001440000000756313442240441014111 0ustar liggesusers#' @title #' The Tsybakov mode estimator #' #' @description #' This mode estimator is based on a gradient-like recursive algorithm, #' more adapted for online estimation. #' It includes the Mizoguchi-Shimura (1976) mode estimator, #' based on the window training procedure. #' #' @details #' If \code{bw} or \code{a} is missing, a default #' value advised by Djeddour et al (2003) is used: #' \code{bw = (1:length(x))^(-1/7)} and \code{a = (1:length(x))^(-alpha)}. #' (with \code{alpha = 0.9} if \code{alpha} is missing). #' #' @note #' The user may call \code{tsybakov} through #' \code{mlv(x, method = "tsybakov", ...)}. #' #' @section #' Warning: The Tsybakov mode estimate as it is presently #' computed does not work very well. #' The reasons of this inefficiency should be further investigated. #' #' @references #' \itemize{ #' \item Mizoguchi R. and Shimura M. (1976). #' Nonparametric Learning Without a Teacher Based on Mode Estimation. #' \emph{IEEE Transactions on Computers}, \bold{C25}(11):1109-1117. #' #' \item Tsybakov A. (1990). #' Recursive estimation of the mode of a multivariate distribution. #' \emph{Probl. Inf. Transm.}, \bold{26}:31-37. #' #' \item Djeddour K., Mokkadem A. et Pelletier M. (2003). #' Sur l'estimation recursive du mode et de la valeur modale d'une densite de #' probabilite. #' \emph{Technical report 105}. #' #' \item Djeddour K., Mokkadem A. et Pelletier M. (2003). #' Application du principe de moyennisation a l'estimation recursive du mode #' et de la valeur modale d'une densite de probabilite. #' \emph{Technical report 106}. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. Vector of length \code{length(x)} #' giving the sequence of smoothing bandwidths to be used. #' #' @param a #' numeric. Vector of length \code{length(x)} used in the #' gradient algorithm #' #' @param alpha #' numeric. An alternative way of specifying \code{a}. See 'Details'. #' #' @param kernel #' character. The kernel to be used. Available kernels are #' \code{"biweight"}, \code{"cosine"}, \code{"eddy"}, #' \code{"epanechnikov"}, \code{"gaussian"}, \code{"optcosine"}, #' \code{"rectangular"}, \code{"triangular"}, \code{"uniform"}. #' See \code{\link[stats]{density}} for more details on some #' of these kernels. #' #' @param dmp #' logical. If \code{TRUE}, Djeddour et al. #' version of the estimate is used. #' #' @param par #' numeric. Initial value in the gradient algorithm. #' Default value is \code{\link[modeest]{shorth}(x)}. #' #' @return #' A numeric value is returned, the mode estimate. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation. #' #' @importFrom statip .kernelsList kernelfun #' @export #' @aliases Tsybakov #' #' @examples #' x <- rbeta(1000, shape1 = 2, shape2 = 5) #' #' ## True mode: #' betaMode(shape1 = 2, shape2 = 5) #' #' ## Estimation: #' tsybakov(x, kernel = "triangular") #' tsybakov(x, kernel = "gaussian", alpha = 0.99) #' mlv(x, method = "tsybakov", kernel = "gaussian", alpha = 0.99) #' tsybakov <- function(x, bw = NULL, a, alpha = 0.9, kernel = "triangular", dmp = TRUE, par = shorth(x)) { if (pmatch(tolower(kernel), "normal", nomatch = 0)) { kernel <- "gaussian" } else { kernel <- match.arg(tolower(kernel), statip::.kernelsList()) } K <- statip::kernelfun(kernel, derivative = TRUE) nx <- length(x) if (missing(a)) { a <- (1:nx)^(-alpha) } if (is.null(bw)) bw <- (1:nx)^(-1/7) ## Initialization M.dmp <- M <- par b <- a/(bw^2) p <- bw^3 p <- p/cumsum(p) for (n in 1:nx) { M <- M + b[n]*K((M-x[n])/bw[n]) M.dmp <- M.dmp + p[n]*(M - M.dmp) } ifelse(dmp, M.dmp, M) } modeest/R/mlv.R0000644000176200001440000002113213564524725013050 0ustar liggesusers # TODO: packages 'ks' et 'kedd', notamment pour les kernel density derivative estimates #' @title #' Estimation of the Mode(s) or Most Likely Value(s) #' #' @description #' \code{mlv} is a generic function for estimating the mode of a univariate distribution. #' Different estimates (or methods) are provided: #' \itemize{ #' \item \code{\link{mfv}}, which returns the most frequent value(s) in a given numerical vector, #' \item the \code{\link{Lientz}} mode estimator, which is the value minimizing the Lientz function estimate, #' \item the Chernoff mode estimator, also called \code{\link{naive}} mode estimator, #' which is defined as the center of the interval of given length containing the most observations, #' \item the \code{\link{Venter}} mode estimator, including the \code{\link{shorth}}, i.e. the midpoint of the modal interval, #' \item the \code{\link{Grenander}} mode estimator, #' \item the half sample mode (\code{\link{HSM}}) and the half range mode (\code{\link{HRM}}), which are iterative versions of the Venter mode estimator, #' \item \code{\link{Parzen}}'s kernel mode estimator, which is the value maximizing the kernel density estimate, #' \item the \code{\link{Tsybakov}} mode estimator, based on a gradient-like recursive algorithm, #' \item the \code{\link{Asselin}} de Beauville mode estimator, based on a algorithm detecting chains and holes in the sample, #' \item the \code{\link{Vieu}} mode estimator, #' \item the \code{\link{meanshift}} mode estimator. #' } #' #' \code{mlv} can also be used to compute the mode of a given distribution, with \code{mlv.character}. #' #' @details #' For the default method of \code{mlv}, available methods are \code{"lientz"}, #' \code{"naive"}, \code{"venter"}, #' \code{"grenander"}, \code{"hsm"}, \code{"parzen"}, #' \code{"tsybakov"}, \code{"asselin"}, and \code{"meanshift"}. #' See the description above and the associated links. #' #' If \code{x} is of class \code{"character"} (with length > 1), #' \code{"factor"}, or \code{"integer"}, then the most frequent value found in #' \code{x} is returned using \code{\link[statip]{mfv}} from package #' \pkg{statip}. #' #' If \code{x} is of class \code{"character"} (with length 1), #' \code{x} should be one of \code{"beta"}, \code{"cauchy"}, \code{"gev"}, etc. #' i.e. a character for which a function \code{*Mode} exists #' (for instance \code{betaMode}, \code{cauchyMode}, etc.). #' See \code{\link[modeest]{distrMode}} for the available functions. #' The mode of the corresponding distribution is returned. #' # #' If \code{x} is of class \code{"density"}, the value where the density is # #' maximised is returned. # #' #' If \code{x} is of class \code{mlv.lientz}, see \code{\link[modeest]{Lientz}} #' for more details. #' #' @references #' See the references on mode estimation on the \code{\link[modeest]{modeest-package}}'s page. #' #' @param x #' numeric (vector of observations), or an object of class \code{"factor"}, \code{"integer"}, etc. #' #' @param bw #' numeric. The bandwidth to be used. #' This may have different meanings regarding the \code{method} used. #' #' @param method #' character. One of the methods available for computing the mode estimate. See 'Details'. #' #' @param na.rm #' logical. Should missing values be removed? #' # #' @param all # #' logical. # #' # #' @param abc # #' logical. If \code{FALSE} (the default), the estimate of the density function # #' is maximised using \code{\link{optim}}. # #' #' @param ... #' Further arguments to be passed to the function called for computation. #' #' @return #' A vector of the same type as \code{x}. #' Be aware that the length of this vector can be \code{> 1}. #' #' @seealso #' \code{\link[statip]{mfv}}, #' \code{\link[modeest]{parzen}}, #' \code{\link[modeest]{venter}}, #' \code{\link[modeest]{meanshift}}, #' \code{\link[modeest]{grenander}}, # #' \code{\link[modeest]{hrm}}, #' \code{\link[modeest]{hsm}}, #' \code{\link[modeest]{lientz}}, #' \code{\link[modeest]{naive}}, #' \code{\link[modeest]{tsybakov}}, #' \code{\link[modeest]{skewness}} #' #' @export #' #' @examples #' # Unimodal distribution #' x <- rbeta(1000,23,4) #' #' ## True mode #' betaMode(23, 4) #' # or #' mlv("beta", shape1 = 23, shape2 = 4) #' #' ## Be aware of this behaviour: #' mlv("norm") # returns 0, the mode of the standard normal distribution #' mlv("normal") # returns 0 again, since "normal" is matched with "norm" #' mlv("abnormal") # returns "abnormal", since the input vector "abrnormal" #' # is not recognized as a distribution name, hence is taken as a character #' # vector from which the most frequent value is requested. #' #' ## Estimate of the mode #' mlv(x, method = "lientz", bw = 0.2) #' mlv(x, method = "naive", bw = 1/3) #' mlv(x, method = "venter", type = "shorth") #' mlv(x, method = "grenander", p = 4) # #' mlv(x, method = "hrm", bw = 0.3) #' mlv(x, method = "hsm") #' mlv(x, method = "parzen", kernel = "gaussian") #' mlv(x, method = "tsybakov", kernel = "gaussian") #' mlv(x, method = "asselin", bw = 2/3) #' mlv(x, method = "vieu") #' mlv(x, method = "meanshift") #' mlv <- function(x, ...) { UseMethod("mlv") } #' @importFrom statip name2distr #' @export #' @rdname mlv #' mlv.character <- function(x, na.rm = FALSE, ...) { stopifnot(is.character(x)) if (length(x)==1L && statip::name2distr(x) %in% .distributionsList()) { distrMode(x, ...) } else { mfv(x, na_rm = na.rm) } } #' @export #' @rdname mlv #' mlv.factor <- function(x, na.rm = FALSE, ...) { stopifnot(is.factor(x)) mfv(x, na_rm = na.rm) } #' @export #' @rdname mlv #' mlv.logical <- function(x, na.rm = FALSE, ...) { stopifnot(is.logical(x)) mfv(x, na_rm = na.rm) } #' @export #' @rdname mlv #' mlv.integer <- function(x, na.rm = FALSE, ...) { stopifnot(is.integer(x)) mfv(x, na_rm = na.rm) } # #' @importFrom bazar is_wholenumber #' @export #' @rdname mlv #' mlv.default <- function(x, bw = NULL, method, na.rm = FALSE, ...) { stopifnot(is.numeric(x)) x <- as.vector(x) #test <- bazar::is_wholenumber(x) #if (is.na(test) || test) { # return(mfv(as.integer(round(x)), na_rm = na.rm)) #} x.na <- is.na(x) if (any(x.na)) { if (na.rm) { x <- x[!x.na] } else { stop("argument 'x' contains missing values", call. = FALSE) } } x.finite <- is.finite(x) if (any(!x.finite)) { x <- x[x.finite] } if (missing(method)) { warning("argument 'method' is missing. Data are supposed to be continuous. Default method 'shorth' is used", call. = FALSE) method <- "shorth" #} else if (tolower(method) == "mfv") { # stop("incorrect 'method' argument") } else if (pmatch(tolower(method), c("density", "kernel"), nomatch = 0)) { method <- "parzen" } else method <- match.arg(tolower(method), .methodsList()) if (method == "lientz") method <- "mlv.lientz" do.call(method, list(x = x, bw = bw, ...)) # possibly length > 1 #mean(theta) } # #' @export # #' @rdname mlv # #' # mlv.density <- # function(x, # all = TRUE, # abc = FALSE, # ...) # { # # TODO: A MODIFIER EN MEME TEMPS QUE 'parzen' # # if (!inherits(x, "density")) stop("argument 'x' must inherit from class 'density'") # # y <- x$y # x <- x$x # # den.s <- stats::smooth.spline(x, y, all.knots=TRUE, spar=spar) # s.1 <- stats::predict(den.s, den.s$x, deriv = 1) # s.0 <- stats::predict(den.s, den.s$x, deriv = 0) # # den.sign <- sign(s.1$y) # b <- rle(den.sign)$values # nmodes <- length(b)/2 # #if (nmodes > 10) { nmodes <- 10 } # if (is.na(nmodes)) { nmodes <- 0 } # # a <- c(1,1+which(diff(den.sign)!=0)) # df <- data.frame(a,b) # df <- df[which(df$b %in% -1),] # modes <- s.1$x[df$a] # density <- s.0$y[df$a] # df2 <- data.frame(modes,density) # df2 <- df2[with(df2, order(-density)), ] # ordered by density # df2 # # # #------------------- # # idx <- y == max(y) # M <- x[idx] # # if (all) { # yy <- c(0, y, 0) # ny <- length(yy) # idx <- (yy[2:(ny - 1)] > yy[1:(ny - 2)]) & (yy[2:(ny - 1)] > yy[3:ny]) # M <- unique(c(x[idx], M)) # } # # M # } #' @export #' @rdname mlv #' mlv1 <- function(x, ...) { mlv(x, ...)[[1L]] } modeest/R/reexports.R0000644000176200001440000000017013442240441014265 0ustar liggesusers #' @importFrom statip mfv #' @export #' statip::mfv #' @importFrom statip mfv1 #' @export #' statip::mfv1 modeest/R/methodsList.R0000644000176200001440000000103613330156527014542 0ustar liggesusers # #' @export .methodsList <- function() { c("asselin", #"chernoff", "density", "discrete", #"devroye", #"ekblom", #"eme", "grenander", "hrm", "hsm", "kernel", #"kim", "lientz", #"lms", #"logspline", # cf. package logspline "meanshift", "mfv",# most frequent value #"mizoguchi", "naive", "parzen", #"robertson", "shorth", #"splines", "tsybakov", "venter", "vieu") #"wavelet", #"wtp") } modeest/R/distrMode.R0000644000176200001440000003414713564340660014210 0ustar liggesusers#' @title #' Mode of some continuous and discrete distributions #' #' @description #' These functions return the mode of the main probability #' distributions implemented in R. #' #' @note #' Some functions like \code{normMode} or \code{cauchyMode}, which relate #' to symmetric distributions, are trivial, but are implemented for the sake of #' exhaustivity. #' #' @param x #' character. The name of the distribution to consider. #' #' @param ... #' Additional parameters. #' #' @return #' A numeric value is returned, the (true) mode of the distribution. #' #' @author #' \code{\link[fBasics]{ghMode}} and \code{\link[fBasics]{ghtMode}} are from #' package \pkg{fBasics}; #' \code{\link[fBasics]{hypMode}} was written by David Scott; #' \code{\link[fBasics]{gldMode}}, \code{\link[fBasics]{nigMode}} and #' \code{\link[stabledist]{stableMode}} were written by Diethelm Wuertz. #' #' @seealso #' \code{\link[modeest]{mlv}} for the estimation of the mode; #' the documentation of the related distributions #' \code{\link[stats]{Beta}}, \code{\link[stats]{GammaDist}}, etc. #' #' @importFrom statip name2distr #' @export #' distrMode <- function(x, ...) { stopifnot(is.character(x)) x <- match.arg(statip::name2distr(x), .distributionsList()) do.call(paste0(x, "Mode"), list(...)) } # Beta distribution #' @inheritParams stats::dbeta #' #' @export #' @rdname distrMode #' #' @examples #' ## Beta distribution #' curve(dbeta(x, shape1 = 2, shape2 = 3.1), #' xlim = c(0,1), ylab = "Beta density") #' M <- betaMode(shape1 = 2, shape2 = 3.1) #' abline(v = M, col = 2) #' mlv("beta", shape1 = 2, shape2 = 3.1) #' betaMode <- function(shape1, shape2, ncp = 0) { if (ncp == 0) { M <- (shape1-1)/(shape1+shape2-2) } else { warning("still to be done. 'NA' is returned", call. = FALSE) M <- NA } M } # Cauchy distribution #' @inheritParams stats::dcauchy #' #' @export #' @rdname distrMode #' cauchyMode <- function(location = 0, ...) { location } # Chi-square distribution #' @inheritParams stats::dchisq #' #' @export #' @rdname distrMode #' chisqMode <- function(df, ncp = 0) { if (ncp == 0) { M <- max(df-2, 0) } else { warning("still to be done. 'NA' is returned", call. = FALSE) M <- NA } M } # Dagum distribution #' @inheritParams VGAM::ddagum #' #' @export #' @rdname distrMode #' dagumMode <- function(scale = 1, shape1.a, shape2.p) { scale*((shape1.a*shape2.p-1)/(shape1.a+1))^(1/shape1.a) } # Exponential distribution #' @inheritParams stats::dexp #' #' @export #' @rdname distrMode #' expMode <- function(...) { 0 } # F distribution #' @inheritParams stats::df #' #' @export #' @rdname distrMode #' fMode <- function(df1, df2) { if (df1 > 2) { M <- (1-2/df1)*(df2/(2+df2)) } else { warning("still to be done. 'NA' is returned", call. = FALSE) M <- NA } M } # Fisk distribution #' @inheritParams VGAM::dfisk #' #' @export #' @rdname distrMode #' fiskMode <- function(scale = 1, shape1.a) { scale*((shape1.a-1)/(shape1.a+1))^(1/shape1.a) } # Frechet distribution #' @inheritParams VGAM::dfrechet #' #' @export #' @rdname distrMode #' frechetMode <- function(location = 0, scale = 1, shape = 1, ...) { location + scale*(shape/(1+shape))^(1/shape) } # Gamma distribution #' @inheritParams stats::dgamma #' #' @export #' @rdname distrMode #' gammaMode <- function(shape, rate = 1, scale = 1/rate) { scale*(shape-1) } # Gaussian (normal) distribution #' @inheritParams stats::dnorm #' #' @export #' @rdname distrMode #' normMode <- function(mean = 0, ...) { mean } # Generalized extreme value distribution #' @inheritParams VGAM::dgev #' #' @export #' @rdname distrMode #' gevMode <- function(location = 0, scale = 1, shape = 0, ...) { k <- pmax(0,(1+shape))^(-shape)-1 shape[shape==0] <- Inf location + (scale/shape)*k } # Generalized hyperbolic distribution #' @inheritParams fBasics::ghMode #' #' @importFrom fBasics ghMode #' @export #' @rdname distrMode #' ghMode <- function(alpha = 1, beta = 0, delta = 1, mu = 0, lambda = -1/2) { fBasics::ghMode(alpha, beta, delta, mu, lambda) } # Generalized Hyperbolic Student-t #' @inheritParams fBasics::ghtMode #' #' @importFrom fBasics ghtMode #' @export #' @rdname distrMode #' ghtMode <- function(beta = 0.1, delta = 1, mu = 0, nu = 10) { fBasics::ghtMode(beta, delta, mu, nu) } # Generalized Lambda Distribution #' @inheritParams fBasics::gldMode #' #' @importFrom fBasics gldMode #' @export #' @rdname distrMode #' gldMode <- function(lambda1 = 0, lambda2 = -1, lambda3 = -1/8, lambda4 = -1/8) { fBasics::gldMode(lambda1, lambda2, lambda3, lambda4) } # Gompertz distribution #' @inheritParams VGAM::dgompertz #' #' @export #' @rdname distrMode #' gompertzMode <- function(scale = 1, shape) { if (shape < scale) { M <- log(scale/shape)/scale } else { M <- 0 } M } # Generalized Pareto distribution #' @inheritParams VGAM::dgpd #' #' @export #' @rdname distrMode #' gpdMode <- function(location = 0, scale = 1, shape = 0) { if (shape == -1) { warning("all values between 'loc' and 'loc+scale' are modes, only the mean value is returned", call. = FALSE) M <- location + scale/2 } else if (-2-1/shape > 0) { M <- location - scale/shape } else { warning("the density is not continuous at the mode", call. = FALSE) M <- location } M } # Gumbel distribution #' @inheritParams VGAM::dgumbel #' #' @export #' @rdname distrMode #' gumbelMode <- function(location = 0, ...) { location } # Hyperbolic distribution #' @inheritParams fBasics::hypMode #' #' @importFrom fBasics hypMode #' @export #' @rdname distrMode #' hypMode <- function(alpha = 1, beta = 0, delta = 1, mu = 0, pm = c(1, 2, 3, 4)) { fBasics::hypMode(alpha, beta, delta, mu, pm) } # Koenker distribution #' @inheritParams stats::dcauchy #' #' @export #' @rdname distrMode #' koenkerMode <- function(location = 0, ...) { location } # Kumaraswamy distribution #' @inheritParams VGAM::dkumar #' #' @export #' @rdname distrMode #' kumarMode <- function(shape1, shape2) { (shape1-1)/(shape1*shape2 - 1)^(1/shape1) } # Laplace distribution #' @inheritParams VGAM::dlaplace #' #' @export #' @rdname distrMode #' laplaceMode <- function(location = 0, ...) { location } # Logistic distribution #' @inheritParams stats::dlogis #' #' @export #' @rdname distrMode #' logisMode <- function(location = 0, ...) { location } # Lognormal distribution #' @inheritParams stats::dlnorm #' #' @export #' @rdname distrMode #' #' @examples #' ## Lognormal distribution #' curve(stats::dlnorm(x, meanlog = 3, sdlog = 1.1), #' xlim = c(0, 10), ylab = "Lognormal density") #' M <- lnormMode(meanlog = 3, sdlog = 1.1) #' abline(v = M, col = 2) #' mlv("lnorm", meanlog = 3, sdlog = 1.1) #' lnormMode <- function(meanlog = 0, sdlog = 1) { exp(meanlog - sdlog^2) } # Lomax distribution #' @inheritParams VGAM::dlomax #' #' @export #' @rdname distrMode #' lomaxMode <- function(...) { 0 } # Maxwell-Boltzmann distribution #' @inheritParams VGAM::dmaxwell #' #' @export #' @rdname distrMode #' maxwellMode <- function(rate) { sqrt(2/rate) } # Multivariate normal distribution #' @inheritParams mvtnorm::dmvnorm #' #' @export #' @rdname distrMode #' mvnormMode <- function(mean, ...) { mean } # Nakagami distribution #' @inheritParams VGAM::dnaka #' #' @export #' @rdname distrMode #' nakaMode <- function(scale = 1, shape) { sqrt(2*(2*shape - 1)*scale/(4*shape)) } # Normal Inverse Gaussian distribution #' @inheritParams fBasics::nigMode #' #' @importFrom fBasics nigMode #' @export #' @rdname distrMode #' nigMode <- function(alpha = 1, beta = 0, delta = 1, mu = 0) { fBasics::nigMode(alpha, beta, delta, mu) } # Paralogistic distribution #' @inheritParams VGAM::dparalogistic #' #' @export #' @rdname distrMode #' paralogisticMode <- function(scale = 1, shape1.a) { if (shape1.a <= 1) { warning("the density is not continuous at the mode", call. = FALSE) 0 } else { scale*((shape1.a-1)/(shape1.a^2+1))^(1/shape1.a) } } # Pareto distribution #' @inheritParams VGAM::dpareto #' #' @export #' @rdname distrMode #' #' @examples #' curve(VGAM::dpareto(x, scale = 1, shape = 1), xlim = c(0, 10)) #' abline(v = paretoMode(scale = 1), col = 2) #' paretoMode <- function(scale = 1, ...) { warning("the density is not continuous at the mode", call. = FALSE) scale } # Rayleigh distribution #' @inheritParams VGAM::drayleigh #' #' @export #' @rdname distrMode #' rayleighMode <- function(scale = 1) { scale } # Stable distribution #' @inheritParams stabledist::dstable #' #' @importFrom stabledist dstable #' @importFrom stats optimize #' @export #' @rdname distrMode #' stableMode <- function(alpha, beta, gamma = 1, delta = 0, pm = 0, ...) { beta.max <- 1 - 1e-11 tol <- .Machine$double.eps^0.25 if (gamma == 1 & delta == 0 & pm == 0) { stopifnot(0 < alpha, alpha <= 2, length(alpha) == 1, -1 <= beta, beta <= 1, length(beta) == 1, length(beta.max) == 1) if (alpha * beta == 0) { M <- 0 } if (beta > beta.max) { beta <- beta.max } M <- stats::optimize(stabledist::dstable, interval = c(-0.7, 0) * sign(beta), alpha = alpha, beta = beta, pm = 0, maximum = TRUE, tol = tol)$maximum return(M) } else { warning("still to be done. 'NA' is returned", call. = FALSE) return(NA) } } # from package 'stable' #' @inheritParams stable::stable.mode #' #' @importFrom stable stable.mode #' @export #' @rdname distrMode #' stableMode2 <- function(loc, disp, skew, tail) { stable::stable.mode(loc, disp, skew, tail) } # Weibull distribution (in the context of extreme value theory) # # #' @export # #' @rdname distrMode # #' # nweibullMode <- # function(loc = 0, # scale = 1, # shape = 1) # { # #return(loc + (scale/-abs(shape))*((1-abs(shape))^(abs(shape))-1)) # if (shape < 1) { # M <- loc # } else { # M <- loc - scale*((shape-1)/shape)^(1/shape) # } # M # } # Student distribution #' @inheritParams stats::dt #' #' @export #' @rdname distrMode #' tMode <- function(df, ncp) { if (ncp == 0) { M <- 0 } else { warning("still to be done. 'NA' is returned", call. = FALSE) M <- NA } M } # Uniform distribution #' @inheritParams stats::dunif #' #' @export #' @rdname distrMode #' unifMode <- function(min = 0, max = 1) { warning("all values between 'min' and 'max' are modes, only the mean value is returned", call. = FALSE) (min+max)/2 } # Weibull distribution #' @inheritParams stats::dweibull #' #' @export #' @rdname distrMode #' weibullMode <- function(shape, scale = 1) { scale*(1-1/shape)^(1/shape) } # Yules-Simon distribution #' @inheritParams VGAM::dyules #' #' @export #' @rdname distrMode #' yulesMode <- function(...) { 1 } ## Discrete distributions #------------------------------------------------------ # Bernoulli distribution #' @inheritParams statip::dbern #' #' @export #' @rdname distrMode #' bernMode <- function(prob) { if (prob > 1 || prob < 0) return(NaN) q <- 1 - prob if (q > prob) return(0) if (q == prob) { c(0,1) } else { 1 } } # Binomial distribution #' @inheritParams stats::dbinom #' #' @export #' @rdname distrMode #' binomMode <- function(size, prob) { if (prob > 1 || prob < 0) return(NaN) if (prob == 0) { return(0) } else { if (prob == 1) { return(size) } else { x <- ceiling((size+1)*prob - 1) if (x == (size+1)*prob - 1) { return(c(x, x+1)) } else { return(x) } } } } # Geometric distribution #' @inheritParams stats::dgeom #' #' @export #' @rdname distrMode #' geomMode <- function(...) { 1 } # Hypergeometric distribution #' @inheritParams stats::dhyper #' #' @export #' @rdname distrMode #' hyperMode <- function(m, n, k, ...) { lambda <- (m+1)*(k+1)/(m+n+1) if (lambda == 0) return(0) x <- floor(lambda) if (lambda == x) { return(c(lambda-1,lambda)) } else { return(x) } } # Negative binomial distribution #' @inheritParams stats::dnbinom #' #' @export #' @rdname distrMode #' nbinomMode <- function(size, prob, mu) { if (prob > 1 || prob < 0) return(NaN) if (!missing(mu)) { prob <- size/(size+mu) } if (size <= 1) { 0 } else { floor((size-1)*(1-prob)/prob) } } # Poisson distribution #' @inheritParams stats::dpois #' #' @export #' @rdname distrMode #' #' @examples #' ## Poisson distribution #' poisMode(lambda = 6) #' poisMode(lambda = 6.1) #' mlv("poisson", lambda = 6.1) #' poisMode <- function(lambda) { if (lambda < 0) return(NaN) if (lambda == 0) return(0) x <- floor(lambda) if (lambda == x) { c(lambda-1,lambda) } else { x } } modeest/R/naive.R0000644000176200001440000000330413442240441013336 0ustar liggesusers#' @title #' The Chernoff or 'naive' mode estimator #' #' @description #' This estimator, also called the *naive* mode estimator, is defined as the #' center of the interval of given length containing the most observations. #' It is identical to Parzen's kernel mode estimator, when the kernel is chosen #' to be the uniform kernel. #' #' @note #' The user may call \code{naive} through #' \code{mlv(x, method = "naive", bw)}. #' #' @references #' \itemize{ #' \item Chernoff H. (1964). #' Estimation of the mode. #' \emph{Ann. Inst. Statist. Math.}, \bold{16}:31-41. #' #' \item Leclerc J. (1997). #' Comportement limite fort de deux estimateurs du mode : #' le shorth et l'estimateur naif. #' \emph{C. R. Acad. Sci. Paris, Serie I}, \bold{325}(11):1207-1210. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The smoothing bandwidth to be used. Should belong to (0, 1). See below. #' #' @return #' A numeric vector is returned, the mode estimate, #' which is the center of the interval of length \code{2*bw} #' containing the most observations. #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation; #' \code{\link[modeest]{parzen}} for Parzen's kernel mode estimation. #' #' @export #' @aliases Chernoff chernoff # #' @rdname parzen #' #' @examples #' # Unimodal distribution #' x <- rf(10000, df1 = 40, df2 = 30) #' #' ## True mode #' fMode(df1 = 40, df2 = 30) #' #' ## Estimate of the mode #' mean(naive(x, bw = 1/4)) #' mlv(x, method = "naive", bw = 1/4) #' naive <- function(x, bw = 1/2) { parzen(x = x, bw = bw, kernel = "uniform", abc = TRUE) } modeest/R/vieu.R0000644000176200001440000000525113442263473013221 0ustar liggesusers#' @title #' Vieu's mode estimator #' #' @description #' Vieu's mode estimator is the value at which the kernel density derivative #' estimate is null. #' #' @note #' The user may call \code{vieu} through #' \code{mlv(x, method = "vieu", ...)}. #' #' Presently, \code{vieu} is quite slow. #' #' @references #' \itemize{ #' \item Vieu P. (1996). A note on density mode estimation. #' \emph{Statistics \& Probability Letters}, \bold{26}:297--307. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param bw #' numeric. The smoothing bandwidth to be used. #' #' @param kernel #' character. The kernel to be used. Available kernels are \code{"biweight"}, #' \code{"cosine"}, \code{"eddy"}, \code{"epanechnikov"}, \code{"gaussian"}, #' \code{"optcosine"}, \code{"rectangular"}, \code{"triangular"}, #' \code{"uniform"}. See \code{\link[stats]{density}} for more details on some #' of these kernels. #' #' @param abc #' logical. If \code{FALSE} (the default), the root of the density derivate #' estimate is searched with \code{\link[stats]{uniroot}}. #' #' @param ... #' If \code{abc = FALSE}, further arguments to be passed to #' \code{\link[stats]{uniroot}}. #' #' @return #' \code{vieu} returns a numeric value, the mode estimate. If \code{abc = TRUE}, #' the \code{x} value at which the density derivative estimate is null is #' returned. Otherwise, the \code{\link[stats]{uniroot}} method is used. #' #' @importFrom stats uniroot #' @importFrom statip .kernelsList bandwidth kernelfun #' @export #' @aliases Vieu #' #' @seealso #' \code{\link[modeest]{mlv}}, \code{\link[modeest]{parzen}}. #' #' @examples #' # Unimodal distribution #' x <- rlnorm(10000, meanlog = 3.4, sdlog = 0.2) #' #' ## True mode #' lnormMode(meanlog = 3.4, sdlog = 0.2) #' #' ## Estimate of the mode #' mlv(x, method = "vieu", kernel = "gaussian") #' vieu <- function(x, bw = NULL, kernel = "gaussian", abc = FALSE, ...) { if (pmatch(tolower(kernel), "normal", nomatch = 0)) { kernel <- "gaussian" } #else { #kernel <- match.arg(tolower(kernel), c(statip::.kernelsList(), "uniform")) #} if (is.null(bw)) bw <- "nrd0" f <- statip::densityfun(x, bw = bw, kernel = kernel, ...) bw <- statip::bandwidth(x, bw) fn <- function(z) { k <- statip::kernelfun(kernel, derivative = TRUE)((z-x)/bw) sum(k) } if (!abc) { r <- stats::uniroot(f = fn, interval = range(x), ...) M <- r$root } else { .NotYetImplemented() #FN <- Vectorize(fn) #f <- abs(FN(x)) #M <- x[f == min(f)] # TODO: } M } modeest/R/skewness.R0000644000176200001440000000564013564340175014115 0ustar liggesusers#' @title #' Skewness #' #' @description #' This function encodes different methods #' to calculate the skewness from a vector of #' observations. #' #' @references #' \itemize{ #' \item Bickel D.R. (2002). #' Robust estimators of the mode and skewness of continuous data. #' \emph{Computational Statistics and Data Analysis}, \bold{39}:153-163. #' #' \item Bickel D.R. et Fruehwirth R. (2006). #' On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. #' \emph{Computational Statistics and Data Analysis}, \bold{50}(12):3500-3530. #' } #' #' @param x #' numeric. Vector of observations. #' #' @param na.rm #' logical. Should missing values be removed? #' #' @param method #' character. Specifies the method of computation. #' These are either \code{"moment"}, \code{"fisher"} or \code{"bickel"}. #' The \code{"moment"} method is based on the definition of #' skewness for distributions; this form should #' be used when resampling (bootstrap or jackknife). The #' \code{"fisher"} method corresponds to the usual "unbiased" #' definition of sample variance, although in the case of skewness #' exact unbiasedness is not possible. #' #' @param M #' numeric. (An estimate of) the mode of the observations \code{x}. #' Default value is \code{\link[modeest]{shorth}(x)}. #' #' @param ... #' Additional arguments. #' #' @return #' \code{skewness} returns a numeric value. #' An attribute reports the method used. #' #' @export #' @importFrom stats sd #' #' @seealso #' \code{\link[modeest]{mlv}} for general mode estimation; #' \code{\link[modeest]{shorth}} for the shorth estimate of the mode #' #' @author #' Diethelm Wuertz and contributors for the original \code{skewness} function #' from package \pkg{fBasics}. #' #' @examples #' ## Skewness = 0 #' x <- rnorm(1000) #' skewness(x, method = "bickel", M = shorth(x)) #' #' ## Skewness > 0 (left skewed case) #' x <- rbeta(1000, 2, 5) #' skewness(x, method = "bickel", M = betaMode(2, 5)) #' #' ## Skewness < 0 (right skewed case) #' x <- rbeta(1000, 7, 2) #' skewness(x, method = "bickel", M = hsm(x, bw = 1/3)) #' skewness <- function(x, na.rm = FALSE, method = c("moment", "fisher", "bickel"), M, ...) { method <- match.arg(tolower(method), c("moment", "fisher", "bickel")) if (!is.numeric(x)) { stop("argument 'x' is must be numeric", call. = FALSE) } if (na.rm) x <- x[!is.na(x)] nx <- length(x) if (missing(M)) M <- shorth(x) if (method == "moment") { sk <- sum((x - mean(x))^3/stats::sd(x)^3)/nx } if (method == "fisher") { if (nx < 3) sk <- NA else sk <- ((sqrt(nx * (nx - 1))/(nx - 2)) * (sum(x^3)/nx))/((sum(x^2)/nx)^(3/2)) } if (method == "bickel") { cdf.value <- (length(x[x < M]) + length(x[x == M])/2)/nx sk <- 1-2*cdf.value } sk } modeest/NEWS.md0000644000176200001440000000454413564524471013032 0ustar liggesusers# modeest 2.4.0 (2019-11-17) ## Breaking changes * Defunct `hrm()` due to difficulties in installing automatically Bioconductor's 'genefilter' dependency. This function shall be reintroduced in a future version. ## Performance * Remove dependency to the 'genefilter' package. # modeest 2.3.3 ## Features * Simplify and improve the code of `parzen()`, `vieu()`, `tsybakov()`, `meanshift()`, `mlv()`. * Improve documentation. # modeest 2.3.2 ## Breaking changes * `mlv()` no longer returns a list, it returns a vector of values (usually one single value) for the sake of simplicity and homogeneity with functions such as `mean()` or `median()`. * Move `mfv()` and `mfv1()` to package `statip` (and reexport them). * Remove `discrete()`. Use `mfv()` or `mfv1()` instead. # modeest 2.3.0 ## Features * Move hidden kernel related functions to package `statip`. * Deprecate `discrete()`; it will be removed in a future version of the package. Start using `mfv()` instead. * Add `mfv1()`, which always returns a length 1 value (so that `mfv1(x)==mfv(x)[[1L]]`). # modeest 2.2 ## Bug fixes * Thank you to W. H. Beasley who pointed out a slight mistake in the calculation of Bickel's skewness in `mlv.integer()`. Now the skewness is set at `NA` in case of multiple modes. * As documented under `?as.numeric`, the function `as.numeric.mlv()` was not correct, and is now replaced by `as.double.mlv()`. ## Features * Add the meanshift mode estimator. # modeest 2.1 ## Breaking changes * Remove function `symstbMode()`. ## Bug fixes * Thank you to C. Lepoittevin and K. Fijorek who pointed out a misuse of `ifelse` in the function `hsm()`. This has been corrected, so now `hsm` works correctly. ## Features * Add functions `fiskMode()`, `gompertzMode()`, `koenkerMode()`, `kumarMode()`, `laplaceMode()`, `paralogisticMode()`, `paretoMode()`, `rayleighMode()`. # modeest 1.09 ## Breaking changes * Remove methods for the Chernoff distribution, because of their lack of efficiency. * Remove the DIP statistic. * In function `tsybakov()`, the argument `djeddour` is renamed `dmp`. * In functions `parzen()` and `mlv.density()`, the argument `biau` is renamed `abc`. ## Features * Add the Asselin de Beauville mode estimator. * Add function `as.numeric.mlv()`. modeest/MD50000644000176200001440000000340013564534375012236 0ustar liggesusersb434c309fc8170b283954fda276f7082 *DESCRIPTION 6f603cedda6f7cbc8440cf2bce0f85c2 *NAMESPACE de88343d95f96172cf2c694df544662f *NEWS.md 5c6765e57ad730fb4a2a1300c6d20082 *R/asselin.R 939276f430cce3c261f47e72dabcac0a *R/deal.ties.R bbff26c04a82ca628e0ef043ede8e8a6 *R/distrMode.R 28a928600746407d55eb1ce00dd8b951 *R/distributionsList.R c4bd8c44ef7b392dcb474b92eb5d16a7 *R/grenander.R c627a02085c440855c7120de4b0f693f *R/hrm.R 211c549e04db91b5d593c7840c23b16c *R/hsm.R c770c245d2714da91e63e62566632377 *R/lientz.R a4f6d97decfbc69c9c8e4f4a80d8e459 *R/meanshift.R 3293b82aac402b92726a4ec4aa4e5986 *R/methodsList.R c1f75d4139c5dca1a646d75e39460804 *R/mlv.R dd093e2e7d926a03771473a765fd627c *R/naive.R 4abb825dffe518862c449969b35e6d7b *R/parzen.R 4ecd2125607731dbea0f685cc2a6fcc3 *R/reexports.R 27643f2e4118eef97ccc738dfc75c733 *R/skewness.R 73503364f48795202a14c06b25823c91 *R/tsybakov.R adb7ffdde29637e5cdbcfd43f48b77ad *R/venter.R d5b473286dc058de9a5ed9a4e1b45e9e *R/vieu.R 8f5500b8eddb7f713f1fe7177b8b745a *README.md 2d3163b7adb1dde3e553432160981151 *man/asselin.Rd cfaba842294713341ea730e55c0d106f *man/distrMode.Rd cee9624398339c9f0d9f516f92cfc4d6 *man/grenander.Rd 1396b418709b1060bca5c97bbff30312 *man/hrm.Rd d792568d355e7ffef410774bec5f6d36 *man/hsm.Rd 9b105bd9423e3ce0ae5ecd0ebf359a59 *man/lientz.Rd 1e1e961b8d58109043270fa3e09f1bf2 *man/meanshift.Rd 66ecc73e7dc592d0f2046204377052a0 *man/mlv.Rd 2ad4a9351a3f23a688a94a2cd0ee0905 *man/modeest-package.Rd 62921943ea0e48f2752cffd96b1d25e2 *man/naive.Rd 9725ec914ae8a99b1a002f36bc3e11c4 *man/parzen.Rd 0dc9dd2abcb842496f7cfd814331605f *man/reexports.Rd 3983b02aa9f0d1c58309fe029b97bbc0 *man/skewness.Rd c8e8ce8f0e71bab26daed4eae4e1f620 *man/tsybakov.Rd 90ed5605567a643739c427e0e82058ff *man/venter.Rd 7a57c8868877971b5171c49300b65463 *man/vieu.Rd