【问题标题】:Estimate overnight returns for many stocks using a for loop and store it in a dataframe with stock names as column names使用 for 循环估计许多股票的隔夜收益,并将其存储在以股票名称作为列名的数据框中
【发布时间】:2017-03-31 12:13:35
【问题描述】:

我正在尝试使用 for 循环估计许多股票的隔夜收益,并将其存储在以股票名称作为列名的数据框中。 trade 有原始盘中数据,trade2 有清理盘中数据。 list.namess 有股票名称。这是我的代码:

require(xts)
require(highfrequency)
OvernightRet<-list()
list.namess<- list.files(pattern="*.IS Equity")
list.namess<- list.namess[2]
for(Q in 1:length(list.namess)){
  trade<-readRDS(list.namess[Q])
  trade<-xts(trade[,-1], order.by = trade[,1])
  colnames(trade)[c(1,2)]<-c("PRICE", "SIZE")
  #Unduplicating
  trade2<-do.call(rbind, lapply(split(trade,"days"), mergeTradesSameTimestamp))
  trade2<-trade2[,1]

  fun.first= function(x) first(x)
  fun.last= function(x) last(x)
  A=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.first))
  B=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.last))
  OvernightRetA <- (as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))
  colnames(OvernightRetA)<-list.namess[Q]
  OvernightRet[[Q]]<-OvernightRetA
}
df.OvernightRet<-do.call(merge, OvernightRet)

但是,它给出了错误,可能是因为无法重命名 OvernightRetA:

    Error in `colnames<-`(`*tmp*`, value = "ACEM IS Equity.rds") : 
      attempt to set 'colnames' on an object with less than two dimensions
    In addition: There were 50 or more warnings (use warnings() to see the first 50)
    > df.OvernightRet<-do.call(merge, OvernightRet)

Error in as.data.frame(x) : argument "x" is missing, with no default

因为 trade 和 trade2 很大,不适合 dput。我发布给定的 Open(A)、Close(B) 和名称列表 (list.namess),以便重现错误。

dput(head(A,10))
structure(c(231.9, 236.35, 230, 226.85, 229.05, 225.7, 226.95, 
224.55, 227, 234.65), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459481850, 
1459741066, 1459827433, 1459913867, 1460000236, 1460086630, 1460345867, 
1460432285, 1460518631, 1460950628), tzone = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))


dput(head(B,10))
structure(c(235.35, 231.2, 226.1, 229.05, 226.45, 225.75, 224.55, 
223.75, 231.1, 228.6), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459508732, 
1459767943, 1459854348, 1459940748, 1460027143, 1460113538, 1460374518, 
1460465873, 1460545568, 1460977541), tzone = "Asia/Calcutta", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))

dput(list.namess) "ACEM IS Equity.rds"

请帮我解决这个错误。

【问题讨论】:

  • 您遇到的错误是什么?包括确切的错误消息。确保您的示例数据重现您遇到的完全相同的错误。
  • @MrFlick,问候,正如我所提到的,trade 和 trade2 是非常大的数据集,不适合 dput。你认为隔夜退货的代码行 14-17 行吗?
  • 该错误暗示OvernightRetA 不是矩阵或data.frame,因此它没有列,因此您无法设置colnames()。您没有花任何时间说出所需的输出是什么或您正在尝试做什么,因此不清楚您想要发生什么。

标签: r rename xts


【解决方案1】:

我认为问题在于,正如错误消息所暗示的那样,您正在尝试将列标题分配给单个值。您可以通过将上面的行更改为:

OvernightRetA &lt;- as.data.frame(as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))

【讨论】:

  • 感谢 aakash,这会在 df.OvernightRet 期间出现问题
  • 我明白了。为什么不在循环中将股票名称和返回值存储为数据框中的行,然后在循环完成后使用t() 进行转置?在 col1 中存储 stock_name,在 col2 中存储值
  • 亲爱的 aakash,这是错误:> df.OvernightRet
  • 您在第二条评论中的建议似乎不错。请在答案中显示
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 2019-09-27
  • 1970-01-01
相关资源
最近更新 更多