Skip to content

@MonkmanMH /gist:4969818
Last active

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
MLB team runs per game
# discussion and output can be found at
# Select the team you want from the franchID variable in the Teams.merge data frame
# and create a new data frame called "Team1"
# For this exercise we will use the Seattle Mariners, which are coded as SEA
# Note the use of double "=" to define the team!
Team1 <- (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) <- Team1$name[1]
# alternate way to define "" <- tail(Team1$name, 1)
# Combine all of the component into a single variable, "title.text"
# "paste" works like "concatenate" in other languages
title.text <- paste(, firstyear, "-", lastyear,
": Runs scored relative to the league average")
# TREND LINE -- using LOESS modeling
# references:
# Model Version 1, using default settings
# Creates a new object Runs.LO for loess model
RunIndex.LO <- loess(Team1$R_index ~ Team1$yearID)
RunIndex.LO.predict <- predict(RunIndex.LO)
# Model Version 2 (with more nuance/zig-zag to the trend line)
# create new object RunIndex.LO for loess model, span=0.25
RunIndex.LO.25 <- loess(Team1$R_index ~ Team1$yearID, span=0.25)
RunIndex.LO.25.predict <- predict(RunIndex.LO.25)
RunIndex.LO.5 <- loess(Team1$R_index ~ Team1$yearID, span=0.5)
RunIndex.LO.5.predict <- predict(RunIndex.LO.5)
# plot the data, add loess curve
ylim <- c(60,140)
plot(Team1$R_index ~ Team1$yearID,
ylim = ylim,
main = title.text,
xlab = "Year", ylab = "Index (league average = 100)")
# loess predicted value line
lines(Team1$yearID, RunIndex.LO.predict, lty="solid", col="black", lwd=1.5)
lines(Team1$yearID, RunIndex.LO.25.predict, lty="dashed", col="red", lwd=2)
lines(Team1$yearID, RunIndex.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))
# straight line at 100
abline(h = 100, lty="dotdash")
# And save the data fram as a csv file
write.csv(Team1, file="Team1.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.