library(dfsOptimizer)

Basic Methods

The package includes a lot of methods for customizing individual players in your model (see the “Customizing your Optimizer” vignette for examples)

## Example of how to block players by ID
# Single Player
opt <- block_players_by_id(opt, player_ids = single_player_id)

# Blocking multiple players uses the same syntax
opt <- block_players_by_id(opt, player_ids = multiple_player_ids)

Customizing Player Sets - Advanced

Let’s say you want to do something complicated, like * Remove every player who either is in the bottom 50% of projected value (fpts/salary) OR plays for Montreal * Lock the two wingers with the highest points * Set the maximum exposure of all players to be proportional to their projected value, within teams

You could use the various sets of functions included in this package, But for complex, multi-step customization, the flow I recommend is: 1. Add players to an optimizer object 2. Pull those players out using get_player_data() 3. Use whatever R code you like on the data.table of players 4. Put the table back into the model with add_players_from_df

Here’s how we’d handle the above example:

# Instantiate
mod <- create_optimizer(site = 'DRAFTKINGS', sport = 'HOCKEY', contest_type = 'CLASSIC')

# Add players (using the data set nhl_players, which is included in the package)
mod <- add_players_from_df(mod, nhl_players)
mod
#> Optimizer Object (S4 class)
#> Site: DRAFTKINGS
#> Sport: HOCKEY
#> Contest Type: CLASSIC
#> Number of Players: 308

One can see we have 308 players in our set – Let’s pull the out into a data.table

# Pull those right back out
# This step is necessary if you added players to your model via CSV,
# otherwise you could just manipulate the original data.frame before
# adding it to the model
players <- get_player_data(mod)
head(players)
#>          id first_name  last_name           fullname team position depth salary
#> 1: 14086443     Nathan  MacKinnon   Nathan MacKinnon  COL        C     1   8300
#> 2: 14087003   Frederik   Andersen  Frederik Andersen  TOR        G     1   8200
#> 3: 14087005    Michael Hutchinson Michael Hutchinson  TOR        G     1   8000
#> 4: 14087004      Carey      Price        Carey Price  MON        G     1   8000
#> 5: 14086445     Auston   Matthews    Auston Matthews  TOR        C     1   7900
#> 6: 14087007    Charlie   Lindgren   Charlie Lindgren  MON        G     1   7800
#>     fpts locked blocked is_injured min_exposure max_exposure variance
#> 1: 19.00  FALSE   FALSE      FALSE           NA           NA       NA
#> 2: 15.78  FALSE   FALSE      FALSE           NA           NA       NA
#> 3: 11.26  FALSE   FALSE      FALSE           NA           NA       NA
#> 4: 12.18  FALSE   FALSE      FALSE           NA           NA       NA
#> 5: 15.85  FALSE   FALSE      FALSE           NA           NA       NA
#> 6: 16.10  FALSE   FALSE      FALSE           NA           NA       NA

Now that we have the players, we can manipulate the table however we want – I’m going to use data.table in this example, but you could just as easily use whatever methods or packages you want.

Remember what we want to do: * Remove every player who either is in the bottom 50% of projected value (fpts/salary) OR plays for Montreal * Lock the two wingers with the highest points * Set the maximum exposure of all players to be proportional to their projected value, within teams

# Remove every player who _either_ is in the bottom 50% of projected value (fpts/salary) OR plays for Montreal
players <-
  players[fpts/salary >= median(fpts/salary)][team != 'MON']

# Lock the two wingers with the highest points
players[ id %in% players[position == 'W', ][order(-fpts)][1:2, id], locked := TRUE]

# Set the maximum exposure of all players to be proportional to their projected value, *within teams*
players[, max_exposure := rank(fpts/salary, ties.method = 'first') / .N, by = team]

From here, you just put the data right back in using add_players_from_df

mod <- add_players_from_df(mod, players)
mod
#> Optimizer Object (S4 class)
#> Site: DRAFTKINGS
#> Sport: HOCKEY
#> Contest Type: CLASSIC
#> Number of Players: 132

You could, of course, use this method in conjunction with external data as well. For instance, if you wanted to remove anyone who played the night before, or who hasn’t player more than N games, you could join in an external set with values like “Days since played” or “N Games Played” and use that to filter the data down before setting it back into the model!