NBA player impact metrics come from a variety of approaches and have names like xRAPM, BPM, DARKO, LEBRON and EPM — all of which have their strengths and weaknesses.
If you’ve wondered how these metrics are created, we are starting a series on how to build your own metric, all by yourself, using publicly available data.
Rather than a mere “how to” guide to building an NBA player metric, this article contains all the necessary Python scripts that allow you to hit the ground running.
For some, this might be merely a window into a process that’s not often described in detail.
But for those willing to put in the time and effort, the contents of this guide should put the best possible public NBA metric within reach — building on the foundation we provide here.
Today, we’re laying the groundwork: how to grab the necessary data from the NBA API, generate lineup data and run a modified version of regularized adjusted plus-minus (RAPM).
Why we need lineup data
RAPM is required to create unbiased, more predictive box-score metrics — as I covered in this article:
By the way, RAPM itself is a valuable metric, too, as long as the sample size is large enough.
To come up with RAPM estimates, we need lineup data, as we need to know which players were on the court at each possession, and how many points were scored.
But there’s another, extremely important reason why we need it: to test our models for accuracy.
In data science, it is of vital importance to test one’s models — rigorously — to avoid falling into various pitfalls.
The method of choice for making statistical approaches and models watertight is to test them against data the model has never seen. This approach is called out-of-sample testing.
It ultimately allows us to build a more accurate metric by giving us valuable feedback about certain model parameters.
My general preference is to run models with all data up to date X, then check prediction error on date X+1. This can be done for every day of each season, though it might require several hours of running and testing models.
Data we need
Here is a more comprehensive overview on how to obtain NBA data:
For today, we need four specific datasets:
the game log, which contains game IDs and game dates
the play-by-play data
rotation data to help deal with the play-by-play
player information, which contains IDs, names etc.
The general approach to obtaining the data works like this:
Check to see if the table already exists in our database. If yes, check which data the table already contains.
Grab the data we’re missing — often with a pause (”sleep”) between requests to avoid getting blocked.
Write the downloaded data to the database.
Here are the scripts — on GitHub — that will grab the data for you:
The tables in your database should resemble the following.
Game log:

Play-by-play:

Game rotation:

Player info:

Creating lineup data from play-by-play
With all necessary data downloaded from the API, we can start generating lineup data.
Lineup data lists the five offensive and five defensive players on the court for each possession, how many points were scored, and when the possession ended.
The resulting table thus needs to include the following columns:
game_id, period, poss_num, home_possession, points, end_time, home1–home5, away1–away5
For those who want to modify and improve the script, here are some additional considerations:
How to handle foul trouble for individual players
Not tracking this might underrate good players — they are often forced to stay on the court regardless, and their defensive effort can suffer as they avoid fouling out.
How to handle free throws (and substitutions during FTs)
The fair approach is crediting the lineup that was on the floor when the foul occurred, not the lineup that ends the possession.
Additionally, the actual points from any free throw that doesn’t end the trip to the line (the first of two, say) could be replaced by the shooter’s FT%, to reduce noise in the data. The last free throw is different: A miss can be rebounded, so the result affects what happens next.
The same trick doesn’t work for 3-pointers, even though 3s are the noisiest action there is, because 3s are defended and a miss can be rebounded, while those free throws are isolated events.
How to handle technical free throws
In the supplied script, these are ignored.
Whether to keep track of individual stats recorded during these possessions
These are helpful for “influence on teammates” analysis.
The script, which can be found here, should create a table looking like this:

Running RAPM
Once all lineup data is generated, we can move on to running RAPM, to get impact estimates for each player.
A full description of the method can be found here:
A quick recap:
We are using a regression approach to determine the impact of each player on his team’s point differential.
Each player is represented by two variables, one for offense and one for defense.
Each NBA possession serves as one observation
We are using L2 regularization to pull player estimates towards zero with a quadratic penalty — a method that’s been shown to vastly outperform standard regression.
We are determining different penalization parameters for offense and defense, using cross-validation.
Additionally, we have variables for home-court advantage (HCA) and a “season” variable to capture season-to-season fluctuations in offensive efficiency (or serve as the regression intercept).
Additional considerations:
How to handle possessions near the end of a quarter
A possession that starts with, say, 45 seconds on the clock has a lower expected value than a standard possession, as the offensive team might want to end the possession quickly to go 2-for-1.
Whether to weight observations differently, according to how far back they are in time
Whether to incorporate additional effects
The supplied script gives the option to generate RAPM estimates for individual dates, which enables us to do single-day out-of-sample testing.
Out-of-sample testing
As we will continuously try to improve our metric, we need a baseline to test against, in order to find out whether we are really seeing improvements from any changes we make to the model.
The out-of-sample script calculates error in two ways:
Using the season and HCA coefficients only, our baseline error.
Using the RAPM coefficients we computed above.
Considerations:
As with RAPM, we need to decide how to handle possessions near the end of a quarter.
If we’ve incorporated additional effects in the RAPM calculations, we need to also account for them here.
Coming next
The above should help anyone who wants to get started in metric development. In Part 2, we will lay the groundwork to create our own box-score metric. Stay tuned.












