[R] Winsorized mean and variance
Jim Lemon
jim at bitwrit.com.au
Thu Aug 27 13:20:45 CEST 2009
Roberto Perdisci wrote:
> Hello everybody,
> after searching around for quite some time, I haven't been able to
> find a package that provides a function to compute the Windorized mean
> and variance. Also I haven't found a function that computes the
> trimmed variance. Is there any such package around?
>
>
Hi Roberto,
The Winsorized variance is similar to the trimmed variance, except that
the extreme values are substituted rather than dropped. Define the
quantiles within which you want to retain the original values and then
substitute the values at the quantiles for all values more extreme in
the respective sign direction. Like this:
testdat<-rnorm(20)
winsorVar<-function(x,probs=c(0.05,0.95)) {
xq<-quantile(x,probs=probs)
x[x < xq[1]]<-xq[1]
x[x > xq[2]]<-xq[2]
return(var(x))
}
Jim
More information about the R-help
mailing list