Run code to load useful montecarlo simulation functions and general seabream seabass assumptions
Code
# Load functions and assumptionssource("a_functions.R")source("b_fish_assumptions.R") source("c_model_assumptions.R")
Modelling assumptions
Define parameters over which to calculate share of simulation results. Definition of xvar provided in the table below.
Approach
Meaning of ‘xvar’ variable means in context of calculations
Moral Value
Moral value of improving fish welfare for 1 year through intervention relative to averting 1 DALY
Welfare Range
Welfare gain from intervention - expressed as % of entire fish welfare range (negative to positive
Code
# Share of fish life affected by welfare intervention fishlifeshare_values <-c(0.01,0.02,0.05,0.10,0.15,0.25,0.35,0.50)# Moral value points mv_table_values <-c(0.01,0.05,0.1,0.25,0.5,0.75,1,5,10,25)# Moral value points wr_table_values <-c(0.001,0.01,0.025,0.5,0.1,0.15,0.35,0.5,0.75,0.9,1)
Calculate number of fish affected per dollar spent
Code
# Number of fish affected per dollarno_fish_affected <-function(country,species) {consumption(country,species)*1E3/weight(species)*mshare(country)*psuccess()*stun_share(species)* years_credit* implementation_discount* fish_grocery}
Life span of affected fish
Code
# Fish years of affected fish}lifespan_affected_fish <-function(country,species) {no_fish_affected(country,species)*lifexp(species)/12}
Execution functions
Produce a table that produces every possible combination of country and species, and a list of functions over which to calculate results.
I now write a function that calculates number of fish / lifespan affected per country/species pair, sums across all the country/species pairs, and then divides by costs across all countries.
Code
output_per_dollar <-function(fish_function) {pmap( # Allows repeated calcs across different input vectors country_species, # Input vector 1: countries to run function overget(fish_function) # Calculation function (no of fish or minutes) ) %>%as.data.frame() %>%# Convert to data.framemutate(total=rowSums(.)) %>%# Work out sum of all country and species combinationspull(total)/(no_of_countries*cost()) # Divide by costs across all countries}
Run calculations
I then produce tables with montecarlo simulation results for each measure in both wide and long format.
#Moral value approachshare_mv <-function(xvar,fishlifeshare,bar) {mean(bar>(1/(fypd*fishlifeshare*xvar)))} # Welfare range approachshare_wr <-function(xvar,fishlifeshare,bar) {mean(bar> (1/ (xvar* fypd* fishlifeshare* salmon_wr/ DALY_share_of_welfare_range )))}
Calculations
Define xvar (moral value or welfare range) values for calculating share of simulations beating a particular $/DALY bar and create combinations of xvar values, bar values and, lifeshare values over which to calculate results.
share_sims_results_table <-# Generate one row for unique combination of xvar_values and scenario lists share_sims_calculation_grid %>%# Core calcualtions using share_mv and share_wr calculation functionsrowwise() %>%mutate(moral_value=share_mv(xvar,fishlifeshare,bar),welfare_range=share_wr(xvar,fishlifeshare,bar) )
Format results table
Code
share_sims_results_table %<>%# Convert pivot into longer format to make results easier to plotpivot_longer(cols=c(moral_value,welfare_range),names_to="approach",values_to="share_of_simulations") %>%# Change descriptions of variables to make results easier to interpertmutate(approach=str_to_sentence(str_replace_all(approach,"_"," ")),bar_factor=factor(bar,levels=bar_values,labels=c("50","1K","70K")),fishlifeshare_factor=factor(fishlifeshare,levels=rev(fishlifeshare_values),labels=paste0(rev(fishlifeshare_values)*100,"%")))
Results tables/charts production functions
Impact per dollar density plots
Code
# Code to create impact per dollar density plot impact_density_plot <-function(desired_filter,named_description) { impact_per_dollar_long %>%filter(description==desired_filter) %>%ggplot(aes(x=number ) ) +geom_density() +theme_light() +scale_x_log10(labels = scales::comma_format(drop0trailing =TRUE),n.breaks=8) +labs(title=named_description,y="Probability density",x=paste0(named_description," (log scale)"))}
Share of simulations
Chart labels
Code
xlab_wr=" Welfare gain from intervention as a % of total fish welfare range"xlab_mv="Moral value of improving a fish life year via interventionrelative to averting a human DALY"
Chart function
Code
share_sims_chart <-function ( chart_approach,xvar_lower_lim=0,xvar_upper_lim=max(xvar_values), xlab_des,bar_value=50 ) {share_sims_results_table %>%filter( approach==chart_approach, xvar<=xvar_upper_lim, xvar>=xvar_lower_lim, bar==bar_value ) %>%ggplot(aes(x=xvar,y=share_of_simulations,color=fishlifeshare_factor))+geom_line()+scale_color_brewer(palette ="Dark2") +theme_light()+labs(subtitle =str_to_title(paste0(chart_approach," approach")),title=paste0("Share of simulations where intervention beats ",bar_value,"$/DALY benchmark"),y="Share of simulations",x=xlab_des,linetype="$/DALY Benchmark",color="Share of fish \nlifespan affected \nby intervention",caption="Note log scale on x-axis")}
share_sims_results_table_alt <-# Generate one row for unique combination of xvar_values and scenario lists share_sims_calculation_grid_alt %>%# Core calcualtions using share_mv and share_wr calculation functionsrowwise() %>%mutate(base=share_wr(xvar,fishlifeshare,bar),alt=share_wr_alt(xvar,fishlifeshare,bar) ) %>%distinct() %>%mutate(fishlifeshare=factor(fishlifeshare,labels=paste0(fishlifeshare*100,"%"))) %>%select(-bar) %>%pivot_longer(cols=c(base,alt),names_to="daly_assumption",values_to="share_of_sims")
Format output
Code
alt_table_comparison <-function(assumption) {share_sims_results_table_alt %>%filter(daly_assumption==assumption) %>%select(-daly_assumption) %>%pivot_wider(id_cols=xvar,names_from=c(fishlifeshare),values_from=share_of_sims) %>%arrange(xvar) %>%gt() %>%fmt_percent(decimals=1) %>%cols_label(xvar="Welfare gain from intervention as a share of fish welfare range") %>%tab_spanner(label="Share of fish lifespan affected by intervention",columns=-1 )}
Generate output tables
Code
alt_table_comparison("base") %>%tab_header(title="Share of simulations beating $50/DALY - baseline assumptions")
Share of simulations beating $50/DALY - baseline assumptions
Welfare gain from intervention as a share of fish welfare range
Share of fish lifespan affected by intervention
1%
2%
5%
10%
15%
25%
35%
50%
0.1%
0.0%
0.0%
0.0%
0.0%
0.0%
0.0%
0.0%
0.0%
1.0%
0.0%
0.0%
0.0%
0.0%
0.0%
0.0%
0.2%
0.4%
2.5%
0.0%
0.0%
0.0%
0.0%
0.2%
0.7%
1.4%
2.8%
10.0%
0.0%
0.0%
0.4%
1.8%
4.0%
8.2%
12.1%
17.0%
15.0%
0.0%
0.1%
1.0%
4.0%
7.2%
13.0%
17.8%
23.3%
35.0%
0.2%
0.9%
5.0%
12.1%
17.8%
25.9%
31.2%
37.3%
50.0%
0.4%
1.8%
8.2%
17.0%
23.3%
31.4%
37.3%
43.8%
75.0%
1.0%
4.0%
13.0%
23.3%
29.8%
38.6%
44.6%
50.5%
90.0%
1.5%
5.3%
15.6%
26.3%
32.7%
41.7%
47.5%
53.4%
100.0%
1.8%
6.2%
17.0%
28.1%
34.6%
43.8%
49.3%
55.0%
Code
alt_table_comparison("alt") %>%tab_header(title="Share of simulations beating $50/DALY - alternative assumptions")
Share of simulations beating $50/DALY - alternative assumptions
Welfare gain from intervention as a share of fish welfare range