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 pathrandomForestAnalysis.R
More file actions
41 lines (34 loc) · 1.43 KB
/
randomForestAnalysis.R
File metadata and controls
41 lines (34 loc) · 1.43 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
randomForest_models <- emptySectorList
for (i in 1:length(randomForest_models)) {
train_mat <- sectorPrices[[i]][["Training Set"]]
randomForest_models[[i]] <- randomForest(as.formula(form),
data = train_mat,
ntree = 500,
na.action = na.omit)
}
names(randomForest_models) <- sectorNames
predictedValuesRF <- emptySectorList
predictedValuesRFMat <- matrix(ncol = length(predictedValuesRF),nrow = nrow(predictedValuesLinearMat))
for (i in 1:length(predictedValuesARIMA)) {
predict_mat <- sectorPrices[[i]][["Testing Set"]]
preds <- predict(randomForest_models[[i]],newdata = predict_mat)
predictedValuesRF[[i]] <- preds
predictedValuesRFMat[,i] <- preds
}
names(predictedValuesRF) <- colnames(predictedValuesRFMat) <- sectorNames
forestError <- sapply(1:ncol(predictedValuesRFMat), FUN=function(i) {
sum(log(predictedValuesRFMat[,i] / sectorPrices[[i]][["Testing Set"]][, 2]) ^ 2)
})
for (i in 1:length(forestError)) {
print(paste(sectorNames[i], " Total Error: ", round(forestError[i], 4), sep = ''))
}
names(forestError) <- sectorNames
size <- size + 1
if (size == 1) {
accuracyMatrix[1,] <- forestError
rownames(accuracyMatrix) <- "Random Forest"
} else {
accuracyMatrix <- rbind(accuracyMatrix, forestError)
rownames(accuracyMatrix)[size] <- "Random Forest"
}
print("Random Forest Analysis: Done!")