Skip to content

Instantly share code, notes, and snippets.

@mrecos
Created April 24, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrecos/44e29a745c5e507174043d411d925d84 to your computer and use it in GitHub Desktop.
Save mrecos/44e29a745c5e507174043d411d925d84 to your computer and use it in GitHub Desktop.
basic assignment loop over last name and case load
##### Your code leading up to this point ######
# initialize active cases
SWDetail$active_cases <- 0
SWDetail[1,"active_cases"] <- 1
# initialize most recent cases
SWDetail$last_case_given <- 0
SWDetail[1,"last_case_given"] <- 1
# resorting this by date
cps_test_assignment <- cps_test_assignment[order(cps_test_assignment$assignment_date),]
bind_ass <- NULL
for(i in seq_len(nrow(cps_test_assignment))){
# for(i in 1:100){
cat(i,"\n")
## For trying to simulate a history, this should be sorted by date, then we can track
assign_date <- cps_test_assignment[i,"assignment_date"]
# outputs a list of SW working at time of case assignment
SW_subset <- subset(SWDetail, Start <= assign_date & End >= assign_date)
# make sure it is sorted correctly
SW_subset <- SW_subset[order(SW_subset$Last),]
###### This is where the action is
# takes simply the first (add complexity here)
# 1) exclude person with last case
# 2) make sure they have fewer active cases
# 3) arrange those by last name
# 4) take the top of the list
# 5) set their active cases + 1
Optimal_SW <- SW_subset %>%
filter(last_case_given != 1) %>%
# trick to go back to top when everyone has the same number of cases
filter(active_cases <= mean(.$active_cases)) %>%
arrange(Last) %>%
slice(1L) %>%
mutate(active_cases = active_cases + 1)
# reset last_case_given and advance active cases
SWDetail$last_case_given <- 0
SWDetail[SWDetail$Assessment.SW == Optimal_SW$Assessment.SW, "last_case_given"] <- 1
SWDetail[SWDetail$Assessment.SW == Optimal_SW$Assessment.SW, "active_cases"] <- SWDetail[SWDetail$Assessment.SW == Optimal_SW$Assessment.SW, "active_cases"] + 1
# bind assigned SW to assigned case
# (this assumes cps_test_assignment is sorted well, could do it a bit better)
first_ass <- cbind(cps_test_assignment[i,17:21], Optimal_SW)
# quick way to keep your output
bind_ass <- rbind(bind_ass, first_ass)
caseloads <- bind_ass %>% #new DF of caseloads..
dplyr::group_by(Assessment.SW) %>%
dplyr::summarise(n= sum(predictions_rf))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment