Fantasy Football Player Forecasting in R

I rewrote my previous post in R, mainly because Paul Rubin mentioned the fact that SAS costs too much on Twitter:

Click here to download the R source [estimate.r]

The R code is a bit shorter (120 lines instead of 157), but they are basically the same complexity. I am following exactly the same logic as last time so I won’t go into the football part of it. I will simply highlight a couple of things about the code.

I rely on read.csv to read each input file into a data frame. Once I have a data frame, the primary operation is to “score” the position. Here is the code to score the running back position. The “within” statement makes it easy.

score.rb <- function(rb) {
  rb <- within(rb, {
    FFPts <- (Rush.TD + Rec.TD) * PtsTD + FumL * PtsFum + Rush.Yds / RushYdsPt + Rec.Yds / RecYdsPt
  })
  return(rb)
}

Remember from the last post that the next thing I do is to “normalize” the scores by subtracting the score of the projected worst starter on each team. This means sorting the players by score and picking one in a particular slot. Here’s how that works:

normalize <- function(p, count) {
  FFPtsMin <- as.numeric(p[with(p, order(-as.numeric(FFPts))), ][count,"FFPts"])
  p$FFPtsN <- apply(p,1,function(row) (as.numeric(row["FFPts"]) - FFPtsMin))
  return(p)
}

So by calling score.rb and normalize in sequence, I get final scores for the RB position. Once I score all of the positions, I simply need to join the data frames for each position together into a final data frame (called “players”). I write this out to CSV. I have saved the complete rankings so you can see the results.

Click here to download player ratings [NFL 2011 Ratings.csv]

It was a lot easier for me to write this code in SAS because it’s much more familiar to me. Even taking my prejudice into account, for this task I prefer SAS to R because the use of DATA steps leads to very simple, easy to understand code. R has great libraries, better extensibility, and is free, but the language itself feels a bit clunky.

Author: natebrix

Follow me on twitter at @natebrix.

1 thought on “Fantasy Football Player Forecasting in R”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s