# Are MLB Games Getting Longer?

**The Log Cabin » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

On July 29, 2010, I had a flight from Denver to Cincinnati. About an hour before boarding, I went to ESPN’s website and found a new article by Bill Simmons, a.k.a The Sports Guy (@sportsguy33 on Twitter). The basic premise of this article is that a core group of fans is losing interest in Red Sox games this season. So he decides to assign percentages to his reasons why people are losing interest (he’s a writer, not a statistician). Anyway, he states that the “biggie”, the “killer”, etc. is the time of games (his 55%), *i.e.*, baseball games are too damn long. He gives some data from baseball-reference.com to back up his claim.

So what does this have to do with me flying to Cincinnati from Denver? Well, being the nerd that I am, I immediately went to baseball-reference.com to see if I could download more data! As an aside, I’ve been obsessing over learning how to use ggplot2 since my return from the useR2010 conference about two weeks ago. This seems like a good time to start learning. Ah shit…it appears that this project is going to be a little harder than I expected. I could download the data for one team and one season before I boarded, but I wanted the past 30 seasons for all teams that played every season. I suppose that I would have to write a scraper to collect the data from about 750 web pages. Sweet, now I have something to do on the flight rather than obsess over whether or not the person next to me will spill over into my seat.

Now I’m on the plane. I’ve downloaded the html of one webpage so that I can test my python scraper (using BeautifulSoup) and I have the data that Simmons used in his article. The first thing that I do is make a few graphs using his data. Here is the first.

He essentially discretized the length of each game into five bins: A – less than or equal to two hours, B – more than two but less than 2 hours and 30 min, and so on. It certainly looks like the relative size of the blue and purple rectangles together is increasing and the golden rectangle seems to be decreasing. Maybe Simmons is onto something. Note that this isn’t the only figure that I made. There were quite a few others, but I want to get to the good stuff.

(Back onboard the plane. I have two or three terminals open, Textmate with some python code open, R is open, and the dude sitting next to me is sufficiently freaked out by what’s going on.)

OK, this is getting too long. It turns out that I didn’t finish the scraper on the flight to Cincinnati. Fortunately there is the return flight and a few ‘down’ hours to work on this project while visiting KY. Success! Upon landing at DIA on Monday, I had the scraper (for the test page that I downloaded on Friday) working. Now I needed to write a script to iterate across all teams and every year since 1970. I wrote the script, processed the ‘soup’ for all 700+ team/season combos and I give you the following figure!

So are baseball games getting longer? Well, the preceding figure gives the median length of games (in minutes) for all teams over each season since 1970. It looks like it is going from blue to red, suggesting an increase in the median length of games. However, you might also notice that I’ve added two vertical lines from 1994 through 2004. This roughly corresponds to the “Steroids Era” in MLB. It looks like that the game times have been decreasing a bit since the middle of this era or so. Can we look at the data in another way? Of course, I give you Figure 3!

Here’s a different way of displaying the same data as given in the second figure. I didn’t separate by team in this figure, however. I’m just looking at the median length of games across all teams for each season and added a smooth curve to show a trend to the data. Note that the peak on the curve corresponds to roughly 1998. If you recall, Mark McGuire and Sammy Sosa were hitting HRs at record rates and teams were scoring a lot of runs. And it looked as if their heads might suddenly explode from all of the ‘roids that they were taking.

So what do I take from this? Well, overall, I would say that they length of baseball games in general seems to be on the decline in the most recent years. Is the same true for just the Red Sox? Ah ha, I can make that figure. Here you go.

Interestingly enough, it looks like the smooth trend line for Red Sox games is increasing in recent years whereas it’s decreasing for all other teams. So maybe this Simmons character is onto something for his beloved Red Sox.

What do I think? I don’t really care. I just wanted an interesting data set to use so that I can learn a bit more about ggplot2, learn a bit more about scraping data using python/beautifulsoup, and kill some time on a flight. So that’s my story.

Note that all of the data was obtained from baseball-reference.com. Figures were made using the ggplot2 package in the R software. Further, I know that I could do more with this data set from a statistical perspective. That’s what I do, I’m a statistician. But I have a full-time job and just wanted to learn some stuff!

Some final thoughts. I’m happy to share the r and/or python code for this project. Just send me a message on twitter or gmail (twitter name @ gmail) and I’ll send it your way. Also, you’ll notice some missing data for the Red Sox, NYY, LAD, and CHC. Why? I’m not sure. Apparently the layout of the html code is a bit different than the rest of the pages and the scraper was returning missing values. I should also say that I scraped the appropriate pages when teams switched cities. For example, I was scraping Montreal prior to 2005 for the Washington Nationals data.

Ryan

@rtelmore on Twitter!

Here is the R code:

library(ggplot2) ## His Data sim.dat <- read.table("baseball_rs_game_times.txt",header=T) ggplot(data = sim.dat, aes(x=Year, y=Count, fill=Cat)) + geom_bar(width=1,stat='identity') + scale_fill_discrete(name="length category") + scale_x_continuous("year") + scale_y_continuous("games") ggplot(data = sim.dat, aes(x=as.factor(Year), y=Count, fill=Cat)) + geom_bar(stat='identity') + scale_fill_discrete(name="length",breaks=unique(sim.dat$Cat), labels=c("(,2]","(2,2.30]","(2.30,3]","(3,4]","(4,)")) + scale_x_discrete("year") + scale_y_continuous("games") ggsave("~/Sports/Simmons/mlb_length_1.png",hei=7,wid=7) ggplot(sim.dat, aes(Cat, y=Count, col=Count, fill=Count)) + geom_bar() + facet_wrap(~ Year) last_plot() + scale_y_continuous("number of games") + scale_colour_continuous("games") + scale_fill_continuous("games") + scale_x_discrete("length of games") ggsave("~/Sports/Simmons/mlb_length_2.png",hei=7,wid=7) ## My analysis rs.dat <- read.csv("~/Sports/Simmons/length_of_baseball_games_20100802.csv", header=F) names(rs.dat) <- c("team","year","mean_len","med_len","std_len","league","TNAT") ggplot(data=rs.dat, aes(x = year, y = team, fill = med_len)) + geom_tile() + scale_fill_continuous("minutes") #+ opts(title = "Median Length of MLB Games by Team (in minutes)") last_plot() + geom_vline(x=c(1993,2004),lty=3) ggsave("~/Sports/Simmons/mlb_length_3.png",hei=7,wid=7) rs.dat$bs <- rs.dat$team=='BOS' qplot(x=year, y=med_len, data=rs.dat, geom = c("point","smooth"), span = .5, colour=bs, ylab="length of game in minutes") last_plot() + scale_colour_discrete(name="Boston?") last_plot() + geom_vline(x=c(1993,2004),lty=2,col="black") ggsave("~/Sports/Simmons/mlb_length_5.png",hei=7,wid=7) qplot(x=year, y=med_len, data=rs.dat, geom = c("point","smooth"), span = .5, ylab="length of game in minutes") last_plot() + geom_vline(x=c(1993,2004),lty=2,col="red") ggsave("~/Sports/Simmons/mlb_length_4.png",hei=7,wid=7)

**leave a comment**for the author, please follow the link and comment on their blog:

**The Log Cabin » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.