
How the winner won it: an NHL or PWHL game story in goals
Source:R/hockey_game_story.R
nhl_pbp_game_story.RdTurns the play-by-play, schedule and skater box scores of one or many games
into six tidy frames that explain the result: the winner's edges in goals,
the play that swung the game, the score by period, each side's longest
unanswered run and each side's points leader. nhl_pbp_game_story() reads
NHL games and pwhl_game_story() PWHL games. They return the same frames,
column for column, as sportsdataverse-py's nhl_game_story() and
pwhl_game_story() (the play id is character here), and a parity test
replays those functions' output on real games.
nhl_pbp_game_story() is not nhl_game_story(), which fetches the NHL
web service's own game-story recap for one game id.
Accounting edges count every goal once, so the winner's less the
loser's add up to the margin exactly: even_strength_goals,
power_play_goals, short_handed_goals, empty_net_goals,
penalty_shot_goals and shootout (1 for the shootout winner: the goal
the official final credits it and no play carries). In the NHL a goal's
situation comes from the skaters on the ice, at most five a side (an extra
attacker for a pulled goalie is not a man advantage, so 6v5 is even
strength), empty_net and the 1v0 penalty-shot state; in the PWHL from
HockeyTech's goal flags.
Split edges are in expected goals. NHL: xg (every unblocked shot's
xg), xg_5v5, high_danger_xg (shots within 20 feet of the goal line's
centre and 30 degrees of the net's axis) and goaltending, a side's goals
saved above expected: the opponent's xG on the side's goalie less the
goals he allowed (empty nets and the shootout left out). PWHL: xg and
goaltending from xg_shots, when given.
The NHL moment is the play with the largest home win-probability change.
Each play's home win probability is a logistic of the score difference,
the square root of the seconds left, the skater difference, whether either
goalie is pulled and a home-ice prior pnorm(hfa / margin_sd): the
coefficients and prior of sportsdataverse-py's in-game model. They are
read from the sportsdataverse-data nhl_xg_models release
(nhl_in_game_wp.json and nhl_prediction_constants.json, checked
against the sha256s in their manifest nhl_model_constants_meta.json) on
the first game story of a session and cached with the xG models, so a
refit in sportsdataverse-py reaches installed copies without a fastRhockey
release. When the release cannot be read (offline, mid-update, or a model
this version cannot score with) the last complete cached copy is used, and
with none the constants bundled in the package (the 2026-11-13 fit, a prior
of pnorm(0.0298 / 0.5109), about 0.523), with a message; that choice
holds for the rest of the session. The first play's change is taken from
the prior. Shootout attempts are not
scored: one closing Shootout row carries the swing from the end of
overtime to the result. The PWHL has no in-game model, so its moment is
the deciding score: the winner's first goal after the last play it was
level or behind after (the shootout, when the game went to one).
Periods are P1-P3, then OT (OT2, ... in the playoffs or a PWHL
game with more overtimes) and SO. The top performer is the side's points
leader (ties: goals, then the smaller id).
xG from the NHL release's boosters runs high before 2024-25; both sides of a game carry the same bias, so a difference is less affected than a sum.
Usage
nhl_pbp_game_story(pbp, schedule, skater_box)
pwhl_game_story(pbp, schedule, skater_box, xg_shots = NULL)Arguments
- pbp
The plays of one or many games. For
nhl_pbp_game_story(), as returned byload_nhl_pbp_full()(the release withxg): needsgame_id,event_idx,period,period_type,period_time,game_seconds_remaining,event_type,event_team_type,description,home_score,away_score,home_skaters,away_skaters,strength_state,home_goalie_in,away_goalie_in,empty_net,xg,x_fixedandy. Forpwhl_game_story(), as returned byload_pwhl_pbp(), in the release's game order: needsgame_id,event,team_id,period_of_game,time_of_period,player_name_first,player_name_last,empty_net,penalty_shot,power_playandshort_handed.- schedule
The games' schedule rows, the official finals (a shootout's goal included). For
nhl_pbp_game_story(), as returned byload_nhl_schedules(): needsgame_id,home_scoreandaway_score(filtergame_statetoOFForFINALfor finished games). Forpwhl_game_story(), as returned byload_pwhl_schedules(): needsgame_id,home_team_id,away_team_id,home_score,away_scoreandgame_status(a status containingSOmarks a shootout).- skater_box
The games' skater box scores. For
nhl_pbp_game_story(), as returned byload_nhl_skater_boxscores(): needsgame_id,team_id,home_away(it names each game's home and away team),player_id,player_name,pointsandgoals. Forpwhl_game_story(), as returned byload_pwhl_skater_boxscores(): needsgame_id,team_id,player_id,first_name,last_name,pointsandgoals.- xg_shots
Optional: the games' shots with expected goals, as returned by
load_pwhl_xg_pbp(). Needsgame_id,team_id,xg,goalandempty_net.NULL(the default) leaves out thexgandgoaltendingedges.
Value
A named list of six tibbles, each sorted by game_id. An NHL game
needs its schedule row, both skater-box sides and plays; a PWHL game its
schedule row and plays. Rows of any input without a game_id are dropped
with one warning that counts them. Ids are R integers except the play id,
which is character.
summary: one row per game.
| col_name | type | description |
| game_id | integer | The game's id (NHL API or HockeyTech). |
| home_team_id | integer | Home team's id. |
| away_team_id | integer | Away team's id. |
| home_score | integer | Home team's official final, a shootout's goal included. |
| away_score | integer | Away team's official final. |
| winner_team_id | integer | Winning team's id; NA on a tie. |
| loser_team_id | integer | Losing team's id; NA on a tie. |
| margin | integer | Winner's final less the loser's. |
| decided | logical | TRUE when the game has a winner. |
| periods_agree | logical | TRUE when the period sums (from the running score) equal the official final. |
| n_periods | integer | Periods played, overtimes and the shootout included. |
| overtime | logical | TRUE when the game went past regulation. |
edges: one row per game and edge, ranked.
| col_name | type | description |
| game_id | integer | The game's id. |
| winner_team_id | integer | Winning team's id (NA on a tie: the home side's view). |
| loser_team_id | integer | Losing team's id (NA on a tie). |
| edge | character | Edge name. Accounting: even_strength_goals, power_play_goals, short_handed_goals, empty_net_goals, penalty_shot_goals, shootout. Splits: xg, xg_5v5, high_danger_xg (NHL), goaltending. |
| kind | character | accounting for the goal counts that add up to the margin, split for the expected-goals cuts. |
| unit | character | Always goals. |
| winner_value | double | Winner's goals (or expected goals) from the edge. |
| loser_value | double | Loser's goals (or expected goals) from the edge. |
| points_worth | double | The winner's value less the loser's. Positive favours the winner. |
| rank | integer | Rank of the edge within the game by points_worth, 1 = largest; equal worths keep the edge list's order. |
moment: the play that swung each game.
| col_name | type | description |
| game_id | integer | The game's id. |
| play_id | character | NHL: the play's event_idx. PWHL: the play's row number in pbp (after rows without a game_id are dropped), from 0. NA for the closing Shootout row. |
| order | integer | The play's place in the game (the same numbers as play_id; the shootout row follows the last play). |
| period | integer | Period of the play (overtimes and the shootout after regulation). |
| clock | character | Time into the period. |
| team_id | integer | Team credited with the play. |
| text | character | NHL: the play's description. PWHL: the event and the player's name. Shootout for the shootout row. |
| wpa | double | Home win probability after the play less before it (NA for a deciding score). |
| winner_wpa | double | The same change from the winner's side (NA on a tie). |
| home_score | integer | Home score after the play. |
| away_score | integer | Away score after the play. |
| rule | character | max_abs_wpa: the play with the largest absolute win-probability change (earliest on ties); deciding_score: the winner's first goal after it was last level or behind, so it leads from there to the end. |
periods: one row per game and period.
| col_name | type | description |
| game_id | integer | The game's id. |
| period | integer | Period number, from 1; overtimes and the shootout follow regulation. |
| period_label | character | P1-P3, then OT, OT2, ... and SO. |
| is_overtime | logical | TRUE for a period after regulation (the shootout included). |
| home_pts | integer | Home goals in the period: its last running score less the previous period's. |
| away_pts | integer | Away goals in the period. |
| home_cum | integer | Home running score at the end of the period. |
| away_cum | integer | Away running score at the end of the period. |
runs: each side's largest unanswered run (home first).
| col_name | type | description |
| game_id | integer | The game's id. |
| team_id | integer | Team that made the run. |
| points | integer | Goals the side scored in a row with no goal by the other side (earliest on ties). |
| start_order | integer | Play order of the run's first goal. |
| end_order | integer | Play order of the run's last goal. |
| start_period | integer | Period of the run's first goal. |
| end_period | integer | Period of the run's last goal. |
| home_score_after | integer | Home score at the end of the run. |
| away_score_after | integer | Away score at the end of the run. |
performers: each side's points leader.
| col_name | type | description |
| game_id | integer | The game's id. |
| team_id | integer | Team of the top performer. |
| player_id | integer | The player's id. |
| player_name | character | The player's name. |
| metric | character | Always points. |
| value | double | The player's points. |
See also
load_nhl_pbp_full(), load_nhl_schedules(),
load_nhl_skater_boxscores(), load_pwhl_pbp(), load_pwhl_schedules(),
load_pwhl_skater_boxscores() and load_pwhl_xg_pbp() for the inputs;
nhl_game_story() for the NHL web service's recap of one game, a
different thing.
Examples
# \donttest{
# Downloads a season of play-by-play, schedules and box scores.
try({
gid <- 2025020001
one <- function(x) x[x$game_id == gid, ]
story <- nhl_pbp_game_story(
one(load_nhl_pbp_full(2026)), one(load_nhl_schedules(2026)),
one(load_nhl_skater_boxscores(2026))
)
story$summary
head(story$edges, 4)
story$moment[, c("text", "rule")]
})
#> Warning: game_story: dropped rows without a game_id (656504 pbp)
#> # A tibble: 1 × 2
#> text rule
#> <chr> <chr>
#> 1 Frank Nazar snap, assists: Tyler Bertuzzi, Teuvo Teravainen max_abs_wpa
# }
# \donttest{
try({
story <- pwhl_game_story(
load_pwhl_pbp(2026), load_pwhl_schedules(2026),
load_pwhl_skater_boxscores(2026), load_pwhl_xg_pbp(2026)
)
story$edges[story$edges$rank == 1, ]
# every shootout
story$periods[story$periods$period_label == "SO", ]
})
#> # A tibble: 10 × 8
#> game_id period period_label is_overtime home_pts away_pts home_cum away_cum
#> <int> <int> <chr> <lgl> <int> <int> <int> <int>
#> 1 227 5 SO TRUE 0 1 1 2
#> 2 237 5 SO TRUE 1 0 3 2
#> 3 245 5 SO TRUE 0 1 2 3
#> 4 251 5 SO TRUE 0 1 1 2
#> 5 259 5 SO TRUE 0 1 1 2
#> 6 269 5 SO TRUE 1 0 4 3
#> 7 273 5 SO TRUE 0 1 2 3
#> 8 276 5 SO TRUE 0 1 3 4
#> 9 308 5 SO TRUE 1 0 2 1
#> 10 326 5 SO TRUE 0 1 1 2
# }