-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.R
77 lines (66 loc) · 3.35 KB
/
utilities.R
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
73
74
75
76
77
simAndWriteJSON <- function(outputSuffix,
times = 10000,
startRound = -1,
outputDir = 'output') {
library(jsonlite)
source('tourney_simulation.R')
sim <- simMultipleTourneys(times = times, startRound = startRound)
write(toJSON(sim$kaggleResults),
file = paste(outputDir, '/kaggle-', outputSuffix, '.json', sep = ''))
write(toJSON(sim$teamResults),
file = paste(outputDir, '/teams-', outputSuffix, '.json', sep = ''))
sim
}
writeFinalResults <- function(outputSuffix,
outputDir = 'output',
results = getResults(),
preds = getAllSubmissions(file = 'data/allPredictions.csv'),
teamNames = getAllTourneyTeams()) {
library(jsonlite)
source('tourney_simulation.R')
teams <- names(teamNames)
teamResults <- getRoundsByTeam(results, teams)
entryResults <- getScores(preds, results)
entryResultsDF <- data.frame(entryResults)
names(teamResults) <- teamNames[as.character(teams)]
Round.1 <- sapply(teamResults, function(x) sum(x >= 1))
Round.2 <- sapply(teamResults, function(x) sum(x >= 2))
Sweet.16 <- sapply(teamResults, function(x) sum(x >= 3))
Elite.8 <- sapply(teamResults, function(x) sum(x >= 4))
Final.4 <- sapply(teamResults, function(x) sum(x >= 5))
Championship <- sapply(teamResults, function(x) sum(x >= 6))
Win <- sapply(teamResults, function(x) sum(x >= 7))
teamResults <- data.frame(Round.1, Round.2, Sweet.16, Elite.8,
Final.4, Championship, Win)
teamResults <- teamResults[order(-teamResults$Win,
-teamResults$Championship,
-teamResults$Final.4,
-teamResults$Elite.8,
-teamResults$Sweet.16,
-teamResults$Round.2,
-teamResults$Round.1),]
kaggleRanks <- rank(entryResults, ties.method = 'min')
score.mean <- sapply(entryResults, mean)
score.best <- sapply(entryResults, min)
score.worst <- sapply(entryResults, max)
rank.mean <- sapply(kaggleRanks, mean)
rank.best <- sapply(kaggleRanks, min)
rank.worst <- sapply(kaggleRanks, max)
rank.1 <- sapply(kaggleRanks, function(x) sum(x == 1))
rank.2 <- sapply(kaggleRanks, function(x) sum(x == 2))
rank.3 <- sapply(kaggleRanks, function(x) sum(x == 3))
rank.4 <- sapply(kaggleRanks, function(x) sum(x == 4))
rank.5 <- sapply(kaggleRanks, function(x) sum(x == 5))
exp.winnings <- rank.1 * 10000 + rank.2 * 6000 + rank.3 * 4000 +
rank.4 * 3000 + rank.5 * 2000
kaggle <- data.frame(score.mean, score.best, score.worst, rank.mean,
rank.best, rank.worst, rank.1, rank.2, rank.3,
rank.4, rank.5, exp.winnings)
kaggle <- kaggle[order(-kaggle$exp.winnings, kaggle$rank.mean),]
write(toJSON(kaggle),
file = paste(outputDir, '/kaggle-', outputSuffix, '.json', sep = ''))
write(toJSON(teamResults),
file = paste(outputDir, '/teams-', outputSuffix, '.json', sep = ''))
list(teamResults = teamResults,
kaggleResults = kaggle)
}