Skip to content

Instantly share code, notes, and snippets.

@hitch17
Created January 24, 2012 21:04
Show Gist options
  • Save hitch17/1672689 to your computer and use it in GitHub Desktop.
Save hitch17/1672689 to your computer and use it in GitHub Desktop.
Computes mean, standard deviation, win percentage and pythagorean expectation from results of fantasy sports matchups, and graphs them.
library(reshape)
matchups <- read.csv('league-matchups.csv') # dont forget to set working directory or change the full path
league.sd <- sd(matchups$team_actual_points)
league.mean <- mean(matchups$team_actual_points)
weeks <- max(matchups$week)
team.melted <- melt(matchups, id.vars="place", measure.vars="team_actual_points")
places <- cast(team.melted, place ~ variable, fun.aggregate=mean)$place
team_mean <- cast(team.melted, place ~ variable, fun.aggregate=mean)$team_actual_points
team_sd <- cast(team.melted, place ~ variable, fun.aggregate=sd)$team_actual_points
opp.melted = melt(matchups, id.vars="place", measure.vars="opp_actual_points")
opp_mean <- cast(opp.melted, place ~ variable, fun.aggregate=mean)$opp_actual_points
opp_sd <- cast(opp.melted, place ~ variable, fun.aggregate=sd)$opp_actual_points
standings.melted <- melt(matchups, id.vars="place", measure.vars="win")
wins <- cast(standings.melted, place ~ variable, fun.aggregate=sum)$win
losses <- weeks - wins
winpct <- wins / (wins + losses)
pyth_opp <- 1 / (1 + ((opp_mean / team_mean) ^ 2))
results <- as.data.frame(cbind(places, team_mean, team_sd, opp_mean, opp_sd, wins, losses, winpct, pyth_opp))
colnames(results) <- cbind("place", "team_mean", "team_sd", "opp_mean", "opp_sd", "wins", "losses", "winpct", "pyth_opp")
print(results)
plot(results$team_sd, results$team_mean, xlab="Std.Dev. (Risk)", ylab="Actual Points", main="Actual Points vs Risk")
abline(h=league.mean, v=league.sd, col="gray", lty="dashed")
points(league.sd, league.mean, col="red")
text(league.sd, league.mean, "League", pos=4, col="red")
text(results$team_sd, results$team_mean, results$place, pos=4)
plot(results$pyth_opp, results$winpct, xlab="Pythagorean Expectation", ylab="Actual Winning Pct", main="Win Percentage (Actual vs Estimated)")
text(results$pyth_opp, results$winpct, results$place, pos=4)
abline(coef(lm(winpct ~ pyth_opp, results)), col='red', lty="dashed")
week team_id team_actual_points team_expected_points opp_id opp_actual_points opp_projected_points win place
1 1 157.33 141.7 8 129.58 113.46 1 3
1 8 129.58 113.46 1 157.33 141.7 0 9
1 2 126.7 134.49 5 168.69 141.57 0 2
1 5 168.69 141.57 2 126.7 134.49 1 10
1 3 187.93 148.87 9 151.22 123.89 1 1
1 9 151.22 123.89 3 187.93 148.87 0 7
1 4 138.12 126.23 10 136.43 102.54 1 4
1 10 136.43 102.54 4 138.12 126.23 0 8
1 6 116.59 119.39 7 159.32 144.37 0 6
1 7 159.32 144.37 6 116.59 119.39 1 5
2 1 169.87 142.63 5 107.97 156.81 1 3
2 5 107.97 156.81 1 169.87 142.63 0 10
2 2 187.72 137.29 9 159.04 143.6 1 2
2 9 159.04 143.6 2 187.72 137.29 0 7
2 3 154.1 156.66 7 180.35 144.85 0 1
2 7 180.35 144.85 3 154.1 156.66 1 5
2 4 176.77 142.5 8 168.98 138.45 1 4
2 8 168.98 138.45 4 176.77 142.5 0 9
2 6 126.66 146.01 10 122.54 132.18 1 6
2 10 122.54 132.18 6 126.66 146.01 0 8
3 1 147.52 150.39 2 156.68 146.64 0 3
3 2 156.68 146.64 1 147.52 150.39 1 2
3 3 188.65 149.72 4 144.08 155.56 1 1
3 4 144.08 155.56 3 188.65 149.72 0 4
3 5 131.28 129.99 6 146.12 130.31 0 10
3 6 146.12 130.31 5 131.28 129.99 1 6
3 7 114.66 138.72 10 90.73 116.63 1 5
3 10 90.73 116.63 7 114.66 138.72 0 8
3 8 123.02 137.63 9 170.41 151.44 0 9
3 9 170.41 151.44 8 123.02 137.63 1 7
4 1 181.44 157.66 3 189.42 157.75 0 3
4 3 189.42 157.75 1 181.44 157.66 1 1
4 2 144.64 134.09 10 181.99 135.87 0 2
4 10 181.99 135.87 2 144.64 134.09 1 8
4 4 160.01 144.32 9 142.15 146.32 1 4
4 9 142.15 146.32 4 160.01 144.32 0 7
4 5 133.32 132.57 7 121.85 114.59 1 10
4 7 121.85 114.59 5 133.32 132.57 0 5
4 6 139.49 138.97 8 142.66 133.64 0 6
4 8 142.66 133.64 6 139.49 138.97 1 9
5 1 124.63 141.89 4 135.95 134.73 0 3
5 4 135.95 134.73 1 124.63 141.89 1 4
5 2 135.33 130.71 3 187.33 155.44 0 2
5 3 187.33 155.44 2 135.33 130.71 1 1
5 5 106.74 123.63 10 171.87 144.79 0 10
5 10 171.87 144.79 5 106.74 123.63 1 8
5 6 136.95 142.83 9 130.54 150.23 1 6
5 9 130.54 150.23 6 136.95 142.83 0 7
5 7 123.82 123.64 8 94.1 122.98 1 5
5 8 94.1 122.98 7 123.82 123.64 0 9
6 1 147.11 143.7 9 153.76 155.81 0 3
6 9 153.76 155.81 1 147.11 143.7 1 7
6 2 155.13 147.99 7 119.34 136.68 1 2
6 7 119.34 136.68 2 155.13 147.99 0 5
6 3 163.92 157.45 10 119.92 144.86 1 1
6 10 119.92 144.86 3 163.92 157.45 0 8
6 4 110.37 120.06 6 137.84 120.24 0 4
6 6 137.84 120.24 4 110.37 120.06 1 6
6 5 107.58 137.98 8 135.63 133.54 0 10
6 8 135.63 133.54 5 107.58 137.98 1 9
7 1 128.47 132.96 10 154.93 131.28 0 3
7 10 154.93 131.28 1 128.47 132.96 1 8
7 2 141.94 145.21 8 98.01 127.46 1 2
7 8 98.01 127.46 2 141.94 145.21 0 9
7 3 150.5 135.63 6 132.69 130.11 1 1
7 6 132.69 130.11 3 150.5 135.63 0 6
7 4 79.12 138.17 7 135.06 137.61 0 4
7 7 135.06 137.61 4 79.12 138.17 1 5
7 5 83.27 126.55 9 151.15 156.6 0 10
7 9 151.15 156.6 5 83.27 126.55 1 7
8 1 144.07 150.06 8 120.59 117.42 1 3
8 8 120.59 117.42 1 144.07 150.06 0 9
8 2 163.94 166.14 5 88.36 102.82 1 2
8 5 88.36 102.82 2 163.94 166.14 0 10
8 3 123.77 149.66 10 113.35 107.34 1 1
8 10 113.35 107.34 3 123.77 149.66 0 8
8 4 186.05 152.93 6 139.4 139.71 1 4
8 6 139.4 139.71 4 186.05 152.93 0 6
8 7 125.13 127.32 9 123.58 141.27 1 5
8 9 123.58 141.27 7 125.13 127.32 0 7
9 1 163.12 164.27 7 164.67 130.87 0 3
9 7 164.67 130.87 1 163.12 164.27 1 5
9 2 155.2 140.87 6 135.97 126.24 1 2
9 6 135.97 126.24 2 155.2 140.87 0 6
9 3 182.08 161.4 8 144.48 113.58 1 1
9 8 144.48 113.58 3 182.08 161.4 0 9
9 4 166.06 127.68 5 124.2 126.69 1 4
9 5 124.2 126.69 4 166.06 127.68 0 10
9 9 113.84 148.7 10 138.76 134.27 0 7
9 10 138.76 134.27 9 113.84 148.7 1 8
10 1 120.25 159.22 6 134.49 125.48 0 3
10 6 134.49 125.48 1 120.25 159.22 1 6
10 2 118.92 151.74 4 138.18 139.92 0 2
10 4 138.18 139.92 2 118.92 151.74 1 4
10 3 182.45 158.33 5 87.08 111.09 1 1
10 5 87.08 111.09 3 182.45 158.33 0 10
10 7 106.04 145.29 9 124 143.23 0 5
10 9 124 143.23 7 106.04 145.29 1 7
10 8 144.2 140.19 10 139.78 137.81 1 9
10 10 139.78 137.81 8 144.2 140.19 0 8
11 1 167.78 136.78 5 142.16 116.77 1 3
11 5 142.16 116.77 1 167.78 136.78 0 10
11 2 143.88 140.19 9 103.83 118.38 1 2
11 9 103.83 118.38 2 143.88 140.19 0 7
11 3 114.66 167.43 7 146.51 137 0 1
11 7 146.51 137 3 114.66 167.43 1 5
11 4 145.35 124.91 10 126.93 130.77 1 4
11 10 126.93 130.77 4 145.35 124.91 0 8
11 6 116.43 119.08 8 121.53 134.13 0 6
11 8 121.53 134.13 6 116.43 119.08 1 9
12 1 187.68 160.14 2 144.38 136.85 1 3
12 2 144.38 136.85 1 187.68 160.14 0 2
12 3 164.22 156.43 6 131.73 119.23 1 1
12 6 131.73 119.23 3 164.22 156.43 0 6
12 4 130.39 139.7 5 130.73 130.42 0 4
12 5 130.73 130.42 4 130.39 139.7 1 10
12 7 117.27 140.79 10 165.15 133.37 0 5
12 10 165.15 133.37 7 117.27 140.79 1 8
12 8 118.44 124.06 9 184.7 149.73 0 9
12 9 184.7 149.73 8 118.44 124.06 1 7
13 1 160.88 158.33 9 160.01 147.85 1 3
13 9 160.01 147.85 1 160.88 158.33 0 7
13 2 197.49 148.27 3 154.59 164.04 1 2
13 3 154.59 164.04 2 197.49 148.27 0 1
13 4 189.44 146.05 7 138.8 136.58 1 4
13 7 138.8 136.58 4 189.44 146.05 0 5
13 5 103.85 115.86 8 112.65 111.68 0 10
13 8 112.65 111.68 5 103.85 115.86 1 9
13 6 126.4 135.15 10 106.17 136.94 1 6
13 10 106.17 136.94 6 126.4 135.15 0 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment