Packages

library(pracma)
library(tidyverse)
library(readxl)
library(glmnet)
library(knitr)
library(kableExtra)
library(plotly)
library(paletteer)

Import data

FN1.tbl <- read_excel("data/CropType2018_PixelCounting.xlsx",
                      sheet = "Feuil1", range = "E2:F8", col_names = FALSE,
                      col_types = c("text", "numeric"), 
                      .name_repair = ~ c('crop', 'value')) %>%
  mutate(crop = recode(crop, 
                       'Mill' = 'Millet', 
                       'Groundnut' = 'Groundnuts', 
                       'Coton' = 'Cotton'))

FN2.tbl <- read_excel("data/CropType2018_ConfusionMatrix.xlsx", 
                      sheet = "Matrice_Regroupement 1", 
                      range = "B2:I9", 
                      .name_repair = ~ if_else(.x == '', 'crop', .x)) %>%
  mutate(crop = recode(crop, 
                       'Mill' = 'Millet', 
                       'Groundnut' = 'Groundnuts', 
                       'Coton' = 'Cotton')) %>% 
  rename(Cotton = Coton, Groundnuts = Groundnut)

Sample confusion matrix

FN2.tbl %>% kbl(caption = "Sample confusion matrix") %>%
    kable_styling(bootstrap_options = 
                      c("striped", "hover", "condensed", "responsive"), 
                  full_width = FALSE, font_size = 12) %>% 
  column_spec(1, bold = TRUE)
Sample confusion matrix
crop Maize Other crops Groundnuts Cotton Rice Sorghum Millet
Maize 9713 954 29 2094 144 20 602
Other crops 2125 2448 95 2598 35 78 297
Groundnuts 441 85 419 464 19 37 41
Cotton 2370 541 114 12770 88 121 255
Rice 514 21 2 327 887 23 0
Sorghum 531 133 32 584 12 888 335
Millet 668 27 4 426 14 83 3510

Image classification vector

FN1.tbl %>% kbl(caption = "R vector") %>%
    kable_styling(bootstrap_options = 
                      c("striped", "hover", "condensed", "responsive"), 
                  full_width = FALSE, font_size = 12) %>% 
    column_spec(1, bold = TRUE)
R vector
crop value
Maize 99454021
Other crops 5373340
Groundnuts 651230
Cotton 102803268
Rice 9654264
Sorghum 10911297
Millet 68759277

R vector and sample confusion matrix

R_vector <- FN1.tbl %>% 
  rename(R = value) %>% 
  mutate(crop = if_else(crop %in% c('Other crops', 'Groundnuts', 'Rice', 'Sorghum'), 'Other crops', crop)) %>% 
  group_by(crop) %>% 
  summarise(R = sum(R), .groups = "drop") %>% 
  filter(crop != 'Other crops') %>% 
  column_to_rownames(var = "crop") %>%
  as.matrix() 

a_matrix <- FN2.tbl %>% 
  mutate(crop = if_else(crop %in% c('Other crops', 'Groundnuts', 'Rice', 'Sorghum'), 'Other crops', crop)) %>% 
  group_by(crop) %>% 
  summarise_all(sum) %>% 
  mutate(`Other crops` = `Other crops` + Groundnuts + Rice + Sorghum) %>% 
  select(crop, Cotton, Maize, Millet) %>% 
  filter(crop != 'Other crops') %>% 
  column_to_rownames(var = "crop") %>%
  as.matrix() 
R_vector %>% 
  as.data.frame() %>% 
  rownames_to_column(var = "crop") %>% 
  mutate(R = format(R, nsmall = 0, big.mark = ",", digits = 10)) %>% 
  kable(caption = "R vector", align = "lr") %>%
  kable_styling(bootstrap_options =
                  c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, font_size = 12)
R vector
crop R
Cotton 102,803,268
Maize 99,454,021
Millet 68,759,277
a_matrix %>% 
  as.data.frame() %>% 
  rownames_to_column(var = "crop") %>% 
  kable(caption = "'a' matrix") %>% 
  kable_styling(bootstrap_options = 
                  c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, font_size = 12) %>% 
  column_spec(1, bold = TRUE)
‘a’ matrix
crop Cotton Maize Millet
Cotton 12770 2370 255
Maize 2094 9713 602
Millet 426 668 3510

Function to generate artifitial population

gen_U_star <- function(a_matrix, size){
  sample_pi <-
    expand_grid(
      c = colnames(a_matrix), 
      g = rownames(a_matrix)) %>% 
    mutate(n = c(a_matrix), 
           pi = n/sum(n), 
           Code = paste0(c, ' - ', g))

  U_star <- sample_pi %>% 
    sample_n(size = size, replace = TRUE, weight = pi) %>% 
    select(c, g, pi)
  return(U_star)
}

Get a artifitial population and associate R vector

# Population Size
Size = 1000000
U_0 <- gen_U_star(a_matrix, size = Size)
R_U_0 <- U_0 %>% 
  count(c) %>% 
  arrange(match(c, colnames(a_matrix))) %>% 
  column_to_rownames(var = 'c') %>% 
  as.matrix() 
U_0 %>% head()
## # A tibble: 6 × 3
##   c      g         pi
##   <chr>  <chr>  <dbl>
## 1 Cotton Cotton 0.394
## 2 Cotton Cotton 0.394
## 3 Cotton Cotton 0.394
## 4 Millet Millet 0.108
## 5 Cotton Cotton 0.394
## 6 Cotton Cotton 0.394

Get a artifitial population for bootstrap sampling

U_star <- gen_U_star(a_matrix, size = Size)

Function to get a sample from artifitial population

get_a_sample <- function(U_star, N_0, order = NULL){
  a_sample <- U_star %>% 
    sample_n(N_0) %>% 
    count(c, g) %>% 
    pivot_wider(names_from = 'c', values_from = n, values_fill = 0) %>% 
    column_to_rownames(var = 'g') %>% 
    as.matrix()  
  if (is.null(order)) return(a_sample)
  else return(a_sample[order, order])
}

Function to get estimates from sample confution matrix from artifitial population

get_estimates <- function(a_matrix, R_U_0){
  e_g_dado_c <- a_matrix %*% solve(diag(colSums(a_matrix)))
  e_c_dado_g <- t(a_matrix) %*% solve(diag(rowSums(a_matrix)))
  inverse_e_c_dado_g <- solve(e_c_dado_g)
  rownames(inverse_e_c_dado_g) <- rownames(e_c_dado_g)
  
  direct <- (e_g_dado_c %*% R_U_0) %>% 
    as_tibble(rownames = 'crop') %>% 
    mutate(estimator = 'direct')
  
  inverse <- (inverse_e_c_dado_g %*% R_U_0) %>%
    as_tibble(rownames = 'crop') %>% 
    mutate(estimator = 'inverse') 
  
  return(bind_rows(direct, inverse))
}

Function to get bootstrap replicas

get_bootstrap <- function(B, U_star, N_0, R_U_0, order = NULL) {
  1:B %>% 
    map(~ get_a_sample(U_star, N_0, order)) %>% 
    map_dfr(~ get_estimates(.x, R_U_0)) %>% 
    mutate(replica = ceiling(row_number()/6))
}

Generate 1000 bootstrap replicas

set.seed(11235)
N_0 <- sum(a_matrix)
result <- get_bootstrap(1000, U_star, N_0, R_U_0, order = colnames(a_matrix))

Plot result

result %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75) + 
  facet_wrap(~ crop, scales = 'free') +
  theme(legend.position="bottom") + labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
   scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`)

Bootstrap summary

result %>% group_by(crop, estimator) %>% 
    summarise(Mean = mean(n), 
              Variance = var(n), 
              `Standard Deviation` = sd(n), 
              CV = `Standard Deviation`/Mean, 
              .groups = 'drop') %>% 
  rename(Crop = crop, Estimator = estimator) %>% 
  mutate(Mean = format(round(Mean), nsmall = 0, big.mark = ","),
         Variance = format(round(Variance), nsmall = 0, big.mark = ","),
         `Standard Deviation` = format(round(`Standard Deviation`), nsmall = 0, big.mark = ","),
         CV = format(round(CV, 4), nsmall = 2, big.mark = ",")) %>% 
  kable(caption = "Direct and Inverse Estimators Bootstrap Results (1000 rounds)", align = "lcrrrr") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                full_width = FALSE, font_size = 12) %>% 
  column_spec(1, bold = TRUE) 
Direct and Inverse Estimators Bootstrap Results (1000 rounds)
Crop Estimator Mean Variance Standard Deviation CV
Cotton direct 475,115 3,943,709 1,986 0.0042
Cotton inverse 476,093 9,296,184 3,049 0.0064
Maize direct 382,685 4,306,957 2,075 0.0054
Maize inverse 381,801 11,091,144 3,330 0.0087
Millet direct 142,200 1,506,982 1,228 0.0086
Millet inverse 142,106 2,841,263 1,686 0.0119

Other plots

result %>% filter(crop == 'Maize') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Maize")

result %>% filter(crop == 'Millet') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Millet")

result %>% filter(crop == 'Cotton') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') + 
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Cotton")

result %>% filter(crop == 'Cotton') %>% ggplot() +
  geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density), color = estimator), 
                 alpha = .75, 
                 bins = 30, 
                 position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  scale_color_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Cotton")

result %>% filter(crop == 'Maize') %>% ggplot() +
  geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density), color = estimator), 
                 alpha = .75, 
                 bins = 30, 
                 position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  scale_color_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Maize")

result %>% filter(crop == 'Millet') %>% ggplot() +
  geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density), color = estimator), 
                 alpha = .75, 
                 bins = 30, 
                 position = 'identity') +
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  scale_color_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Millet")

result %>% 
    ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density), color = estimator), 
                   alpha = .5, 
                   bins = 30, 
                   position = 'identity') +
    geom_density(aes(x = n/1000, color = estimator), size = .75, position = 'identity') + 
    facet_wrap(~ crop, scales = 'free') +
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Estimator') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    scale_color_paletteer_d(`"ggthemes::wsj_red_green"`)

Jacques’ Matrix

jacques_tibble <- read_excel("data/Simdata.xlsx", 
                      sheet = "Sheet1", 
                      range = "B3:G8",
                      .name_repair = ~ if_else(.x == '', 'crop', .x))


jacques_matrix <- jacques_tibble %>% 
  column_to_rownames(var = "crop") %>% 
  as.matrix() 
jacques_matrix %>% 
  as.data.frame() %>% 
  rownames_to_column(var = "crop") %>% 
  kable(caption = "Jacques' matrix") %>% 
  kable_styling(bootstrap_options = 
                  c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, font_size = 12) %>% 
  column_spec(1, bold = TRUE)
Jacques’ matrix
crop Wheat Rapeseed Corn Sugarbeet Others
Wheat 0.200 0.020 0.005 0.005 0.020
Rapeseed 0.010 0.030 0.000 0.000 0.010
Corn 0.001 0.010 0.080 0.005 0.004
Sugarbeet 0.005 0.015 0.040 0.120 0.020
Others 0.100 0.020 0.010 0.030 0.240

\(T\) and \(V\) vectors

R_vector <- jacques_matrix %>% 
  { matrix(colSums(.), ncol = 1, dimnames = list(colnames(.), 'value')) * Size } %>%
  t() 
T_vector <- jacques_matrix %>% 
  { matrix(rowSums(.), ncol = 1, dimnames = list(colnames(.), 'value')) * Size } 

Reference <- 
  bind_rows(
    T_vector %>% as_tibble(rownames = "crop") %>% mutate(References = "T"),
    R_vector %>% t() %>% as_tibble(rownames = "crop") %>% mutate(References = "R")
  ) %>% 
  mutate(crop = factor(crop, levels = colnames(jacques_matrix), ordered = TRUE))
jacques_tibble %>% 
  mutate_if(is.numeric, function(x) 1000*x) %>% 
  bind_rows(as_tibble(R_vector/1000) %>% mutate(crop = 'R')) %>% 
  bind_cols(as_tibble(T_vector/1000) %>% rename(T = value) %>% 
              bind_rows(tibble(T = 1000))) %>% 
  kable(caption = "Jacques' confusion sample matrix") %>% 
  kable_styling(bootstrap_options = 
                  c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, font_size = 12) %>% 
  column_spec(c(1,7), bold = TRUE) %>% 
  row_spec(6, bold = TRUE)
Jacques’ confusion sample matrix
crop Wheat Rapeseed Corn Sugarbeet Others T
Wheat 200 20 5 5 20 250
Rapeseed 10 30 0 0 10 50
Corn 1 10 80 5 4 100
Sugarbeet 5 15 40 120 20 200
Others 100 20 10 30 240 400
R 316 95 135 160 294 1000

Get a artifitial population and associate R vector

# Population Size
Size = 1000000
U_0 <- gen_U_star(jacques_matrix, size = Size)
R_U_0 <- U_0 %>% 
  count(c) %>% 
  arrange(match(c, colnames(jacques_matrix))) %>% 
  column_to_rownames(var = 'c') %>% 
  as.matrix() 
U_0 %>% head()
## # A tibble: 6 × 3
##   c        g            pi
##   <chr>    <chr>     <dbl>
## 1 Rapeseed Sugarbeet 0.015
## 2 Wheat    Wheat     0.2  
## 3 Corn     Corn      0.08 
## 4 Others   Others    0.24 
## 5 Corn     Sugarbeet 0.04 
## 6 Wheat    Others    0.1

Get a artifitial population for bootstrap sampling

U_star <- gen_U_star(jacques_matrix, size = Size)

Generate 1000 bootstrap replicas

set.seed(112358)
N_0 <- 1000
result <- get_bootstrap(1000, U_star, N_0, R_U_0, order = colnames(jacques_matrix))

Plot result

Palette <- palettes_d_names %>% 
  filter(length == 3) %>% 
  mutate(code = paste0(package, "::", palette)) %>% 
  pull(code) %>% 
  .[7] #3 5


result %>% 
  mutate(crop = factor(crop, levels = colnames(jacques_matrix), ordered = TRUE)) %>% 
  ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  facet_wrap(~ crop, scales = 'free', ncol = 2) +
  theme(legend.position="bottom") + labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  geom_vline(aes(xintercept = value/1000, color = References), size = 1, data = Reference) + 
  scale_color_paletteer_d(Palette)

Bootstrap summary

table <- result %>% group_by(crop, estimator) %>% 
    summarise(Mean = mean(n), 
              Variance = var(n), 
              `Standard Deviation` = sd(n), 
              CV = `Standard Deviation`/Mean, 
              .groups = 'drop') %>% 
  mutate(crop = factor(crop, levels = colnames(jacques_matrix), ordered = TRUE)) %>% 
  left_join(Reference %>% pivot_wider(names_from = References), by = "crop") %>% 
  mutate(`T Bias` = `T` - Mean,
         `R Bias` = `R` - Mean) %>% 
  rename(Crop = crop, Estimator = estimator) %>% 
  mutate(Mean = format(round(Mean), nsmall = 0, big.mark = ","),
         Variance = format(round(Variance), nsmall = 0, big.mark = ","),
         `Standard Deviation` = format(round(`Standard Deviation`), nsmall = 0, big.mark = ","),
         CV = format(round(CV, 4), nsmall = 2, big.mark = ","), 
         `T` = format(round(`T`), nsmall = 0, big.mark = ","),
         `T Bias` = format(round(`T Bias`), nsmall = 0, big.mark = ","),
         `R` = format(round(`R`), nsmall = 0, big.mark = ","),
         `R Bias` = format(round(`R Bias`), nsmall = 0, big.mark = ","))
table %>% 
  kable(caption = "Direct and Inverse Estimators Bootstrap Results (1000 rounds)", align = "lcrrrr") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                full_width = FALSE, font_size = 12) %>% 
  column_spec(1, bold = TRUE) %>% 
  column_spec(9, color = if_else(str_detect(table$`T Bias`, "-"), 'red', 'black')) %>%  
  column_spec(10, color = if_else(str_detect(table$`R Bias`, "-"), 'red', 'black')) 
Direct and Inverse Estimators Bootstrap Results (1000 rounds)
Crop Estimator Mean Variance Standard Deviation CV T R T Bias R Bias
Corn direct 99,732 53,116,043 7,288 0.0731 100,000 135,000 268 35,268
Corn inverse 99,058 159,367,831 12,624 0.1274 100,000 135,000 942 35,942
Others direct 399,778 167,847,636 12,956 0.0324 400,000 294,000 222 -105,778
Others inverse 396,422 602,911,322 24,554 0.0619 400,000 294,000 3,578 -102,422
Rapeseed direct 50,288 40,733,170 6,382 0.1269 50,000 95,000 -288 44,712
Rapeseed inverse 52,218 262,466,182 16,201 0.3103 50,000 95,000 -2,218 42,782
Sugarbeet direct 200,513 104,372,156 10,216 0.0510 200,000 160,000 -513 -40,513
Sugarbeet inverse 201,257 303,354,316 17,417 0.0865 200,000 160,000 -1,257 -41,257
Wheat direct 249,689 120,334,207 10,970 0.0439 250,000 316,000 311 66,311
Wheat inverse 251,045 436,308,560 20,888 0.0832 250,000 316,000 -1,045 64,955

Other plots

result %>% filter(crop == 'Wheat') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Wheat") +
  geom_vline(aes(xintercept = value/1000, color = References), 
             size = 1, data = Reference %>% filter(crop == 'Wheat')) + 
  scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Rapeseed') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') +
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Rapeseed") +
  geom_vline(aes(xintercept = value/1000, color = References), 
             size = 1, data = Reference %>% filter(crop == 'Rapeseed')) + 
  scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Corn') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') + 
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Corn") +
  geom_vline(aes(xintercept = value/1000, color = References), 
             size = 1, data = Reference %>% filter(crop == 'Corn')) + 
  scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Sugarbeet') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') + 
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Sugarbeet") +
  geom_vline(aes(xintercept = value/1000, color = References), 
             size = 1, data = Reference %>% filter(crop == 'Sugarbeet')) + 
  scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Others') %>% ggplot() +
  geom_density(aes(x = n/1000, fill = estimator), alpha = .75, position = 'identity') + 
  theme(legend.position="bottom") + 
  labs(x = 'Estimates', y  = 'Density', fill = 'Estimator') + 
  scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
  ggtitle("Crop: Others") +
  geom_vline(aes(xintercept = value/1000, color = References), 
             size = 1, data = Reference %>% filter(crop == 'Others')) + 
  scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Wheat') %>% ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), color = 'gray', 
                   alpha = .75, 
                   bins = 30, 
                   position = 'identity') + 
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Reference') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    ggtitle("Crop: Wheat") +
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference %>% filter(crop == 'Wheat')) + 
    scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Rapeseed') %>% ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), color = 'gray', 
                   alpha = .75, 
                   bins = 30, 
                   position = 'identity') + 
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Reference') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    ggtitle("Crop: Rapeseed") +
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference %>% filter(crop == 'Rapeseed')) + 
    scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Corn') %>% ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), color = 'gray', 
                   alpha = .75, 
                   bins = 30, 
                   position = 'identity') + 
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Reference') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    ggtitle("Crop: Corn") +
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference %>% filter(crop == 'Corn')) + 
    scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Sugarbeet') %>% ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), color = 'gray', 
                   alpha = .75, 
                   bins = 30, 
                   position = 'identity') + 
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Reference') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    ggtitle("Crop: Sugarbeet") +
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference %>% filter(crop == 'Sugarbeet')) + 
    scale_color_paletteer_d(Palette)

result %>% filter(crop == 'Others') %>% ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), color = 'gray', 
                   alpha = .75, 
                   bins = 30, 
                   position = 'identity') + 
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Reference') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    ggtitle("Crop: Others") +
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference %>% filter(crop == 'Others')) + 
    scale_color_paletteer_d(Palette)

result %>% 
    ggplot() +
    geom_histogram(aes(x = n/1000, fill = estimator, y=stat(density)), 
                   alpha = .5, 
                   bins = 30, 
                   position = 'identity') +
    geom_density(aes(x = n/1000), size = .75, position = 'identity') + 
    facet_wrap(~ crop, scales = 'free') +
    theme(legend.position="bottom") + 
    labs(x = 'Estimates', y  = 'Density', fill = 'Estimator', color = 'Estimator') +
    scale_fill_paletteer_d(`"ggthemes::wsj_red_green"`) + 
    geom_vline(aes(xintercept = value/1000, color = References), 
               size = 1, data = Reference) + 
    scale_color_paletteer_d(Palette)