Skip to contents

Turns 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 by load_nhl_pbp_full() (the release with xg): needs game_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_fixed and y. For pwhl_game_story(), as returned by load_pwhl_pbp(), in the release's game order: needs game_id, event, team_id, period_of_game, time_of_period, player_name_first, player_name_last, empty_net, penalty_shot, power_play and short_handed.

schedule

The games' schedule rows, the official finals (a shootout's goal included). For nhl_pbp_game_story(), as returned by load_nhl_schedules(): needs game_id, home_score and away_score (filter game_state to OFF or FINAL for finished games). For pwhl_game_story(), as returned by load_pwhl_schedules(): needs game_id, home_team_id, away_team_id, home_score, away_score and game_status (a status containing SO marks a shootout).

skater_box

The games' skater box scores. For nhl_pbp_game_story(), as returned by load_nhl_skater_boxscores(): needs game_id, team_id, home_away (it names each game's home and away team), player_id, player_name, points and goals. For pwhl_game_story(), as returned by load_pwhl_skater_boxscores(): needs game_id, team_id, player_id, first_name, last_name, points and goals.

xg_shots

Optional: the games' shots with expected goals, as returned by load_pwhl_xg_pbp(). Needs game_id, team_id, xg, goal and empty_net. NULL (the default) leaves out the xg and goaltending edges.

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_nametypedescription
game_idintegerThe game's id (NHL API or HockeyTech).
home_team_idintegerHome team's id.
away_team_idintegerAway team's id.
home_scoreintegerHome team's official final, a shootout's goal included.
away_scoreintegerAway team's official final.
winner_team_idintegerWinning team's id; NA on a tie.
loser_team_idintegerLosing team's id; NA on a tie.
marginintegerWinner's final less the loser's.
decidedlogicalTRUE when the game has a winner.
periods_agreelogicalTRUE when the period sums (from the running score) equal the official final.
n_periodsintegerPeriods played, overtimes and the shootout included.
overtimelogicalTRUE when the game went past regulation.

edges: one row per game and edge, ranked.

col_nametypedescription
game_idintegerThe game's id.
winner_team_idintegerWinning team's id (NA on a tie: the home side's view).
loser_team_idintegerLosing team's id (NA on a tie).
edgecharacterEdge 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.
kindcharacteraccounting for the goal counts that add up to the margin, split for the expected-goals cuts.
unitcharacterAlways goals.
winner_valuedoubleWinner's goals (or expected goals) from the edge.
loser_valuedoubleLoser's goals (or expected goals) from the edge.
points_worthdoubleThe winner's value less the loser's. Positive favours the winner.
rankintegerRank 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_nametypedescription
game_idintegerThe game's id.
play_idcharacterNHL: 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.
orderintegerThe play's place in the game (the same numbers as play_id; the shootout row follows the last play).
periodintegerPeriod of the play (overtimes and the shootout after regulation).
clockcharacterTime into the period.
team_idintegerTeam credited with the play.
textcharacterNHL: the play's description. PWHL: the event and the player's name. Shootout for the shootout row.
wpadoubleHome win probability after the play less before it (NA for a deciding score).
winner_wpadoubleThe same change from the winner's side (NA on a tie).
home_scoreintegerHome score after the play.
away_scoreintegerAway score after the play.
rulecharactermax_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_nametypedescription
game_idintegerThe game's id.
periodintegerPeriod number, from 1; overtimes and the shootout follow regulation.
period_labelcharacterP1-P3, then OT, OT2, ... and SO.
is_overtimelogicalTRUE for a period after regulation (the shootout included).
home_ptsintegerHome goals in the period: its last running score less the previous period's.
away_ptsintegerAway goals in the period.
home_cumintegerHome running score at the end of the period.
away_cumintegerAway running score at the end of the period.

runs: each side's largest unanswered run (home first).

col_nametypedescription
game_idintegerThe game's id.
team_idintegerTeam that made the run.
pointsintegerGoals the side scored in a row with no goal by the other side (earliest on ties).
start_orderintegerPlay order of the run's first goal.
end_orderintegerPlay order of the run's last goal.
start_periodintegerPeriod of the run's first goal.
end_periodintegerPeriod of the run's last goal.
home_score_afterintegerHome score at the end of the run.
away_score_afterintegerAway score at the end of the run.

performers: each side's points leader.

col_nametypedescription
game_idintegerThe game's id.
team_idintegerTeam of the top performer.
player_idintegerThe player's id.
player_namecharacterThe player's name.
metriccharacterAlways points.
valuedoubleThe player's points.

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
# }