Description Usage Arguments Value Author(s) References Examples
View source: R/ff_panel_cumsum.R
svr_id are individuals, observed at multiple points; svr_x is date of observation; svr_y is some value observed at each time point. Imagine these are SAT scores for individuals. we want to calculate the sum/avg etc cumulatively at each date for all individuals, but only using their last score.
1 2 3 4 5 6 7 8 9 10 | ff_panel_cumsum_grouplast(
df,
svr_id,
svr_x,
svr_y,
svr_cumsumtop = "y_movingavg_lastestscores",
stat = "mean",
quick = TRUE,
verbose = FALSE
)
|
df |
dataframe |
svr_id |
string name of individual id variable |
svr_x |
string name of the ranking variable |
svr_y |
string name of the value variable |
svr_cumsumtop |
string variable name that stores the cumulative summed variable |
stat |
string type of statistics to compute, mean, sum, max, min, median |
quick |
boolean faster algorithm without repeating calculation, quick should be used, slow was initial bad algorithm, results identical |
verbose |
boolean when quick is FALSE, could verbose which generates slower results |
a dataframe with the cumulative summed/averaged etc column up to each row
Fan Wang, http://fanwangecon.github.io
https://fanwangecon.github.io/REconTools/reference/ff_panel_cumsum_grouplast.html https://fanwangecon.github.io/REconTools/articles/fv_panel_cumsum_grouplast.html https://github.com/FanWangEcon/REconTools/blob/master/R/ff_panel_cumsum.R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | library(tidyr)
library(dplyr)
library(tibble)
set.seed(12345)
it_N <- 5
ar_it_M <- sample(1:10, it_N, replace = TRUE)
ar_it_M_ID <- sample(1:it_N, it_N, replace = FALSE)
tb_combine <- as_tibble(cbind(ar_it_M, ar_it_M_ID)) %>% rowid_to_column(var = "id")
tb_long <- tb_combine %>% uncount(ar_it_M)
tb_long <- tb_long %>% add_column(xrand = runif(dim(tb_long)[1])) %>% arrange(xrand) %>% mutate(x = row_number())
tb_long <- tb_long %>% arrange(id, x) %>% group_by(id) %>% mutate(rank_l = row_number())
df <- tb_long %>% select(id, x) %>% add_column(y = runif(dim(tb_long)[1])) %>% arrange(id,x) %>% group_by(id) %>% mutate(y = cumsum(y))
print(df)
quick <- TRUE
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmean_lastestscore', stat='mean', quick=quick)
quick <- FALSE
verbose <- TRUE
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmean_lastestscore', stat='mean', quick=quick, verbose=verbose)
verbose <- FALSE
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmean_lastestscore', stat='mean', quick=quick, verbose=verbose)
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmedian_lastestscore', stat='median')
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingsum_lastestscore', stat='sum')
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmin_lastestscore', stat='min')
ff_panel_cumsum_grouplast(df, svr_id='id', svr_x='x', svr_y='y',
svr_cumsumtop = 'y_movingmax_lastestscore', stat='max')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.