2016-09-06 134 views
0

我有两个数据帧,第一个是3个证券每日返回,第二个是证券的权重,如下:如何通过时间间隔来分割数据帧

daily.return <- data.frame(date = seq.Date(from = as.Date("2015-01-01"), 
              by = "days", 
              length.out = 100), 
           a = runif(100,-0.1,0.1), 
           b = runif(100,-0.1,0.1), 
           c = runif(100,-0.1,0.1)) 
    weights <- data.frame(startDate = c(as.Date("2015-01-01"), 
             as.Date("2015-02-10"), 
             as.Date("2015-03-15")), 
          endDate = c(as.Date("2015-02-09"), 
             as.Date("2015-03-14"), 
             as.Date("2015-04-10")), 
            a = c(0.3,0.5,0.2), 
            b = c(0.4,0.2,0.1), 
            c = c(0.3,0.3,0.7)   
          ) 

我知道如何分割如果我们将数据帧转换为xts;但如何分割这daily.return根据startDate和endDate在权重? 假设基金有这三种证券,如何计算基金净值和日收益?

回答

1

这应该做的工作。

daily.return <- data.frame(date = seq.Date(from = as.Date("2015-01-01"), 
              by = "days", 
              length.out = 100), 
          a = runif(100,-0.1,0.1), 
          b = runif(100,-0.1,0.1), 
          c = runif(100,-0.1,0.1)) 
weights <- data.frame(startDate = c(as.Date("2015-01-01"), 
            as.Date("2015-02-10"), 
            as.Date("2015-03-15")), 
         endDate = c(as.Date("2015-02-09"), 
            as.Date("2015-03-14"), 
            as.Date("2015-04-10")), 
         a = c(0.3,0.5,0.2), 
         b = c(0.4,0.2,0.1), 
         c = c(0.3,0.3,0.7)   
) 

library(quantmod) 

daily.xts <- as.xts(daily.return[,-1],daily.return[,1]) 

# Assuming that the total period is the same in both the data frames 
weights.xts <- xts(matrix(NA,nrow(daily.xts),3),order.by=index(daily.xts)) 
names(weights.xts) <- c("a","b","c") 

for (i in 1:nrow(weights)){ 

    temp.inputs <- weights[i,] 
    temp.period <- paste(temp.inputs[,1],temp.inputs[,2],sep="/") 
    len <- nrow(weights.xts[temp.period]) 
    weights.xts[temp.period,1:3] <- matrix(rep(as.numeric(temp.inputs[,3:5]),len),len,byrow=T) 

} 

weighted.returns <- daily.xts * weights.xts 
weighted.returns <- as.xts(rowSums(weighted.returns),index(weighted.returns)) 
names(weighted.returns) <- "Weighted Returns" 
weighted.returns$Cumulative <- cumsum(weighted.returns) 

plot(weighted.returns$Cumulative) 

enter image description here

0

您可以根据daily.return启动和使用apply权的最后一天,进行逐行操作

apply(weights, 1, function(x) daily.return[daily.return$date >= x[1] 
                & daily.return$date <= x[2], ]) 

这将给根据分裂的3个dataframes列表拆分范围在weights

编辑

如果我理解正确的,你想在列ab,该daily.returnc每个值与在weights各列繁殖。

apply(weights, 1, function(x) { 
     A <- daily.return[daily.return$date >= x[1] & daily.return$date <= x[2], ] 
     t(t(A[, 2:4]) * as.numeric(x[3:5])) 
    } 
) 
+0

非常感谢。 我试图使用“应用”,但失败了,这段代码更短且有用。 但如何乘以(%*%)daily.return列表的每个元素的权重data.frame对应的行? 我试图做到这一点,但也失败了,如下所示: lapply(daily.list, function(x,y){as.matrix(x [, - 1])%*% ,1,function(x)x)[ - 1,] [ - 1,] [,i])}) – ghoost2010

+0

@ ghoost2010我已经更新了答案。检查这是你想要的。 –

+1

非常感谢!在你更新你的答案之前,我写了一个for循环来解决这个问题。我不熟悉申请家庭功能,你的答案真的让我开始思考,我想我会尝试使用更频繁的申请。并且我很抱歉只能标记一个答案是最有帮助的答案。 – ghoost2010