From 91373172d797a919ddbc3e06787138b39b2d480a Mon Sep 17 00:00:00 2001 From: Phil Mike Jones Date: Fri, 9 Sep 2016 18:42:18 +0100 Subject: [PATCH 1/2] Add examples to integerise and simulate --- R/rake_functions.R | 44 ++++++++++++++++++++++++++++++++++++++++++-- man/integerise.Rd | 21 ++++++++++++++++++++- man/simulate.Rd | 22 +++++++++++++++++++++- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/R/rake_functions.R b/R/rake_functions.R index d657c58..6ea50b2 100644 --- a/R/rake_functions.R +++ b/R/rake_functions.R @@ -190,7 +190,27 @@ weight <- function(cons, inds, vars = NULL, iterations = 10) { #' @return A data frame of integerised weights to be used by \code{simulate()} #' @export #' -#' @examples # not run +#' @examples +#' cons <- data.frame( +#' "zone" = letters[1:3], +#' "a0_49" = c(8, 2, 7), +#' "a_gt50" = c(4, 8, 4), +#' "f" = c(6, 6, 8), +#' "m" = c(6, 4, 3) +#' ) +#' +#' inds <- data.frame( +#' "id" = LETTERS[1:5], +#' "age" = c("a_gt50", "a_gt50", "a0_49", "a_gt50", "a0_49"), +#' "sex" = c("m", "m", "m", "f", "f"), +#' "income" = c(2868, 2474, 2231, 3152, 2473), +#' stringsAsFactors = FALSE +#' ) +#' vars <- c("age", "sex") +#' +#' weights <- weight(cons = cons, inds = inds, vars = vars) +#' weights_int <- integerise(weights) +#' weights_int integerise <- function(weights, method = "trs") { # Ensures the output of the function is reproducible (uses sample()) @@ -246,7 +266,27 @@ integerise <- function(weights, method = "trs") { #' @export #' #' @examples -#' # not run +#' cons <- data.frame( +#' "zone" = letters[1:3], +#' "a0_49" = c(8, 2, 7), +#' "a_gt50" = c(4, 8, 4), +#' "f" = c(6, 6, 8), +#' "m" = c(6, 4, 3) +#' ) +#' +#' inds <- data.frame( +#' "id" = LETTERS[1:5], +#' "age" = c("a_gt50", "a_gt50", "a0_49", "a_gt50", "a0_49"), +#' "sex" = c("m", "m", "m", "f", "f"), +#' "income" = c(2868, 2474, 2231, 3152, 2473), +#' stringsAsFactors = FALSE +#' ) +#' vars <- c("age", "sex") +#' +#' weights <- weight(cons = cons, inds = inds, vars = vars) +#' weights_int <- integerise(weights) +#' sim_df <- simulate(weights_int, inds) +#' sim_df simulate <- function(weights, inds) { weights <- as.matrix(weights) diff --git a/man/integerise.Rd b/man/integerise.Rd index 6bebbeb..b122fb3 100644 --- a/man/integerise.Rd +++ b/man/integerise.Rd @@ -28,6 +28,25 @@ Other methods (for example proportional probabilities) may be implemented at a later date. } \examples{ -# not run +cons <- data.frame( + "zone" = letters[1:3], + "a0_49" = c(8, 2, 7), + "a_gt50" = c(4, 8, 4), + "f" = c(6, 6, 8), + "m" = c(6, 4, 3) +) + +inds <- data.frame( + "id" = LETTERS[1:5], + "age" = c("a_gt50", "a_gt50", "a0_49", "a_gt50", "a0_49"), + "sex" = c("m", "m", "m", "f", "f"), + "income" = c(2868, 2474, 2231, 3152, 2473), + stringsAsFactors = FALSE +) +vars <- c("age", "sex") + +weights <- weight(cons = cons, inds = inds, vars = vars) +weights_int <- integerise(weights) +weights_int } diff --git a/man/simulate.Rd b/man/simulate.Rd index 14ca5e7..191598f 100644 --- a/man/simulate.Rd +++ b/man/simulate.Rd @@ -24,6 +24,26 @@ A data frame with spatial microsimulated data, with one row per simulate } \examples{ -# not run +cons <- data.frame( + "zone" = letters[1:3], + "a0_49" = c(8, 2, 7), + "a_gt50" = c(4, 8, 4), + "f" = c(6, 6, 8), + "m" = c(6, 4, 3) +) + +inds <- data.frame( + "id" = LETTERS[1:5], + "age" = c("a_gt50", "a_gt50", "a0_49", "a_gt50", "a0_49"), + "sex" = c("m", "m", "m", "f", "f"), + "income" = c(2868, 2474, 2231, 3152, 2473), + stringsAsFactors = FALSE +) +vars <- c("age", "sex") + +weights <- weight(cons = cons, inds = inds, vars = vars) +weights_int <- integerise(weights) +sim_df <- simulate(weights_int, inds) +sim_df } From 990398f41a100074957bb7460e942fe8c7245c23 Mon Sep 17 00:00:00 2001 From: Phil Mike Jones Date: Fri, 9 Sep 2016 18:43:18 +0100 Subject: [PATCH 2/2] Bump version --- DESCRIPTION | 2 +- tests/testthat/test_integerise.R | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b437a7a..377ffb1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rakeR Title: Spatial Microsimulation (Raking) Helper Functions -Version: 0.0.0.9000 +Version: 0.0.1 Authors@R: person("Phil Mike", "Jones", email = "philmikejones@gmail.com", role = c("aut", "cre")) Description: Helper functions for performing spatial microsimulation ('raking') in R. diff --git a/tests/testthat/test_integerise.R b/tests/testthat/test_integerise.R index 48c687f..f3f7cee 100644 --- a/tests/testthat/test_integerise.R +++ b/tests/testthat/test_integerise.R @@ -25,7 +25,6 @@ vars <- c("age", "sex") weights <- weight(cons = cons, inds = inds, vars = vars) weights_int <- integerise(weights) - test_that("integerised weights should add up to cons population", { expect_equal(sum(weights_int), sum(weights)) expect_equal(sum(weights_int), (sum(cons[, -1] / length(vars))))