#THIS FILE TAKES AS INPUTS THE LAKES_SELECTED.X FILES PRODUCED BY THE STATE, COUNTY, AND COLLABORATION MODELS AND UNPACKS THEM INTO RESULTS WE CAN INTERPRET AND GRAPH. #STEP 0: LOAD LIBRARIES. library(tidyverse) #STEP 1: LOAD RESULTS OUTPUT OBJECT -- START WITH LAKES_SELECTED_COUNTY lakes.selected = readRDS("lakes_selected_county") #STEP 2: LOAD IN AND REPROCESS THE VARIOUS SET UP OBJECTS (NIJ OBJECT, COUNTIES OBJECT, AND BUDGET/BUDGET.LIST OBJECTS) FROM THE MODEL RUN--JUST COPY-PASTED HERE FROM THE MODEL FILE. boats.reduce = readRDS(file = "boats_reduce") NIJ2 = boats.reduce %>% dplyr::select(-bij) NIJ2$from = as.character(NIJ2$from) NIJ2$to = as.character(NIJ2$to) Counties = read.csv("Lakes_total_collabs.csv") Counties = dplyr::select(Counties, DOW, county_name) Counties$DOW = as.character(Counties$DOW) NIJ3 = NIJ2 %>% left_join(Counties, by = c("from" = "DOW"), relationship = "many-to-many") NIJ4 = NIJ3 %>% left_join(Counties, by = c("to" = "DOW"), suffix = c(".from", ".to"), relationship = "many-to-many") NIJ4$lake.D = as.numeric(factor(NIJ4$from, levels=unique(c(NIJ4$from, NIJ4$to)))) NIJ4$lake.R = as.numeric(factor(NIJ4$to, levels=unique(c(NIJ4$from, NIJ4$to)))) NIJ4 = NIJ4 %>% distinct(from, to, weight,county_name.to, .keep_all = TRUE) counties.list = unique(c(NIJ4$county_name.from, NIJ4$county_name.to)) budgets = read.csv("countymod_budgets.csv") budgets = budgets[budgets$County %in% unique(counties.list),] budgets = budgets[c(which(budgets$X700!=0)),] budgets.list = apply(budgets[,2:ncol(budgets)], 1, function(x) { unique(c(x)) } ) for(x in 1:length(budgets.list)) { if(budgets.list[[x]][1] == 0) { budgets.list[[x]] = budgets.list[[x]][-1] } } #STEP 3--APPEND COUNTY INFO TO ALL THE SEPARATE LISTS WE HAVE INSIDE OF THE LAKES.SELECTED OBJECT SO THAT WE CAN REDUCE THIS LIST TO A SINGLE DATA FRAME. JUST TO BE EXTRA CAUTIOUS WHEN DOING THIS COUNTY MATCHING, WE GO THROUGH EACH COUNTY AND IDENTIFY THE RESULTS LIST INSIDE LAKES.SELECTED THAT SPECIFICALLY CONCERNS LAKES IN THAT COUNTY, JUST IN CASE THE RESULTS ARE IN A DIFFERENT ORDER THAN THE COUNTIES ARE LISTED IN THE COUNTIES OBJECT. for(cou in 1:length(budgets$County)) { #Go county by county. current.co = budgets$County[cou] #Get current county. cos.lakes = unique(NIJ4$lake.R[NIJ4$county_name.to == current.co]) #Grab this county's lakes. for(sol in 1:length(lakes.selected)) { #Go entry by entry in our solutions list. if(all(lakes.selected[[sol]]$global.lakes %in% cos.lakes)) { #If the lakes in this solution match those owned by the current county... lakes.selected[[sol]]$county = current.co #Mark this county as the one for this solution } } } #STEP 4: COMPILED INTO A SINGLE DATA FRAME lakes.selected2 = bind_rows(lakes.selected) tail(lakes.selected2) #Shows what this looks like. #STEP 5: TRANSLATING COUNTY-LEVEL DECISIONS AT VARIOUS BUDGETS INTO STATEWIDE TOTALS OF NUMBERS OF BOAT INSPECTED. #We go column by column in the budgets file. For each county that got at least one inspection station for that overall budget, we find what lakes that county would have inspected with their share of the overall budget. We then figure out what that set of lakes would accomplish statewide to find our "solution" for that budget level all.sols = vector() #Storage object. for(bud in 2:ncol(budgets)) { #Go column by column thru the budget levels sol.set = vector() #Empty vector to store the lakes getting inspected at this budget level. for (cou in 1:nrow(budgets)) { #Go row by row through the counties. if(budgets[cou, bud] > 0) { #If this county got a budget to spend for this overall budget LEVEL ... current.co = budgets$County[cou] #Grab this county's name. current.b = budgets[cou,bud] #Grab the current budget. tmp1 = lakes.selected2 %>% filter(county == current.co, B == current.b) #Find that county and budget in our solutions object. lakes2grab = tmp1$global.lakes #Grab those lakes from that condensed solution. sol.set = c(sol.set, lakes2grab) } } #Now that we know which lakes are being inspected under this budget, we need to figure out how many risky boats that means we're intercepting. this.sol = NIJ4 %>% filter(lake.D %in% sol.set | lake.R %in% sol.set) this.sol = sum(this.sol$weight) all.sols = c(all.sols, this.sol)#Save the result. print(this.sol) #Progress bar. } #STEP 6: ORGANIZING THE RESULTS DATA FOR GRAPHING. chart.dat1 = data.frame(Budget = c(10,20,30,40,50,60,70,80,90,100,120,140,160,180,200,300,400,500,600,700), County = all.sols) ##REPEAT AGAIN WITH THE LAKES_SELECTED_COLLABS OBJECT #STEP 1: LOAD RESULTS OUTPUT OBJECT -- START WITH LAKES_SELECTED_COLLABS lakes.selected = readRDS("lakes_selected_collabs") #STEP 2: LOAD IN AND REPROCESS THE VARIOUS SET UP OBJECTS (NIJ OBJECT, COUNTIES OBJECT, AND BUDGET/BUDGET.LIST OBJECTS) FROM THE MODEL RUN--JUST COPY-PASTED HERE FROM THE MODEL FILE. boats.reduce = readRDS(file = "boats_reduce") NIJ2 = boats.reduce %>% dplyr::select(-bij) NIJ2$from = as.character(NIJ2$from) NIJ2$to = as.character(NIJ2$to) Counties = read.csv("Lakes_total_collabs.csv") Counties = dplyr::select(Counties, DOW, county_name = collab_name) Counties$DOW = as.character(Counties$DOW) NIJ3 = NIJ2 %>% left_join(Counties, by = c("from" = "DOW")) NIJ4 = NIJ3 %>% left_join(Counties, by = c("to" = "DOW"), suffix = c(".from", ".to")) NIJ4$lake.D = as.numeric(factor(NIJ4$from, levels=unique(c(NIJ4$from, NIJ4$to)))) NIJ4$lake.R = as.numeric(factor(NIJ4$to, levels=unique(c(NIJ4$from, NIJ4$to)))) NIJ4 = NIJ4 %>% distinct(from, to, weight,county_name.to, .keep_all = TRUE) counties.list = unique(c(NIJ4$county_name.from, NIJ4$county_name.to)) budgets = read.csv("collabmod_budgets_collabsandcounties.csv") budgets = budgets[budgets$County %in% unique(counties.list),] budgets = budgets[c(which(budgets$X700!=0)),] budgets.list = apply(budgets[,2:ncol(budgets)], 1, function(x) { unique(c(x)) } ) for(x in 1:length(budgets.list)) { if(budgets.list[[x]][1] == 0) { budgets.list[[x]] = budgets.list[[x]][-1] } } #STEP 3--APPEND COUNTY INFO TO ALL THE SEPARATE LISTS WE HAVE INSIDE OF THE LAKES.SELECTED OBJECT SO THAT WE CAN REDUCE THIS LIST TO A SINGLE DATA FRAME. JUST TO BE EXTRA CAUTIOUS WHEN DOING THIS COUNTY MATCHING, WE GO THROUGH EACH COUNTY AND IDENTIFY THE RESULTS LIST INSIDE LAKES.SELECTED THAT SPECIFICALLY CONCERNS LAKES IN THAT COUNTY, JUST IN CASE THE RESULTS ARE IN A DIFFERENT ORDER THAN THE COUNTIES ARE LISTED IN THE COUNTIES OBJECT. for(cou in 1:length(budgets$County)) { #Go county by county. current.co = budgets$County[cou] #Get current county. cos.lakes = unique(NIJ4$lake.R[NIJ4$county_name.to == current.co]) #Grab this county's lakes. for(sol in 1:length(lakes.selected)) { #Go entry by entry in our solutions list. if(all(lakes.selected[[sol]]$global.lakes %in% cos.lakes)) { #If the lakes in this solution match those owned by the current county... lakes.selected[[sol]]$county = current.co #Mark this county as the one for this solution } } } #STEP 4: COMPILED INTO A SINGLE DATA FRAME lakes.selected2 = bind_rows(lakes.selected) tail(lakes.selected2) #Shows what this looks like. #STEP 5: TRANSLATING COUNTY-LEVEL DECISIONS AT VARIOUS BUDGETS INTO STATEWIDE TOTALS OF NUMBERS OF BOAT INSPECTED. #We go column by column in the budgets file. For each county that got at least one inspection station for that overall budget, we find what lakes that county would have inspected with their share of the overall budget. We then figure out what that set of lakes would accomplish statewide to find our "solution" for that budget level all.sols = vector() #Storage object. for(bud in 2:ncol(budgets)) { #Go column by column thru the budget levels sol.set = vector() #Empty vector to store the lakes getting inspected at this budget level. for (cou in 1:nrow(budgets)) { #Go row by row through the counties. if(budgets[cou, bud] > 0) { #If this county got a budget to spend for this overall budget LEVEL ... current.co = budgets$County[cou] #Grab this county's name. current.b = budgets[cou,bud] #Grab the current budget. tmp1 = lakes.selected2 %>% filter(county == current.co, B == current.b) #Find that county and budget in our solutions object. lakes2grab = tmp1$global.lakes #Grab those lakes from that condensed solution. sol.set = c(sol.set, lakes2grab) } } #Now that we know which lakes are being inspected under this budget, we need to figure out how many risky boats that means we're intercepting. this.sol = NIJ4 %>% filter(lake.D %in% sol.set | lake.R %in% sol.set) this.sol = sum(this.sol$weight) all.sols = c(all.sols, this.sol)#Save the result. print(this.sol) #Progress bar. } #STEP 6: ORGANIZING THE RESULTS DATA FOR GRAPHING. chart.dat2 = data.frame(Budget = c(10,20,30,40,50,60,70,80,90,100,120,140,160,180,200,300,400,500,600,700), Collabs = all.sols) #STEP 1: LOADING IN THE STATE DATA--THANKFULLY MUCH SIMPLER! chart.dat3 = readRDS("lakes_selected_state") chart.dat3 = data.frame(Budget = c(10,20,30,40,50,60,70,80,90,100,120,140,160,180,200,300,400,500,600,700), State = chart.dat3) #Add and rename some columns chart.dat1$Model = "County" chart.dat2$Model = "Collaborations" chart.dat3$Model = "State" chart.dat1 = chart.dat1 %>% rename(Boats = County) chart.dat2 = chart.dat2 %>% rename(Boats = Collabs) chart.dat3 = chart.dat3 %>% rename(Boats = State) chart.dat = bind_rows(chart.dat1, chart.dat2, chart.dat3) #Graphing the results! ggplot(chart.dat, aes(x=Budget, y = Boats, color = Model)) + geom_line(size = 2) + geom_line(size = 2) + geom_line(size = 2) + geom_point(size = 6) + geom_point(size = 6) + geom_point(size = 6) + theme(panel.background = element_blank(), axis.line = element_line(color ="black", size = 1.5), axis.text = element_text(size = 16), axis.title = element_text(size = 18), panel.grid.major = element_line(color = "gray20"), axis.ticks = element_line(color = "black", size = 1.5), axis.ticks.length = unit(0.5, "cm"), legend.background = element_rect(color="transparent"), legend.key = element_rect(fill="white", color = "black"), legend.text = element_text(size = 16), legend.title = element_text(size = 18) ) + scale_y_continuous("Risky boats intercepted\n", labels = c("0", "200K", "400K", "600K", "800K")) + scale_x_continuous("\nTotal budget (B)") + scale_color_manual("Model version\n", values = c("State" = "red", "County" = "blue", "Collaborations" = "purple"), guide = guide_legend(override.aes = list(size = 15)))