Open szimmer opened 3 years ago
Example using ipfr
from CRAN
library(ipfr)
library(tidyverse)
hh_seed <- tibble(
geo_region = 1,
id = c(1:8),
hhsiz = c(1, 1, 1, 2, 2, 2, 2, 2),
hhveh = c(0, 2, 1, 1, 1, 2, 1, 0)
)
hh_targets <- list()
hh_targets$hhsiz <- tibble(
geo_region = 1,
`1` = 35,
`2` = 65
)
hh_targets$hhveh <- tibble(
geo_region = 1,
`0` = 100,
`1` = 100,
`2` = 100
)
result <- ipu(hh_seed, hh_targets, max_iterations = 30, verbose = TRUE)
#> Scaling target tables: hhveh
#> Finished iteration 2 . %RMSE = 9.044233 Finished iteration 3 . %RMSE = 0.4706742 Finished iteration 4 . %RMSE = 0.02385747 Finished iteration 5 . %RMSE = 0.001207629
#>
#> IPU converged
#> All targets matched within the absolute_diff of 10
result
#> $weight_tbl
#> # A tibble: 8 x 7
#> geo_region id hhsiz hhveh weight avg_weight weight_factor
#> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 1 0 14.2 12.5 1.14
#> 2 1 2 1 2 14.2 12.5 1.14
#> 3 1 3 1 1 6.61 12.5 0.529
#> 4 1 4 2 1 8.91 12.5 0.713
#> 5 1 5 2 1 8.91 12.5 0.713
#> 6 1 6 2 2 19.1 12.5 1.53
#> 7 1 7 2 1 8.91 12.5 0.713
#> 8 1 8 2 0 19.1 12.5 1.53
#>
#> $weight_dist
#> Warning: Use of `primary_seed$weight_factor` is discouraged. Use `weight_factor`
#> instead.
#>
#> $primary_comp
#> # A tibble: 5 x 6
#> geography category target result diff pct_diff
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 geo_region_1 hhsiz_1 35 35.0 0 0
#> 2 geo_region_1 hhsiz_2 65 65.0 0 0
#> 3 geo_region_1 hhveh_0 33.3 33.3 0 0
#> 4 geo_region_1 hhveh_1 33.3 33.3 0 0
#> 5 geo_region_1 hhveh_2 33.3 33.3 0 0
Created on 2020-11-30 by the reprex package (v0.3.0)
Example using updated version (no warning)
library(ipfr)
library(tidyverse)
hh_seed <- tibble(
geo_region = 1,
id = c(1:8),
hhsiz = c(1, 1, 1, 2, 2, 2, 2, 2),
hhveh = c(0, 2, 1, 1, 1, 2, 1, 0)
)
hh_targets <- list()
hh_targets$hhsiz <- tibble(
geo_region = 1,
`1` = 35,
`2` = 65
)
hh_targets$hhveh <- tibble(
geo_region = 1,
`0` = 100,
`1` = 100,
`2` = 100
)
result <- ipu(hh_seed, hh_targets, max_iterations = 30, verbose = TRUE)
#> Scaling target tables: hhveh
#> Finished iteration 2 . %RMSE = 9.044233 Finished iteration 3 . %RMSE = 0.4706742 Finished iteration 4 . %RMSE = 0.02385747 Finished iteration 5 . %RMSE = 0.001207629
#>
#> IPU converged
#> All targets matched within the absolute_diff of 10
result
#> $weight_tbl
#> # A tibble: 8 x 7
#> geo_region id hhsiz hhveh weight avg_weight weight_factor
#> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 1 0 14.2 12.5 1.14
#> 2 1 2 1 2 14.2 12.5 1.14
#> 3 1 3 1 1 6.61 12.5 0.529
#> 4 1 4 2 1 8.91 12.5 0.713
#> 5 1 5 2 1 8.91 12.5 0.713
#> 6 1 6 2 2 19.1 12.5 1.53
#> 7 1 7 2 1 8.91 12.5 0.713
#> 8 1 8 2 0 19.1 12.5 1.53
#>
#> $weight_dist
#>
#> $primary_comp
#> # A tibble: 5 x 6
#> geography category target result diff pct_diff
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 geo_region_1 hhsiz_1 35 35.0 0 0
#> 2 geo_region_1 hhsiz_2 65 65.0 0 0
#> 3 geo_region_1 hhveh_0 33.3 33.3 0 0
#> 4 geo_region_1 hhveh_1 33.3 33.3 0 0
#> 5 geo_region_1 hhveh_2 33.3 33.3 0 0
#>
#> $converged
#> [1] TRUE
Created on 2020-11-30 by the reprex package (v0.3.0)
This pull request does the following:
ipu
function so there is no warning presentipu
function which is useful when saving output