2.18 seems to be a fine answer.
library(gsheet)
library(tidyverse)
observed=gsheet2tbl('docs.google.com/spreadsheets/d/14dXMkK_h-tlA4jQg9nn0VraXD39H6bWtjBJ7HCFi0f0/edit?usp=sharing')
head(observed)
synthetic=gsheet2tbl("docs.google.com/spreadsheets/d/1vmCMwFhrBTEimhfgQEBfsdZl56tohfoC_pLhu8d2QCM/edit#gid=2075705027")
head(synthetic)
(observed_non_zero <- sum(observed$rain!=0))
myfunc <- function(threshold,onz){
s1 <- synthetic %>% mutate(rv = 1 * (rain>=threshold)) %>% pull(rv) %>% sum
abs(s1 - onz) # ideally this should be zero
}
(result1 <- optimise(f = myfunc,
interval = range(synthetic$rain),onz=observed_non_zero))
options(scipen=99999)
result1$minimum
#zoomed out
seq_to_plot <- seq(from=0,to=14,length.out=100)
excess_non_zeros <- map_dbl(seq_to_plot,~myfunc(.,observed_non_zero))
plot(seq_to_plot,excess_non_zeros)
abline(v=result1$minimum)
# zoomed in
seq_to_plot <- seq(from=2.16,to=2.19,length.out=100)
excess_non_zeros <- map_dbl(seq_to_plot,~myfunc(.,observed_non_zero))
plot(seq_to_plot,excess_non_zeros)
abline(v=result1$minimum)
#confirm
myfunc(2.18,observed_non_zero)