0

我有一个数据框:

head(df)

GMT
               MSCI ACWI        DJGlbl  Russell 1000   Russell Dev S&P GSCI Industrial 
1999-03-01 -0.0070000000  0.0020000000 -0.0010000000 -0.0150000000        -0.0100000000
1999-03-02 -0.0050352467  0.0009980040 -0.0070070070 -0.0020304569         0.0040404040
1999-03-03 -0.0020242915  0.0029910269  0.0000000000 -0.0061037640         0.0010060362
1999-03-04  0.0070993915  0.0029821074  0.0151209677 -0.0010235415         0.0050251256
1999-03-05  0.0241691843  0.0089197225  0.0218470705  0.0276639344        -0.0120000000
1999-03-08  0.0039331367  0.0039292731  0.0058309038 -0.0009970090        -0.0020242915
1999-03-09  0.0029382958  0.0000000000 -0.0028985507  0.0069860279        -0.0010141988
1999-03-10  0.0078125000  0.0088062622  0.0058139535  0.0118929633         0.0091370558

我确实使用下面的代码计算了滚动窗口协方差矩阵,其中名称(windowsList)实现的值例如1-60是前 60 天 cov.matrix的值,2-61当窗口移动一天时,下一个协方差矩阵的值是 $:

windowSize <- 60
windows <- embed(1:nrow(df), windowSize)

lapplyApproach <- function(df, windows) {

windowsList <- split(t(windows), rep(1:nrow(windows), each=ncol(windows)))

names(windowsList) <- unlist(lapply(windowsList, function(x)paste(range(x), sep="",   collapse="-")))
return(lapply(windowsList, function(x)cov(df[x, ])))
}

我的问题是我想获取日期范围内的名称(windowsList)值而不是数字。例如,1-60我想要的不是 $ ,而是 $ 1999-03-01 - 1999-05-21。我试图制作一个日期矩阵,然后计算它们之间的范围,但没有成功。这个想法是名称($1999-03-01 - 1999-05-21等)将作为工作表的名称传递给 Excel。例如:

library("XLConnect")
hb <- loadWorkbook("stuff.xlsx", create = TRUE)
createSheet(hb, names(windowsList)) 
writeWorksheet(hb,windowsList, names(windowsList),header=TRUE)
saveWorkbook(hb)

有人可以指出我正确的方向吗?:) 最好的问候

4

1 回答 1

0

如果您使用rownames(df)[range(x)].

df <- read.table(text = "MSCI_ACWI        DJGlbl  Russell_1000   Russell-Dev S&P_GSCI_Industrial 
1999-03-01 -0.0070000000  0.0020000000 -0.0010000000 -0.0150000000        -0.0100000000
1999-03-02 -0.0050352467  0.0009980040 -0.0070070070 -0.0020304569         0.0040404040
1999-03-03 -0.0020242915  0.0029910269  0.0000000000 -0.0061037640         0.0010060362
1999-03-04  0.0070993915  0.0029821074  0.0151209677 -0.0010235415         0.0050251256
1999-03-05  0.0241691843  0.0089197225  0.0218470705  0.0276639344        -0.0120000000
1999-03-08  0.0039331367  0.0039292731  0.0058309038 -0.0009970090        -0.0020242915
1999-03-09  0.0029382958  0.0000000000 -0.0028985507  0.0069860279        -0.0010141988
1999-03-10  0.0078125000  0.0088062622  0.0058139535  0.0118929633         0.0091370558", header = TRUE)

windowSize <- 6
windows <- embed(1:nrow(df), windowSize)

我还修改了函数的最后一个命令:df[x, ]没有工作,因为x是一个列表。所以,我df[unlist(x),]改用了。

lapplyApproach <- function(df, windows) {  
 windowsList <- split(t(windows), rep(1:nrow(windows), each=ncol(windows)))  
 names(windowsList) <- unlist(lapply(windowsList,
            function(x) paste(rownames(df)[range(x)], sep="",   collapse=" - ")))
 return(lapply(windowsList, function(x) cov(df[unlist(x),])))
}

输出:

lapplyApproach(df, windows)

$`1999-03-01 - 1999-03-08`
                        MSCI_ACWI        DJGlbl  Russell_1000   Russell.Dev S.P_GSCI_Industrial
MSCI_ACWI            1.307947e-04  3.004810e-05  1.155990e-04  1.552330e-04       -3.310649e-05
DJGlbl               3.004810e-05  7.696563e-06  2.547058e-05  3.607536e-05       -1.244441e-05
Russell_1000         1.155990e-04  2.547058e-05  1.176579e-04  1.195526e-04       -2.707752e-05
Russell.Dev          1.552330e-04  3.607536e-05  1.195526e-04  2.065329e-04       -3.872039e-05
S.P_GSCI_Industrial -3.310649e-05 -1.244441e-05 -2.707752e-05 -3.872039e-05        5.164804e-05

...
于 2012-09-17T11:20:06.563 回答