2016-09-06 136 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