Tuesday, September 1, 2015

Simulating a baseball game

As many of you know, I'm an avid baseball fan. Therefore, whenever I come across a topic involving Operations Research and baseball, I can't just simply ignore it. Awhile ago, I discovered this website that analyzes the strategies of a team throughout a baseball game. It's an interesting approach that uses dynamic programming to maximize the probability of winning the overall game by breaking up each inning into the various situations that are possible and then picking the optimal strategy for that situation.

My first reaction was, "Cool! I want to play with it!" But wait, it's in MATLAB?! I don't have MATLAB at home. I had a couple of options, I could try and get a copy of MATLAB (which is not very cheap) or translate to a language I had access to on my laptop. I thought about R, Python, or potentially learning Octave but eventually sided with R. Below are links to the Git that has the translated code in R. Note that this version is strictly a translation and has not been optimized for the R language.

This is really a work in progress since I'd like to build it out a bit. Here's what I see as future work I hope to accomplish on this problem:
  1. Optimize for the R language and decrease overall runtime for a particular run.
  2. Adjust for a team of different players (currently assumes the same player at all nine hitting spots). 
  3. Create a nice way to visualize the results.
  4. Link to more recent data. 
    1. Through the Lahman package.
    2. Through "live" data.
  5. Add in ability to manipulate lineup order and be able to compare pros and cons behind various strategies.
  6. Add in ability for lineup substitutions.
Links:
Github repository
Original Problem website

Sunday, December 14, 2014

DR Alumni Race Results: First attempt with Shiny

I've always touted how nice Tableau can be for producing interactive graphics quickly. Although Tableau requires having a license to work with "large" datasets or connecting outside of local text files. As far as I'm concerned, Tableau is still great for quick prototyping visualizations and has a relatively cheap license. What do you do when your data is online and Tableau isn't an option?

That's when I picked up on RStudio's Shiny. Shiny is a web application framework for being able to host your R scripts online. Well, about two nights of going through their awesome tutorial and I had myself a basic working visualization using the ggplot2. I was able to make a pretty nice static visualization that could redraw based on an input string (someone's name). In order to recreate the interactivity of Tableau, I needed a different package and chose to use rCharts. rCharts allows for a nice interface to the D3 library which allows for interactive charts. Below is a simple example extending Shiny to redraw an interactive scatter plot using the Polychart.JS library. What do you think?