Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (46 sloc) 2.03 KB

Implementing the code for Baltimore

Jaiwei's code

     library(XML)
htmlRaven=htmlTreeParse("http://espn.go.com/nfl/team/_/name/bal/baltimore-ravens",useInternalNodes=TRUE)
 
## Choose the list items with class='score' ##
Score=xpathSApply(htmlRaven,"//li[@class='score']",xmlValue)
 
## Choose the list items with class='team-name' ##
TeamName=xpathSApply(htmlRaven,"//li[@class='team-name']",xmlValue)
 
## Choose the whole unordered list with class='score' ##
WinLoss=xpathSApply(htmlRaven,"//ul[@class='game-schedule']//span",xmlValue)
 
## This is tricky ##
## First get all the //td items under the rows(//tr) whose @class contains 'row' ##
## Choose those which contains ',', which are the actual dates ##
## There is no class or id under the date component. I had to do this. ##
Date=grep(',',xpathSApply(htmlRaven,"//tr[contains(@class,'row')]//td",xmlValue),value=TRUE)
 
## Combine the data ##
RavenData=data.frame(date=Date,team=TeamName,status=WinLoss,score=Score)
 
## Hack the scores a bit
firstScore <- as.numeric(sapply(as.character(RavenData$score),function(x){strsplit(x,"-")[[1]][1]}))
secondScore <- sapply(as.character(RavenData$score),function(x){strsplit(x,"-")[[1]][2]})
secondScore <- sapply(secondScore,function(x){strsplit(x," ")[[1]][1]})
ravenScore <- as.numeric(ifelse(RavenData$status=="W",firstScore,secondScore))
opponentScore <- as.numeric(ifelse(RavenData$status=="L",firstScore,secondScore))

Create the design matrix - fit the model

X <- model.matrix(~ RavenData$status=="W")
y <- ravenScore
betaHat <- solve(t(X)%*%X)%*%t(X)%*%y
betaHat
lm1 <- lm(ravenScore ~ RavenData$status=="W")
lm1$coeff

Calculate the sandwich

library(sandwich)
D <- (y - X %*% betaHat) %*% t(y - X %*% betaHat)
sHat0 <- solve(t(X)%*%X) %*% t(X)%*%D %*% X %*% solve(t(X) %*% X)
sHat0

D <- diag(as.vector((y - X %*% betaHat)^2))
sHat <- solve(t(X)%*%X) %*% t(X)%*%D %*% X %*% solve(t(X) %*% X)
sHat
vcovHC(lm1,type="HC0")