目前,我正在撰写经济学学士论文。我工作的一部分是ETF回报与基准指数回报的比较。为此,我想使用r脚本。目前我已在程序中加载原始收盘价,并将表格命名为“ETF”和“基准”。我的下一步是用这个收盘价来计算ETF的每日回报。我开始使用for-loop但它失败了。 错误术语是:
Error in daylyreturn_ETFs[r, c] <- (ETFs[r, currColName]/ETFs[(r + 1), :
incorrect number of subscripts on matrix
Varaibles:
ETF的:
'data.frame': 1672 obs. of 21 variables:
$ Name : Factor w/ 1636 levels "01.02.2010","01.02.2011",..: 1608 1557 1502 1449 1252 1194 1139 1084 1029 863 ...
$ iShares.Core.S.P.500.USD.Acc : num 203 203 206 205 205 ...
$ iShares.Core.DAX.U.00AE...DE. : num 100 100 100 100 100 ...
$ iShares.Core.MSCI.World.USD.Acc : num 42 42.2 42.8 42.5 42.6 ...
$ iShares.S.P.500.USD.Dist : num 21.2 21.3 21.7 21.6 21.5 ...
$ iShares.EURO.STOXX.50..DE. : num 33.1 32.9 33 33 32.9 ...
$ iShares.Core..U.0080..Corp.Bond.EUR.Dist : num 130 130 130 130 130 ...
$ Lyxor.Euro.Stoxx.50.DR.ETF.D.EUR.A.I : num 31.9 31.9 32 32 32 ...
$ iShares..U.0080..High.Yield.Corp.Bond.EUR.Dist: num 107 107 106 106 106 ...
$ iShares.JP.Morgan...EM.Bond.USD.Dist : num 104 104 105 104 104 ...
$ iShares.MSCI.Europe.Dist : num 22.6 22.5 22.6 22.5 22.5 ...
$ iShares.STOXX.Europe.600..DE. : num 36.1 36 36.1 36 36 ...
$ iShares.EURO.STOXX.50.Dist : num 33.1 33.1 33.2 33.2 33.2 ...
$ iShares.MSCI.World.USD.Dist : num 35.5 35.5 35.9 35.8 35.7 ...
$ iShares.Edge.MSCI.USA.Size.Factor : num 5.28 5.27 5.3 5.29 4.32 4.32 4.31 4.32 4.33 4.28 ...
$ ETFS.Physical.Gold : num 106 106 106 105 104 ...
$ iShares.iBonds.Mar.2020.Term.Corp.exFncl : num 24.6 24.6 24.5 24.5 24.5 ...
$ iShares.Euro.Corporate.Bond.Large.Cap : num 135 135 135 135 135 ...
$ db.x.trackers.Euro.Stoxx.50..DR..1D : num 34.8 34.6 34.7 34.7 34.6 ...
$ db.x.trackers.Euro.Stoxx.50..DR..1C : num 44.1 44 44.1 44.1 44 ...
$ Xetra.Gold : num 35.3 35.5 35.3 35 34.9 ...`
代码:
library(readr)
werte <- read_delim("~/Uni Frankfurt/Semester 7/Bachelorarbeit/R/werte.csv", ";", escape_double = FALSE, trim_ws = TRUE)
library(readr)
werte1 <- read_delim("~/Uni Frankfurt/Semester 7/Bachelorarbeit/R/werte1.csv", ";", escape_double = FALSE, trim_ws = TRUE)
write.table(x=werte, file = "werte.dat", sep = ";", dec = ",", row.names = FALSE, col.names = TRUE)
ETFs <- read.table("werte.dat", sep = ";", dec = ",", header = TRUE)
ETFs
write.table(x=werte1, file = "werte1.dat", sep = ";", dec = ".", row.names = FALSE, col.names = TRUE)
Benchmark <- read.table("werte1.dat", sep = ";", dec = ".", header = TRUE)
Benchmark
dailyreturn_ETFs<- array()
str(ETFs)
for(c in 2:ncol(ETFs))
{
currColName <- colnames(ETFs)[c];
for(r in nrow(ETFs[c])-1:1)
{
dailyreturn_ETFs[r,c] <- (ETFs[r,currColName]/ETFs[(r+1),currColName])-1
}
}
我非常感谢每一个帮助。如果您需要其他信息,请轻松解决问题。