eheinzen / elo

An R package to calculate Elo ratings
https://eheinzen.github.io/elo/
37 stars 3 forks source link

Including multiple players within the players() function #61

Closed SamEhrlich closed 1 year ago

SamEhrlich commented 1 year ago

I left a comment yesterday about the players() function within the open issue about implementing the players function, but I am not sure if that was the best way of inquiring. I am interested in modeling a 5v5 game where each player is valuable to the team, but when I try to include more than 2 players within a players function I receive an error. In the ultimate frisbee example provided in the vignette, the first term players should be able to handle 7 different players. In my case I am looking to have 5 players in each term specified by unique playerids. My dataframe contains 10 columns (one for each playerid) and a column for the winner of the game. Each row of the dataframe is an individual game, and my playerid columns are strings. I have no issue when providing only 2 playerids for each player term, however when expanding further I get my issue.

Below I will provide my code and the error I am getting, and I hope someone can point me in the right direction. I will be happy to provide additional code or answer any questions that may help in resolving my error. Thanks!

Error: "Error in mtfrm.default(c("99322214618656216", "99322214241694525", "101389713973624205", : cannot mtfrm"

Code: elo.run(home_team_win ~ players(playerid_1, playerid_2, playerid_3) + players(playerid_6, playerid_7, playerid_8), data = test_players_run, k = 20)

eheinzen commented 1 year ago

What version of the package are you using, and what version of R?


test_players_run <- data.frame(
  home_team_win = 1:0,
  playerid_1 = "A",
  playerid_2 = "B",
  playerid_3 = "C",
  playerid_4 = "D",
  playerid_5 = c("E", "I"),
  playerid_6 = "F",
  playerid_7 = "G",
  playerid_8 = "H"
)
elo.run(home_team_win ~ players(playerid_1, playerid_2, playerid_3) +
          players(playerid_6, playerid_7, playerid_8),
        data = test_players_run, k = 20)

This works for me. Can you dump a simplified data.frame for me to test with that shows the error?

SamEhrlich commented 1 year ago

Ok I just tried your sample dataframe and it the function ran for me without a problem. I am using elo package 3.0.1 and my r version is 4.2.1. i created a sample of my data to share where I am getting this issue. I think the issue may be coming on my end with the playerids.

test_players_run <- data.frame( home_team_win = c(1,0,0,1,0,1), playerid_1 = c("101388913280994733","99322214598412197","105501829364113001","105515294798291232","105501829364113001","105515294798291232"), playerid_2 = c("100205576295412145","105501794903516897","105576833572820346","105536930572842229","105576833572820346","105536930572842229"), playerid_3 = c("99566407782924807","100174547541438669","102181670376678547","106301975219458494","102181670376678547","106301975219458494"), playerid_6 = c("99566408210057665","103536968829352943","103877632442764991","108396643302446310","103877632442764991","108396643302446310"), playerid_7 = c("103743617977630492","100238780551262852","107464179845128878","105589333565331737","107464179845128878","105589333565331737"), playerid_8 = c("99566407784212776","100356590519370319","104711049101664016","104711227672883806","104711049101664016","104711227672883806") )

test_frame <- elo.run(home_team_win ~ players(playerid_1, playerid_2, playerid_3) + players(playerid_6, playerid_7, playerid_8), data = test_players_run, k = 20)

I get this error when running my sample: Error in mtfrm.default(c("101388913280994733", "99322214598412197", "105501829364113001", : cannot mtfrm

SamEhrlich commented 1 year ago

even when i shrink the playerids I get this error. a second example I just tried by simplifying the playerid number

test_players_run <- data.frame( home_team_win = c(1,0,0,1,0,1), playerid_1 = c("1","7","13","14","13","14"), playerid_2 = c("2","8","15","16","15","16"), playerid_3 = c("3","9","17","18","17","18"), playerid_6 = c("4","10","19","20","19","20"), playerid_7 = c("5","11","21","22","21","22"), playerid_8 = c("6","12","23","24","23","24") )

test_frame <- elo.run(home_team_win ~ players(playerid_1, playerid_2, playerid_3) + players(playerid_6, playerid_7, playerid_8), data = test_players_run, k = 20)

eheinzen commented 1 year ago

Fascinating. Let me look

eheinzen commented 1 year ago

Oddly, it works if you take any row out of the data.frame... I'm pretty sure I can fix this, but I'm still confused why it happens sometimes and not others.

test_players_run <- data.frame(
  home_team_win = c(1,0,0,1,0,1),
  playerid_1 = c("1","7","13","14","13","14"),
  playerid_2 = c("2","8","15","16","15","16"),
  playerid_3 = c("3","9","17","18","17","18"),
  playerid_6 = c("4","10","19","20","19","20"),
  playerid_7 = c("5","11","21","22","21","22"),
  playerid_8 = c("6","12","23","24","23","24")
)

test_frame <- elo.run(home_team_win ~ players(playerid_1, playerid_2, playerid_3) +
                        players(playerid_6, playerid_7, playerid_8),
                      data = head(test_players_run, -1), k = 20)
SamEhrlich commented 1 year ago

I have been playing with other dataframes and I noticed when you get to more than 6 rows the error occurs. The first dataframe works and the second one errors.

test_players_run <- data.frame( home_team_win = c(1,0,1,1,1), playerid_1 = c("1","2","1","2","1"), playerid_2 = c("3","4","3","4","3"), playerid_3 = c("5","6","5","6","5"), playerid_6 = c("7","8","7","8","7"), playerid_7 = c("9","10","9","10","9"), playerid_8 = c("11","12","11","12","11") )

test_players_run <- data.frame( home_team_win = c(1,0,1,1,1,0), playerid_1 = c("1","2","1","2","1","1"), playerid_2 = c("3","4","3","4","3","4"), playerid_3 = c("5","6","5","6","5","6"), playerid_6 = c("7","8","7","8","7","7"), playerid_7 = c("9","10","9","10","9","9"), playerid_8 = c("11","12","11","12","11","12") )

eheinzen commented 1 year ago

Try that

SamEhrlich commented 1 year ago

do i need to update the package to test this? Im sorry this is the first issue I have opened on a package. Also an additional question while you are here, am I able to set initial elos the same way as I do for teams with players? Just a named vector with elos assigned to each player?

eheinzen commented 1 year ago

Try a devtools::install_github("eheinzen/elo") to get the devel version (3.0.2.9000).

And yes, you should be able to!

SamEhrlich commented 1 year ago

I seem to still be getting the same error after restarting and updating. This is the dataframe I just tested with

test_players_run <- data.frame( home_team_win = c(1,0,0,1,0,1), playerid_1 = c("1","7","13","14","13","14"), playerid_2 = c("2","8","15","16","15","16"), playerid_3 = c("3","9","17","18","17","18"), playerid_6 = c("4","10","19","20","19","20"), playerid_7 = c("5","11","21","22","21","22"), playerid_8 = c("6","12","23","24","23","24") )

and I got this again Error in mtfrm.default(c("1", "7", "13", "14", "13", "14", "2", "8", "15", : cannot mtfrm

I am able to run more than 6 rows with 2 or less players on each team OR many players with 5 or less rows.

eheinzen commented 1 year ago

Did you restart R? Double-check with packageVersion("elo") that you have the right one?

SamEhrlich commented 1 year ago

I tried again last night using elo 3.02.900 that I installed using the devtools method and was still getting the same error. I will be home in a few hours to test again.

SamEhrlich commented 1 year ago

Hey Ethan, I just got home and restarted my Rstudio and elo package again. This time it worked! Thank you for fixing that issue! Hopefully I got everything sorted and I appreciate you helping so quickly.