Using R to Estimate the Pythaogrean Won-Loss Formula in Hockey

by Kevin Dayaratna

I have a paper under review at a journal where I (along with my co-author) provided theoretical verification for applying the Pythagorean Won-Loss formula to hockey. Originally devised for baseball, the Pythagorean Won-Loss formula estimates the percentage of games a team should have won at a particular point in a season. For decades, this formula had no mathematical justification from first principles. In 2006, the second author provided a statistical derivation by making some heuristic assumptions about the distributions of runs scored and allowed by baseball teams. We make a similar set of assumptions about hockey teams and show that the formula is just as applicable to hockey as it is to baseball. We conclude with a discussion of other models, and derive the linear predictor model as a first order approximation to the Pythagorean model. Our work is useful from a managerial perspective for a number of reasons as it allows one to understand what teams are over-performing and underperforming. As a result, managers could use our work as a basis for comparing teams of different eras and understanding the effects of hiring well-known coaches or superstars, as well as the expected gains resulting from mid-season signings.

I used R to estimate the Weibull densities, perform goodness of fit tests, compute the theoretical and expected Won-Loss percentages, and plot the Weibull densities versus the actual histograms of goals scored and goals allowed.

A preprint can be downloaded in its entirety here.
http://dl.dropbox.com/u/8437178/R%20Contest/HockeyPaper_contest.pdf

A few sample plots of estimated Weibull densities overlayed upon actual histograms of three NHL teams' goals scored and goals allowed are below

Here's a dropbox link to download the individual JPEG files
http://dl.dropbox.com/u/8437178/R%20Contest/Plots%20for%20Contest.zip

TEST

``````loglike<-function(p) - sum(dweibull(data[,1]+0.5,p[1],p[2],log=T +
(dweibull(data[,2]+0.5,p[1],p[3],log=T)))
results<-optim(c(1,5,6),loglike)
print(results)``````

The following code Estimate Parameters_20102011.R uses llweibull.R to estimate the Weibull parameters as well as the expected won-loss percentage for each of the 30 NHL teams over the course of the 2010-2011 season.

``````ematrix<-matrix(0,30,10)
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Anaheim Ducks.txt")
print("Anaheim Ducks")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[1,1:3]<-results\$par
ematrix[1,4]<-mean(data[,1])
ematrix[1,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[1,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Atlanta Thrashers.txt")
print("Atlanta Thrashers")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[2,1:3]<-results\$par
ematrix[2,4]<-mean(data[,1])
ematrix[2,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[2,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Boston Bruins.txt")
print("Boston Bruins")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[3,1:3]<-results\$par
ematrix[3,4]<-mean(data[,1])
ematrix[3,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[3,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Buffalo Sabres.txt")
print("Buffalo Sabres")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[4,1:3]<-results\$par
ematrix[4,4]<-mean(data[,1])
ematrix[4,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[4,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Calgary Flames.txt")
print("Calgary Flames")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[5,1:3]<-results\$par
ematrix[5,4]<-mean(data[,1])
ematrix[5,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[5,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Carolina Hurricanes.txt")
print("Carolina Hurricanes")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[6,1:3]<-results\$par
ematrix[6,4]<-mean(data[,1])
ematrix[6,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[6,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Chicago Blackhawks.txt")
print("Chicago Blackhawks")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[7,1:3]<-results\$par
ematrix[7,4]<-mean(data[,1])
ematrix[7,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[7,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Colorado Avalanche.txt")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[8,1:3]<-results\$par
ematrix[8,4]<-mean(data[,1])
ematrix[8,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[8,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Columbus Blue Jackets.txt")
print("Columbus Blue Jackets")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[9,1:3]<-results\$par
ematrix[9,4]<-mean(data[,1])
ematrix[9,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[9,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Dallas Stars.txt")
print("Dallas Stars")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[10,1:3]<-results\$par
ematrix[10,4]<-mean(data[,1])
ematrix[10,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[10,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Detroit Red Wings.txt")
print("Detroit Red Wings")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[11,1:3]<-results\$par
ematrix[11,4]<-mean(data[,1])
ematrix[11,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[11,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Edmonton Oilers.txt")
print("Edmonton Oilers")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[12,1:3]<-results\$par
ematrix[12,4]<-mean(data[,1])
ematrix[12,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[12,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Florida Panthers.txt")
print("Florida Panthers")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[13,1:3]<-results\$par
ematrix[13,4]<-mean(data[,1])
ematrix[13,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[13,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//LA Kings.txt")
print("LA Kings")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[14,1:3]<-results\$par
ematrix[14,4]<-mean(data[,1])
ematrix[14,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[14,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Minnesota Wild.txt")
print("Minnesota Wild")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[15,1:3]<-results\$par
ematrix[15,4]<-mean(data[,1])
ematrix[15,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[15,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Montreal Canadiens.txt")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[16,1:3]<-results\$par
ematrix[16,4]<-mean(data[,1])
ematrix[16,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[16,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Nashville Predators.txt")
print("Nashville Predators")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[17,1:3]<-results\$par
ematrix[17,4]<-mean(data[,1])
ematrix[17,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[17,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//New York Islanders.txt")
print("New York Islanders")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[18,1:3]<-results\$par
ematrix[18,4]<-mean(data[,1])
ematrix[18,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[18,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//New York Rangers.txt")
print("New York Rangers")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[19,1:3]<-results\$par
ematrix[19,4]<-mean(data[,1])
ematrix[19,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[19,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//NJ Devils.txt")
print("NJ Devils")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[20,1:3]<-results\$par
ematrix[20,4]<-mean(data[,1])
ematrix[20,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[20,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Ottawa Senators.txt")
print("Ottawa Senators")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[21,1:3]<-results\$par
ematrix[21,4]<-mean(data[,1])
ematrix[21,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[21,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Philadelphia Flyers.txt")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[22,1:3]<-results\$par
ematrix[22,4]<-mean(data[,1])
ematrix[22,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[22,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Phoenix Coyotes.txt")
print("Phoneix Coyotes")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[23,1:3]<-results\$par
ematrix[23,4]<-mean(data[,1])
ematrix[23,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[23,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Pittsburgh Penguins.txt")
print("Pittsburgh Penguins")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[24,1:3]<-results\$par
ematrix[24,4]<-mean(data[,1])
ematrix[24,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[24,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//San Jose Sharks.txt")
print("San Jose Sharks")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[25,1:3]<-results\$par
ematrix[25,4]<-mean(data[,1])
ematrix[25,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[25,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//St Louis Blues.txt")
print("St Louis Blues")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[26,1:3]<-results\$par
ematrix[26,4]<-mean(data[,1])
ematrix[26,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[26,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Tampa Bay Lightning.txt")
print("Tampa Bay Lightning")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[27,1:3]<-results\$par
ematrix[27,4]<-mean(data[,1])
ematrix[27,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[27,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Toronto Maple Leafs.txt")
print("Toronto Maple Leafs")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[28,1:3]<-results\$par
ematrix[28,4]<-mean(data[,1])
ematrix[28,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[28,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Vancouver Canucks.txt")
print("Vancouver Canucks")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[29,1:3]<-results\$par
ematrix[29,4]<-mean(data[,1])
ematrix[29,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[29,7]<-actualpercent
data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Washington Capitals.txt")
print("Washington Capitals")
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/llweibull.R")
ematrix[30,1:3]<-results\$par
ematrix[30,4]<-mean(data[,1])
ematrix[30,5]<-mean(data[,2])
source("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/computeWL.R")
ematrix[30,7]<-actualpercent
team<-"Anaheim Ducks"
team[1]<-"Anaheim Ducks"
team[2]<-"Atlanta Thrashers"
team[3]<-"Boston Bruins"
team[4]<-"Buffalo Sabres"
team[5]<-"Calgary Flames"
team[6]<-"Carolina Hurricanes"
team[7]<-"Chicago Blackhawks"
team[9]<-"Columbus Blue Jackets"
team[10]<-"Dallas Stars"
team[11]<-"Detroit Red Wings"
team[12]<-"Edmonton Oilers"
team[13]<-"Florida Panthers"
team[14]<-"LA Kings"
team[15]<-"Minnesota Wild"
team[17]<-"Nashville Predators"
team[18]<-"New York Islanders"
team[19]<-"New York Rangers"
team[20]<-"NJ Devils"
team[21]<-"Ottawa Senators"
team[23]<-"Phoenix Coyotes"
team[24]<-"Pittsburgh Penguins"
team[25]<-"San Jose Sharks"
team[26]<-"St Louis Blues"
team[27]<-"Tampa Bay Lightning"
team[28]<-"Toronto Maple Leafs"
team[29]<-"Vancouver Canucks"
team[30]<-"Washington Capitals"

for (i in 1:30) {ematrix[i,6]<-(ematrix[i,2]^ematrix[i,1])/((ematrix[i,2])^ematrix[i,1]+(ematrix[i,3])^ematrix[i,1])
ematrix[i,9]<-(ematrix[i,4]-mean(rweibull(1000,ematrix[i,1],ematrix[i,2])))/sd(rweibull(1000,ematrix[i,1],ematrix[i,2]))
ematrix[i,10]<-(ematrix[i,5]-mean(rweibull(1000,ematrix[i,1],ematrix[i,3])))/sd(rweibull(1000,ematrix[i,1],ematrix[i,3]))}

preteamlist<-t(team)
teamlist<-t(preteamlist)
Team<-teamlist
Gamma<-ematrix[,1]
Alpha_GS<-ematrix[,2]
Alpha_GA<-ematrix[,3]
Pythagorean_WL<-ematrix[,6]
Actual_WL<-ematrix[,7]
#print(data.frame(Team,Gamma,Alpha_GS,Alpha_GA,Pythagorean_WL,Actual_WL))
ematrix[,8]<-82*(ematrix[,7]-ematrix[,6])
GamesDiff<-ematrix[,8]
zstatGS<-ematrix[,9]
zstatGA<-ematrix[,10]

quantile_matrix_win<-matrix(0,30,11)
for (i in 1:30) { for (y in 0:10) {quantile_matrix_win[i,y+1]<-82*(pweibull(1+y, ematrix[i,1], ematrix[i,2])-pweibull(0+y, ematrix[i,1], ematrix[i,2]))}}
quantile_matrix_loss<-matrix(0,30,11)
for (i in 1:30) { for (y in 0:10) {quantile_matrix_loss[i,y+1]<-82*(pweibull(1+y, ematrix[i,1], ematrix[i,3])-pweibull(0+y, ematrix[i,1], ematrix[i,3]))}}

#print(quantile_matrix_win)
#print(quantile_matrix_loss)
chisqval<-matrix(0,11,1)
chisqval2<-matrix(0,11,1)
chisqmatrix<-matrix(0,30,4)

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Anaheim Ducks.txt")
obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[1,i])^2/(quantile_matrix_win[1,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[1,i])^2/(quantile_matrix_loss[1,i])}
chisqmatrix[1,1]<-(sum(chisqval))
chisqmatrix[1,2]<-(sum(chisqval2))
chisqmatrix[1,3]<-(dim(obswin_matrix)[1])
chisqmatrix[1,4]<-(dim(obsloss_matrix)[1])

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Atlanta Thrashers.txt")

obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[2,i])^2/(quantile_matrix_win[2,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[2,i])^2/(quantile_matrix_loss[2,i])}
chisqmatrix[2,1]<-(sum(chisqval))
chisqmatrix[2,2]<-(sum(chisqval2))
chisqmatrix[2,3]<-(dim(obswin_matrix)[1])
chisqmatrix[2,4]<-(dim(obsloss_matrix)[1])

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Boston Bruins.txt")
obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[3,i])^2/(quantile_matrix_win[3,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[3,i])^2/(quantile_matrix_loss[3,i])}
chisqmatrix[3,1]<-(sum(chisqval))
chisqmatrix[3,2]<-(sum(chisqval2))
chisqmatrix[3,3]<-(dim(obswin_matrix)[1])
chisqmatrix[3,4]<-(dim(obsloss_matrix)[1])

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Buffalo Sabres.txt")
obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[4,i])^2/(quantile_matrix_win[4,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[4,i])^2/(quantile_matrix_loss[4,i])}
chisqmatrix[4,1]<-(sum(chisqval))
chisqmatrix[4,2]<-(sum(chisqval2))
chisqmatrix[4,3]<-(dim(obswin_matrix)[1])
chisqmatrix[4,4]<-(dim(obsloss_matrix)[1])

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Calgary Flames.txt")
obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[5,i])^2/(quantile_matrix_win[5,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[5,i])^2/(quantile_matrix_loss[5,i])}
chisqmatrix[5,1]<-(sum(chisqval))
chisqmatrix[5,2]<-(sum(chisqval2))
chisqmatrix[5,3]<-(dim(obswin_matrix)[1])
chisqmatrix[5,4]<-(dim(obsloss_matrix)[1])

data<-read.table("C:/Users/Owner/Documents/Documents from old hard drives/Kevin/Hockey Data 2010-2011 Season//Carolina Hurricanes.txt")
obswin_matrix<-as.matrix(table(data[,1]))
obsloss_matrix<-as.matrix(table(data[,2]))
for (i in 1:dim(obswin_matrix)[1]) {chisqval[i]<-(obswin_matrix[i]-quantile_matrix_win[6,i])^2/(quantile_matrix_win[6,i])}
for (i in 1:dim(obsloss_matrix)[1]) {chisqval2[i]<-(obsloss_matrix[i]-quantile_matrix_loss[6,i])^2/(quantile_matrix_loss[6,i])}
chisqmatrix[6,1]<-(sum(chisqval))
chisqmatrix[6,2]<-(sum(chisqval2))
chisqmatrix[6,3]<-(dim(obswin_matrix)[1])
chisqmatrix[6,4]<-(dim(obsloss_matrix)[1])``````

The above codes were used for the 2010-2011 season. The codes in their entirety as well as similar codes for the 2008-2009 and 2009-2010 seasons can be downloaded here - http://dl.dropbox.com/u/8437178/R%20Contest/R%20Code.zip

The hockey data that the R code uses as inputs from textfiles can be downloaded here. This data was downloaded via a JavaScript from ESPN.com. - http://dl.dropbox.com/u/8437178/R%20Contest/Hockey%20Data.zip

Posted On Oct 31, 2011. Originally posted on inside-R.org.