Commit 179bbdf8 authored by ashtondrew's avatar ashtondrew

cleaning functions documentation

parent 3e01cf5c
......@@ -8,6 +8,28 @@
############################################
# NB: This script is not actually written as a function, but must be run manually create a set of results for simulated budgets under each efficiency scenario.
# OBJECTIVE
# Create simulated results for an incremental set of budget values and save as .rds files for use within the Shiny application. This code is run independent from the Shiny application and the Shiny app looks for the rds files saved here as output.
# REQUIRED LIBRARIES
# dplyr, maggrittr
# also the source functions: function_PossibleByBudget and function_IncExcByBudget
# INPUT
# Requires the datLONG verson of the tidy data.
# OUTPUT
# National_budgetSimList.rds
# Regional_budgetSimList.rds
# Taxa_budgetSimList.rds
# National_regional_budgetSimList.rds
# Regional_regional_budgetSimList.rds
# Taxa_regional_budgetSimList.rds
####################################
# Create a list object that will receive the results for each CE method
budgetSimList <- list()
regional_budgetSimList <- list()
......@@ -57,11 +79,11 @@ for (method in c("CE", "CEs", "CEw", "CE1", "CE12")) {
out$TotSpend[out$Budget==budget] <- sum(tmp_included$Cost[which(tmp_included$Status=="Included")]/50)
tmp_regions <- tmp_included %>%
filter(Status=="Included") %>%
group_by(RegionName) %>%
summarize(NPlanIncluded = n(), NSppIncluded = sum(SpeciesInPlan), TotSpend = sum(Cost)) %>%
ungroup() %>%
mutate(Budget=budget)
dplyr::filter(Status=="Included") %>%
dplyr::group_by(RegionName) %>%
dplyr::summarize(NPlanIncluded = n(), NSppIncluded = sum(SpeciesInPlan), TotSpend = sum(Cost)) %>%
dplyr::ungroup() %>%
dplyr::mutate(Budget=budget)
out_regional <- rbind(tmp_regions, out_regional)
......
......@@ -74,24 +74,24 @@ library(magrittr)
# Create the wide version of the data
datWIDE <- raw %>%
select(RID = row, NID = unique_plan_num, TID = unique_id,
dplyr::select(RID = row, NID = unique_plan_num, TID = unique_id,
Title = plan_title, SpeciesInPlan = SpeciesInPlan,
Taxa = taxa_code_text, Region = Responsible_region,
Cost = Co, Benefit = B, Success = S, Weight = W,
CE=CE, CEs=CEs, CEw=CEw, CE1=CE1, CE12=CE12, everything()) %>%
# Add text region names
mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
dplyr::mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
# Create all the centered and rescaled CE values
mutate(crCE=(CE-mean(CE,na.rm=T))/sd(CE,na.rm=T),
dplyr::mutate(crCE=(CE-mean(CE,na.rm=T))/sd(CE,na.rm=T),
crCEs=(CEs-mean(CEs,na.rm=T))/sd(CEs,na.rm=T),
crCEw=(CEw-mean(CEw,na.rm=T))/sd(CEw,na.rm=T),
crCE1=(CE1-mean(CE1,na.rm=T))/sd(CE1,na.rm=T),
crCE12=(CE12-mean(CE12,na.rm=T))/sd(CE12,na.rm=T)) %>%
# Sort by CE then calculate CumSum costs and CumSum species
arrange(desc(CE)) %>%
dplyr::arrange(desc(CE)) %>%
# These columns in wide version only reflect the CE & national choice version of costs and ranks
# Provides a non-reactive dataframe for some calculations - excluded consideration of one year budget restraints etc.
mutate(CumulativeCost = cumsum(Cost), CumulativeSpecies = cumsum(SpeciesInPlan),
dplyr::mutate(CumulativeCost = cumsum(Cost), CumulativeSpecies = cumsum(SpeciesInPlan),
CumPercCost = (CumulativeCost*100)/max(CumulativeCost),
CumPercSpecies = (CumulativeSpecies*100)/max(CumulativeSpecies),
MeanAnnualCost = Cost/50,
......@@ -100,21 +100,21 @@ datWIDE <- raw %>%
#Create the long version of the data - this version is the primary data table called from in the Shiny app
datLONG <- raw %>%
select(RID = row, NID = unique_plan_num, TID = unique_id,
dplyr::select(RID = row, NID = unique_plan_num, TID = unique_id,
Title = plan_title, SpeciesInPlan = SpeciesInPlan,
Taxa = taxa_code_text, Region = Responsible_region,
Cost = Co, Benefit = B, Success = S, Weight = W,
CE=CE, CEs=CEs, CEw=CEw, CE1=CE1, CE12=CE12, everything()) %>%
# Add text region names
mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
dplyr::mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
# Reshape to long format using CE method as new column
gather(key=CEmethod, value=EfficiencyScore, CE, CEs, CEw, CE1, CE12) %>%
group_by(CEmethod) %>%
tidyr::gather(key=CEmethod, value=EfficiencyScore, CE, CEs, CEw, CE1, CE12) %>%
dplyr::group_by(CEmethod) %>%
#center and rescale the efficiency scores within CEmethod
mutate(crEfficiencyScore = (EfficiencyScore-mean(EfficiencyScore,na.rm=T))/
dplyr::mutate(crEfficiencyScore = (EfficiencyScore-mean(EfficiencyScore,na.rm=T))/
sd(EfficiencyScore,na.rm=T)) %>%
arrange(desc(crEfficiencyScore)) %>%
ungroup()
dplyr::arrange(desc(crEfficiencyScore)) %>%
dplyr::ungroup()
#write_csv(datLONG, path="C:/P_SESYNCapp/RecoveryExplorer/Data/TidyPPPOutput_LONG.csv")
......
......@@ -14,7 +14,7 @@
# Create tidy version of Uncertainty results table 1 (from Stephanie AveryGomm) for use within the Shiny application. This code is run independent from the Shiny application and the Shiny app looks for the csv files saved here as output.
# REQUIRED LIBRARIES
# readr, dplyr, tidyr, maggrittr
# readr, dplyr, maggrittr
# INPUT
# A *.csv file of the the Table 1 Uncertainty results
......@@ -30,7 +30,7 @@ library(magrittr)
library(dplyr)
tidy <- raw %>%
mutate(SimBS=substr(SimSet,1,1), SimCo=substr(SimSet,2,2), SimBudget=substr(SimSet,3,3))
dplyr::mutate(SimBS=substr(SimSet,1,1), SimCo=substr(SimSet,2,2), SimBudget=substr(SimSet,3,3))
# write_csv(tidy, path="C:/P_SESYNCapp/RecoveryExplorer/Data/TidyUncertaintyTable1.csv")
......
# Code supporting the
# SESYNC Endangered Species Recovery Decision Explorer Tool
#
# by C. Ashton Drew, KDV Decision Analysis LLC
# ashton.drew@kdv-decisions.com
# March 2017
# R version 3.3.2 (2016-10-31)
############################################
# NB: This script is not actually written as a function, but must be run manually to clean and inspect the data prior to using Uncertainty data in the Shiny app.
# OBJECTIVE
# Create tidy version of Uncertainty results table 2 (from Stephanie Avery-Gomm) for use within the Shiny application. This code is run independent from the Shiny application and the Shiny app looks for the csv files saved here as output.
# REQUIRED LIBRARIES
# readr, dplyr, tidyr, maggrittr, plyr
# INPUT
# A *.csv file of the the Table 2 Uncertainty results
# OUTPUT
# TidyUncertaintyTable2.csv saved in the Shiny application's Data folder.
library(readr)
library(magrittr)
......
......@@ -32,15 +32,15 @@ library(dplyr)
library(tidyr)
tidy <- raw %>%
gather(key=Scenario, value=PlanCount, -Result) %>%
mutate(SimBS=substr(Scenario,1,1),
tidyr::gather(key=Scenario, value=PlanCount, -Result) %>%
dplyr::mutate(SimBS=substr(Scenario,1,1),
SimCo=substr(Scenario,2,2),
SimBudget=substr(Scenario,3,3)) %>%
extract(Result, into=c("Treatment","Status"), "([[:alnum:]]+)_([[:alnum:]]+)", remove=TRUE) %>%
spread(Status, PlanCount) %>%
mutate(sometimes=545-(always+never)) %>%
gather(key=Status, value=PlanCount, -c(Treatment, Scenario, SimBS, SimCo, SimBudget)) %>%
mutate(Budget=plyr::mapvalues(SimBudget, from=c("L","M","H"), to=c("$500,000","$1,000,000","$1,500,000")))
tidyr::extract(Result, into=c("Treatment","Status"), "([[:alnum:]]+)_([[:alnum:]]+)", remove=TRUE) %>%
tidyr::spread(Status, PlanCount) %>%
dplyr::mutate(sometimes=545-(always+never)) %>%
tidyr::gather(key=Status, value=PlanCount, -c(Treatment, Scenario, SimBS, SimCo, SimBudget)) %>%
dplyr::mutate(Budget=plyr::mapvalues(SimBudget, from=c("L","M","H"), to=c("$500,000","$1,000,000","$1,500,000")))
# write_csv(tidy, path="C:/P_SESYNCapp/RecoveryExplorer/Data/TidyUncertaintyTable3.csv")
......@@ -10,7 +10,9 @@
library(shiny) #Version 1.0.3
library(readr) #Version 1.1.1
library(magrittr) #Version 1.5
library(plyr) #Version 1.8.4
library(dplyr) #Version 0.7.1
library(tidyr) #Version 0.6.3
library(forcats) #Version 0.2.0
library(ggplot2) # Version 2.2.1
library(scales) #Version 0.4.1
......@@ -949,10 +951,10 @@ server <- function(input, output) {
output$DA_RawData <- renderDataTable({
out <- datWIDE %>%
mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
select(TID, RegionName, Cost=Cost, Benefit, Success, Weight,
dplyr::mutate(RegionName = plyr::mapvalues(Region, c(1:8), c("Pacific", "Southwest", "Midwest", "Southeast", "Northeast", "Mountain Prairie", "Alaska", "Pacific Southwest"))) %>%
dplyr::select(TID, RegionName, Cost=Cost, Benefit, Success, Weight,
crCE, crCEs, crCEw, crCE1, crCE12) %>%
mutate(crCE=round(crCE,2),crCEs=round(crCEs,2), crCEw=round(crCEw,2),
dplyr::mutate(crCE=round(crCE,2),crCEs=round(crCEs,2), crCEw=round(crCEw,2),
crCE1=round(crCE1,2),crCE12=round(crCE12,2))
out$Cost <- dollar(out$Cost)
datatable(out, rownames=FALSE, colnames=c("Plan ID", "Lead Region",
......@@ -1243,14 +1245,14 @@ server <- function(input, output) {
# generate quadrant data subset
quadINCEXC_A <- reactive({
tmpA <- datLONG %>%
mutate(PlanPossible = ifelse(MaxYearCost<=input$BS_A_Budget, "Possible", "Impossible"))
dplyr::mutate(PlanPossible = ifelse(MaxYearCost<=input$BS_A_Budget, "Possible", "Impossible"))
outA <- IncExcByBudget(x=tmpA, AnnBudget=input$BS_A_Budget, SplitLvl = input$BS_A_SplitLvl)
outA <- outA[outA$CEmethod==input$BS_A_CEmethod,c("TID","Status")]
})
quadINCEXC_B <- reactive({
tmpB <- datLONG %>%
mutate(PlanPossible = ifelse(MaxYearCost<=input$BS_B_Budget, "Possible", "Impossible"))
dplyr::mutate(PlanPossible = ifelse(MaxYearCost<=input$BS_B_Budget, "Possible", "Impossible"))
outB <- IncExcByBudget(x=tmpB, AnnBudget=input$BS_B_Budget, SplitLvl = input$BS_B_SplitLvl)
outB <- outB[outB$CEmethod==input$BS_B_CEmethod,c("TID","Status")]
})
......@@ -1265,12 +1267,12 @@ server <- function(input, output) {
tmp <- datWIDE %>%
# remove 7 outlier high cost plans
filter(Cost/SpeciesInPlan <= 200000000) %>%
dplyr::filter(Cost/SpeciesInPlan <= 200000000) %>%
# select the columns for visualization
select(TID, Cost, SpeciesInPlan, Benefit, Success, Weight, RegionName, Taxa, CE, CEs, CEw, CE1, CE12) %>%
mutate(Highlight = "N", CostPerSpecies = Cost/SpeciesInPlan) %>%
dplyr::select(TID, Cost, SpeciesInPlan, Benefit, Success, Weight, RegionName, Taxa, CE, CEs, CEw, CE1, CE12) %>%
dplyr::mutate(Highlight = "N", CostPerSpecies = Cost/SpeciesInPlan) %>%
# Assign all labels as quandrant A to save effort later, then adjust
mutate(CBquad = "A", CSquad = "A", CWquad = "A")
dplyr::mutate(CBquad = "A", CSquad = "A", CWquad = "A")
# Adjust letter labels for BENEFIT quadrants
tmp$CBquad[which(tmp$CostPerSpecies <= input$qCost & tmp$Benefit <= input$qBslider)] <- "B"
......@@ -1295,19 +1297,19 @@ server <- function(input, output) {
# Assign the optional Highlight column based on optional choices
if (input$qRegionTaxa == "R"){
tmp <- tmp %>%
mutate(Highlight = ifelse(RegionName==input$qRegion, "Y", "N"))
dplyr::mutate(Highlight = ifelse(RegionName==input$qRegion, "Y", "N"))
} else {
if (input$qRegionTaxa == "T"){
tmp <- tmp %>%
mutate(Highlight = ifelse(Taxa==input$qTaxa, "Y", "N"))
dplyr::mutate(Highlight = ifelse(Taxa==input$qTaxa, "Y", "N"))
} else {
if (input$qRegionTaxa == "A"){
tmp <- tmp %>%
mutate(Highlight = ifelse(ScenarioA=="Included", "Y", "N"))
dplyr::mutate(Highlight = ifelse(ScenarioA=="Included", "Y", "N"))
}else{
if (input$qRegionTaxa == "B"){
tmp <- tmp %>%
mutate(Highlight = ifelse(ScenarioB=="Included", "Y", "N"))
dplyr::mutate(Highlight = ifelse(ScenarioB=="Included", "Y", "N"))
}
}
}
......@@ -1393,7 +1395,7 @@ server <- function(input, output) {
need(input$BS_B_Budget >= 0, "Vist the Compare Scenarios tab first, then this table will appear.")
)
tmp <- quadDAT() %>%
select(TID, RegionName, CBquad, CSquad, CWquad, ScenarioA, ScenarioB)
dplyr::select(TID, RegionName, CBquad, CSquad, CWquad, ScenarioA, ScenarioB)
datatable(tmp, filter = 'top', rownames=FALSE,
colnames=c("Plan ID", "Lead Region", "Cost-Benefit Quad", "Cost-Success Quad", "Cost-Weight Quad",
"Scenario A", "Scenario B"),
......@@ -1511,9 +1513,9 @@ server <- function(input, output) {
output$US_InOutProbability <- renderDataTable({
tmp <- uncProbInc %>%
filter(SimVar==input$US_Sim & SimCo==input$US_Level & SimBS==input$US_Level) %>%
select(TID, RegionName, L, M, H, Cost=Cost, Benefit, Success, Weight) %>%
mutate(Cost=round(Cost,0))
dplyr::filter(SimVar==input$US_Sim & SimCo==input$US_Level & SimBS==input$US_Level) %>%
dplyr::select(TID, RegionName, L, M, H, Cost=Cost, Benefit, Success, Weight) %>%
dplyr::mutate(Cost=round(Cost,0))
datatable(tmp, filter =list(position='top', plain=TRUE), rownames=FALSE,
colnames=c("Plan ID", "Lead Region", "$7.5m", "$15m", "$30m", "Cost", "Benefit", "Success", "Weight"))%>%
#options = list(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment