Aggregate and Weighted Mean in R

前端 未结 4 2082
迷失自我
迷失自我 2020-12-06 06:04

I\'m trying to calculate asset-weighted returns by asset class. For the life of me, I can\'t figure out how to do it using the aggregate command.

My data frame look

4条回答
  •  庸人自扰
    2020-12-06 06:27

    The recently released collapse package provides a fast solution to this and similar problems (using weighted median, mode etc.) by providing a full set of Fast Statistical Functions performing grouped and weighted computations internally in C++:

    library(collapse)
    dat <- data.frame(assetclass = sample(LETTERS[1:5], 20, replace = TRUE), 
                      return = rnorm(20), assets = 1e7+1e7*runif(20))
    
    # Using collap() function with fmean, which supports weights: (by default weights are aggregated using the sum, which is prevented using keep.w = FALSE)
    collap(dat, return ~ assetclass, fmean, w = ~ assets, keep.w = FALSE)
    ##   assetclass     return
    ## 1          A -0.4667822
    ## 2          B  0.5417719
    ## 3          C -0.8810705
    ## 4          D  0.6301396
    ## 5          E  0.3101673
    
    # Can also use a dplyr-like workflow: (use keep.w = FALSE to omit sum.assets)
    library(magrittr)
    dat %>% fgroup_by(assetclass) %>% fmean(assets)
    ##   assetclass sum.assets     return
    ## 1          A   80683025 -0.4667822
    ## 2          B   27411156  0.5417719
    ## 3          C   22627377 -0.8810705
    ## 4          D  146355734  0.6301396
    ## 5          E   25463042  0.3101673
    
    # Or simply a direct computation yielding a vector:
    dat %$% fmean(return, assetclass, assets)
    ##          A          B          C          D          E 
    ## -0.4667822  0.5417719 -0.8810705  0.6301396  0.3101673 
    

提交回复
热议问题