Skip to content

Weighted median

6 messages · Markus Jäntti, Peter Dalgaard, Agustin Lobo +2 more

#
Hi -- I have written the following functions fo my own use but am unhappy
with them. I first define a weighted.quantile function and then weighted.median
that uses the first one. 

The reason I am unhappy with my version is that 
I wanted to define a weighted.quantile function that reproduced the
R quantile function for the unweighted case (R:s quantile does some
smart interpolating), but I did not manage to do so. The below version
works, but if you can write a weighted version of the quantile function, let me
know.

Markus


# weighted quantiles. various versions, none good.

weighted.quantile <-
  function(x, w, probs = seq(0, 1, 0.25), na.rm = FALSE, names = TRUE) {
    # n <- length(x)
    # check:
    # - if w exists (length(w)>0) => if not, then w <- rep(1,length(x))
    # - if w is the weight or the inclusion probability
    #   + a test sum(w) > length(x)??????
    if (length(x) != length(w))
      stop("Weights and variable vectors are of unequal length!")
    if (na.rm)
      { valid.obs <- !is.na(x) & !is.na(w)
        x <- x[valid.obs]
        w <- w[valid.obs]}
    else if (any(is.na(x)) | any(is.na(w))) 
      stop("Missing values and NaN's not allowed if `na.rm' is FALSE")
    if (any((p.ok <- !is.na(probs)) & (probs < 0 | probs > 1))) 
      stop("probs outside [0,1]")
    if (na.p <- any(!p.ok)) {
      o.pr <- probs
      probs <- probs[p.ok]
    }
    np <- length(probs)
    if (missing(w)) 
      w <- rep(1, length(x))
    if (na.rm) {
      w <- w[i <- !is.na(x)]
      x <- x[i]
    }
    # check if weights are probs or unit correspondences
    ind <- order(x)
    cumw <- cumsum(w[ind])/sum(w)
    x <- x[ind]
    med <- numeric(0)
    for(i in 1:length(probs)) {
      if(probs[i] > 0 & probs[i] < 1) med[i] <- max(x[cumw<=probs[i]])
      else if (probs[i] == 0) med[i] <- min(x)
      else if (probs[i] == 1) med[i] <- max(x)
    }
    # the treatment of names
    med
  }

weighted.median <- function(x,w, na.rm = FALSE) {
  if (missing(w)) 
    w <- rep(1, length(x))
  if (na.rm) {
    w <- w[i <- !is.na(x)]
    x <- x[i]
    }
  weighted.quantile(x,w,.5)
}
#
--
Markus Jantti
Statistics Finland and University of Tampere
markus.jantti at iki.fi
http://www.iki.fi/~mjantti
#
Hello, R users:

When I use help.start() function and the browser (Netscape) opens, and
I use the Search Engenie & Keywords, it doesn't work.
And some times show me this message:

Search Results
No matches for "resgression" have been found!


Thank you for your help

Kenneth Cabrera

$platform  "i386-pc-mingw32"
$arch        "x86"
$os           "Win32"
$system    "x86, Win32"
$status      ""
$major      "1"
$minor      "4.1"
$year        "2002"
$month     "01"
$day         "30"
$language "R"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: krcabrer.vcf
Type: text/x-vcard
Size: 270 bytes
Desc: Card for Kenneth Cabrera
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020206/55770a5a/krcabrer.vcf
#
Kenneth Cabrera <krcabrer at epm.net.co> writes:
Try spelling it correctly?
#
I've found a similar problem anf the problem was
that the browser was not runing java scripts. Check
your browser configuration. You might need to download
a java plugin.

Agus

Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es
On Wed, 6 Feb 2002, Kenneth Cabrera wrote:

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

            
It wasn't the misspelling of the word...
I try any word or the section "Seach Engenie & Keyworkds"  in that page and it
still the same!!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: krcabrer.vcf
Type: text/x-vcard
Size: 270 bytes
Desc: Card for Kenneth Cabrera
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020207/007b7f7f/krcabrer.vcf
#
On Thu, 7 Feb 2002, Kenneth Cabrera wrote:

            
Is Java installed (it is an extra under Netscape 6.x) and is it enabled?

I've tested the rw1041 release on machines with Netscape 4.79 and 6.2.1
and the search does work.

Another possibility is that the file rw1041/doc/html/search/index.txt
has become corrupted: it so try link.html.help() or try re-installing.