在R中计算股票收益率

时间:2014-11-14 03:06:23

标签: r

绝对的诺布在这里伙计们,所以请耐心等待。我有以下代码从csv文件加载数据并进行一些计算。其中一项计算是获得每只股票的月度回报。但我显然做错了,因为我得到以下信息:

Error in log(mprices) : non-numeric argument to mathematical function

我正在编写我到目前为止编写的代码。非常基本的东西。

stockdata <- read.csv("NZXALL.csv", stringsAsFactors = F)
# Converting to XTS
dates <- stockdata$Date
stock.xts <- as.xts(stockdata, order.by=as.Date(dates, "%d/%m/%Y"))
data <- stock.xts
data$Date <- NULL                                      #Remove Date field
mprices <- aggregate(data, as.yearmon, tail, 1)       #Convert to monthly prices
monthly.returns <- diff(log(mprices))                 # convert prices to log returns

知道我哪里错了吗?数据集采用以下方式

Date        StockX    StockY    StockZ...
dd/mm/yyyy  price1    price1    price1

目前在2005年和2005年之间有142个股票和数据范围。 2014

谢谢!

> dput(head(stockdata))
structure(list(Date = c("5/01/2005", "6/01/2005", "7/01/2005", 
"10/01/2005", "11/01/2005", "12/01/2005"), ABA = c(1.11, 1.11, 
1.12, 1.13, 1.12, 1.12), ACY = c(3.61, 3.61, 3.7, 3.7, 3.75, 
3.75), AFI = c(3.89, 3.89, 3.9, 3.92, 3.92, 3.98), AIA = c(8.05, 
8.2, 8.1, 8.11, 8.1, 8.08), AIR = c(1.65, 1.63, 1.63, 1.63, 1.62, 
1.62), ALF = c(2.6, 2.6, 2.6, 2.6, 2.65, 2.5), AMP = c(7.75, 
7.65, 7.75, 8, 8.03, 8), ANZ = c(22.3, 22.3, 22.2, 22.5, 22.63, 
22.6), AOR = c(0.03, 0.03, 0.03, 0.03, 0.03, 0.03), APN = c(5.58, 
5.58, 5.58, 5.58, 5.58, 5.58), ARG = c(1.18, 1.18, 1.18, 1.18, 
1.18, 1.18), ASBPA = c(1.07, 1.07, 1.07, 1.05, 1.07, 1.06), ASBPB = c(1.01, 
1, 1.01, 1.01, 1.01, 1.01), ATM = c(0.08, 0.08, 0.08, 0.08, 0.08, 
0.08), ATR = c(2.05, 2.05, 2.05, 2.05, 2.05, 2.05), AWF = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), BGR = c(1.13, 
1.14, 1.14, 1.14, 1.15, 1.15), BIT = c(7.47, 7.47, 7.45, 7.45, 
7.4, 7.58), BLT = c(0.18, 0.19, 0.25, 0.2, 0.2, 0.2), CAV = c(4.55, 
4.59, 4.6, 4.55, 4.55, 4.5), CDI = c(0.32, 0.32, 0.32, 0.33, 
0.35, 0.36), CEN = c(6.37, 6.53, 6.52, 6.5, 6.44, 6.4), CMO = c(2.96, 
2.96, 2.99, 2.99, 3, 3), CVT = c(2.1, 2.1, 2.1, 2.1, 2.1, 2.1
), DPC = c(2.79, 2.8, 2.76, 2.76, 2.76, 2.69), EBO = c(4.54, 
4.52, 4.53, 4.55, 4.55, 4.55), EUT = c(12.35, 12.35, 12.35, 12.35, 
12.05, 12.05), FBU = c(6.6, 6.57, 6.5, 6.4, 6.44, 6.46), FCS = c(6.85, 
6.85, 6.85, 6.85, 6.85, 6.85), FCT = c(5.17, 5.15, 5.14, 5.12, 
5.15, 5.15), FIN = c(1, 1, 1, 1, 1, 1), FNZ = c(1.46, 1.46, 1.47, 
1.47, 1.48, 1.48), FPH = c(3.28, 3.23, 3.18, 3.24, 3.23, 3.17
), FRE = c(3.1, 3.1, 3.1, 3.1, 3.11, 3.1), GFF = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), GLL = c(0.85, 
0.84, 0.84, 0.85, 0.85, 0.85), GMT = c(1.14, 1.14, 1.15, 1.15, 
1.14, 1.15), GPG = c(2.21, 2.23, 2.22, 2.19, 2.21, 2.25), GXH = c(0.1, 
0.1, 0.1, 0.1, 0.12, 0.13), HBY = c(6.4, 6.45, 6.46, 6.5, 6.6, 
6.5), HED = c(4.2, 4.25, 4.25, 4.22, 4.22, 4.22), HLG = c(3.8, 
3.75, 3.77, 3.75, 3.73, 3.61), IFT = c(3.36, 3.45, 3.45, 3.45, 
3.42, 3.4), JFJ = c(5.3, 5.3, 5.3, 5.3, 5.3, 5.35), JMO = c(11.35, 
11.35, 11.35, 11.38, 11.4, 11.65), KFL = c(1.04, 1.04, 1.07, 
1.08, 1.08, 1.09), KIP = c(1.13, 1.12, 1.15, 1.16, 1.15, 1.12
), KRK = c(2.28, 2.28, 2.26, 2.3, 2.3, 2.3), LPC = c(1.75, 1.73, 
1.75, 1.77, 1.88, 1.86), MCK = c(0.6, 0.6, 0.6, 0.59, 0.6, 0.59
), MDZ = c(2.43, 2.47, 2.48, 2.48, 2.48, 2.45), MET = c(3.46, 
3.45, 3.45, 3.45, 3.45, 3.45), MFT = c(2.3, 2.35, 2.38, 2.34, 
2.32, 2.32), MGL = c(1.12, 1.15, 1.15, 1.15, 1.2, 1.2), MHI = c(8.22, 
8.2, 8.14, 8, 8.05, 8), MOW = c(1.62, 1.62, 1.62, 1.62, 1.62, 
1.62), MVN = c(1.65, 1.65, 1.65, 1.64, 1.64, 1.6), MZY = c(4.44, 
4.45, 4.48, 4.48, 4.5, 4.5), NPT = c(0.99, 0.98, 0.98, 0.99, 
1, 1), NPX = c(5.94, 5.95, 5.95, 5.9, 5.84, 5.9), NTH = c(2.95, 
2.95, 2.96, 2.96, 2.96, 2.99), NTL = c(0.11, 0.11, 0.11, 0.11, 
0.1, 0.1), NWF = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), NZF = c(0.4, 0.4, 0.41, 0.42, 0.42, 0.41), NZO = c(1.08, 
1.11, 1.13, 1.12, 1.11, 1.1), NZR = c(31, 30.1, 30.95, 30.55, 
30.9, 30.8), NZX = c(9.2, 9.3, 9.38, 9.35, 9.35, 9.29), OZY = c(2.3, 
2.28, 2.28, 2.28, 2.3, 2.3), PBG = c(3.45, 3.39, 3.33, 3.44, 
3.4, 3.4), PCT = c(0.91, 0.91, 0.92, 0.92, 0.91, 0.91), PEB = c(0.28, 
0.28, 0.28, 0.28, 0.28, 0.28), PFI = c(1.04, 1.05, 1.05, 1.04, 
1.04, 1.03), PGC = c(4.01, 4.1, 4.15, 4.15, 4.15, 4.02), PGW = c(2.15, 
2.18, 2.2, 2.21, 2.2, 2.18), PIL = c(0.02, 0.02, 0.02, 0.02, 
0.02, 0.02), POT = c(5.59, 5.58, 5.6, 5.6, 5.6, 5.45), PPG = c(0.84, 
0.84, 0.83, 0.84, 0.86, 0.85), PPL = c(2.76, 2.72, 2.69, 2.65, 
2.61, 2.7), PPP = c(0.14, 0.14, 0.14, 0.14, 0.15, 0.14), RBC = c(1.06, 
1.06, 1.05, 1.06, 1.06, 1.05), RBD = c(1.28, 1.28, 1.28, 1.28, 
1.28, 1.28), RNS = c(0.94, 0.99, 1.03, 1.04, 1, 1.01), RYM = c(3.65, 
3.7, 3.7, 3.7, 3.68, 3.64), SAN = c(4.55, 4.5, 4.6, 4.6, 4.55, 
4.51), SCT = c(3.25, 3.19, 3.1, 3.2, 3.21, 3.25), SCY = c(0.83, 
0.84, 0.84, 0.83, 0.83, 0.83), SEA = c(0.07, 0.07, 0.07, 0.08, 
0.07, 0.07), SEK = c(4.9, 4.9, 4.8, 4.92, 4.92, 4.92), SKC = c(5.43, 
5.48, 5.45, 5.41, 5.5, 5.22), SKL = c(1.25, 1.25, 1.25, 1.25, 
1.25, 1.25), SKT = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), SLG = c(0.39, 0.4, 0.39, 0.38, 0.39, 0.39
), SPN = c(1.29, 1.29, 1.26, 1.29, 1.29, 1.28), SPY = c(0.08, 
0.08, 0.08, 0.08, 0.08, 0.08), STU = c(4.74, 4.76, 4.85, 4.8, 
4.74, 4.72), TCL = c(5.85, 5.99, 5.99, 5.99, 6.02, 6), TEL = c(6.25, 
6.23, 6.23, 6.2, 6.23, 6.2), TEM = c(4.5, 4.5, 4.5, 4.55, 4.55, 
4.55), TEN = c(2.18, 2.18, 2.19, 2.18, 2.19, 2.18), THL = c(2.09, 
2.1, 2.09, 2.09, 2.15, 2.15), TLS = c(5.36, 5.36, 5.35, 5.4, 
5.42, 5.42), TNZ = c(1.19, 1.19, 1.2, 1.19, 1.2, 1.19), TPW = c(5.7, 
5.6, 5.61, 5.6, 5.6, 5.6), TRS = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), TTK = c(2.6, 2.6, 2.63, 2.64, 
2.55, 2.55), TUA = c(4.22, 4.15, 4.16, 4.2, 4.15, 4.15), TUR = c(3, 
3, 3, 3.1, 3.1, 3.1), TWR = c(2.14, 2.14, 2.11, 2.08, 2.08, 2.06
), VCT = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), VHP = c(0.99, 0.99, 1, 0.99, 0.99, 0.99), VIL = c(0.85, 
0.85, 0.9, 0.86, 0.85, 0.84), WBC = c(18.32, 18.32, 18.32, 18.32, 
18.32, 18.32), WDT = c(0.58, 0.53, 0.58, 0.54, 0.5, 0.5), WHS = c(3.71, 
3.77, 3.72, 3.69, 3.66, 3.63), AUG = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), BRM = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), BRM.1 = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), DGL = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), HFL = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), RAK = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), TPI = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_), AORWA = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), DIL = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MLN = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), OGC = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), OIC = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), XRO = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), CUE = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), KIPGC = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), KMD = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), DNZ = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), TIL = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), CNU = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), HNZ = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MAD = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), SUM = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), TME = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MOA = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), NTLOA = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), FSF = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), JMOOA = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("Date", 
"ABA", "ACY", "AFI", "AIA", "AIR", "ALF", "AMP", "ANZ", "AOR", 
"APN", "ARG", "ASBPA", "ASBPB", "ATM", "ATR", "AWF", "BGR", "BIT", 
"BLT", "CAV", "CDI", "CEN", "CMO", "CVT", "DPC", "EBO", "EUT", 
"FBU", "FCS", "FCT", "FIN", "FNZ", "FPH", "FRE", "GFF", "GLL", 
"GMT", "GPG", "GXH", "HBY", "HED", "HLG", "IFT", "JFJ", "JMO", 
"KFL", "KIP", "KRK", "LPC", "MCK", "MDZ", "MET", "MFT", "MGL", 
"MHI", "MOW", "MVN", "MZY", "NPT", "NPX", "NTH", "NTL", "NWF", 
"NZF", "NZO", "NZR", "NZX", "OZY", "PBG", "PCT", "PEB", "PFI", 
"PGC", "PGW", "PIL", "POT", "PPG", "PPL", "PPP", "RBC", "RBD", 
"RNS", "RYM", "SAN", "SCT", "SCY", "SEA", "SEK", "SKC", "SKL", 
"SKT", "SLG", "SPN", "SPY", "STU", "TCL", "TEL", "TEM", "TEN", 
"THL", "TLS", "TNZ", "TPW", "TRS", "TTK", "TUA", "TUR", "TWR", 
"VCT", "VHP", "VIL", "WBC", "WDT", "WHS", "AUG", "BRM", "BRM.1", 
"DGL", "HFL", "RAK", "TPI", "AORWA", "DIL", "MLN", "OGC", "OIC", 
"XRO", "CUE", "KIPGC", "KMD", "DNZ", "TIL", "CNU", "HNZ", "MAD", 
"SUM", "TME", "MOA", "NTLOA", "FSF", "JMOOA"), row.names = c(NA, 
6L), class = "data.frame")

0 个答案:

没有答案