R/weighted.var.R

weighted.var <-
function (x, w, na.rm = FALSE) 
{
    if (missing(w)) 
        w <- rep.int(1, length(x))
    else if (length(w) != length(x)) 
        stop("x and w must have the same length")
    if (min(w)<0) stop("there are negative weights")
    if (is.integer(w)) 
        w <- as.numeric(w)
    if (na.rm) {
        w <- w[obs.ind <- !is.na(x)]
        x <- x[obs.ind]
    }
    w<-w*length(w)/sum(w)
    sum(w*(x-weighted.mean(x,w))^2)/(sum(w)-1)
}

Try the modi package in your browser

Any scripts or data that you put into this service are public.

modi documentation built on May 2, 2019, 6:48 p.m.