1

我有一个从工作目录导入的大型时间序列文件,然后通过以下方式将它们转换为日志返回:

read.csv("/Volumes/3TB/ALLsince1996.csv",header=T)-> ALL
all <- xts(ALL[,2:dim(ALL)[2]], order.by= as.POSIXct(ALL[,1], format="%m/%d/%y"))
RETS <- CalculateReturns(all, method= c("log"))
RETS<- na.locf(RETS)
RETS[is.na(RETS)] <- 0

然后我通过 FRED 通过以下方式下载 3 个月的国库券:

# 3-Mo Treasury
data <- new.env()
FEDs <- c( "DGS3MO") # DGS3MO : 3-Mo Treasury Constant maturity
getSymbols( FEDs
        , src = "FRED"  
        , env = data
)
data$DGS3MO -> TB3
TB3/100/365 -> TB3
na.locf(TB3["1996-01-01::"])-> TB3

然后我尝试将日志返回系列与 3 个月的国库结合使用cbind()并获得以下信息:

both <- cbind(RETS[,1], TB3)

两个都:

    row.names      ZX.Adjusted  DGS3MO
1   1995-12-31 16:00:00 NA  NA
2   1996-01-01 00:00:00 0   NA
3   1996-01-01 16:00:00 NA  0.0001424658
4   1996-01-02 00:00:00 0   NA
5   1996-01-02 16:00:00 NA  0.0001424658
6   1996-01-03 00:00:00 0   NA
7   1996-01-03 16:00:00 NA  0.0001421918
8   1996-01-04 00:00:00 0   NA
9   1996-01-04 16:00:00 NA  0.0001421918

但这会返回一个每天两次的向量;比如1996-01-01 00:00:001996-01-01 16:00:00。我想要的是按日期而不是按时间将两者结合起来。

可重复的数据:

#Pull Data from getSymbols() 
library(quantmod)

dataset<- xts()
symbols <- c( "GLD", "IWM", "SPY", "GS")

system.time(
for(i in 1:length(symbols)) {
symbols[i]-> symbol
tryit <- try(getSymbols(symbol, from="1995-12-31", src='yahoo'))
if(inherits(tryit, "try-error")){
i <- i+1
} else {
data <- getSymbols(symbol, from="1995-12-31", src='yahoo')
dataset <- merge(dataset, Ad(get(symbols[i])))
rm(symbol)
}
}
)

因为它是一个大文件,所以我保存datasetindex(dataset)两个单独的文件中,因为我无法将索引与数据集一起保存

write.csv(dataset, "dataset.csv")
write.csv(index(dataset), "index.csv")

我后来index.csv在 Excel 中打开了文件并手动将索引粘贴到dataset.csv并保存了文件。我后来尝试重新打开.csv到我的工作区并计算日志返回

read.csv("dataset.csv",header=T)-> ALL
all <- xts(ALL[,2:dim(ALL)[2]], order.by= as.POSIXct(ALL[,1], format="%m/%d/%y"))
RETS <- CalculateReturns(all, method= c("log"))
RETS<- na.locf(RETS)
RETS[is.na(RETS)] <- 0

Next 下载 3-Month T-Bill,与上述代码相同...

# 3-Mo Treasury
data <- new.env()
FEDs <- c( "DGS3MO") # DGS3MO : 3-Mo Treasury Constant maturity
getSymbols( FEDs
        , src = "FRED"  
        , env = data
)
data$DGS3MO -> TB3
TB3/100/365 -> TB3
na.locf(TB3["1996-01-01::"])-> TB3

现在尝试RETS1结合TB3...

both <- cbind(RETS1, TB3)
4

2 回答 2

1

@Rime,要在没有时间信息的情况下重新格式化索引,请使用该strptime函数,然后使用merge上面建议的系列。

index(dataset) <- strptime(index(dataset),"%Y-%m-%d")

完成您正在尝试做的事情的一种更简单、更优雅的方法是makeReturnFrame使用神奇的 qmao-package ( https://r-forge.r-project.org/R/?group_id=1113 ) 和 a这种东西有很多实用程序和辅助功能。

library(quantmod)
library(qmao)

symbols <- c( "GLD", "IWM", "SPY", "GS")

getSymbols(symbols, from="1995-12-31", src='yahoo')
rets <- makeReturnFrame(symbols,silent = TRUE)
FEDs <- c( "DGS3MO") # DGS3MO : 3-Mo Treasury Constant maturity
data <- new.env()
getSymbols( FEDs
            , src = "FRED"  
            , env = data
)
data$DGS3MO -> TB3
TB3/100/365 -> TB3
na.locf(TB3["1996-01-01::"])-> TB3
series.merged <- merge(rets,TB3,join = "inner") 

> tail(series.merged)
                     GLD          IWM          SPY            GS       DGS3MO
2014-08-07  4.050035e-03 -0.004844797 -0.005429405 -0.0037775986 8.219178e-07
2014-08-08  7.924872e-05  0.009666235  0.011502456  0.0185147075 8.219178e-07
2014-08-11 -1.824311e-03  0.009485466  0.002893760  0.0011603622 1.095890e-06
2014-08-12  2.381425e-04 -0.006905738 -0.001394160 -0.0007540822 8.219178e-07
2014-08-13  1.665411e-03  0.007787650  0.006746170  0.0002320859 1.095890e-06
2014-08-14  8.712527e-04  0.001497468  0.004710710  0.0020863525 1.095890e-06
于 2014-08-17T09:04:00.850 回答
0

必须从两个数据集的索引中删除时间分量,以便跨日期索引正确合并

#Read stock returns data
require(quantmod)

data_agg<- xts()
symbols <- c( "GLD", "IWM", "SPY", "GS")


for(i in 1:length(symbols)) {
symbols[i]->symbol #assign
tryit <- try(getSymbols(symbol, from="1995-12-31", src='yahoo'))
if(inherits(tryit, "try-error")){
i <- i+1
} else {
data <- getSymbols(symbol, from="1995-12-31", src='yahoo')
data_agg <- merge(data_agg, Ad(get(symbols[i])))
rm(symbol)
}
}

head(data_agg)
#                    GLD.Adjusted IWM.Adjusted SPY.Adjusted GS.Adjusted
#1996-01-02 05:30:00           NA           NA        44.91          NA
#1996-01-03 05:30:00           NA           NA        45.03          NA
#1996-01-04 05:30:00           NA           NA        44.60          NA
#1996-01-05 05:30:00           NA           NA        44.51          NA
#1996-01-08 05:30:00           NA           NA        44.68          NA
#1996-01-09 05:30:00           NA           NA        43.91          NA


#Read interest rate data

data <- new.env()
FEDs <- c( "DGS3MO") # DGS3MO : 3-Mo Treasury Constant maturity
getSymbols( FEDs
            , src = "FRED"  
            , env = data
)
TB3 <- data$DGS3MO
TB3 <- TB3/100/365 
TB3 <- na.locf(TB3["1996-01-01::"])

head(TB3)
#                 DGS3MO
#1996-01-01           NA
#1996-01-02 0.0001424658
#1996-01-03 0.0001424658
#1996-01-04 0.0001421918
#1996-01-05 0.0001421918
#1996-01-08 0.0001419178

不建议使用 cbind 进行合并,因为它通过强制转换为相同格式将输入并排放置。merge.xts 是跨 xts 对象合并的方法

#The index of the datasets contain time component hence the doubled rows here

head(merge.xts(data_agg,TB3))
#           GLD.Adjusted IWM.Adjusted SPY.Adjusted GS.Adjusted       DGS3MO
#1996-01-01           NA           NA           NA          NA           NA
#1996-01-01           NA           NA        44.91          NA           NA
#1996-01-02           NA           NA           NA          NA 0.0001424658
#1996-01-02           NA           NA        45.03          NA           NA
#1996-01-03           NA           NA           NA          NA 0.0001424658
#1996-01-03           NA           NA        44.60          NA           NA


head(as.Date(index(data_agg)))
#[1] "1996-01-02" "1996-01-03" "1996-01-04" "1996-01-05" "1996-01-08" "1996-01-09"

#Only one observation per day, since there are no duplicate dates
#We can safely strip the time component from dates
any(duplicated(as.Date(index(data_agg))))
#[1] FALSE

any(duplicated(as.Date(index(TB3))))
#[1] FALSE


#Keep only the date component across both datasets
index(data_agg)<-as.Date(index(data_agg))
index(TB3)<-as.Date(index(TB3))


#Properly merged datasets across date index
head(merge.xts(data_agg,TB3))
#           GLD.Adjusted IWM.Adjusted SPY.Adjusted GS.Adjusted       DGS3MO
#1995-12-31           NA           NA        44.91          NA           NA
#1996-01-01           NA           NA        45.03          NA 0.0001424658
#1996-01-02           NA           NA        44.60          NA 0.0001424658
#1996-01-03           NA           NA        44.51          NA 0.0001421918
#1996-01-04           NA           NA           NA          NA 0.0001421918
#1996-01-06           NA           NA        44.68          NA           NA

您可以使用complete.cases仅保留非遗漏退货。

于 2014-08-17T09:01:21.383 回答