# Function to calculate the weighted variance # x is a vector for which we want to compute the weighted variance # w is a vector that contains the weights # weighted.var<-function(x,w) { x.wmean<-weighted.mean(x=x,w=w) w.mean<-mean(w) n<-length(x) wvar<-sum(w*(x-x.wmean)^2)/(n-1)*w.mean list(wvar=wvar) }