Test the lower-bounded linear-continuous optimal allocation solution function from package. Use the Guatemala-Cebu scrambled nutrition and height early childhood data, same as line by line.

The objective of this file is to solve the linear \(N_i\) and \(H_i\) problem without invoking any functions. This function first tested out the linear solution algorithm.

File name ffv_opt_solin_relow_allfn:

  • opt: optimal allocation project
  • solin: linear production function
  • relow: solution method relying on relative allocation against lowest (first) to receive subsidy
  • allfn: all code by calling relevant functions

See associated functions in the ffv_opt_dtgch_cbem4 file.

Load Packages and Data

Load Dependencies

rm(list = ls(all.names = TRUE))
options(knitr.duplicate.label = 'allow')

Get Data and Regression Results

Generate four categories by initial height and mother’s education levels combinations.

# Load Data and Estimation Results: A and alpha, lin and loglin
ls_opti_alpha_A <- ffy_opt_dtgch_cbem4()
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
df_raw <- ls_opti_alpha_A$df_raw
df_hw_cebu_m24 <- df_raw
df_esti <- ls_opti_alpha_A$df_esti

# Review dataframes
# raw file
head(df_raw, 10)
## # A tibble: 10 x 17
##    S.country vil.id indi.id sex    svymthRound momEdu wealthIdx   hgt    wgt
##    <chr>      <dbl>   <dbl> <chr>        <dbl>  <dbl>     <dbl> <dbl>  <dbl>
##  1 Cebu           1       2 Female          24    7.1       7.3  79.2 10776.
##  2 Cebu           1       3 Female          24    9.4      10.3  76.7 10296.
##  3 Cebu           1       4 Female          24   13.9      13.3  78.1  7604.
##  4 Cebu           1       5 Male            24   11.3       9.3  84.1 11787.
##  5 Cebu           1       6 Female          24    7.3       7.3  76.9  7991.
##  6 Cebu           1       7 Male            24   10.4       8.3  79.6 12583.
##  7 Cebu           1       8 Female          24   13.5       9.3  81.5  8358.
##  8 Cebu           1       9 Female          24   10.4      17.3  74.7  8195.
##  9 Cebu           1      10 Male            24   10.5       6.3  77.1  9442.
## 10 Cebu           1      11 Male            24    1.9       6.3  72.1  6627.
## # ... with 8 more variables: hgt0 <dbl>, wgt0 <dbl>, prot <dbl>, cal <dbl>,
## #   p.A.prot <dbl>, p.A.nProt <dbl>, momEduRound <fct>, hgt0med <fct>
head(df_esti, 10)
## # A tibble: 10 x 9
##    S.country vil.id indi.id svymthRound alpha_lin alpha_log A_lin A_log     beta
##    <chr>      <dbl>   <dbl>       <dbl>     <dbl>     <dbl> <dbl> <dbl>    <dbl>
##  1 Cebu           1       2          24    0.0101   0.00669  78.4  4.34 0.000959
##  2 Cebu           1       3          24    0.0323   0.00925  79.9  4.36 0.000959
##  3 Cebu           1       4          24    0.0323   0.00925  78.9  4.35 0.000959
##  4 Cebu           1       5          24    0.0662   0.0139   80.6  4.37 0.000959
##  5 Cebu           1       6          24    0.0101   0.00669  77.8  4.34 0.000959
##  6 Cebu           1       7          24    0.0662   0.0139   79.6  4.36 0.000959
##  7 Cebu           1       8          24    0.0101   0.00669  77.8  4.34 0.000959
##  8 Cebu           1       9          24    0.0101   0.00669  78.0  4.34 0.000959
##  9 Cebu           1      10          24    0.0662   0.0139   80.0  4.36 0.000959
## 10 Cebu           1      11          24    0.0602   0.0121   77.7  4.33 0.000959
# Attach
attach(df_raw)
## The following objects are masked from df_hw:
## 
##     cal, hgt, hgt0, hgt0med, indi.id, momEdu, momEduRound, p.A.nProt,
##     p.A.prot, prot, S.country, sex, svymthRound, vil.id, wealthIdx,
##     wgt, wgt0

Optimal Allocations

Common Parameters for Optimal Allocation

# Child Count
df_hw_cebu_m24_full <- df_hw_cebu_m24
it_obs = dim(df_hw_cebu_m24)[1]

# Total Resource Count
ar_prot_data = df_hw_cebu_m24$prot
fl_N_agg = sum(ar_prot_data)

# Vector of Planner Preference
ar_rho = c(seq(-200, -100, length.out=5), seq(-100, -25, length.out=5), seq(-25, -5, length.out=5),
           seq(-5, -1, length.out=5), seq(-1, -0.01, length.out=5), seq(0.01, 0.25, length.out=5), seq(0.25, 0.99, length.out=5))
ar_rho = c(-50, -25, -10)
ar_rho = unique(ar_rho)

Optimal Linear Allocation (CRS)

This also works with any CRS CES.

# Matrixes to Store Solution Results
tb_opti_alloc_all_rho <- df_esti
mt_hev_lin = matrix(, nrow = length(ar_rho), ncol = 2)
mt_opti_N = matrix(, nrow = it_obs, ncol = length(ar_rho))
mt_opti_H = matrix(, nrow = it_obs, ncol = length(ar_rho))

# A. First Loop over Planner Preference
# Generate Rank Order
for (it_rho_ctr in seq(1,length(ar_rho))) {
  rho = ar_rho[it_rho_ctr]

  # B. Parameters for solving the optimal allocation problem
  df <- df_esti
  svr_A_i <- 'A_lin'
  svr_alpha_i <- 'alpha_lin'
  svr_beta_i <- 'beta'
  fl_N_agg <- fl_N_agg
  fl_rho <- rho

  # C. Invoke optimal linear (crs) solution problem
  # ar_opti is the array of optimal choices, it is in df_opti as well.
  # use df_opti for merging, because that contains the individual keys.
  # actually file here should contain unique keys, unique key ID as required input. should return?
  # actually it is fine, the function here needs the key, not solin_flinr
  
  svr_inpalc <- 'opti_allocate'
  svr_expout <- 'opti_exp_outcome'
  ls_lin_solu <- ffp_opt_solin_relow(df, svr_A_i, svr_alpha_i, svr_beta_i, fl_N_agg, fl_rho,
                                     svr_inpalc, svr_expout)
  tb_opti <- ls_lin_solu$df_opti
  ar_opti_inpalc <- ls_lin_solu$ar_opti_inpalc
  ar_opti_expout <- ls_lin_solu$ar_opti_expout

  mt_opti_N[, it_rho_ctr] = ar_opti_inpalc
  mt_opti_H[, it_rho_ctr] = ar_opti_expout

  # m. Keep for df collection individual key + optimal allocation
  # _on stands for optimal nutritional choices
  # _eh stands for expected height
  tb_opti_main_results <- tb_opti %>%
    select(-one_of(c('lowest_rank_alpha', 'lowest_rank_beta')))
  tb_opti_allocate_wth_key <- tb_opti %>% select(one_of('indi.id', svr_inpalc, svr_expout)) %>%
                                rename(!!paste0('rho_c', it_rho_ctr, '_on') := !!sym(svr_inpalc),
                                       !!paste0('rho_c', it_rho_ctr, '_eh') := !!sym(svr_expout))

  # n. merge optimal allocaiton results from different planner preference
  tb_opti_alloc_all_rho <- tb_opti_alloc_all_rho %>% left_join(tb_opti_allocate_wth_key, by='indi.id')

  # print subset
  if (it_rho_ctr == 1 | it_rho_ctr == length(ar_rho) | it_rho_ctr == round(length(ar_rho)/2)) {
    print('')
    print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
    print(paste0('xxx rho:', rho))
    print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
    print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

    print(summary(tb_opti_main_results))
  }
}
## Warning: Unknown columns: `lowest_rank_alpha`, `lowest_rank_beta`
## [1] ""
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxx rho:-50"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
##   S.country             vil.id         indi.id        svymthRound
##  Length:1043        Min.   : 1.00   Min.   :   2.0   Min.   :24  
##  Class :character   1st Qu.: 7.00   1st Qu.: 298.5   1st Qu.:24  
##  Mode  :character   Median :12.00   Median : 627.0   Median :24  
##                     Mean   :12.52   Mean   : 634.8   Mean   :24  
##                     3rd Qu.:17.00   3rd Qu.: 939.5   3rd Qu.:24  
##                     Max.   :33.00   Max.   :1349.0   Max.   :24  
##      alpha           alpha_log              A             A_log      
##  Min.   :0.01014   Min.   :0.006687   Min.   :73.70   Min.   :4.287  
##  1st Qu.:0.01014   1st Qu.:0.006687   1st Qu.:77.73   1st Qu.:4.334  
##  Median :0.06018   Median :0.012090   Median :78.45   Median :4.343  
##  Mean   :0.04108   Mean   :0.010321   Mean   :78.46   Mean   :4.343  
##  3rd Qu.:0.06018   3rd Qu.:0.012090   3rd Qu.:79.15   3rd Qu.:4.351  
##  Max.   :0.06619   Max.   :0.013940   Max.   :84.61   Max.   :4.417  
##       beta              rank_val         rank_idx      lowest_rank_A 
##  Min.   :0.0009588   Min.   : 89.24   Min.   :   1.0   Min.   :73.7  
##  1st Qu.:0.0009588   1st Qu.: 95.00   1st Qu.: 261.5   1st Qu.:73.7  
##  Median :0.0009588   Median : 96.39   Median : 522.0   Median :73.7  
##  Mean   :0.0009588   Mean   : 96.25   Mean   : 522.0   Mean   :73.7  
##  3rd Qu.:0.0009588   3rd Qu.: 97.58   3rd Qu.: 782.5   3rd Qu.:73.7  
##  Max.   :0.0009588   Max.   :103.71   Max.   :1043.0   Max.   :73.7  
##  rela_slope_to_lowest rela_intercept_to_lowest rela_slope_to_lowest_cumsum
##  Min.   :0.9108       Min.   :-823.49          Min.   :   1.0             
##  1st Qu.:1.0000       1st Qu.:-601.87          1st Qu.: 297.9             
##  Median :1.0000       Median :-101.68          Median : 735.0             
##  Mean   :2.6677       Mean   :-286.05          Mean   : 990.1             
##  3rd Qu.:5.7284       3rd Qu.: -76.81          3rd Qu.:1599.0             
##  Max.   :5.7284       Max.   :   0.00          Max.   :2782.4             
##  rela_intercept_to_lowest_cumsum rela_slope_to_lowest_cumsum_invert
##  Min.   :-298346                 Min.   :0.0003594                 
##  1st Qu.:-151286                 1st Qu.:0.0006254                 
##  Median : -58624                 Median :0.0013606                 
##  Mean   : -91892                 Mean   :0.0066321                 
##  3rd Qu.: -19563                 3rd Qu.:0.0033568                 
##  Max.   :      0                 Max.   :1.0000000                 
##  rela_intercept_to_lowest_cumsum_invert rela_x_intercept
##  Min.   :  0.00                         Min.   :  0.00  
##  1st Qu.: 65.67                         1st Qu.: 79.01  
##  Median : 79.76                         Median : 98.16  
##  Mean   : 78.57                         Mean   : 96.11  
##  3rd Qu.: 94.61                         3rd Qu.:114.36  
##  Max.   :107.22                         Max.   :198.59  
##  opti_lowest_spline_knots    tot_devi      allocate_lowest opti_allocate   
##  Min.   :     0           Min.   :-22823   Min.   :107.9   Min.   :  0.00  
##  1st Qu.:  3973           1st Qu.:-18849   1st Qu.:107.9   1st Qu.:  0.00  
##  Median : 13521           Median : -9301   Median :107.9   Median : 14.17  
##  Mean   : 21151           Mean   : -1672   Mean   :107.9   Mean   : 21.88  
##  3rd Qu.: 31587           3rd Qu.:  8764   3rd Qu.:107.9   3rd Qu.: 33.13  
##  Max.   :254218           Max.   :231396   Max.   :107.9   Max.   :302.98  
##  allocate_total  opti_exp_outcome
##  Min.   :22823   Min.   :77.44   
##  1st Qu.:22823   1st Qu.:78.36   
##  Median :22823   Median :80.19   
##  Mean   :22823   Mean   :79.45   
##  3rd Qu.:22823   3rd Qu.:80.19   
##  Max.   :22823   Max.   :84.61
## Warning: Unknown columns: `lowest_rank_alpha`, `lowest_rank_beta`
## [1] ""
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxx rho:-25"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
##   S.country             vil.id         indi.id        svymthRound
##  Length:1043        Min.   : 1.00   Min.   :   2.0   Min.   :24  
##  Class :character   1st Qu.: 7.00   1st Qu.: 298.5   1st Qu.:24  
##  Mode  :character   Median :12.00   Median : 627.0   Median :24  
##                     Mean   :12.52   Mean   : 634.8   Mean   :24  
##                     3rd Qu.:17.00   3rd Qu.: 939.5   3rd Qu.:24  
##                     Max.   :33.00   Max.   :1349.0   Max.   :24  
##      alpha           alpha_log              A             A_log      
##  Min.   :0.01014   Min.   :0.006687   Min.   :73.70   Min.   :4.287  
##  1st Qu.:0.01014   1st Qu.:0.006687   1st Qu.:77.73   1st Qu.:4.334  
##  Median :0.06018   Median :0.012090   Median :78.45   Median :4.343  
##  Mean   :0.04108   Mean   :0.010321   Mean   :78.46   Mean   :4.343  
##  3rd Qu.:0.06018   3rd Qu.:0.012090   3rd Qu.:79.15   3rd Qu.:4.351  
##  Max.   :0.06619   Max.   :0.013940   Max.   :84.61   Max.   :4.417  
##       beta              rank_val        rank_idx      lowest_rank_A 
##  Min.   :0.0009588   Min.   :107.3   Min.   :   1.0   Min.   :73.7  
##  1st Qu.:0.0009588   1st Qu.:114.2   1st Qu.: 261.5   1st Qu.:73.7  
##  Median :0.0009588   Median :116.7   Median : 522.0   Median :73.7  
##  Mean   :0.0009588   Mean   :117.2   Mean   : 522.0   Mean   :73.7  
##  3rd Qu.:0.0009588   3rd Qu.:120.5   3rd Qu.: 782.5   3rd Qu.:73.7  
##  Max.   :0.0009588   Max.   :126.1   Max.   :1043.0   Max.   :73.7  
##  rela_slope_to_lowest rela_intercept_to_lowest rela_slope_to_lowest_cumsum
##  Min.   :0.9125       Min.   :-1055.06         Min.   :   1.0             
##  1st Qu.:1.0000       1st Qu.: -833.45         1st Qu.: 259.5             
##  Median :1.0000       Median :  -99.67         Median : 513.6             
##  Mean   :2.6023       Mean   : -366.22         Mean   : 834.7             
##  3rd Qu.:5.5393       3rd Qu.:  -75.96         3rd Qu.:1300.9             
##  Max.   :5.5393       Max.   :    0.00         Max.   :2714.1             
##  rela_intercept_to_lowest_cumsum rela_slope_to_lowest_cumsum_invert
##  Min.   :-381972                 Min.   :0.0003684                 
##  1st Qu.:-146435                 1st Qu.:0.0007687                 
##  Median : -39659                 Median :0.0019471                 
##  Mean   : -93578                 Mean   :0.0070255                 
##  3rd Qu.: -16917                 3rd Qu.:0.0038538                 
##  Max.   :      0                 Max.   :1.0000000                 
##  rela_intercept_to_lowest_cumsum_invert rela_x_intercept
##  Min.   :  0.00                         Min.   :  0.00  
##  1st Qu.: 65.19                         1st Qu.: 79.02  
##  Median : 77.22                         Median :108.08  
##  Mean   : 86.34                         Mean   :112.93  
##  3rd Qu.:112.56                         3rd Qu.:151.52  
##  Max.   :140.73                         Max.   :215.41  
##  opti_lowest_spline_knots    tot_devi      allocate_lowest opti_allocate   
##  Min.   :     0           Min.   :-22823   Min.   :120.6   Min.   :  0.00  
##  1st Qu.:  3588           1st Qu.:-19234   1st Qu.:120.6   1st Qu.:  0.00  
##  Median : 15848           Median : -6975   Median :120.6   Median : 13.87  
##  Mean   : 29311           Mean   :  6489   Mean   :120.6   Mean   : 21.88  
##  3rd Qu.: 50677           3rd Qu.: 27855   3rd Qu.:120.6   3rd Qu.: 40.99  
##  Max.   :202670           Max.   :179848   Max.   :120.6   Max.   :121.45  
##  allocate_total  opti_exp_outcome
##  Min.   :22823   Min.   :75.60   
##  1st Qu.:22823   1st Qu.:78.36   
##  Median :22823   Median :80.96   
##  Mean   :22823   Mean   :79.79   
##  3rd Qu.:22823   3rd Qu.:80.96   
##  Max.   :22823   Max.   :84.61
## Warning: Unknown columns: `lowest_rank_alpha`, `lowest_rank_beta`
## [1] ""
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxx rho:-10"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
## [1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
##   S.country             vil.id         indi.id        svymthRound
##  Length:1043        Min.   : 1.00   Min.   :   2.0   Min.   :24  
##  Class :character   1st Qu.: 7.00   1st Qu.: 298.5   1st Qu.:24  
##  Mode  :character   Median :12.00   Median : 627.0   Median :24  
##                     Mean   :12.52   Mean   : 634.8   Mean   :24  
##                     3rd Qu.:17.00   3rd Qu.: 939.5   3rd Qu.:24  
##                     Max.   :33.00   Max.   :1349.0   Max.   :24  
##      alpha           alpha_log              A             A_log      
##  Min.   :0.01014   Min.   :0.006687   Min.   :73.70   Min.   :4.287  
##  1st Qu.:0.01014   1st Qu.:0.006687   1st Qu.:77.73   1st Qu.:4.334  
##  Median :0.06018   Median :0.012090   Median :78.45   Median :4.343  
##  Mean   :0.04108   Mean   :0.010321   Mean   :78.46   Mean   :4.343  
##  3rd Qu.:0.06018   3rd Qu.:0.012090   3rd Qu.:79.15   3rd Qu.:4.351  
##  Max.   :0.06619   Max.   :0.013940   Max.   :84.61   Max.   :4.417  
##       beta              rank_val        rank_idx      lowest_rank_A 
##  Min.   :0.0009588   Min.   :179.0   Min.   :   1.0   Min.   :73.7  
##  1st Qu.:0.0009588   1st Qu.:190.1   1st Qu.: 261.5   1st Qu.:73.7  
##  Median :0.0009588   Median :194.0   Median : 522.0   Median :73.7  
##  Mean   :0.0009588   Mean   :202.8   Mean   : 522.0   Mean   :73.7  
##  3rd Qu.:0.0009588   3rd Qu.:220.6   3rd Qu.: 782.5   3rd Qu.:73.7  
##  Max.   :0.0009588   Max.   :227.1   Max.   :1043.0   Max.   :73.7  
##  rela_slope_to_lowest rela_intercept_to_lowest rela_slope_to_lowest_cumsum
##  Min.   :0.917        Min.   :-1659.82         Min.   :   1.0             
##  1st Qu.:1.000        1st Qu.:-1438.21         1st Qu.: 257.5             
##  Median :1.000        Median :  -94.39         Median : 505.2             
##  Mean   :2.431        Mean   : -575.93         Mean   : 787.4             
##  3rd Qu.:5.046        3rd Qu.:  -73.43         3rd Qu.:1221.2             
##  Max.   :5.046        Max.   :    0.00         Max.   :2535.6             
##  rela_intercept_to_lowest_cumsum rela_slope_to_lowest_cumsum_invert
##  Min.   :-600691                 Min.   :0.0003944                 
##  1st Qu.:-203379                 1st Qu.:0.0008189                 
##  Median : -37596                 Median :0.0019792                 
##  Mean   :-129856                 Mean   :0.0070661                 
##  3rd Qu.: -16569                 3rd Qu.:0.0038832                 
##  Max.   :      0                 Max.   :1.0000000                 
##  rela_intercept_to_lowest_cumsum_invert rela_x_intercept
##  Min.   :  0.00                         Min.   :  0.00  
##  1st Qu.: 64.34                         1st Qu.: 76.34  
##  Median : 74.41                         Median :102.59  
##  Mean   :109.21                         Mean   :162.65  
##  3rd Qu.:166.54                         3rd Qu.:285.05  
##  Max.   :236.91                         Max.   :328.97  
##  opti_lowest_spline_knots    tot_devi      allocate_lowest opti_allocate   
##  Min.   :     0           Min.   :-22823   Min.   :119.1   Min.   :  0.00  
##  1st Qu.:  3091           1st Qu.:-19732   1st Qu.:119.1   1st Qu.:  0.00  
##  Median : 14234           Median : -8588   Median :119.1   Median : 15.17  
##  Mean   : 63977           Mean   : 41154   Mean   :119.1   Mean   : 21.88  
##  3rd Qu.:144722           3rd Qu.:121899   3rd Qu.:119.1   3rd Qu.: 41.46  
##  Max.   :233432           Max.   :210609   Max.   :119.1   Max.   :119.13  
##  allocate_total  opti_exp_outcome
##  Min.   :22823   Min.   :74.37   
##  1st Qu.:22823   1st Qu.:78.35   
##  Median :22823   Median :80.87   
##  Mean   :22823   Mean   :79.82   
##  3rd Qu.:22823   3rd Qu.:80.87   
##  Max.   :22823   Max.   :84.61