Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate deviations from weighted mean in data.table?

Tags:

r

data.table

I would like to calculate deviations from (weighted) mean for many variables in a data.table.

Let's take this example set:

mydt <- data.table(
    id = c(1, 2, 2, 3, 3, 3),
    x = 1:6,
    y = 6:1,
    w = rep(1:2, 3)
)

mydt
   id x y w
1:  1 1 6 1
2:  2 2 5 2
3:  2 3 4 1
4:  3 4 3 2
5:  3 5 2 1
6:  3 6 1 2

I can calculate the weighted means of x and y as follows:

mydt[
    ,
    lapply(
        as.list(.SD)[c("x", "y")], 
        weighted.mean, w = w
    ),
    by = id
]

(I use the relatively complicated as.list(.SD)[...] construct instead of .SDcols because of this bug.)

I tried to first create the means for each row, but did not find how to combine := with lapply().

like image 484
janosdivenyi Avatar asked Oct 19 '22 20:10

janosdivenyi


1 Answers

Just tweak the weighted mean calculation a bit:

mydt[
    ,
    lapply(
        .SD[, .(x, y)], 
        function(var) var - weighted.mean(var, w = w)
    ),
    by = id
]

   id       x       y
1:  1  0.0000  0.0000
2:  2 -0.3333  0.3333
3:  2  0.6667 -0.6667
4:  3 -1.0000  1.0000
5:  3  0.0000  0.0000
6:  3  1.0000 -1.0000

The solution is updated by the suggested notational simplification of @DavidArenburg.

like image 83
janosdivenyi Avatar answered Nov 01 '22 09:11

janosdivenyi