Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Created February 25, 2013 04:44
Show Gist options
  • Save MonkmanMH/5027793 to your computer and use it in GitHub Desktop.
Save MonkmanMH/5027793 to your computer and use it in GitHub Desktop.
MLB team history - runs allowed
# INDIVIDUAL TEAM HISTORY - RUNS ALLOWED
#
# select the team you want from the franchID variable in the Teams.merge data frame
# and create a new data frame called "Team1"
# note the use of double "=" to define the team!
Team1 <- as.data.frame(subset (Teams.merge, franchID == "SEA"))
#
# create what will be the chart title from the contents of Team1
# note that teams sprang into existence in different years, thus the requirement to define both the start and end dates
firstyear <- Team1$yearID[1]
lastyear <- tail(Team1$yearID, 1)
team.name <- Team1$name[1]
# alternate way to define "team.name"
team.name <- tail(Team1$name, 1)
#
title.text <- paste(team.name, firstyear, "-", lastyear,
": Runs allowed relative to the league average")
#
#
# TREND LINE -- using loess modeling
# references:
# http://princeofslides.blogspot.ca/2011/05/sab-r-metrics-basics-of-loess.html
# http://research.stowers-institute.org/efg/R/Statistics/loess.htm
#
# create new object Runs.LO for loess model
RunAIndex.LO <- loess(Team1$RA_index ~ Team1$yearID)
RunAIndex.LO.predict <- predict(RunAIndex.LO)
#
# plot the data, add loess curve
ylim <- c(60,140)
plot(Team1$RA_index ~ Team1$yearID,
ylim = ylim,
main = title.text,
xlab = "year", ylab = "index (league average = 100)")
# chart tidying
grid()
# loess predicted value line
lines(Team1$yearID, RunAIndex.LO.predict,
lty="solid", col="red", lwd=2)
#
# MODEL VERSION 2 (with more nuance/zig-zag to the trend line)
# create new object RunAIndex.LO for loess model, span=0.25
RunAIndex.LO.25 <- loess(Team1$RA_index ~ Team1$yearID, span=0.25)
RunAIndex.LO.25.predict <- predict(RunAIndex.LO.25)
#
RunAIndex.LO.5 <- loess(Team1$RA_index ~ Team1$yearID, span=0.5)
RunAIndex.LO.5.predict <- predict(RunAIndex.LO.5)
#
# plot the data, add loess curve
ylim <- c(60,140)
plot(Team1$RA_index ~ Team1$yearID,
ylim = ylim,
main = title.text,
xlab = "Year", ylab = "Index (league average = 100)")
# loess predicted value line
lines(Team1$yearID, RunAIndex.LO.predict, lty="solid", col="black", lwd=1.5)
lines(Team1$yearID, RunAIndex.LO.25.predict, lty="dashed", col="red", lwd=2)
lines(Team1$yearID, RunAIndex.LO.5.predict, lty="dotdash", col="blue", lwd=1.5)
# chart legend and grid lines
legend(firstyear+2, 140,
c("default", "span=0.25", "span=0.50"),
lty=c("solid", "dashed", "dotdash"),
col=c("black", "red", "blue"),
lwd=c(1.5, 2, 1.5))
grid()
# straight line at 100
abline(h = 100, lty="dotdash")
#
#
write.csv(Team1, file="Team1.csv")
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment