This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharimaAnalysis.R
More file actions
72 lines (64 loc) · 2.79 KB
/
arimaAnalysis.R
File metadata and controls
72 lines (64 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
arima_models <- emptySectorList
regMatrices = vector("list", length = length(arima_models))
for (i in 1:length(arima_models)) {
x <- sectorPrices[[i]][["Training Set"]]
regMatrix <- matrix(ncol = ncol(x)-2,nrow = nrow(x))
colN <- NULL
for (j in 3:ncol(x)) {
if (colnames(x)[j] == "EPSGrowthRate" || colnames(x)[j] == "PEG") {
next()
} else {
regMatrix[,j-2] <- sectorPrices[[i]][["Training Set"]][,j]
colN <- c(colN,colnames(x)[j])
}
}
regMatrix <- regMatrix[,-c(5,6)] # Remove the column where PEG and EPS Growth Rate was skipped over
colnames(regMatrix) <- colN
regMatrix <- regMatrix[,-which(sapply(as.data.frame(regMatrix), function(x){all(x==0 | is.na(x))}))] #remove cols with all zeroes
regMatrices[[i]] = regMatrix
arima_models[[i]] <- auto.arima(x[,2],xreg = regMatrix)
}
names(arima_models) <- sectorNames
coeffMatARIMA <- as.data.frame(matrix(nrow = ncol(regMatrix),ncol = length(sectorNames)))
colnames(coeffMatARIMA) <- sectorNames
rownames(coeffMatARIMA) <- colnames(regMatrix)
for (i in names(arima_models)) {
coeffMatARIMA[,i] = eval(parse(text = paste("arima_models$",i,"$coef[rownames(coeffMatARIMA)]",sep = '')))
}
plotHeatMap3 = function(df) {
tmp <- cbind(rownames(df),df)
tmp <- melt(tmp)
colnames(tmp) <- c("Factors", "Sectors", "Value")
coeffHeatGGPlot2 <<- 'ggplot(tmp, aes(x = Sectors, y = Factors, fill = Value)) + geom_tile() +
scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
labs(title = "Coeff Heatmap, ARIMA",caption = "Rounded to 4 Decimal Places") +
geom_text(aes(label = round(Value,digits = 4)))'
eval(parse(text = coeffHeatGGPlot2))
}
plotHeatMap3(coeffMatARIMA)
predictedValuesARIMA <- emptySectorList
predictedValuesARIMAMat <- as.data.frame(matrix(nrow = nrow(predictedValuesLinearMat),ncol = length(arima_models)))
for (i in 1:length(arima_models)) {
sectDat <- sectorPrices[[i]][["Testing Set"]]
preds <- forecast(arima_models[[i]],xreg = as.matrix(sectDat[,which(colnames(sectDat) %in% colnames(regMatrices[[i]]))]))
preds <- preds$mean
predictedValuesARIMA[[i]] <- preds
predictedValuesARIMAMat[,i] <- preds
}
colnames(predictedValuesARIMAMat) <- names(predictedValuesARIMA) <- sectorNames
arimaError <- sapply(1:ncol(predictedValuesARIMAMat), FUN=function(i) {
sum(log(c(predictedValuesARIMAMat[,i]) / sectorPrices[[i]][["Testing Set"]][, 2]) ^ 2)
})
for (i in 1:length(arimaError)) {
print(paste(sectorNames[i], " Total Error: ", round(arimaError[i], 4), sep = ''))
}
names(arimaError) <- sectorNames
size <- size + 1
if (size == 1) {
accuracyMatrix[1,] <- arimaError
rownames(accuracyMatrix) <- "ARIMA Regression"
} else {
accuracyMatrix <- rbind(accuracyMatrix, arimaError)
rownames(accuracyMatrix)[size] <- "ARIMA Regression"
}
print("ARIMA Analysis: Done!")