gpt4 book ai didi

r - 合并两个 xts 对象时出错

转载 作者:行者123 更新时间:2023-12-04 18:13:52 27 4
gpt4 key购买 nike

我在网上找不到解决方案。两个 xts 对象在行数和列数上匹配。我仍然收到合并操作的以下错误 - “要替换的项目数不是替换长度的倍数”。

以下是 R 代码以及中间步骤的打印输出。我对 R 有点陌生。因此,如果您注意到程序中的任何步骤可以做得更好,那么您也可以就此向我提出建议。谢谢。

> # LOAD THE SPY DATA AND CREATE A DATA FRAME WITH RETURN COLUMN
> library(quantmod)
> library(PerformanceAnalytics)
> getSymbols("SPY", src='yahoo', index.class=c("POSIXt","POSIXct"), from='2002-01-01')
> SPY<-to.monthly(SPY)
> SPY.ret<-Return.calculate(SPY$SPY.Close)
> print(head(SPY.ret))
SPY.Close
Jan 2002 NA
Feb 2002 -0.018098831
Mar 2002 0.029868840
Apr 2002 -0.059915390
May 2002 -0.005951292
Jun 2002 -0.080167070
> index(SPY.ret) = as.Date(index(SPY)) # Convert to Date format as xts index is a Date.
> colnames(SPY.ret) <- "SPY"
> print(head(SPY.ret))
SPY
2002-01-01 NA
2002-02-01 -0.018098831
2002-03-01 0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070

> #LOAD THE TRADE FILE & CREATE A DATA FRAME WITH PROFIT COLUMN
> trades = as.xts(read.zoo(file="Anvi/CSV/ARS_EW_R2_SPDR.csv", index.column="Exit.time", format="%m/%d/%Y", header=TRUE, sep=","))
Warning message:
In zoo(rval3, ix) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
> df = trades$Profit
> print(head(df))
Profit
2003-09-30 " 0.079734219"
2004-01-31 " 0.116722585"
2004-03-31 " 0.060347888"
2004-04-30 " 0.100379816"
2004-07-31 " 0.084048027"
2004-07-31 " 0.018710103"
> df$Profits = as.numeric(trades$Profit)
> df = df$Profit #Inefficent way to convert Profit column to numeric?
> print(head(df))
Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.08404803
2004-07-31 0.01871010
> df = aggregate(df, by=index(df))
> colnames(df) = "Profit"
> print(head(df))
Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
>
> #MERGE THE SPY RET AND TRADE RESULTS DATA FRAMES
> temp = head(df)
> temp1 = head(SPY.ret)
> print(temp)
Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
> print(temp1)
SPY
2002-01-01 NA (Note: I tried replacing NA with 0 but still same error).
2002-02-01 -0.018098831
2002-03-01 0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070
> mdf = merge(x=temp, y=temp1, all=TRUE)
Error in z[match0(index(a), indexes), ] <- a[match0(indexes, index(a)), :
number of items to replace is not a multiple of replacement length
>

我在上面尝试做的是合并对象,使得结果对象的索引是一个 UNION 并且有两列“SPY”,“PROFIT”。合并对象中每一列的空单元格用 0 填充。

最佳答案

aggregate返回一个 zoo 对象,而不是 xts 对象。这意味着 merge 的 zoo 方法正在调度而不是 xts 方法。如果两个对象都是 xts 对象,则您的代码可以正常工作。

temp <- 
structure(c(0.07973422, 0.11672259, 0.06034789, 0.10037982, 0.10275813,
0.02533904), .Dim = c(6L, 1L), index = structure(c(12325, 12448,
12508, 12538, 12630, 12752), class = "Date"), class = "zoo",
.Dimnames = list(NULL, "Profit"))
temp1 <-
structure(c(NA, -0.018098831, 0.02986884, -0.05991539, -0.005951292,
-0.08016707), .Dim = c(6L, 1L), index = structure(c(1009864800,
1012543200, 1014962400, 1017640800, 1020229200, 1022907600), tzone = "",
tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "",
tzone = "", .Dimnames = list(NULL, "SPY"), class = c("xts", "zoo"))
merge(temp, temp1) # error
merge(as.xts(temp), temp1, fill=0) # works, filled with zeros
# Profit SPY
# 2002-01-01 0.00000000 NA
# 2002-02-01 0.00000000 -0.018098831
# 2002-03-01 0.00000000 0.029868840
# 2002-04-01 0.00000000 -0.059915390
# 2002-05-01 0.00000000 -0.005951292
# 2002-06-01 0.00000000 -0.080167070
# 2003-09-30 0.07973422 0.000000000
# 2004-01-31 0.11672259 0.000000000
# 2004-03-31 0.06034789 0.000000000
# 2004-04-30 0.10037982 0.000000000
# 2004-07-31 0.10275813 0.000000000
# 2004-11-30 0.02533904 0.000000000

关于r - 合并两个 xts 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com