I have monthly weight observations and daily returns, and I am trying to compute a geometric return for every day in a month. It might be easier to see the pattern:
How do I reproduce the "desired output" column? Either a solution from the base functions in R or any package suggestions are appreciated!
- Edit 1: Thank you.
Here is some sample data and the solution that I have been working on:
set.seed(33)
z <- c(.35,NA,NA,NA,.2,NA,NA)
z1 <- c(.35,.35,.35,.35,.2,.2,.2)
z2 <- rnorm(7)
zCbind <- data.frame(cbind(z,z1,z2))
colnames(zCbind) <- c("months","na.locf(months)","values")
solution1 <- ifelse(zCbind[,1] == zCbind[,2],
zCbind[,1], # if TRUE
zCbind[,2]*apply(zCbind[,3],2,cumprod)) # if FALSE
I know my problem is in the false condition. Solutions that I have tried are:
- replace cumprod with the prod function
- changed the format of zCbind[,3] by binding or converting it matrix/df
- this looked promising, but i can't find any more literature on the "cumprod.column" wrappers to the cumprod function: http://braverock.com/brian/R/PerformanceAnalytics/html/cum.utils.html