Dear everyone,
Currently I have a dataset including various companies (gvkey) over multiple years (fyear).
Now I want to calculate the cummulative median per company (over de years) for variable NIBI. Can anyone help me how to do this?
Library(plyr)
gvkey <- c(1, 1, 1, 1, 2,2,2, 4, 4 )
fyear <- c(2005,2006,2007,2008, 2007,2008,2009 , 2011,2012)
nibi <- c(100, 110, 120, 130, 500, 550, 600, 50, 60)
lagAT <- c(1000,1500,1300,1200, 300,500, 800, 70, 40)(Thesis <- data.frame(gvkey, fyear, nibi, lagAT) %>% arrange(gvkey,fyear))
Thesis <- ddply(Thesis, .(gvkey), mutate,
lagNibi = c(NA, nibi[-length(nibi)])
)Thesis$difNibi <- Thesis$nibi - Thesis$lagNibi
Thesis$relNibi <- Thesis$difNibi /Thesis$lagAT
I already tried to calculate this via:
Library(cumstats)
Thesis$medbath <- cummedian(Thesis$relNibi[Thesis$relNibi<0])
tmp$csum <- ave(Thesis$relNibi[Thesis$relNibi<0], Thesis$gvkey, FUN=cummedian)
But these only give NA's.
Thank you in advance!