Hi all. I tried to calculate sum of deviations from the weighted mean and i didn't get what i expected - 0. Here is an example:> wt <- c(10,25,38,22,5) > x <- 6:10 > wm <- weighted.mean(x,wt) > (x-wm)*wt[1] -18.70 -21.75 4.94 24.86 10.65> sum((x-wm)*wt)[1] -1.24345e-14 With simple mean I got 0:> sum(x-mean(x))[1] 0 Could someone explain me why we didn't get 0, with weighted mean, as it is expected? Thanks in advance Andrija [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Dec-08 14:20 UTC
[R] sum of deviations from the weighted mean
R FAQ 7.31 I'd imagine. (Floating point arithmetic and limitations thereof) Michael On Dec 8, 2011, at 9:15 AM, andrija djurovic <djandrija at gmail.com> wrote:> Hi all. I tried to calculate sum of deviations from the weighted mean and i > didn't get what i expected - 0. Here is an example: > >> wt <- c(10,25,38,22,5) >> x <- 6:10 >> wm <- weighted.mean(x,wt) >> (x-wm)*wt > [1] -18.70 -21.75 4.94 24.86 10.65 >> sum((x-wm)*wt) > [1] -1.24345e-14 > > With simple mean I got 0: >> sum(x-mean(x)) > [1] 0 > > Could someone explain me why we didn't get 0, with weighted mean, as it is > expected? > Thanks in advance > > Andrija > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.