├── .Rbuildignore ├── .gitattributes ├── .gitignore ├── .travis.yml ├── CONDUCT.md ├── DESCRIPTION ├── NAMESPACE ├── NEWS.md ├── R ├── deprecated_functions.R └── simulation_functions.R ├── README.Rmd ├── README.md ├── codecov.yml ├── codemeta.json ├── cran-comments.md ├── data-raw └── prep-cakemap.R ├── docs ├── CONDUCT.html ├── authors.html ├── docsearch.css ├── docsearch.js ├── index.html ├── link.svg ├── news │ └── index.html ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml └── reference │ ├── check_constraint.html │ ├── check_ind.html │ ├── extract.html │ ├── extract_weights.html │ ├── index.html │ ├── integerise.html │ ├── rake.html │ ├── rk_extract.html │ ├── rk_integerise.html │ ├── rk_rake.html │ ├── rk_weight.html │ └── weight.html ├── inst └── extdata │ ├── cakemap_cons.csv │ └── cakemap_inds.csv ├── man ├── extract.Rd ├── extract_weights.Rd ├── integerise.Rd ├── rake.Rd ├── rk_extract.Rd ├── rk_integerise.Rd ├── rk_rake.Rd ├── rk_weight.Rd └── weight.Rd ├── rakeR.Rproj └── tests ├── cakemap_cons.csv ├── cakemap_inds.csv ├── testthat.R └── testthat ├── test_deprecated.R ├── test_rk_extract.R ├── test_rk_integerise.R ├── test_rk_rake.R └── test_rk_weight.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^codemeta\.json$ 2 | ^.*\.Rproj$ 3 | ^\.Rproj\.user$ 4 | 5 | .travis.yml 6 | codecov.yml 7 | cran-comments.md 8 | data-raw/ 9 | ^README\.Rmd$ 10 | ^README-.*\.png$ 11 | ^CONDUCT\.md$ 12 | inst/ 13 | revdep/ 14 | docs/ 15 | ^docs$ 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philmikejones/rakeR/676dbb0c36e7cf6beeac6f3fb8d350d1758123cc/.gitattributes -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | revdep/ 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | 3 | cache: packages 4 | 5 | r_packages: 6 | - covr 7 | 8 | notifications: 9 | on_success: change 10 | on_failure: change 11 | 12 | after_success: 13 | - Rscript -e 'library(covr); codecov()' 14 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (http:contributor-covenant.org), version 1.0.0, available at 25 | http://contributor-covenant.org/version/1/0/0/ 26 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: rakeR 2 | Title: Easy Spatial Microsimulation (Raking) in R 3 | Version: 0.2.1.9000 4 | Date: 2018-02-26 5 | Authors@R: c( 6 | person("Phil Mike", "Jones", 7 | email = "philmikejones@gmail.com", 8 | role = c("aut", "cre"), 9 | comment = c(ORCID = "0000-0001-5173-3245")), 10 | person("Robin", "Lovelace", role = "aut", 11 | comment = "Many functions are based on code by Robin Lovelace and Morgane Dumont"), 12 | person("Morgane", "Dumont", role = "aut", 13 | comment = "Many functions are based on code by Robin Lovelace and Morgane Dumont"), 14 | person("Andrew", "Smith", role = "ctb")) 15 | Description: Functions for performing spatial microsimulation ('raking') 16 | in R. 17 | Depends: 18 | R (>= 3.4.0) 19 | License: GPL-3 20 | Encoding: UTF-8 21 | LazyData: true 22 | RoxygenNote: 6.1.1 23 | Imports: ipfp, 24 | wrswoR 25 | Suggests: testthat, 26 | readr 27 | URL: https://philmikejones.github.io/rakeR/ 28 | BugReports: https://github.com/philmikejones/rakeR/issues 29 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(extract) 4 | export(extract_weights) 5 | export(integerise) 6 | export(rake) 7 | export(rk_extract) 8 | export(rk_integerise) 9 | export(rk_rake) 10 | export(rk_weight) 11 | export(weight) 12 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | v 0.2.2 2 | ======= 3 | 4 | * Functions have been renamed to include a `rk_` prefix. Function names without prefixes have been deprecated but will continue to work for now so as not to affect existing code. 5 | * Increased code coverage. 6 | * check_constraint() and check_ind() are now deprecated. These checks are carried out by the weight() and/or integerise()/extract() functions automatically. 7 | * Update README.md documentation 8 | * Add additional unit tests - thanks Derrick Atherton for feedback 9 | * Add appropriate acknowledgements for source of data set used for examples and testing. 10 | 11 | v 0.2.1 12 | ======= 13 | 14 | Patch release: 15 | 16 | * Fixes to examples and tests to use correct variable labels and names (thanks Derrick Atherton for pointing this out). 17 | * Fix version number in NEWS 18 | 19 | v 0.2.0 20 | ======= 21 | 22 | * integerise() now uses the wrswoR package for sampling without replacement. 23 | This is in the order of 100s of times faster, reducing the time taken for the function to return from hours to minutes. 24 | I have implemented the sample_int_crank() function as this has given results 25 | similar to that of base R's sample() in testing, so this should minimise changes 26 | between the two. 27 | See https://stackoverflow.com/questions/15113650/faster-weighted-sampling-without-replacement or https://cran.r-project.org/package=wrswoR for details of the 28 | implementation. 29 | * simulate() is deprecated. Instead of weight() %>% integerise() %>% simulate(), 30 | just use weight() %>% integerise(). This is to improve consistency with the 31 | steps to produce fractional weights (weight() %>% extract()). 32 | * extract_weights() has been deprecated. Use extract() instead. 33 | * extract() (previously extract_weights()) now stops if it encounters a numeric 34 | variable. See https://github.com/philmikejones/rakeR/issues/49 35 | * integerise() now returns weights unmodified with a note if they are 36 | already integers. See https://github.com/philmikejones/rakeR/issues/42 and https://github.com/philmikejones/rakeR/issues/46 37 | * set.seed() is no longer hard-coded in the integerise() function and can be 38 | specified as a function argument. See: 39 | https://github.com/philmikejones/rakeR/issues/41 40 | 41 | v 0.1.2 42 | ======= 43 | 44 | Patch release 45 | 46 | New functions 47 | ------------- 48 | 49 | * check_ind() checks individual level data for common errors 50 | 51 | 52 | Fixes: 53 | ------ 54 | 55 | * Fix weight() #33: https://github.com/philmikejones/rakeR/issues/33 56 | * check functions now named prep functions to prevent conflicts with validation 57 | * Add date to DESCRIPTION for bibliography building 58 | 59 | 60 | v 0.1.1 61 | ======= 62 | 63 | Initial CRAN release 64 | 65 | Fixes: 66 | ------ 67 | 68 | * Fix license issue: remove LICENSE, state GPL-3 69 | 70 | 71 | v 0.1.0 72 | ======= 73 | 74 | New functions 75 | ------------- 76 | 77 | * weight(), 78 | * integerise(), 79 | * simulate(), 80 | * rake(), and 81 | * check_constraint() 82 | -------------------------------------------------------------------------------- /R/deprecated_functions.R: -------------------------------------------------------------------------------- 1 | #' weight 2 | #' 3 | #' Deprecated. Use rk_weight() 4 | #' 5 | #' @param cons A data frame containing all the constraints. This 6 | #' should be in the format of one row per zone, one column per constraint 7 | #' category. The first column should be a zone code; all other columns must be 8 | #' numeric counts. 9 | #' @param inds A data frame containing individual-level (survey) data. This 10 | #' should be in the format of one row per individual, one column per 11 | #' constraint. The first column should be an individual ID. 12 | #' @param vars A character vector of variables that constrain the simulation 13 | #' (i.e. independent variables) 14 | #' @param iterations The number of iterations the algorithm should complete. 15 | #' Defaults to 10 16 | #' 17 | #' @return A data frame of fractional weights for each individual in each zone 18 | #' with zone codes recorded in column names and individual id recorded in row 19 | #' names. 20 | #' @export 21 | #' 22 | #' @examples 23 | #' # Deprecated. Use rk_weight() 24 | weight <- function(cons, inds, vars = NULL, iterations = 10) { 25 | 26 | .Deprecated("rk_weight") 27 | 28 | rk_weight(cons = cons, inds = inds, vars = vars, iterations = iterations) 29 | 30 | } 31 | 32 | #' extract 33 | #' 34 | #' Deprecated. Use rk_extract() instead. 35 | #' 36 | #' @param weights A weight table, typically produced by rakeR::weight() 37 | #' @param inds The individual level data 38 | #' @param id The unique id variable in the individual level data (inds), 39 | #' usually the first column 40 | #' 41 | #' @return A data frame with zones and aggregated simulated values for each 42 | #' variable 43 | #' @export 44 | #' 45 | #' @examples 46 | #' \dontrun{ 47 | #' Deprecated. Use rk_extract() 48 | #' } 49 | extract <- function(weights, inds, id) { 50 | 51 | .Deprecated("rk_extract") 52 | 53 | rk_extract(weights = weights, inds = inds, id = id) 54 | 55 | } 56 | 57 | 58 | #' extract_weights 59 | #' 60 | #' Deprecated: use rakeR::rk_extract() 61 | #' 62 | #' @param weights A weight table, typically produced using rakeR::rk_weight() 63 | #' @param inds The individual level data 64 | #' @param id The unique id variable in the individual level data (inds), 65 | #' usually the first column 66 | #' 67 | #' @return A data frame with zones and aggregated simulated values for each 68 | #' variable 69 | #' @export 70 | #' 71 | #' @examples 72 | #' \dontrun{ 73 | #' extract_weights() is deprecated, use rk_extract() instead 74 | #' } 75 | extract_weights <- function(weights, inds, id) { 76 | 77 | stop("extract_weights() is deprecated. Use rk_extract()") 78 | 79 | } 80 | 81 | 82 | #' integerise 83 | #' 84 | #' Deprecated. Use rk_integerise() 85 | #' 86 | #' @param weights A matrix or data frame of fractional weights, typically 87 | #' provided by \code{rakeR::weight()} 88 | #' @param inds The individual--level data (i.e. one row per individual) 89 | #' @param method The integerisation method specified as a character string. 90 | #' Defaults to \code{"trs"}; currently other methods are not implemented. 91 | #' @param seed The seed to use, defaults to 42. 92 | #' 93 | #' @return A data frame of integerised cases 94 | #' @aliases integerize 95 | #' @export 96 | #' 97 | #' @examples 98 | #' \dontrun{ 99 | #' Deprecated. Use rk_integerise() 100 | #' } 101 | integerise <- function(weights, inds, method = "trs", seed = 42) { 102 | 103 | .Deprecated("rk_integerise") 104 | 105 | rk_integerise(weights = weights, inds = inds, method = method, seed = seed) 106 | 107 | } 108 | 109 | 110 | #' rake 111 | #' 112 | #' Deprecated. Use rk_rake() 113 | #' 114 | #' @param cons A data frame of constraint variables 115 | #' @param inds A data frame of individual--level (survey) data 116 | #' @param vars A character string of variables to iterate over 117 | #' @param output A string specifying the desired output, either "fraction" 118 | #' (rk_extract()) or "integer" (rk_integerise()) 119 | #' @param iterations The number of iterations to perform. Defaults to 10. 120 | #' @param ... Additional arguments to pass to depending on desired output: 121 | #' \itemize{ 122 | #' \item{if "fraction" specify 'id' (see rk_extract() documentation)} 123 | #' \item{if "integer" specify 'method' and 'seed' (see rk_integerise() 124 | #' documentation)} 125 | #' } 126 | #' 127 | #' @return A data frame with extracted weights (if output == "fraction", the 128 | #' default) or integerised cases (if output == "integer") 129 | #' @export 130 | #' 131 | #' @examples 132 | #' \dontrun{ 133 | #' Deprecated. Use rk_rake() 134 | #' } 135 | rake <- function(cons, inds, vars, output = "fraction", iterations = 10, ...) { 136 | 137 | .Deprecated("rk_rake") 138 | 139 | arguments <- list(...) 140 | 141 | result <- rk_rake( 142 | cons = cons, inds = inds, vars = vars, 143 | output = output, iterations = iterations, 144 | id = arguments[["id"]] 145 | ) 146 | 147 | result 148 | 149 | } 150 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | ```{r, echo = FALSE} 6 | knitr::opts_chunk$set( 7 | collapse = TRUE, 8 | comment = "#>", 9 | fig.path = "README-" 10 | ) 11 | ``` 12 | 13 | 14 | # rakeR 15 | 16 | [![Build Status](https://travis-ci.org/philmikejones/rakeR.svg?branch=master)](https://travis-ci.org/philmikejones/rakeR) 17 | [![codecov](https://codecov.io/gh/philmikejones/rakeR/branch/master/graph/badge.svg)](https://codecov.io/gh/philmikejones/rakeR) 18 | [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rakeR)](https://cran.r-project.org/package=rakeR) 19 | [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.821506.svg)](https://doi.org/10.5281/zenodo.821506) 20 | [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) 21 | 22 | 23 | Create a spatial microsimulated data set in R using iterative proportional 24 | fitting ('raking'). 25 | 26 | 27 | ## Install 28 | 29 | Install the latest stable version from CRAN: 30 | 31 | ```{r install-cran, eval=FALSE, include=TRUE} 32 | install.packages("rakeR") 33 | ``` 34 | 35 | Or install the development version with `devtools`: 36 | 37 | ```{r install-rakeR, eval=FALSE, include=TRUE} 38 | # Obtain devtools if you don't already have it installed 39 | # install.packages("devtools") 40 | 41 | # Install rakeR development version from GitHub 42 | devtools::install_github("philmikejones/rakeR") 43 | ``` 44 | 45 | Load the package with: 46 | 47 | ```{r load-rakeR} 48 | library("rakeR") 49 | ``` 50 | 51 | 52 | ## Overview 53 | 54 | `rakeR` has three main functions. 55 | The first stage is always to use `rk_weight()` to produce a matrix of fractional weights. 56 | This matrix stores weights for each individual in each zone. 57 | 58 | From this weight matrix, `rakeR` has functions to create fractional weights (`rk_extract()`) or integerised cases (`rk_integerise()`), depending on your needs and use cases. 59 | Fractional (`rk_extract()`ed) weights are generally more accurate, while integer cases are probably the most intuitive to use and are useful as inputs 60 | for further modeling. 61 | 62 | To create fractional weights use `rk_weight()` then `rk_extract()`, and to produce integerised weights use `rk_weight()` then `rk_integerise()`. 63 | 64 | 65 | ## Inputs 66 | 67 | To perform the weighting you should supply two data frames. 68 | One data frame should contain the constraint information (`cons`) with counts per category for each zone (e.g. census counts). 69 | The other data frame should contain the individual--level data (`inds`), i.e. one row per individual. 70 | 71 | In addition, it is necessary to supply a character vector with the names of the constraint variables in `inds` (`vars`). 72 | This is so that `rakeR` knows which are the contraint variables and which variables it should be simulating as an output. 73 | 74 | Below are examples of `cons`, `inds`, and `vars`. 75 | 76 | ```{r data} 77 | cons <- data.frame( 78 | "zone" = letters[1:3], 79 | "age_0_49" = c(8, 2, 7), 80 | "age_gt_50" = c(4, 8, 4), 81 | "sex_f" = c(6, 6, 8), 82 | "sex_m" = c(6, 4, 3), 83 | stringsAsFactors = FALSE 84 | ) 85 | 86 | inds <- data.frame( 87 | "id" = LETTERS[1:5], 88 | "age" = c("age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49"), 89 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 90 | "income" = c(2868, 2474, 2231, 3152, 2473) 91 | ) 92 | 93 | vars <- c("age", "sex") 94 | ``` 95 | 96 | It is _essential_ that the unique levels in the constraint variables in the 97 | `inds` data set match the variables names in the `cons` data set. 98 | For example, `age_0_49` and `age_gt_50` are variable names in `cons` and the unique levels of the `age` variable in `inds` precisely match these: 99 | 100 | ```{r labels-test} 101 | all.equal( 102 | levels(inds$age), colnames(cons[, 2:3]) # cons[, 1] is the id column 103 | ) 104 | ``` 105 | 106 | Without this, the functions do not know how to match the `inds` and `cons` data 107 | and will fail so as not to return spurious results. 108 | 109 | 110 | ## `rk_weight()` 111 | 112 | (Re-)weighting is done with `rk_weight()` which returns a data frame of raw weights. 113 | 114 | ```{r weight} 115 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 116 | weights 117 | ``` 118 | 119 | The raw weights tell you how frequently each individual (`A`-`E`) should appear 120 | in each zone (`a`-`c`). 121 | The raw weights are useful when validating and checking performance of the 122 | model, so it can be necessary to save these separately. 123 | They aren't very useful for analysis however, so we can `rk_extract()` or `rk_integerise()` them into a useable form. 124 | 125 | 126 | ## `rk_extract()` 127 | 128 | `rk_extract()` produces aggregated totals of the simulated data for each category in each zone. 129 | `rk_extract()`ed data is generally more accurate than `rk_integerise()`d data, although the user should be careful this isn't spurious precision based on context and knowledge of the domain. 130 | Because `rk_extract()` creates a column for each level of each variable, numerical variables (e.g. income) must be removed or `cut()` (otherwise the result would include a new column for each unique numerical value): 131 | 132 | ```{r extract} 133 | inds$income <- cut(inds$income, breaks = 2, include.lowest = TRUE, 134 | labels = c("low", "high")) 135 | 136 | ext_weights <- rk_extract(weights, inds = inds, id = "id") 137 | ext_weights 138 | ``` 139 | 140 | `rk_extract()` returns one row per zone, and the total of each category (for 141 | example female and male, or high and low income) will match the known 142 | population. 143 | 144 | 145 | ## `rk_integerise()` 146 | 147 | The `rk_integerise()` function produces a simulated data frame populated with simulated individuals. 148 | This is typically useful when: 149 | 150 | * You need to include numerical variables, such as income in the example. 151 | * You want individual cases to use as input to a dynamic or agent-based model. 152 | * You want 'case studies' to illustrate characteristics of individuals in an 153 | area. 154 | * Individual-level data is more intuitive to work with. 155 | 156 | ```{r integerise} 157 | int_weights <- rk_integerise(weights, inds = inds) 158 | int_weights[1:6, ] 159 | ``` 160 | 161 | `rk_integerise()` returns one row per case, and the number of rows will match 162 | the known population (taken from `cons`). 163 | 164 | 165 | ## `rk_rake()` 166 | 167 | `rk_rake()` is a wrapper for `rk_weight() %>% rk_extract()` or 168 | `rk_weight() %>% rk_integerise()`. 169 | This is useful if the raw weights (from `rk_weight()`) are not required. 170 | The desired output is specified with the `output` argument, which can be 171 | specified with `"fraction"` (the default) or `"integer"`. 172 | The function takes the following arguments in all cases: 173 | 174 | * `cons` 175 | * `inds` 176 | * `vars` 177 | * `output` (default `"fraction"`) 178 | * `iterations` (default 10) 179 | 180 | Additional arguments are required depending on the output requested. 181 | For `output = "fraction"`: 182 | 183 | * `id` 184 | 185 | For `output = "integer"`: 186 | 187 | * `method` (default `"trs"`) 188 | * `seed` (default 42) 189 | 190 | Details of these context-specific arguments can be found in the 191 | respective documentation for `rk_integerise()` or `rk_extract()`. 192 | 193 | ```{r rake-int-exaple} 194 | rake_int <- rk_rake(cons, inds, vars, output = "integer", 195 | method = "trs", seed = 42) 196 | rake_int[1:6, ] 197 | ``` 198 | 199 | ```{r rake-frac-example} 200 | rake_frac <- rk_rake(cons, inds, vars, output = "fraction", id = "id") 201 | rake_frac 202 | ``` 203 | 204 | 205 | ## Contributing 206 | 207 | Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). 208 | By participating in this project you agree to abide by its terms. 209 | 210 | 211 | ## Issues and feedback 212 | 213 | Feedback on the API, 214 | [bug reports/issues](https://github.com/philmikejones/rakeR/issues), 215 | and pull requests are very welcome. 216 | 217 | 218 | ## Acknowledgements 219 | 220 | Many of the functions in this package are based on code written by 221 | [Robin Lovelace](https://github.com/Robinlovelace) and 222 | [Morgane Dumont](https://github.com/modumont) for their book 223 | [*Spatial Microsimulation with R* (2016), Chapman and Hall/CRC Press](https://www.crcpress.com/Spatial-Microsimulation-with-R/Lovelace-Dumont/p/book/9781498711548). 224 | 225 | Their book is an excellent resource for learning 226 | about spatial microsimulation and understanding what's going on under the hood 227 | of this package. 228 | 229 | The book and code are licensed under the terms below: 230 | 231 | Copyright (c) 2014 Robin Lovelace 232 | 233 | Permission is hereby granted, free of charge, to any person obtaining a copy 234 | of this software and associated documentation files (the "Software"), to deal 235 | in the Software without restriction, including without limitation the rights 236 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 237 | copies of the Software, and to permit persons to whom the Software is 238 | furnished to do so, subject to the following conditions: 239 | 240 | The above copyright notice and this permission notice shall be included in all 241 | copies or substantial portions of the Software. 242 | 243 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 244 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 245 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 246 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 247 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 248 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 249 | SOFTWARE. 250 | 251 | 252 | The [rewighting (ipfp) algorithm](https://github.com/awblocker/ipfp) is 253 | written by Andrew Blocker. 254 | 255 | The [`wrswoR` package](http://krlmlr.github.io/wrswoR/) used for fast sampling 256 | without replacement is written by Kirill Müller. 257 | 258 | 259 | Thanks to [Tom Broomhead](http://mhs.group.shef.ac.uk/members/tom-broomhead/) 260 | for his feedback on error handling and suggestions on function naming, to [Andrew Smith](https://github.com/virgesmith) for bug fixes, and Derrick Atherton for suggestions, feedback, and testing. 261 | 262 | 263 | Data used in some of the examples and tests ('cakeMap') are anonymised data from the [Adult Dental Health Survey](https://data.gov.uk/dataset/adult_dental_health_survey), used under terms of the [Open Government Licence](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/). 264 | 265 | 266 | ## Contact 267 | 268 | philmikejones at gmail dot com 269 | 270 | 271 | ## License 272 | 273 | Copyright 2016-18 Phil Mike Jones. 274 | 275 | rakeR is free software: you can redistribute it and/or modify 276 | it under the terms of the GNU General Public License as published by 277 | the Free Software Foundation, either version 3 of the License, or 278 | (at your option) any later version. 279 | 280 | rakeR is distributed in the hope that it will be useful, 281 | but WITHOUT ANY WARRANTY; without even the implied warranty of 282 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 283 | GNU General Public License for more details. 284 | 285 | You should have received a copy of the GNU General Public License 286 | along with rakeR. If not, see . 287 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | rakeR 3 | ===== 4 | 5 | [![Build Status](https://travis-ci.org/philmikejones/rakeR.svg?branch=master)](https://travis-ci.org/philmikejones/rakeR) [![codecov](https://codecov.io/gh/philmikejones/rakeR/branch/master/graph/badge.svg)](https://codecov.io/gh/philmikejones/rakeR) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/rakeR)](https://cran.r-project.org/package=rakeR) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.821506.svg)](https://doi.org/10.5281/zenodo.821506) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) 6 | 7 | Create a spatial microsimulated data set in R using iterative proportional fitting ('raking'). 8 | 9 | Install 10 | ------- 11 | 12 | Install the latest stable version from CRAN: 13 | 14 | ``` r 15 | install.packages("rakeR") 16 | ``` 17 | 18 | Or install the development version with `devtools`: 19 | 20 | ``` r 21 | # Obtain devtools if you don't already have it installed 22 | # install.packages("devtools") 23 | 24 | # Install rakeR development version from GitHub 25 | devtools::install_github("philmikejones/rakeR") 26 | ``` 27 | 28 | Load the package with: 29 | 30 | ``` r 31 | library("rakeR") 32 | ``` 33 | 34 | Overview 35 | -------- 36 | 37 | `rakeR` has three main functions. The first stage is always to use `rk_weight()` to produce a matrix of fractional weights. This matrix stores weights for each individual in each zone. 38 | 39 | From this weight matrix, `rakeR` has functions to create fractional weights (`rk_extract()`) or integerised cases (`rk_integerise()`), depending on your needs and use cases. Fractional (`rk_extract()`ed) weights are generally more accurate, while integer cases are probably the most intuitive to use and are useful as inputs for further modeling. 40 | 41 | To create fractional weights use `rk_weight()` then `rk_extract()`, and to produce integerised weights use `rk_weight()` then `rk_integerise()`. 42 | 43 | Inputs 44 | ------ 45 | 46 | To perform the weighting you should supply two data frames. One data frame should contain the constraint information (`cons`) with counts per category for each zone (e.g. census counts). The other data frame should contain the individual--level data (`inds`), i.e. one row per individual. 47 | 48 | In addition, it is necessary to supply a character vector with the names of the constraint variables in `inds` (`vars`). This is so that `rakeR` knows which are the contraint variables and which variables it should be simulating as an output. 49 | 50 | Below are examples of `cons`, `inds`, and `vars`. 51 | 52 | ``` r 53 | cons <- data.frame( 54 | "zone" = letters[1:3], 55 | "age_0_49" = c(8, 2, 7), 56 | "age_gt_50" = c(4, 8, 4), 57 | "sex_f" = c(6, 6, 8), 58 | "sex_m" = c(6, 4, 3), 59 | stringsAsFactors = FALSE 60 | ) 61 | 62 | inds <- data.frame( 63 | "id" = LETTERS[1:5], 64 | "age" = c("age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49"), 65 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 66 | "income" = c(2868, 2474, 2231, 3152, 2473) 67 | ) 68 | 69 | vars <- c("age", "sex") 70 | ``` 71 | 72 | It is *essential* that the unique levels in the constraint variables in the `inds` data set match the variables names in the `cons` data set. For example, `age_0_49` and `age_gt_50` are variable names in `cons` and the unique levels of the `age` variable in `inds` precisely match these: 73 | 74 | ``` r 75 | all.equal( 76 | levels(inds$age), colnames(cons[, 2:3]) # cons[, 1] is the id column 77 | ) 78 | #> [1] TRUE 79 | ``` 80 | 81 | Without this, the functions do not know how to match the `inds` and `cons` data and will fail so as not to return spurious results. 82 | 83 | `rk_weight()` 84 | ------------- 85 | 86 | (Re-)weighting is done with `rk_weight()` which returns a data frame of raw weights. 87 | 88 | ``` r 89 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 90 | weights 91 | #> a b c 92 | #> A 1.227998 1.7250828 0.7250828 93 | #> B 1.227998 1.7250828 0.7250828 94 | #> C 3.544004 0.5498344 1.5498344 95 | #> D 1.544004 4.5498344 2.5498344 96 | #> E 4.455996 1.4501656 5.4501656 97 | ``` 98 | 99 | The raw weights tell you how frequently each individual (`A`-`E`) should appear in each zone (`a`-`c`). The raw weights are useful when validating and checking performance of the model, so it can be necessary to save these separately. They aren't very useful for analysis however, so we can `rk_extract()` or `rk_integerise()` them into a useable form. 100 | 101 | `rk_extract()` 102 | -------------- 103 | 104 | `rk_extract()` produces aggregated totals of the simulated data for each category in each zone. `rk_extract()`ed data is generally more accurate than `rk_integerise()`d data, although the user should be careful this isn't spurious precision based on context and knowledge of the domain. Because `rk_extract()` creates a column for each level of each variable, numerical variables (e.g. income) must be removed or `cut()` (otherwise the result would include a new column for each unique numerical value): 105 | 106 | ``` r 107 | inds$income <- cut(inds$income, breaks = 2, include.lowest = TRUE, 108 | labels = c("low", "high")) 109 | 110 | ext_weights <- rk_extract(weights, inds = inds, id = "id") 111 | ext_weights 112 | #> code total age_0_49 age_gt_50 sex_f sex_m high low 113 | #> 1 a 12 8 4 6 6 2.772002 9.227998 114 | #> 2 b 10 2 8 6 4 6.274917 3.725083 115 | #> 3 c 11 7 4 8 3 3.274917 7.725083 116 | ``` 117 | 118 | `rk_extract()` returns one row per zone, and the total of each category (for example female and male, or high and low income) will match the known population. 119 | 120 | `rk_integerise()` 121 | ----------------- 122 | 123 | The `rk_integerise()` function produces a simulated data frame populated with simulated individuals. This is typically useful when: 124 | 125 | - You need to include numerical variables, such as income in the example. 126 | - You want individual cases to use as input to a dynamic or agent-based model. 127 | - You want 'case studies' to illustrate characteristics of individuals in an area. 128 | - Individual-level data is more intuitive to work with. 129 | 130 | ``` r 131 | int_weights <- rk_integerise(weights, inds = inds) 132 | int_weights[1:6, ] 133 | #> id age sex income zone 134 | #> 1 A age_gt_50 sex_m high a 135 | #> 1.1 A age_gt_50 sex_m high a 136 | #> 2 B age_gt_50 sex_m low a 137 | #> 3 C age_0_49 sex_m low a 138 | #> 3.1 C age_0_49 sex_m low a 139 | #> 3.2 C age_0_49 sex_m low a 140 | ``` 141 | 142 | `rk_integerise()` returns one row per case, and the number of rows will match the known population (taken from `cons`). 143 | 144 | `rk_rake()` 145 | ----------- 146 | 147 | `rk_rake()` is a wrapper for `rk_weight() %>% rk_extract()` or `rk_weight() %>% rk_integerise()`. This is useful if the raw weights (from `rk_weight()`) are not required. The desired output is specified with the `output` argument, which can be specified with `"fraction"` (the default) or `"integer"`. The function takes the following arguments in all cases: 148 | 149 | - `cons` 150 | - `inds` 151 | - `vars` 152 | - `output` (default `"fraction"`) 153 | - `iterations` (default 10) 154 | 155 | Additional arguments are required depending on the output requested. For `output = "fraction"`: 156 | 157 | - `id` 158 | 159 | For `output = "integer"`: 160 | 161 | - `method` (default `"trs"`) 162 | - `seed` (default 42) 163 | 164 | Details of these context-specific arguments can be found in the respective documentation for `rk_integerise()` or `rk_extract()`. 165 | 166 | ``` r 167 | rake_int <- rk_rake(cons, inds, vars, output = "integer", 168 | method = "trs", seed = 42) 169 | rake_int[1:6, ] 170 | #> id age sex income zone 171 | #> 1 A age_gt_50 sex_m high a 172 | #> 1.1 A age_gt_50 sex_m high a 173 | #> 2 B age_gt_50 sex_m low a 174 | #> 3 C age_0_49 sex_m low a 175 | #> 3.1 C age_0_49 sex_m low a 176 | #> 3.2 C age_0_49 sex_m low a 177 | ``` 178 | 179 | ``` r 180 | rake_frac <- rk_rake(cons, inds, vars, output = "fraction", id = "id") 181 | rake_frac 182 | #> code total age_0_49 age_gt_50 sex_f sex_m high low 183 | #> 1 a 12 8 4 6 6 2.772002 9.227998 184 | #> 2 b 10 2 8 6 4 6.274917 3.725083 185 | #> 3 c 11 7 4 8 3 3.274917 7.725083 186 | ``` 187 | 188 | Contributing 189 | ------------ 190 | 191 | Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms. 192 | 193 | Issues and feedback 194 | ------------------- 195 | 196 | Feedback on the API, [bug reports/issues](https://github.com/philmikejones/rakeR/issues), and pull requests are very welcome. 197 | 198 | Acknowledgements 199 | ---------------- 200 | 201 | Many of the functions in this package are based on code written by [Robin Lovelace](https://github.com/Robinlovelace) and [Morgane Dumont](https://github.com/modumont) for their book [*Spatial Microsimulation with R* (2016), Chapman and Hall/CRC Press](https://www.crcpress.com/Spatial-Microsimulation-with-R/Lovelace-Dumont/p/book/9781498711548). 202 | 203 | Their book is an excellent resource for learning about spatial microsimulation and understanding what's going on under the hood of this package. 204 | 205 | The book and code are licensed under the terms below: 206 | 207 | Copyright (c) 2014 Robin Lovelace 208 | 209 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 210 | 211 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 212 | 213 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 214 | 215 | The [rewighting (ipfp) algorithm](https://github.com/awblocker/ipfp) is written by Andrew Blocker. 216 | 217 | The [`wrswoR` package](http://krlmlr.github.io/wrswoR/) used for fast sampling without replacement is written by Kirill Müller. 218 | 219 | Thanks to [Tom Broomhead](http://mhs.group.shef.ac.uk/members/tom-broomhead/) for his feedback on error handling and suggestions on function naming, to [Andrew Smith](https://github.com/virgesmith) for bug fixes, and Derrick Atherton for suggestions, feedback, and testing. 220 | 221 | Data used in some of the examples and tests ('cakeMap') are anonymised data from the [Adult Dental Health Survey](https://data.gov.uk/dataset/adult_dental_health_survey), used under terms of the [Open Government Licence](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/). 222 | 223 | Contact 224 | ------- 225 | 226 | philmikejones at gmail dot com 227 | 228 | License 229 | ------- 230 | 231 | Copyright 2016-18 Phil Mike Jones. 232 | 233 | rakeR is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 234 | 235 | rakeR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 236 | 237 | You should have received a copy of the GNU General Public License along with rakeR. If not, see . 238 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | branch: master 3 | -------------------------------------------------------------------------------- /codemeta.json: -------------------------------------------------------------------------------- 1 | { 2 | "@context": [ 3 | "https://doi.org/10.5063/schema/codemeta-2.0", 4 | "http://schema.org" 5 | ], 6 | "@type": "SoftwareSourceCode", 7 | "identifier": "rakeR", 8 | "description": "Functions for performing spatial microsimulation ('raking')\n in R.", 9 | "name": "rakeR: Easy Spatial Microsimulation (Raking) in R", 10 | "codeRepository": "https://philmikejones.github.io/rakeR/", 11 | "issueTracker": "https://github.com/philmikejones/rakeR/issues", 12 | "license": "https://spdx.org/licenses/GPL-3.0", 13 | "version": "0.2.1.9000", 14 | "programmingLanguage": { 15 | "@type": "ComputerLanguage", 16 | "name": "R", 17 | "version": "3.5.1", 18 | "url": "https://r-project.org" 19 | }, 20 | "runtimePlatform": "R version 3.5.1 (2018-07-02)", 21 | "provider": { 22 | "@id": "https://cran.r-project.org", 23 | "@type": "Organization", 24 | "name": "Comprehensive R Archive Network (CRAN)", 25 | "url": "https://cran.r-project.org" 26 | }, 27 | "author": [ 28 | { 29 | "@type": "Person", 30 | "givenName": "Phil Mike", 31 | "familyName": "Jones", 32 | "email": "philmikejones@gmail.com", 33 | "@id": "https://orcid.org/0000-0001-5173-3245" 34 | }, 35 | { 36 | "@type": "Person", 37 | "givenName": "Robin", 38 | "familyName": "Lovelace" 39 | }, 40 | { 41 | "@type": "Person", 42 | "givenName": "Morgane", 43 | "familyName": "Dumont" 44 | } 45 | ], 46 | "contributor": [ 47 | { 48 | "@type": "Person", 49 | "givenName": "Andrew", 50 | "familyName": "Smith" 51 | } 52 | ], 53 | "maintainer": [ 54 | { 55 | "@type": "Person", 56 | "givenName": "Phil Mike", 57 | "familyName": "Jones", 58 | "email": "philmikejones@gmail.com", 59 | "@id": "https://orcid.org/0000-0001-5173-3245" 60 | } 61 | ], 62 | "softwareSuggestions": [ 63 | { 64 | "@type": "SoftwareApplication", 65 | "identifier": "testthat", 66 | "name": "testthat", 67 | "provider": { 68 | "@id": "https://cran.r-project.org", 69 | "@type": "Organization", 70 | "name": "Comprehensive R Archive Network (CRAN)", 71 | "url": "https://cran.r-project.org" 72 | }, 73 | "sameAs": "https://CRAN.R-project.org/package=testthat" 74 | }, 75 | { 76 | "@type": "SoftwareApplication", 77 | "identifier": "readr", 78 | "name": "readr", 79 | "provider": { 80 | "@id": "https://cran.r-project.org", 81 | "@type": "Organization", 82 | "name": "Comprehensive R Archive Network (CRAN)", 83 | "url": "https://cran.r-project.org" 84 | }, 85 | "sameAs": "https://CRAN.R-project.org/package=readr" 86 | } 87 | ], 88 | "softwareRequirements": [ 89 | { 90 | "@type": "SoftwareApplication", 91 | "identifier": "R", 92 | "name": "R", 93 | "version": ">= 3.4.0" 94 | }, 95 | { 96 | "@type": "SoftwareApplication", 97 | "identifier": "ipfp", 98 | "name": "ipfp", 99 | "provider": { 100 | "@id": "https://cran.r-project.org", 101 | "@type": "Organization", 102 | "name": "Comprehensive R Archive Network (CRAN)", 103 | "url": "https://cran.r-project.org" 104 | }, 105 | "sameAs": "https://CRAN.R-project.org/package=ipfp" 106 | }, 107 | { 108 | "@type": "SoftwareApplication", 109 | "identifier": "wrswoR", 110 | "name": "wrswoR", 111 | "provider": { 112 | "@id": "https://cran.r-project.org", 113 | "@type": "Organization", 114 | "name": "Comprehensive R Archive Network (CRAN)", 115 | "url": "https://cran.r-project.org" 116 | }, 117 | "sameAs": "https://CRAN.R-project.org/package=wrswoR" 118 | } 119 | ], 120 | "releaseNotes": "https://github.com/philmikejones/rakeR/blob/master/NEWS.md", 121 | "readme": "https://github.com/philmikejones/rakeR/blob/master/README.md", 122 | "fileSize": "29.318KB", 123 | "contIntegration": [ 124 | "https://travis-ci.org/philmikejones/rakeR", 125 | "https://codecov.io/gh/philmikejones/rakeR" 126 | ], 127 | "developmentStatus": "https://www.repostatus.org/#active", 128 | "relatedLink": "https://CRAN.R-project.org/package=rakeR" 129 | } 130 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | * Local Ubuntu Linux (16.06 Xenial) 64-bit, R 3.4.2 3 | * Ubuntu Linux (14.04.5 Trusty) on Travis-CI, R 3.4.1 4 | * win-builder: R-release v3.4.2 (2017-09-28) 5 | * win-builder: R-devel (unstable) (2017-09-12 r73242) 6 | 7 | 8 | ## R CMD check results 9 | * Local: no ERRORs, WARNINGs, or NOTEs 10 | * R-release: 1 NOTE - spelling of 'microsimulation' (which is correct) 11 | * R-devel: 1 NOTE - development version 12 | 13 | 14 | ## Downstream dependencies 15 | There are currently no reverse/downstream dependencies 16 | -------------------------------------------------------------------------------- /data-raw/prep-cakemap.R: -------------------------------------------------------------------------------- 1 | # Prep and export cons and inds for tests 2 | 3 | # Cons 4 | cons <- readr::read_csv("inst/extdata/cakemap_cons.csv") 5 | colnames(cons) <- gsub("X", "", colnames(cons)) 6 | colnames(cons) <- gsub("\\.", "_", colnames(cons)) 7 | colnames(cons)[13:14] <- c("car_yes", "car_no") 8 | colnames(cons)[15:ncol(cons)] <- paste0("n_", colnames(cons)[15:ncol(cons)]) 9 | colnames(cons)[ncol(cons)] <- "n_97" 10 | cons$code <- paste0("c", 1:nrow(cons)) 11 | cons <- cons[, c(ncol(cons), 14:13, 15:24, 7:12, 1:6)] 12 | 13 | # population of nssec is 3 out compared to age/sex and car 14 | cons[1, 6] <- 2775 15 | 16 | cons[, 2:ncol(cons)] <- round(cons[, 2:ncol(cons)], digits = 2) 17 | readr::write_csv(cons, path = "tests/cakemap_cons.csv") 18 | 19 | 20 | # inds 21 | inds <- readr::read_csv("inst/extdata/cakemap_inds.csv") 22 | inds$code <- paste0("i", 1:nrow(inds)) 23 | inds <- inds[, c(6, 2:5, 1)] 24 | 25 | inds$ageband4 <- gsub("-", "_", inds$ageband4) 26 | inds$NSSEC8 <- gsub("\\.", "_", inds$NSSEC8) 27 | inds$NSSEC8 <- paste0("n_", inds$NSSEC8) 28 | inds$Sex <- factor(inds$Sex, levels = 1:2, 29 | labels = c("male", "female")) 30 | inds$Car <- factor(inds$Car, levels = 1:2, 31 | labels = c("car_yes", "car_no")) 32 | 33 | inds$ageband4[inds$Sex == "male"] <- 34 | paste0("sex_m", inds$ageband4[inds$Sex == "male"]) 35 | inds$ageband4[inds$Sex == "female"] <- 36 | paste0("sex_f", inds$ageband4[inds$Sex == "female"]) 37 | inds <- inds[, -3] 38 | 39 | readr::write_csv(inds, path = "tests/cakemap_inds.csv") 40 | -------------------------------------------------------------------------------- /docs/CONDUCT.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Contributor Code of Conduct • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
51 |
52 | 90 | 91 | 92 |
93 | 94 |
95 |
96 | 99 | 100 |
101 | 102 |

As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

103 |

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.

104 |

Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.

105 |

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.

106 |

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

107 |

This Code of Conduct is adapted from the Contributor Covenant (http:contributor-covenant.org), version 1.0.0, available at http://contributor-covenant.org/version/1/0/0/

108 |
109 | 110 |
111 | 112 |
113 | 114 | 115 |
116 | 119 | 120 |
121 |

Site built with pkgdown 1.3.0.

122 |
123 |
124 |
125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /docs/authors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Authors • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
51 |
52 | 90 | 91 | 92 |
93 | 94 |
95 |
96 | 99 | 100 |
    101 |
  • 102 |

    Phil Mike Jones. Author, maintainer. ORCID 103 |

    104 |
  • 105 |
  • 106 |

    Robin Lovelace. Author. 107 |
    Many functions are based on code by Robin Lovelace and Morgane Dumont

    108 |
  • 109 |
  • 110 |

    Morgane Dumont. Author. 111 |
    Many functions are based on code by Robin Lovelace and Morgane Dumont

    112 |
  • 113 |
  • 114 |

    Andrew Smith. Contributor. 115 |

    116 |
  • 117 |
118 | 119 |
120 | 121 |
122 | 123 | 124 |
125 | 128 | 129 |
130 |

Site built with pkgdown 1.3.0.

131 |
132 |
133 |
134 | 135 | 136 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /docs/docsearch.css: -------------------------------------------------------------------------------- 1 | /* Docsearch -------------------------------------------------------------- */ 2 | /* 3 | Source: https://github.com/algolia/docsearch/ 4 | License: MIT 5 | */ 6 | 7 | .algolia-autocomplete { 8 | display: block; 9 | -webkit-box-flex: 1; 10 | -ms-flex: 1; 11 | flex: 1 12 | } 13 | 14 | .algolia-autocomplete .ds-dropdown-menu { 15 | width: 100%; 16 | min-width: none; 17 | max-width: none; 18 | padding: .75rem 0; 19 | background-color: #fff; 20 | background-clip: padding-box; 21 | border: 1px solid rgba(0, 0, 0, .1); 22 | box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .175); 23 | } 24 | 25 | @media (min-width:768px) { 26 | .algolia-autocomplete .ds-dropdown-menu { 27 | width: 175% 28 | } 29 | } 30 | 31 | .algolia-autocomplete .ds-dropdown-menu::before { 32 | display: none 33 | } 34 | 35 | .algolia-autocomplete .ds-dropdown-menu [class^=ds-dataset-] { 36 | padding: 0; 37 | background-color: rgb(255,255,255); 38 | border: 0; 39 | max-height: 80vh; 40 | } 41 | 42 | .algolia-autocomplete .ds-dropdown-menu .ds-suggestions { 43 | margin-top: 0 44 | } 45 | 46 | .algolia-autocomplete .algolia-docsearch-suggestion { 47 | padding: 0; 48 | overflow: visible 49 | } 50 | 51 | .algolia-autocomplete .algolia-docsearch-suggestion--category-header { 52 | padding: .125rem 1rem; 53 | margin-top: 0; 54 | font-size: 1.3em; 55 | font-weight: 500; 56 | color: #00008B; 57 | border-bottom: 0 58 | } 59 | 60 | .algolia-autocomplete .algolia-docsearch-suggestion--wrapper { 61 | float: none; 62 | padding-top: 0 63 | } 64 | 65 | .algolia-autocomplete .algolia-docsearch-suggestion--subcategory-column { 66 | float: none; 67 | width: auto; 68 | padding: 0; 69 | text-align: left 70 | } 71 | 72 | .algolia-autocomplete .algolia-docsearch-suggestion--content { 73 | float: none; 74 | width: auto; 75 | padding: 0 76 | } 77 | 78 | .algolia-autocomplete .algolia-docsearch-suggestion--content::before { 79 | display: none 80 | } 81 | 82 | .algolia-autocomplete .ds-suggestion:not(:first-child) .algolia-docsearch-suggestion--category-header { 83 | padding-top: .75rem; 84 | margin-top: .75rem; 85 | border-top: 1px solid rgba(0, 0, 0, .1) 86 | } 87 | 88 | .algolia-autocomplete .ds-suggestion .algolia-docsearch-suggestion--subcategory-column { 89 | display: block; 90 | padding: .1rem 1rem; 91 | margin-bottom: 0.1; 92 | font-size: 1.0em; 93 | font-weight: 400 94 | /* display: none */ 95 | } 96 | 97 | .algolia-autocomplete .algolia-docsearch-suggestion--title { 98 | display: block; 99 | padding: .25rem 1rem; 100 | margin-bottom: 0; 101 | font-size: 0.9em; 102 | font-weight: 400 103 | } 104 | 105 | .algolia-autocomplete .algolia-docsearch-suggestion--text { 106 | padding: 0 1rem .5rem; 107 | margin-top: -.25rem; 108 | font-size: 0.8em; 109 | font-weight: 400; 110 | line-height: 1.25 111 | } 112 | 113 | .algolia-autocomplete .algolia-docsearch-footer { 114 | width: 110px; 115 | height: 20px; 116 | z-index: 3; 117 | margin-top: 10.66667px; 118 | float: right; 119 | font-size: 0; 120 | line-height: 0; 121 | } 122 | 123 | .algolia-autocomplete .algolia-docsearch-footer--logo { 124 | background-image: url("data:image/svg+xml;utf8,"); 125 | background-repeat: no-repeat; 126 | background-position: 50%; 127 | background-size: 100%; 128 | overflow: hidden; 129 | text-indent: -9000px; 130 | width: 100%; 131 | height: 100%; 132 | display: block; 133 | transform: translate(-8px); 134 | } 135 | 136 | .algolia-autocomplete .algolia-docsearch-suggestion--highlight { 137 | color: #FF8C00; 138 | background: rgba(232, 189, 54, 0.1) 139 | } 140 | 141 | 142 | .algolia-autocomplete .algolia-docsearch-suggestion--text .algolia-docsearch-suggestion--highlight { 143 | box-shadow: inset 0 -2px 0 0 rgba(105, 105, 105, .5) 144 | } 145 | 146 | .algolia-autocomplete .ds-suggestion.ds-cursor .algolia-docsearch-suggestion--content { 147 | background-color: rgba(192, 192, 192, .15) 148 | } 149 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /docs/link.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/news/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Changelog • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
51 |
52 | 90 | 91 | 92 |
93 | 94 |
95 |
96 | 100 | 101 |
102 |

103 | v 0.2.2 Unreleased 104 |

105 |
    106 |
  • Functions have been renamed to include a rk_ prefix. Function names without prefixes have been deprecated but will continue to work for now so as not to affect existing code.
  • 107 |
  • Increased code coverage.
  • 108 |
  • check_constraint() and check_ind() are now deprecated. These checks are carried out by the weight() and/or integerise()/extract() functions automatically.
  • 109 |
  • Update README.md documentation
  • 110 |
  • Add additional unit tests - thanks Derrick Atherton for feedback
  • 111 |
  • Add appropriate acknowledgements for source of data set used for examples and testing.
  • 112 |
113 |
114 |
115 |

116 | v 0.2.1 2017-10-10 117 |

118 |

Patch release:

119 |
    120 |
  • Fixes to examples and tests to use correct variable labels and names (thanks Derrick Atherton for pointing this out).
  • 121 |
  • Fix version number in NEWS
  • 122 |
123 |
124 |
125 |

126 | v 0.2.0 2017-06-30 127 |

128 | 139 |
140 |
141 |

142 | v 0.1.2 2016-11-14 143 |

144 |

Patch release

145 |
146 |

147 | New functions

148 |
    149 |
  • check_ind() checks individual level data for common errors
  • 150 |
151 |
152 |
153 |

154 | Fixes:

155 | 161 |
162 |
163 |
164 |

165 | v 0.1.1 2016-09-19 166 |

167 |

Initial CRAN release

168 |
169 |

170 | Fixes:

171 |
    172 |
  • Fix license issue: remove LICENSE, state GPL-3
  • 173 |
174 |
175 |
176 |
177 |

178 | v 0.1.0 Unreleased 179 |

180 |
181 |

182 | New functions

183 |
    184 |
  • weight(),
  • 185 |
  • integerise(),
  • 186 |
  • simulate(),
  • 187 |
  • rake(), and
  • 188 |
  • check_constraint()
  • 189 |
190 |
191 |
192 |
193 | 194 | 207 | 208 |
209 | 210 |
211 | 214 | 215 |
216 |

Site built with pkgdown 1.3.0.

217 |
218 |
219 |
220 | 221 | 222 | 223 | 224 | 225 | 226 | -------------------------------------------------------------------------------- /docs/pkgdown.css: -------------------------------------------------------------------------------- 1 | /* Sticky footer */ 2 | 3 | /** 4 | * Basic idea: https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ 5 | * Details: https://github.com/philipwalton/solved-by-flexbox/blob/master/assets/css/components/site.css 6 | * 7 | * .Site -> body > .container 8 | * .Site-content -> body > .container .row 9 | * .footer -> footer 10 | * 11 | * Key idea seems to be to ensure that .container and __all its parents__ 12 | * have height set to 100% 13 | * 14 | */ 15 | 16 | html, body { 17 | height: 100%; 18 | } 19 | 20 | body > .container { 21 | display: flex; 22 | height: 100%; 23 | flex-direction: column; 24 | 25 | padding-top: 60px; 26 | } 27 | 28 | body > .container .row { 29 | flex: 1 0 auto; 30 | } 31 | 32 | footer { 33 | margin-top: 45px; 34 | padding: 35px 0 36px; 35 | border-top: 1px solid #e5e5e5; 36 | color: #666; 37 | display: flex; 38 | flex-shrink: 0; 39 | } 40 | footer p { 41 | margin-bottom: 0; 42 | } 43 | footer div { 44 | flex: 1; 45 | } 46 | footer .pkgdown { 47 | text-align: right; 48 | } 49 | footer p { 50 | margin-bottom: 0; 51 | } 52 | 53 | img.icon { 54 | float: right; 55 | } 56 | 57 | img { 58 | max-width: 100%; 59 | } 60 | 61 | /* Fix bug in bootstrap (only seen in firefox) */ 62 | summary { 63 | display: list-item; 64 | } 65 | 66 | /* Typographic tweaking ---------------------------------*/ 67 | 68 | .contents .page-header { 69 | margin-top: calc(-60px + 1em); 70 | } 71 | 72 | /* Section anchors ---------------------------------*/ 73 | 74 | a.anchor { 75 | margin-left: -30px; 76 | display:inline-block; 77 | width: 30px; 78 | height: 30px; 79 | visibility: hidden; 80 | 81 | background-image: url(./link.svg); 82 | background-repeat: no-repeat; 83 | background-size: 20px 20px; 84 | background-position: center center; 85 | } 86 | 87 | .hasAnchor:hover a.anchor { 88 | visibility: visible; 89 | } 90 | 91 | @media (max-width: 767px) { 92 | .hasAnchor:hover a.anchor { 93 | visibility: hidden; 94 | } 95 | } 96 | 97 | 98 | /* Fixes for fixed navbar --------------------------*/ 99 | 100 | .contents h1, .contents h2, .contents h3, .contents h4 { 101 | padding-top: 60px; 102 | margin-top: -40px; 103 | } 104 | 105 | /* Static header placement on mobile devices */ 106 | @media (max-width: 767px) { 107 | .navbar-fixed-top { 108 | position: absolute; 109 | } 110 | .navbar { 111 | padding: 0; 112 | } 113 | } 114 | 115 | 116 | /* Sidebar --------------------------*/ 117 | 118 | #sidebar { 119 | margin-top: 30px; 120 | } 121 | #sidebar h2 { 122 | font-size: 1.5em; 123 | margin-top: 1em; 124 | } 125 | 126 | #sidebar h2:first-child { 127 | margin-top: 0; 128 | } 129 | 130 | #sidebar .list-unstyled li { 131 | margin-bottom: 0.5em; 132 | } 133 | 134 | .orcid { 135 | height: 16px; 136 | vertical-align: middle; 137 | } 138 | 139 | /* Reference index & topics ----------------------------------------------- */ 140 | 141 | .ref-index th {font-weight: normal;} 142 | 143 | .ref-index td {vertical-align: top;} 144 | .ref-index .icon {width: 40px;} 145 | .ref-index .alias {width: 40%;} 146 | .ref-index-icons .alias {width: calc(40% - 40px);} 147 | .ref-index .title {width: 60%;} 148 | 149 | .ref-arguments th {text-align: right; padding-right: 10px;} 150 | .ref-arguments th, .ref-arguments td {vertical-align: top;} 151 | .ref-arguments .name {width: 20%;} 152 | .ref-arguments .desc {width: 80%;} 153 | 154 | /* Nice scrolling for wide elements --------------------------------------- */ 155 | 156 | table { 157 | display: block; 158 | overflow: auto; 159 | } 160 | 161 | /* Syntax highlighting ---------------------------------------------------- */ 162 | 163 | pre { 164 | word-wrap: normal; 165 | word-break: normal; 166 | border: 1px solid #eee; 167 | } 168 | 169 | pre, code { 170 | background-color: #f8f8f8; 171 | color: #333; 172 | } 173 | 174 | pre code { 175 | overflow: auto; 176 | word-wrap: normal; 177 | white-space: pre; 178 | } 179 | 180 | pre .img { 181 | margin: 5px 0; 182 | } 183 | 184 | pre .img img { 185 | background-color: #fff; 186 | display: block; 187 | height: auto; 188 | } 189 | 190 | code a, pre a { 191 | color: #375f84; 192 | } 193 | 194 | a.sourceLine:hover { 195 | text-decoration: none; 196 | } 197 | 198 | .fl {color: #1514b5;} 199 | .fu {color: #000000;} /* function */ 200 | .ch,.st {color: #036a07;} /* string */ 201 | .kw {color: #264D66;} /* keyword */ 202 | .co {color: #888888;} /* comment */ 203 | 204 | .message { color: black; font-weight: bolder;} 205 | .error { color: orange; font-weight: bolder;} 206 | .warning { color: #6A0366; font-weight: bolder;} 207 | 208 | /* Clipboard --------------------------*/ 209 | 210 | .hasCopyButton { 211 | position: relative; 212 | } 213 | 214 | .btn-copy-ex { 215 | position: absolute; 216 | right: 0; 217 | top: 0; 218 | visibility: hidden; 219 | } 220 | 221 | .hasCopyButton:hover button.btn-copy-ex { 222 | visibility: visible; 223 | } 224 | 225 | /* mark.js ----------------------------*/ 226 | 227 | mark { 228 | background-color: rgba(255, 255, 51, 0.5); 229 | border-bottom: 2px solid rgba(255, 153, 51, 0.3); 230 | padding: 1px; 231 | } 232 | 233 | /* vertical spacing after htmlwidgets */ 234 | .html-widget { 235 | margin-bottom: 10px; 236 | } 237 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | /* http://gregfranko.com/blog/jquery-best-practices/ */ 2 | (function($) { 3 | $(function() { 4 | 5 | $("#sidebar") 6 | .stick_in_parent({offset_top: 40}) 7 | .on('sticky_kit:bottom', function(e) { 8 | $(this).parent().css('position', 'static'); 9 | }) 10 | .on('sticky_kit:unbottom', function(e) { 11 | $(this).parent().css('position', 'relative'); 12 | }); 13 | 14 | $('body').scrollspy({ 15 | target: '#sidebar', 16 | offset: 60 17 | }); 18 | 19 | $('[data-toggle="tooltip"]').tooltip(); 20 | 21 | var cur_path = paths(location.pathname); 22 | var links = $("#navbar ul li a"); 23 | var max_length = -1; 24 | var pos = -1; 25 | for (var i = 0; i < links.length; i++) { 26 | if (links[i].getAttribute("href") === "#") 27 | continue; 28 | // Ignore external links 29 | if (links[i].host !== location.host) 30 | continue; 31 | 32 | var nav_path = paths(links[i].pathname); 33 | 34 | var length = prefix_length(nav_path, cur_path); 35 | if (length > max_length) { 36 | max_length = length; 37 | pos = i; 38 | } 39 | } 40 | 41 | // Add class to parent
  • , and enclosing
  • if in dropdown 42 | if (pos >= 0) { 43 | var menu_anchor = $(links[pos]); 44 | menu_anchor.parent().addClass("active"); 45 | menu_anchor.closest("li.dropdown").addClass("active"); 46 | } 47 | }); 48 | 49 | function paths(pathname) { 50 | var pieces = pathname.split("/"); 51 | pieces.shift(); // always starts with / 52 | 53 | var end = pieces[pieces.length - 1]; 54 | if (end === "index.html" || end === "") 55 | pieces.pop(); 56 | return(pieces); 57 | } 58 | 59 | // Returns -1 if not found 60 | function prefix_length(needle, haystack) { 61 | if (needle.length > haystack.length) 62 | return(-1); 63 | 64 | // Special case for length-0 haystack, since for loop won't run 65 | if (haystack.length === 0) { 66 | return(needle.length === 0 ? 0 : -1); 67 | } 68 | 69 | for (var i = 0; i < haystack.length; i++) { 70 | if (needle[i] != haystack[i]) 71 | return(i); 72 | } 73 | 74 | return(haystack.length); 75 | } 76 | 77 | /* Clipboard --------------------------*/ 78 | 79 | function changeTooltipMessage(element, msg) { 80 | var tooltipOriginalTitle=element.getAttribute('data-original-title'); 81 | element.setAttribute('data-original-title', msg); 82 | $(element).tooltip('show'); 83 | element.setAttribute('data-original-title', tooltipOriginalTitle); 84 | } 85 | 86 | if(ClipboardJS.isSupported()) { 87 | $(document).ready(function() { 88 | var copyButton = ""; 89 | 90 | $(".examples, div.sourceCode").addClass("hasCopyButton"); 91 | 92 | // Insert copy buttons: 93 | $(copyButton).prependTo(".hasCopyButton"); 94 | 95 | // Initialize tooltips: 96 | $('.btn-copy-ex').tooltip({container: 'body'}); 97 | 98 | // Initialize clipboard: 99 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', { 100 | text: function(trigger) { 101 | return trigger.parentNode.textContent; 102 | } 103 | }); 104 | 105 | clipboardBtnCopies.on('success', function(e) { 106 | changeTooltipMessage(e.trigger, 'Copied!'); 107 | e.clearSelection(); 108 | }); 109 | 110 | clipboardBtnCopies.on('error', function() { 111 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy'); 112 | }); 113 | }); 114 | } 115 | })(window.jQuery || window.$) 116 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: 1.19.2.4 2 | pkgdown: 1.3.0 3 | pkgdown_sha: ~ 4 | articles: [] 5 | 6 | -------------------------------------------------------------------------------- /docs/reference/check_constraint.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | check_constraint — check_constraint • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 94 | 95 | 96 |
    97 | 98 |
    99 |
    100 | 105 | 106 |
    107 | 108 |

    Deprecated: these checks are now done as part of the weight() and/or 109 | extract()/integerise() steps automatically

    110 | 111 |
    112 | 113 |
    check_constraint(constraint_var, num_zones)
    114 | 115 |

    Arguments

    116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 |
    constraint_var

    The constraint table to check, usually a data frame

    num_zones

    The number of zones that should be present in the table

    127 | 128 |

    Value

    129 | 130 |

    If no errors are detected the function returns silently. Any errors 131 | will stop the function or script to be investigated.

    132 | 133 |

    Details

    134 | 135 |

    Checks a constraint table for the following common errors:

      136 |
    • Ensures all zone codes are unique

    • 137 |
    • Ensures there are the expected number of zones

    • 138 |
    • Ensures all but the zone column are numeric (integer or double)

    • 139 |
    140 | 141 | 142 |

    Examples

    143 |
    ## Not run 144 | ## check_constraint() is deprecated. These checks are automatically 145 | ## carried out as part of the weight() and/or extract()/integerise() 146 | ## functions 147 |
    148 |
    149 | 162 |
    163 | 164 |
    165 | 168 | 169 |
    170 |

    Site built with pkgdown 1.3.0.

    171 |
    172 |
    173 |
    174 | 175 | 176 | 177 | 178 | 179 | 180 | -------------------------------------------------------------------------------- /docs/reference/check_ind.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | check_ind — check_ind • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 94 | 95 | 96 |
    97 | 98 |
    99 |
    100 | 105 | 106 |
    107 | 108 |

    Deprecated: these checks are now automatically carried out by 109 | weight() and/or extract()/integerise()

    110 | 111 |
    112 | 113 |
    check_ind(ind_var)
    114 | 115 |

    Arguments

    116 | 117 | 118 | 119 | 120 | 121 | 122 |
    ind_var

    the individual (survey) variable you want to check

    123 | 124 |

    Value

    125 | 126 |

    If no errors are detected the function returns silently. Any errors 127 | will stop the function or script to be investigated.

    128 | 129 |

    Details

    130 | 131 |

    Checks an individual (survey) variable for the following common errors:

      132 |
    • That each row sums to 1 (i.e. correctly converted to a dummy 133 | variable)

    • 134 |
    135 | 136 | 137 |

    Examples

    138 |
    ## Not run 139 | ## checks are now automatically carried out by weight() and/or 140 | ## extract()/integerise() 141 |
    142 |
    143 | 156 |
    157 | 158 |
    159 | 162 | 163 |
    164 |

    Site built with pkgdown 1.3.0.

    165 |
    166 |
    167 |
    168 | 169 | 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /docs/reference/extract.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | extract — extract • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Deprecated. Use rk_extract() instead.

    108 | 109 |
    110 | 111 |
    extract(weights, inds, id)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 128 | 129 |
    weights

    A weight table, typically produced by rakeR::weight()

    inds

    The individual level data

    id

    The unique id variable in the individual level data (inds), 127 | usually the first column

    130 | 131 |

    Value

    132 | 133 |

    A data frame with zones and aggregated simulated values for each 134 | variable

    135 | 136 | 137 |

    Examples

    138 |
    # NOT RUN {
    139 | Deprecated. Use rk_extract()
    140 | # }
    141 | 
    142 |
    143 | 154 |
    155 | 156 |
    157 | 160 | 161 |
    162 |

    Site built with pkgdown 1.3.0.

    163 |
    164 |
    165 |
    166 | 167 | 168 | 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /docs/reference/extract_weights.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | extract_weights — extract_weights • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Deprecated: use rakeR::rk_extract()

    108 | 109 |
    110 | 111 |
    extract_weights(weights, inds, id)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 128 | 129 |
    weights

    A weight table, typically produced using rakeR::rk_weight()

    inds

    The individual level data

    id

    The unique id variable in the individual level data (inds), 127 | usually the first column

    130 | 131 |

    Value

    132 | 133 |

    A data frame with zones and aggregated simulated values for each 134 | variable

    135 | 136 | 137 |

    Examples

    138 |
    # NOT RUN {
    139 | extract_weights() is deprecated, use rk_extract() instead
    140 | # }
    141 | 
    142 |
    143 | 154 |
    155 | 156 |
    157 | 160 | 161 |
    162 |

    Site built with pkgdown 1.3.0.

    163 |
    164 |
    165 |
    166 | 167 | 168 | 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /docs/reference/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Function reference • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 |
    51 |
    52 | 90 | 91 | 92 |
    93 | 94 |
    95 |
    96 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 114 | 115 | 116 | 117 | 120 | 121 | 122 | 123 | 126 | 127 | 128 | 129 | 132 | 133 | 134 | 135 | 138 | 139 | 140 | 141 | 144 | 145 | 146 | 147 | 150 | 151 | 152 | 153 | 156 | 157 | 158 | 159 | 162 | 163 | 164 | 165 | 168 | 169 | 170 | 171 | 174 | 175 | 176 | 177 | 180 | 181 | 182 | 183 |
    111 |

    All functions

    112 |

    113 |
    118 |

    check_constraint()

    119 |

    check_constraint

    124 |

    check_ind()

    125 |

    check_ind

    130 |

    extract()

    131 |

    extract

    136 |

    extract_weights()

    137 |

    extract_weights

    142 |

    integerise()

    143 |

    integerise

    148 |

    rake()

    149 |

    rake

    154 |

    rk_extract()

    155 |

    rk_extract

    160 |

    rk_integerise()

    161 |

    rk_integerise

    166 |

    rk_rake()

    167 |

    rk_rake

    172 |

    rk_weight()

    173 |

    rk_weight

    178 |

    weight()

    179 |

    weight

    184 |
    185 | 186 | 192 |
    193 | 194 |
    195 | 198 | 199 |
    200 |

    Site built with pkgdown 1.3.0.

    201 |
    202 |
    203 |
    204 | 205 | 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /docs/reference/integerise.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | integerise — integerise • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Deprecated. Use rk_integerise()

    108 | 109 |
    110 | 111 |
    integerise(weights, inds, method = "trs", seed = 42)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 129 | 130 | 131 | 132 | 133 | 134 |
    weights

    A matrix or data frame of fractional weights, typically 119 | provided by rakeR::weight()

    inds

    The individual--level data (i.e. one row per individual)

    method

    The integerisation method specified as a character string. 128 | Defaults to "trs"; currently other methods are not implemented.

    seed

    The seed to use, defaults to 42.

    135 | 136 |

    Value

    137 | 138 |

    A data frame of integerised cases

    139 | 140 | 141 |

    Examples

    142 |
    # NOT RUN {
    143 | Deprecated. Use rk_integerise()
    144 | # }
    145 | 
    146 |
    147 | 158 |
    159 | 160 |
    161 | 164 | 165 |
    166 |

    Site built with pkgdown 1.3.0.

    167 |
    168 |
    169 |
    170 | 171 | 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /docs/reference/rake.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | rake — rake • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Deprecated. Use rk_rake()

    108 | 109 |
    110 | 111 |
    rake(cons, inds, vars, output = "fraction", iterations = 10, ...)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 144 | 145 |
    cons

    A data frame of constraint variables

    inds

    A data frame of individual--level (survey) data

    vars

    A character string of variables to iterate over

    output

    A string specifying the desired output, either "fraction" 131 | (rk_extract()) or "integer" (rk_integerise())

    iterations

    The number of iterations to perform. Defaults to 10.

    ...

    Additional arguments to pass to depending on desired output:

      140 |
    • if "fraction" specify 'id' (see rk_extract() documentation)

    • 141 |
    • if "integer" specify 'method' and 'seed' (see rk_integerise() 142 | documentation)

    • 143 |
    146 | 147 |

    Value

    148 | 149 |

    A data frame with extracted weights (if output == "fraction", the 150 | default) or integerised cases (if output == "integer")

    151 | 152 | 153 |

    Examples

    154 |
    # NOT RUN {
    155 | Deprecated. Use rk_rake()
    156 | # }
    157 | 
    158 |
    159 | 170 |
    171 | 172 |
    173 | 176 | 177 |
    178 |

    Site built with pkgdown 1.3.0.

    179 |
    180 |
    181 |
    182 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /docs/reference/rk_extract.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | rk_extract — rk_extract • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Extract aggregate weights from individual weight table

    108 | 109 |
    110 | 111 |
    rk_extract(weights, inds, id)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 128 | 129 |
    weights

    A weight table, typically produced by rakeR::weight()

    inds

    The individual level data

    id

    The unique id variable in the individual level data (inds), 127 | usually the first column

    130 | 131 |

    Value

    132 | 133 |

    A data frame with zones and aggregated simulated values for each 134 | variable

    135 | 136 |

    Details

    137 | 138 |

    Extract aggregate weights from individual weight table, typically produced 139 | by rakeR::rk_weight()

    140 |

    Extract cannot operate with numeric variables because it creates a new 141 | variable for each unique factor of each variable 142 | If you want numeric information, like income, you need to cut() the 143 | numeric values, or use integerise() instead.

    144 | 145 | 146 |

    Examples

    147 |
    ## Not run 148 | ## Use weights object from rk_weight() 149 | ## ext_weights <- rk_extract(weights = weights, inds = inds, id = "id") 150 |
    151 |
    152 | 165 |
    166 | 167 |
    168 | 171 | 172 |
    173 |

    Site built with pkgdown 1.3.0.

    174 |
    175 |
    176 |
    177 | 178 | 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /docs/reference/rk_integerise.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | rk_integerise — rk_integerise • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Generate integer cases from numeric weights matrix.

    108 | 109 |
    110 | 111 |
    rk_integerise(weights, inds, method = "trs", seed = 42)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 129 | 130 | 131 | 132 | 133 | 134 |
    weights

    A matrix or data frame of fractional weights, typically 119 | provided by rakeR::rk_weight()

    inds

    The individual--level data (i.e. one row per individual)

    method

    The integerisation method specified as a character string. 128 | Defaults to "trs"; currently other methods are not implemented.

    seed

    The seed to use, defaults to 42.

    135 | 136 |

    Value

    137 | 138 |

    A data frame of integerised cases

    139 | 140 |

    Details

    141 | 142 |

    Extracted weights (using rakeR::rk_extract()) are more 'precise' than 143 | integerised weights (although the user should be careful this is not 144 | spurious precision based on context) as they return fractions. 145 | Nevertheless, integerised weights are useful in cases when:

      146 |
    • Numeric information (such as income) is required, as this needs 147 | to be cut() to work with rakeR::rk_extract()

    • 148 |
    • Simulated 'individuals' are required for case studies of key 149 | areas.

    • 150 |
    • Input individual-level data for agent-based or dynamic models are 151 | required

    • 152 |
    153 |

    The default integerisation method uses the 'truncate, replicate, sample' 154 | method developed by Robin Lovelace and Dimitris Ballas 155 | http://www.sciencedirect.com/science/article/pii/S0198971513000240

    156 |

    Other methods (for example proportional probabilities) may be implemented 157 | at a later date.

    158 | 159 | 160 |

    Examples

    161 |
    cons <- data.frame( 162 | "zone" = letters[1:3], 163 | "age_0_49" = c(8, 2, 7), 164 | "age_gt_50" = c(4, 8, 4), 165 | "sex_f" = c(6, 6, 8), 166 | "sex_m" = c(6, 4, 3), 167 | stringsAsFactors = FALSE 168 | ) 169 | 170 | inds <- data.frame( 171 | "id" = LETTERS[1:5], 172 | "age" = c( 173 | "age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49" 174 | ), 175 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 176 | "income" = c(2868, 2474, 2231, 3152, 2473), 177 | stringsAsFactors = FALSE 178 | ) 179 | vars <- c("age", "sex") 180 | 181 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 182 | weights_int <- rk_integerise(weights, inds = inds)
    183 |
    184 | 197 |
    198 | 199 |
    200 | 203 | 204 |
    205 |

    Site built with pkgdown 1.3.0.

    206 |
    207 |
    208 |
    209 | 210 | 211 | 212 | 213 | 214 | 215 | -------------------------------------------------------------------------------- /docs/reference/rk_rake.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | rk_rake — rk_rake • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 94 | 95 | 96 |
    97 | 98 |
    99 |
    100 | 105 | 106 |
    107 | 108 |

    A convenience function wrapping rk_weight() and rk_extract() or 109 | rk_weight() and rk_integerise()

    110 | 111 |
    112 | 113 |
    rk_rake(cons, inds, vars, output = "fraction", iterations = 10, ...)
    114 | 115 |

    Arguments

    116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 146 | 147 |
    cons

    A data frame of constraint variables

    inds

    A data frame of individual--level (survey) data

    vars

    A character string of variables to iterate over

    output

    A string specifying the desired output, either "fraction" 133 | (rk_extract()) or "integer" (rk_integerise())

    iterations

    The number of iterations to perform. Defaults to 10.

    ...

    Additional arguments to pass to depending on desired output:

      142 |
    • if "fraction" specify 'id' (see rk_extract() documentation)

    • 143 |
    • if "integer" specify 'method' and 'seed' (see rk_integerise() 144 | documentation)

    • 145 |
    148 | 149 |

    Value

    150 | 151 |

    A data frame with extracted weights (if output == "fraction", the 152 | default) or integerised cases (if output == "integer")

    153 | 154 | 155 |

    Examples

    156 |
    # NOT RUN {
    157 | frac_weights <- rake(cons, inds, vars, output = "fraction",
    158 |                      id = "id")
    159 | 
    160 | int_weight <- rake(cons, inds, vars, output = "integer",
    161 |                    method = "trs", seed = "42")
    162 | # }
    163 |
    164 | 175 |
    176 | 177 |
    178 | 181 | 182 |
    183 |

    Site built with pkgdown 1.3.0.

    184 |
    185 |
    186 |
    187 | 188 | 189 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /docs/reference/rk_weight.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | rk_weight — rk_weight • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 |
    55 |
    56 | 94 | 95 | 96 |
    97 | 98 |
    99 |
    100 | 105 | 106 |
    107 | 108 |

    Produces fractional weights using the iterative proportional fitting 109 | algorithm.

    110 | 111 |
    112 | 113 |
    rk_weight(cons, inds, vars = NULL, iterations = 10)
    114 | 115 |

    Arguments

    116 | 117 | 118 | 119 | 120 | 124 | 125 | 126 | 127 | 130 | 131 | 132 | 133 | 135 | 136 | 137 | 138 | 140 | 141 |
    cons

    A data frame containing all the constraints. This 121 | should be in the format of one row per zone, one column per constraint 122 | category. The first column should be a zone code; all other columns must be 123 | numeric counts.

    inds

    A data frame containing individual-level (survey) data. This 128 | should be in the format of one row per individual, one column per 129 | constraint. The first column should be an individual ID.

    vars

    A character vector of variables that constrain the simulation 134 | (i.e. independent variables)

    iterations

    The number of iterations the algorithm should complete. 139 | Defaults to 10

    142 | 143 |

    Value

    144 | 145 |

    A data frame of fractional weights for each individual in each zone 146 | with zone codes recorded in column names and individual id recorded in row 147 | names.

    148 | 149 |

    Details

    150 | 151 |

    rk_weight() requires three arguments:

      152 |
    • A data frame of constraints (e.g. census tables)

    • 153 |
    • A data frame of individual data (e.g. a survey)

    • 154 |
    • A character vector of constraint variable names

    • 155 |
    156 |

    The first column of each data frame should be an ID. The first column of 157 | cons should contain the zone codes. The first column of inds 158 | should contain the individual unique identifier.

    159 |

    Both data frames should only contain:

      160 |
    • an ID column (zone ID cons or individual ID inds).

    • 161 |
    • constraints inds or constraint category cons.

    • 162 |
    • inds can optionally contain additional dependent variables 163 | that do not influence the weighting process.

    • 164 |
    165 |

    No other columns should be present (the user can merge these back in later).

    166 |

    It is essential that the levels in each inds constraint (i.e. column) 167 | match exactly with the column names in cons. In the example below see 168 | how the column names in cons ('age_0_49', 'sex_f', ...) match exactly 169 | the levels in the appropriate inds variables.

    170 |

    The columns in cons must be arranged in alphabetical order because 171 | these are created alphabetically when they are 'spread' in the 172 | individual-level data.

    173 | 174 | 175 |

    Examples

    176 |
    # SimpleWorld 177 | cons <- data.frame( 178 | "zone" = letters[1:3], 179 | "age_0_49" = c(8, 2, 7), 180 | "age_gt_50" = c(4, 8, 4), 181 | "sex_f" = c(6, 6, 8), 182 | "sex_m" = c(6, 4, 3), 183 | stringsAsFactors = FALSE 184 | ) 185 | inds <- data.frame( 186 | "id" = LETTERS[1:5], 187 | "age" = c( 188 | "age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49" 189 | ), 190 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 191 | "income" = c(2868, 2474, 2231, 3152, 2473), 192 | stringsAsFactors = FALSE 193 | ) 194 | # Set variables to constrain over 195 | vars <- c("age", "sex") 196 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 197 | print(weights)
    #> a b c 198 | #> A 1.227998 1.7250828 0.7250828 199 | #> B 1.227998 1.7250828 0.7250828 200 | #> C 3.544004 0.5498344 1.5498344 201 | #> D 1.544004 4.5498344 2.5498344 202 | #> E 4.455996 1.4501656 5.4501656
    203 |
    204 | 217 |
    218 | 219 |
    220 | 223 | 224 |
    225 |

    Site built with pkgdown 1.3.0.

    226 |
    227 |
    228 |
    229 | 230 | 231 | 232 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /docs/reference/weight.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | weight — weight • rakeR 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 93 | 94 | 95 |
    96 | 97 |
    98 |
    99 | 104 | 105 |
    106 | 107 |

    Deprecated. Use rk_weight()

    108 | 109 |
    110 | 111 |
    weight(cons, inds, vars = NULL, iterations = 10)
    112 | 113 |

    Arguments

    114 | 115 | 116 | 117 | 118 | 122 | 123 | 124 | 125 | 128 | 129 | 130 | 131 | 133 | 134 | 135 | 136 | 138 | 139 |
    cons

    A data frame containing all the constraints. This 119 | should be in the format of one row per zone, one column per constraint 120 | category. The first column should be a zone code; all other columns must be 121 | numeric counts.

    inds

    A data frame containing individual-level (survey) data. This 126 | should be in the format of one row per individual, one column per 127 | constraint. The first column should be an individual ID.

    vars

    A character vector of variables that constrain the simulation 132 | (i.e. independent variables)

    iterations

    The number of iterations the algorithm should complete. 137 | Defaults to 10

    140 | 141 |

    Value

    142 | 143 |

    A data frame of fractional weights for each individual in each zone 144 | with zone codes recorded in column names and individual id recorded in row 145 | names.

    146 | 147 | 148 |

    Examples

    149 |
    # Deprecated. Use rk_weight() 150 |
    151 |
    152 | 163 |
    164 | 165 | 174 |
    175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /inst/extdata/cakemap_cons.csv: -------------------------------------------------------------------------------- 1 | m16_24,m25_34,m35_44,m45_54,m55_64,m65_74,f16_24,f25_34,f35_44,f45_54,f55_64,f65_74,Car,NoCar,X1.1,X1.2,X2,X3,X4,X5,X6,X7,X8,Other 2 | 671,771,1033,1160,1165,772,679,760,1053,1283,1139,859,9449,1896,347,1068,2772,1731,1132,657,1173,760,288,1417 3 | 887,1254,1217,1344,1229,752,832,1169,1255,1369,1228,886,10497,2925,397,1313,3264,1978,1128,735,1285,1041,378,1902 4 | 883,864,1195,1382,1170,878,811,962,1285,1493,1276,933,11018,2114,355,1001,2999,1894,1261,797,1483,1060,366,1915 5 | 976,1085,1184,1073,852,482,1021,1202,1034,1177,793,587,8264,3202,143,398,1583,1455,803,791,1587,1265,886,2556 6 | 1368,1764,1528,978,768,462,1421,1771,1296,1004,765,485,7285,6325,83,253,971,961,803,706,1638,1749,2106,4341 7 | 1552,1847,1425,908,688,349,1572,1893,1414,887,626,355,7852,5664,48,173,717,696,844,517,1305,1339,2605,5270 8 | 3607,3146,1638,814,623,297,3108,1968,1087,720,531,371,6983,10927,43,312,790,674,660,476,1127,1377,2142,10309 9 | 830,1042,1139,1031,927,465,1101,1152,1225,1145,925,593,8193,3382,144,449,1727,1355,798,699,1525,1429,847,2600 10 | 677,809,1094,1191,1131,862,715,868,1117,1289,1204,987,10201,1743,345,972,2786,1569,1364,742,1399,1101,221,1445 11 | 1040,1173,1086,1190,955,532,1084,1434,1197,1259,944,646,8242,4298,128,378,1649,1517,816,958,1989,1815,900,2391 12 | 1058,1361,1247,914,823,436,1138,1484,1122,1022,805,482,7668,4224,85,330,1128,1065,808,576,1466,1489,1494,3451 13 | 1127,1317,1159,829,738,414,1071,1362,1175,901,704,457,8224,3030,104,531,1343,906,861,418,963,869,1533,3726 14 | 830,1059,1259,1254,994,610,776,1069,1239,1310,1033,700,9686,2447,367,928,2739,1803,1052,793,1395,1011,387,1658 15 | 574,534,911,1059,956,744,551,542,1019,1164,984,881,8232,1687,407,1636,2811,1047,931,343,769,410,182,1383 16 | 1165,1564,1204,841,858,493,1207,1459,1036,853,811,498,7243,4746,109,335,1068,799,950,589,1358,1392,1801,3587 17 | 838,1106,1192,1131,1014,686,889,1087,1178,1166,998,730,9117,2898,258,641,2095,1413,1102,741,1524,1375,711,2156 18 | 940,933,1068,1140,944,657,1001,1061,1198,1203,912,720,7645,4132,153,342,1553,1328,791,919,2015,1849,698,2128 19 | 1425,2028,1485,900,718,422,1430,1977,1345,1006,656,434,6924,6902,53,222,777,779,744,586,1498,1800,2520,4846 20 | 1469,2039,1518,809,700,394,1478,1800,1192,776,626,378,6725,6454,35,248,691,631,755,477,1099,1282,2801,5160 21 | 883,1078,1233,1249,950,524,929,1101,1291,1324,902,573,9590,2447,268,695,2258,1762,1024,886,1533,1192,480,1940 22 | 943,1143,1152,1119,877,531,1060,1273,1251,1225,920,640,8182,3952,146,346,1621,1408,768,891,1939,1786,911,2318 23 | 777,1319,1215,1056,915,526,724,1218,1135,1028,921,631,7966,3499,252,1138,2627,1497,775,617,1238,867,538,1917 24 | 883,1071,1222,1150,971,625,955,1254,1234,1215,1024,642,8855,3391,193,570,2114,1505,967,751,1616,1379,834,2317 25 | 1472,1941,1343,832,695,369,1446,1820,1301,836,665,419,8826,4313,64,338,920,750,950,422,1149,1164,2404,4978 26 | 1181,1423,1382,1229,988,579,1259,1766,1359,1471,953,613,8166,6037,148,446,1523,1278,770,981,2128,2440,1374,3115 27 | 474,454,840,906,809,534,407,485,912,979,864,641,7299,1006,351,1188,2378,1074,794,331,667,376,108,1039 28 | 804,897,985,1001,830,514,812,979,1085,1058,811,607,6984,3399,128,344,1385,1413,772,745,1468,1459,692,1976 29 | 878,1108,1160,1130,917,593,968,1125,1193,1149,938,663,8127,3695,202,431,1739,1543,812,888,1864,1610,676,2056 30 | 646,794,1093,1169,1069,591,649,777,1140,1231,1062,624,9082,1763,318,827,2466,1349,1393,670,1228,944,303,1348 31 | 826,938,997,1063,983,645,855,951,1046,1104,995,715,8226,2892,202,416,1701,1523,869,890,1599,1485,579,1854 32 | 563,632,782,897,771,516,546,602,802,832,754,595,5967,2325,227,561,1649,1080,665,594,1117,937,290,1172 33 | 508,465,899,1024,905,475,481,534,1032,1139,954,490,6708,2198,270,999,2397,804,987,442,904,630,251,1223 34 | 595,716,845,839,705,500,651,763,838,850,724,550,6177,2399,154,439,1461,1110,673,626,1238,1126,425,1322 35 | 545,595,912,908,785,486,484,651,928,970,770,462,7120,1376,282,705,1904,1058,886,561,971,718,245,1166 36 | 512,592,766,911,797,545,495,609,820,870,832,546,6899,1396,271,741,1978,1170,776,477,919,718,202,1043 37 | 719,676,948,903,751,525,706,755,898,935,819,633,6284,2984,140,342,1339,1071,662,662,1528,1288,556,1678 38 | 519,459,735,890,775,503,511,465,834,924,758,510,6313,1570,239,702,1810,866,893,418,835,735,222,1164 39 | 582,503,816,939,810,580,503,563,862,972,820,649,7141,1458,258,627,1963,1367,881,550,955,715,185,1098 40 | 756,780,839,879,649,414,814,883,888,886,682,455,5453,3472,97,212,1054,928,520,626,1556,1531,655,1746 41 | 1094,1318,1129,702,617,332,1139,1396,953,654,546,335,5757,4458,43,225,645,619,637,385,1149,1148,1738,3625 42 | 620,570,807,822,753,618,503,558,867,783,826,632,6249,2110,202,583,1702,1044,615,576,1110,957,342,1226 43 | 549,571,858,893,784,518,527,585,925,964,761,487,6926,1496,278,771,1985,901,897,460,917,768,260,1186 44 | 649,866,917,873,754,507,586,798,935,920,758,562,7132,1993,270,1071,2074,1149,679,396,949,640,380,1517 45 | 652,854,809,889,710,507,637,755,817,852,781,563,6123,2703,149,516,1617,1146,712,650,1307,1066,360,1304 46 | 599,677,925,891,824,487,626,728,897,917,850,521,6303,2639,172,535,1774,882,889,575,1280,986,441,1408 47 | 706,1000,881,946,717,474,688,868,794,822,641,522,5396,3663,126,392,1370,1034,641,714,1510,1343,526,1404 48 | 585,822,788,772,718,527,670,855,817,813,703,567,6071,2566,141,368,1316,1073,626,593,1302,1103,545,1571 49 | 1132,1253,1332,1235,1033,726,1183,1440,1346,1344,1061,830,8951,4964,251,694,2063,1242,880,806,1724,1806,1128,3321 50 | 1173,1652,1439,1114,922,578,1239,1537,1224,1048,899,610,9300,4135,123,440,1386,1097,922,645,1648,1756,1784,3635 51 | 1148,1403,1348,1091,1047,722,1168,1490,1280,1150,1038,743,9840,3788,176,513,1632,1336,1006,877,1865,2009,1266,2948 52 | 816,899,1176,1168,1045,723,838,1041,1182,1226,1083,770,9291,2676,309,728,2305,1669,1051,774,1493,1507,436,1696 53 | 844,1001,1262,1221,1123,748,879,998,1223,1179,1082,817,9523,2854,292,694,2325,1620,1108,947,1707,1505,461,1715 54 | 1176,1062,1230,1232,974,711,1070,1120,1199,1306,1058,796,8494,4440,188,491,1843,1358,894,952,1845,1795,763,2804 55 | 740,765,1198,1207,1202,890,668,766,1264,1209,1254,926,10554,1535,415,1080,2786,1383,1253,840,1391,1167,285,1491 56 | 1170,1289,1415,1331,1020,709,1145,1225,1311,1262,1066,791,9541,4193,206,525,1886,1503,1014,1023,1907,2164,942,2565 57 | 1369,1252,1260,1130,1019,649,1194,1304,1236,1172,893,686,9437,3727,147,399,1377,1061,1051,672,1527,1700,1519,3710 58 | 1315,1652,1468,1029,822,454,1362,1690,1305,1012,739,449,8435,4862,103,323,1120,877,1014,695,1487,2041,1847,3790 59 | 1035,1272,1375,1303,1070,628,1033,1318,1365,1346,1029,695,9943,3526,258,738,2559,1531,983,1025,1892,1680,626,2177 60 | 1593,1820,1545,1227,907,604,1447,1605,1274,1076,861,648,9188,5419,184,794,1948,1057,985,626,1391,1417,1315,4892 61 | 1015,1194,1230,1107,1025,588,978,1192,1208,1107,981,682,9355,2952,201,483,1737,1341,1060,850,1649,1551,943,2491 62 | 877,775,1386,1572,1432,905,755,790,1503,1648,1388,906,12084,1853,538,1458,3536,1503,1558,747,1340,1070,309,1880 63 | 945,1063,1360,1405,1181,822,960,1151,1422,1456,1207,963,10744,3191,399,1223,2994,1713,1052,847,1587,1240,539,2340 64 | 1042,1178,1410,1314,1207,851,976,1182,1481,1362,1267,971,10907,3334,372,755,2519,1703,1182,1100,2111,1808,602,2089 65 | 958,939,1326,1458,1329,1077,930,985,1362,1388,1405,1174,11478,2853,397,1005,3032,1939,1250,1023,1704,1462,439,2080 66 | 2443,1653,1263,1181,1039,647,2263,1361,1098,1019,970,685,8773,6849,121,509,1604,986,709,692,1453,1465,1054,7028 67 | 838,797,1274,1483,1372,1010,751,906,1337,1532,1516,1141,11844,2113,499,1976,3509,1831,1298,560,1209,649,371,2054 68 | 1085,1338,1599,1511,1379,937,1078,1529,1579,1723,1469,1053,13151,3129,439,2184,3654,1997,1432,633,1473,821,707,2940 69 | 1140,1416,1841,1685,1232,776,1120,1626,1917,1705,1225,851,13958,2576,397,1132,3323,2440,1361,1110,2087,1715,577,2392 70 | 1661,2732,2026,1681,1191,691,1673,2417,1694,1514,1067,804,10619,8532,191,891,2668,2075,1142,1335,2802,2840,1516,3691 71 | 1255,1951,1616,1488,1077,695,1460,1927,1459,1340,992,737,8417,7580,148,524,1897,1773,951,1078,2438,2338,1399,3452 72 | 1171,1655,1506,1371,1135,714,1213,1934,1618,1403,1134,790,10174,5470,242,836,2494,1966,978,1118,2420,2061,882,2647 73 | 1753,2195,1907,1521,1153,704,1846,2146,1688,1393,1073,699,7374,10704,91,450,1768,1578,736,1000,2593,2814,1973,5075 74 | 1061,1702,1743,1457,1299,861,1025,1929,1697,1561,1370,973,13686,2992,420,1584,3732,2356,1419,1056,1894,1424,515,2277 75 | 1490,2376,1751,1374,908,572,1613,2543,1752,1536,1028,710,10347,7306,245,1685,3193,1588,930,727,1775,1382,1441,4688 76 | 4918,5487,2118,1299,903,531,5507,4174,1371,996,796,499,11739,16860,256,2297,3394,1842,808,990,2002,1931,1692,13388 77 | 1161,1310,1370,1422,1412,956,1170,1469,1462,1521,1482,1191,10868,5058,252,732,2671,2304,1113,1260,2543,1933,672,2448 78 | 1363,1761,1685,1673,1299,875,1312,1981,1788,1695,1320,969,12063,5658,281,816,2791,2485,1215,1350,2643,2373,909,2858 79 | 1013,889,1336,1409,1247,1114,959,926,1492,1427,1331,1316,11607,2852,342,907,2929,2218,1088,1112,2044,1388,328,2104 80 | 1871,2296,1952,1352,931,580,1868,2431,1947,1330,943,654,8032,10123,74,385,1340,1231,1117,895,2361,2119,2715,5918 81 | 988,1305,1697,1616,1401,921,997,1443,1772,1619,1498,1017,13764,2510,521,1847,3954,2284,1542,916,1758,1068,328,2054 82 | 736,641,1170,1408,1223,1090,667,711,1331,1446,1344,1200,11806,1161,592,1788,3419,1641,1493,523,1039,569,255,1650 83 | 6566,1631,511,340,404,187,7694,993,324,260,401,182,11453,8040,48,533,958,533,221,223,468,309,337,15863 84 | 1142,1471,1435,1428,1179,974,1129,1607,1500,1480,1319,1056,12883,2837,497,2061,3988,2042,1241,694,1330,866,344,2658 85 | 6361,2597,1117,655,576,276,7287,1969,789,587,474,312,8796,14204,44,462,964,706,352,418,943,790,940,17381 86 | 1424,1410,1519,1546,1208,818,1472,1680,1740,1685,1252,892,9184,7462,145,490,1871,1730,891,1180,2902,2533,1419,3485 87 | 1043,1246,1558,1471,1218,992,983,1348,1658,1527,1284,1032,12470,2890,335,895,2927,2234,1233,1324,2213,1839,436,1924 88 | 2777,2343,1304,1077,916,524,2721,2074,1201,1025,878,601,10032,7409,144,1001,2365,1537,705,743,1673,1337,851,7084 89 | 1556,1798,1765,1630,1281,871,1748,2132,1906,1737,1271,955,10605,8045,179,495,2015,2039,969,1368,3250,3140,1479,3716 90 | 1151,1801,1693,1479,1199,744,1088,1888,1764,1645,1255,925,13075,3557,389,2303,3852,1980,1247,680,1517,901,730,3032 91 | 1055,1476,1746,1660,1332,1009,998,1616,1708,1681,1495,1114,13560,3330,418,1239,3505,2648,1346,1258,2153,1751,460,2111 92 | 1034,1733,1765,1517,1210,775,1096,1821,1706,1530,1245,891,11718,4605,325,1149,3154,2436,1130,1206,2308,1969,583,2062 93 | 1029,1112,1475,1596,1527,1076,973,1194,1634,1656,1549,1200,12316,3705,353,1294,3473,2049,1533,1075,2306,1468,423,2047 94 | 1071,1394,1613,1577,1249,894,1105,1589,1707,1621,1381,1075,11902,4374,335,1014,3087,2387,1330,1137,2314,1857,582,2235 95 | 936,1136,1464,1419,1170,992,922,1296,1582,1470,1321,1079,11517,3270,391,1094,2952,2219,1123,1073,1978,1491,448,2018 96 | 1109,1631,1673,1445,1241,712,1098,1664,1716,1586,1248,833,12616,3340,456,2353,3729,1611,1200,557,1269,746,889,3148 97 | 1216,1088,1427,1630,1248,933,1155,1244,1552,1664,1259,1091,11024,4483,277,820,2729,2139,1106,1075,2070,1792,765,2733 98 | 2567,1844,1218,1227,1075,608,2737,1625,1250,1304,1177,705,12701,4636,287,1716,2929,1386,851,589,1195,743,592,7050 99 | 1085,1093,1445,1430,1340,1189,717,829,1346,1416,1482,1307,12459,2220,555,1634,3249,1888,1287,731,1518,1200,588,2028 100 | 813,845,1109,1230,1052,822,775,888,1187,1245,1025,904,9781,2114,254,677,2284,1312,992,900,1472,1935,434,1634 101 | 879,783,964,1026,920,620,879,893,996,1060,914,677,6986,3625,100,233,1036,932,535,936,1936,2270,868,1764 102 | 919,1033,1278,1211,905,727,880,1068,1257,1196,899,757,9049,3081,188,496,1957,1589,796,1001,1831,2156,477,1639 103 | 903,975,1188,1220,905,653,931,1015,1114,1173,932,724,8361,3372,159,361,1683,1366,686,1124,2204,2149,484,1517 104 | 824,713,997,1152,1104,757,774,776,1084,1150,1062,861,9010,2244,252,613,2020,1332,863,850,1719,1626,374,1606 105 | 869,850,1072,1155,991,818,933,971,1144,1089,995,859,8298,3448,142,314,1475,1129,774,988,2111,2369,687,1755 106 | 937,820,999,1082,977,754,902,923,1020,1110,990,803,7799,3518,131,289,1370,1081,716,856,1814,2466,711,1884 107 | 733,811,1058,1103,996,715,663,908,1098,1107,1094,816,8718,2384,249,683,2264,1552,956,746,1504,1378,356,1414 108 | 838,797,861,1022,851,618,911,851,862,1015,891,640,6934,3223,122,176,1085,904,485,1179,2110,1935,621,1540 109 | 890,1000,1245,1184,1022,604,908,1109,1301,1161,965,660,8550,3499,172,440,1869,1377,790,927,1827,2350,543,1755 110 | 787,852,1237,1166,1069,736,728,979,1249,1136,1106,820,9237,2628,273,625,2266,1633,1027,924,1675,1539,407,1496 111 | 866,995,1249,1195,879,679,857,1056,1247,1099,965,692,8582,3197,183,441,1912,1333,717,1054,2103,1863,572,1600 112 | 759,669,882,1099,1044,789,727,746,993,1183,1047,864,7992,2810,248,580,1897,1260,731,851,1688,1518,490,1538 113 | 986,1076,1202,1303,1028,795,1011,1097,1231,1247,1051,824,8988,3863,146,316,1562,1197,763,998,1873,3210,747,2037 114 | 791,927,1159,1246,1004,632,784,981,1189,1229,970,691,9493,2110,276,729,2399,1622,938,890,1449,1383,366,1552 115 | 966,1386,1219,1007,889,491,1019,1286,1029,974,756,504,6717,4809,108,423,1364,957,762,684,1699,2077,1094,2359 116 | 845,1426,1401,1287,957,587,907,1169,1062,962,852,559,7695,4319,168,514,1803,1248,816,868,1643,2092,821,2042 117 | 788,835,1208,1306,1221,871,865,999,1388,1309,1228,899,11241,1676,380,914,2758,1630,1179,842,1579,1491,440,1704 118 | 722,817,885,937,915,644,669,820,867,943,931,698,7254,2594,264,848,1924,1079,704,522,1142,1228,540,1596 119 | 872,898,1025,1063,899,619,950,1009,1096,1065,852,677,7028,3997,141,326,1462,1047,679,794,1774,1857,820,2125 120 | 744,645,976,1164,1040,702,703,673,1086,1179,1077,780,8693,2076,267,621,2241,1548,868,729,1414,1264,349,1468 121 | 1063,1126,1295,1186,1104,801,1008,1133,1323,1255,1135,982,9931,3480,319,798,2496,1490,1005,902,1791,1435,544,2630 122 | 822,1064,1356,1424,1141,710,771,1095,1338,1356,1128,728,10336,2597,310,1002,2774,1446,1359,990,1583,1376,379,1714 123 | 1166,1330,1337,1150,999,699,1096,1373,1183,1161,993,764,8653,4598,172,572,1654,1160,1011,709,1469,1463,1436,3605 124 | 799,762,1166,1273,1159,817,728,782,1239,1306,1223,851,9869,2236,436,1066,2707,1488,1160,759,1378,1093,320,1698 125 | 1252,713,1168,1215,1057,743,1175,734,1228,1225,1085,766,10936,1425,429,1046,2581,1202,1112,630,1000,839,222,3300 126 | -------------------------------------------------------------------------------- /man/extract.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated_functions.R 3 | \name{extract} 4 | \alias{extract} 5 | \title{extract} 6 | \usage{ 7 | extract(weights, inds, id) 8 | } 9 | \arguments{ 10 | \item{weights}{A weight table, typically produced by rakeR::weight()} 11 | 12 | \item{inds}{The individual level data} 13 | 14 | \item{id}{The unique id variable in the individual level data (inds), 15 | usually the first column} 16 | } 17 | \value{ 18 | A data frame with zones and aggregated simulated values for each 19 | variable 20 | } 21 | \description{ 22 | Deprecated. Use rk_extract() instead. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | Deprecated. Use rk_extract() 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /man/extract_weights.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated_functions.R 3 | \name{extract_weights} 4 | \alias{extract_weights} 5 | \title{extract_weights} 6 | \usage{ 7 | extract_weights(weights, inds, id) 8 | } 9 | \arguments{ 10 | \item{weights}{A weight table, typically produced using rakeR::rk_weight()} 11 | 12 | \item{inds}{The individual level data} 13 | 14 | \item{id}{The unique id variable in the individual level data (inds), 15 | usually the first column} 16 | } 17 | \value{ 18 | A data frame with zones and aggregated simulated values for each 19 | variable 20 | } 21 | \description{ 22 | Deprecated: use rakeR::rk_extract() 23 | } 24 | \examples{ 25 | \dontrun{ 26 | extract_weights() is deprecated, use rk_extract() instead 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /man/integerise.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated_functions.R 3 | \name{integerise} 4 | \alias{integerise} 5 | \alias{integerize} 6 | \title{integerise} 7 | \usage{ 8 | integerise(weights, inds, method = "trs", seed = 42) 9 | } 10 | \arguments{ 11 | \item{weights}{A matrix or data frame of fractional weights, typically 12 | provided by \code{rakeR::weight()}} 13 | 14 | \item{inds}{The individual--level data (i.e. one row per individual)} 15 | 16 | \item{method}{The integerisation method specified as a character string. 17 | Defaults to \code{"trs"}; currently other methods are not implemented.} 18 | 19 | \item{seed}{The seed to use, defaults to 42.} 20 | } 21 | \value{ 22 | A data frame of integerised cases 23 | } 24 | \description{ 25 | Deprecated. Use rk_integerise() 26 | } 27 | \examples{ 28 | \dontrun{ 29 | Deprecated. Use rk_integerise() 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/rake.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated_functions.R 3 | \name{rake} 4 | \alias{rake} 5 | \title{rake} 6 | \usage{ 7 | rake(cons, inds, vars, output = "fraction", iterations = 10, ...) 8 | } 9 | \arguments{ 10 | \item{cons}{A data frame of constraint variables} 11 | 12 | \item{inds}{A data frame of individual--level (survey) data} 13 | 14 | \item{vars}{A character string of variables to iterate over} 15 | 16 | \item{output}{A string specifying the desired output, either "fraction" 17 | (rk_extract()) or "integer" (rk_integerise())} 18 | 19 | \item{iterations}{The number of iterations to perform. Defaults to 10.} 20 | 21 | \item{...}{Additional arguments to pass to depending on desired output: 22 | \itemize{ 23 | \item{if "fraction" specify 'id' (see rk_extract() documentation)} 24 | \item{if "integer" specify 'method' and 'seed' (see rk_integerise() 25 | documentation)} 26 | }} 27 | } 28 | \value{ 29 | A data frame with extracted weights (if output == "fraction", the 30 | default) or integerised cases (if output == "integer") 31 | } 32 | \description{ 33 | Deprecated. Use rk_rake() 34 | } 35 | \examples{ 36 | \dontrun{ 37 | Deprecated. Use rk_rake() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /man/rk_extract.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/simulation_functions.R 3 | \name{rk_extract} 4 | \alias{rk_extract} 5 | \title{rk_extract} 6 | \usage{ 7 | rk_extract(weights, inds, id) 8 | } 9 | \arguments{ 10 | \item{weights}{A weight table, typically produced by rakeR::weight()} 11 | 12 | \item{inds}{The individual level data} 13 | 14 | \item{id}{The unique id variable in the individual level data (inds), 15 | usually the first column} 16 | } 17 | \value{ 18 | A data frame with zones and aggregated simulated values for each 19 | variable 20 | } 21 | \description{ 22 | Extract aggregate weights from individual weight table 23 | } 24 | \details{ 25 | Extract aggregate weights from individual weight table, typically produced 26 | by rakeR::rk_weight() 27 | 28 | Extract cannot operate with numeric variables because it creates a new 29 | variable for each unique factor of each variable 30 | If you want numeric information, like income, you need to cut() the 31 | numeric values, or use integerise() instead. 32 | } 33 | \examples{ 34 | ## Not run 35 | ## Use weights object from rk_weight() 36 | ## ext_weights <- rk_extract(weights = weights, inds = inds, id = "id") 37 | } 38 | -------------------------------------------------------------------------------- /man/rk_integerise.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/simulation_functions.R 3 | \name{rk_integerise} 4 | \alias{rk_integerise} 5 | \alias{rk_integerize} 6 | \title{rk_integerise} 7 | \usage{ 8 | rk_integerise(weights, inds, method = "trs", seed = 42) 9 | } 10 | \arguments{ 11 | \item{weights}{A matrix or data frame of fractional weights, typically 12 | provided by \code{rakeR::rk_weight()}} 13 | 14 | \item{inds}{The individual--level data (i.e. one row per individual)} 15 | 16 | \item{method}{The integerisation method specified as a character string. 17 | Defaults to \code{"trs"}; currently other methods are not implemented.} 18 | 19 | \item{seed}{The seed to use, defaults to 42.} 20 | } 21 | \value{ 22 | A data frame of integerised cases 23 | } 24 | \description{ 25 | Generate integer cases from numeric weights matrix. 26 | } 27 | \details{ 28 | Extracted weights (using rakeR::rk_extract()) are more 'precise' than 29 | integerised weights (although the user should be careful this is not 30 | spurious precision based on context) as they return fractions. 31 | Nevertheless, integerised weights are useful in cases when: 32 | \itemize{ 33 | \item{Numeric information (such as income) is required, as this needs 34 | to be cut() to work with rakeR::rk_extract()} 35 | \item{Simulated 'individuals' are required for case studies of key 36 | areas.} 37 | \item{Input individual-level data for agent-based or dynamic models are 38 | required} 39 | } 40 | 41 | The default integerisation method uses the 'truncate, replicate, sample' 42 | method developed by Robin Lovelace and Dimitris Ballas 43 | \url{http://www.sciencedirect.com/science/article/pii/S0198971513000240} 44 | 45 | Other methods (for example proportional probabilities) may be implemented 46 | at a later date. 47 | } 48 | \examples{ 49 | cons <- data.frame( 50 | "zone" = letters[1:3], 51 | "age_0_49" = c(8, 2, 7), 52 | "age_gt_50" = c(4, 8, 4), 53 | "sex_f" = c(6, 6, 8), 54 | "sex_m" = c(6, 4, 3), 55 | stringsAsFactors = FALSE 56 | ) 57 | 58 | inds <- data.frame( 59 | "id" = LETTERS[1:5], 60 | "age" = c( 61 | "age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49" 62 | ), 63 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 64 | "income" = c(2868, 2474, 2231, 3152, 2473), 65 | stringsAsFactors = FALSE 66 | ) 67 | vars <- c("age", "sex") 68 | 69 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 70 | weights_int <- rk_integerise(weights, inds = inds) 71 | } 72 | -------------------------------------------------------------------------------- /man/rk_rake.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/simulation_functions.R 3 | \name{rk_rake} 4 | \alias{rk_rake} 5 | \title{rk_rake} 6 | \usage{ 7 | rk_rake(cons, inds, vars, output = "fraction", iterations = 10, ...) 8 | } 9 | \arguments{ 10 | \item{cons}{A data frame of constraint variables} 11 | 12 | \item{inds}{A data frame of individual--level (survey) data} 13 | 14 | \item{vars}{A character string of variables to iterate over} 15 | 16 | \item{output}{A string specifying the desired output, either "fraction" 17 | (rk_extract()) or "integer" (rk_integerise())} 18 | 19 | \item{iterations}{The number of iterations to perform. Defaults to 10.} 20 | 21 | \item{...}{Additional arguments to pass to depending on desired output: 22 | \itemize{ 23 | \item{if "fraction" specify 'id' (see rk_extract() documentation)} 24 | \item{if "integer" specify 'method' and 'seed' (see rk_integerise() 25 | documentation)} 26 | }} 27 | } 28 | \value{ 29 | A data frame with extracted weights (if output == "fraction", the 30 | default) or integerised cases (if output == "integer") 31 | } 32 | \description{ 33 | A convenience function wrapping \code{rk_weight()} and \code{rk_extract()} or 34 | \code{rk_weight()} and \code{rk_integerise()} 35 | } 36 | \examples{ 37 | \dontrun{ 38 | frac_weights <- rake(cons, inds, vars, output = "fraction", 39 | id = "id") 40 | 41 | int_weight <- rake(cons, inds, vars, output = "integer", 42 | method = "trs", seed = "42") 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /man/rk_weight.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/simulation_functions.R 3 | \name{rk_weight} 4 | \alias{rk_weight} 5 | \title{rk_weight} 6 | \usage{ 7 | rk_weight(cons, inds, vars = NULL, iterations = 10) 8 | } 9 | \arguments{ 10 | \item{cons}{A data frame containing all the constraints. This 11 | should be in the format of one row per zone, one column per constraint 12 | category. The first column should be a zone code; all other columns must be 13 | numeric counts.} 14 | 15 | \item{inds}{A data frame containing individual-level (survey) data. This 16 | should be in the format of one row per individual, one column per 17 | constraint. The first column should be an individual ID.} 18 | 19 | \item{vars}{A character vector of variables that constrain the simulation 20 | (i.e. independent variables)} 21 | 22 | \item{iterations}{The number of iterations the algorithm should complete. 23 | Defaults to 10} 24 | } 25 | \value{ 26 | A data frame of fractional weights for each individual in each zone 27 | with zone codes recorded in column names and individual id recorded in row 28 | names. 29 | } 30 | \description{ 31 | Produces fractional weights using the iterative proportional fitting 32 | algorithm. 33 | } 34 | \details{ 35 | rk_weight() requires three arguments: 36 | \itemize{ 37 | \item A data frame of constraints (e.g. census tables) 38 | \item A data frame of individual data (e.g. a survey) 39 | \item A character vector of constraint variable names 40 | } 41 | 42 | The first column of each data frame should be an ID. The first column of 43 | \code{cons} should contain the zone codes. The first column of \code{inds} 44 | should contain the individual unique identifier. 45 | 46 | Both data frames should only contain: 47 | \itemize{ 48 | \item an ID column (zone ID \code{cons} or individual ID \code{inds}). 49 | \item constraints \code{inds} or constraint category \code{cons}. 50 | \item \code{inds} can optionally contain additional dependent variables 51 | that do not influence the weighting process. 52 | } 53 | 54 | No other columns should be present (the user can merge these back in later). 55 | 56 | It is essential that the levels in each \code{inds} constraint (i.e. column) 57 | match exactly with the column names in \code{cons}. In the example below see 58 | how the column names in cons (\code{'age_0_49', 'sex_f', ...}) match exactly 59 | the levels in the appropriate \code{inds} variables. 60 | 61 | The columns in \code{cons} must be arranged in alphabetical order because 62 | these are created alphabetically when they are 'spread' in the 63 | individual-level data. 64 | } 65 | \examples{ 66 | # SimpleWorld 67 | cons <- data.frame( 68 | "zone" = letters[1:3], 69 | "age_0_49" = c(8, 2, 7), 70 | "age_gt_50" = c(4, 8, 4), 71 | "sex_f" = c(6, 6, 8), 72 | "sex_m" = c(6, 4, 3), 73 | stringsAsFactors = FALSE 74 | ) 75 | inds <- data.frame( 76 | "id" = LETTERS[1:5], 77 | "age" = c( 78 | "age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49" 79 | ), 80 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 81 | "income" = c(2868, 2474, 2231, 3152, 2473), 82 | stringsAsFactors = FALSE 83 | ) 84 | # Set variables to constrain over 85 | vars <- c("age", "sex") 86 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 87 | print(weights) 88 | } 89 | -------------------------------------------------------------------------------- /man/weight.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/deprecated_functions.R 3 | \name{weight} 4 | \alias{weight} 5 | \title{weight} 6 | \usage{ 7 | weight(cons, inds, vars = NULL, iterations = 10) 8 | } 9 | \arguments{ 10 | \item{cons}{A data frame containing all the constraints. This 11 | should be in the format of one row per zone, one column per constraint 12 | category. The first column should be a zone code; all other columns must be 13 | numeric counts.} 14 | 15 | \item{inds}{A data frame containing individual-level (survey) data. This 16 | should be in the format of one row per individual, one column per 17 | constraint. The first column should be an individual ID.} 18 | 19 | \item{vars}{A character vector of variables that constrain the simulation 20 | (i.e. independent variables)} 21 | 22 | \item{iterations}{The number of iterations the algorithm should complete. 23 | Defaults to 10} 24 | } 25 | \value{ 26 | A data frame of fractional weights for each individual in each zone 27 | with zone codes recorded in column names and individual id recorded in row 28 | names. 29 | } 30 | \description{ 31 | Deprecated. Use rk_weight() 32 | } 33 | \examples{ 34 | # Deprecated. Use rk_weight() 35 | } 36 | -------------------------------------------------------------------------------- /rakeR.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(rakeR) 3 | 4 | test_check("rakeR") 5 | -------------------------------------------------------------------------------- /tests/testthat/test_deprecated.R: -------------------------------------------------------------------------------- 1 | context("Check deprecated functions") 2 | 3 | 4 | cons <- data.frame( 5 | "zone" = letters[1:3], 6 | "age_0_49" = c(8, 2, 7), 7 | "age_gt_50" = c(4, 8, 4), 8 | "sex_f" = c(6, 6, 8), 9 | "sex_m" = c(6, 4, 3), 10 | stringsAsFactors = FALSE 11 | ) 12 | 13 | inds <- data.frame( 14 | "id" = LETTERS[1:5], 15 | "age" = c("age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49"), 16 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 17 | "income" = c(2868, 2474, 2231, 3152, 2473), 18 | stringsAsFactors = FALSE 19 | ) 20 | 21 | vars <- c("age", "sex") 22 | 23 | weights <- rk_weight(cons, inds, vars) 24 | 25 | 26 | test_that("weight() returns deprecated", { 27 | expect_warning(weight(cons, inds, vars), regexp = "rk_weight") 28 | }) 29 | 30 | test_that("extract() returns deprecated", { 31 | inds$income <- cut( 32 | inds$income, breaks = 2, include.lowest = TRUE, labels = c("low", "high") 33 | ) 34 | expect_warning(extract(weights, inds, id = "id"), regexp = "rk_extract") 35 | }) 36 | 37 | test_that("extract_weights() errors", { 38 | # extract_weights() is hard deprecated 39 | expect_error(extract_weights(), regexp = "rk_extract") 40 | }) 41 | 42 | test_that("integerise() returns deprecated", { 43 | expect_warning(integerise(weights, inds), regexp = "rk_integerise") 44 | }) 45 | 46 | test_that("rake() returns deprecated", { 47 | expect_warning( 48 | rake(cons, inds[, c("id", "age", "sex")], vars = vars, id = "id"), 49 | regexp = "rk_rake" 50 | ) 51 | }) 52 | -------------------------------------------------------------------------------- /tests/testthat/test_rk_extract.R: -------------------------------------------------------------------------------- 1 | library("testthat") 2 | context("Check rk_extract() function") 3 | 4 | cons <- data.frame( 5 | "zone" = letters[1:3], 6 | "age_0_49" = c(8, 2, 7), 7 | "age_gt_50" = c(4, 8, 4), 8 | "sex_f" = c(6, 6, 8), 9 | "sex_m" = c(6, 4, 3), 10 | stringsAsFactors = FALSE 11 | ) 12 | inds <- data.frame( 13 | "id" = LETTERS[1:5], 14 | "age" = c("age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49"), 15 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 16 | "income" = c(2868, 2474, 2231, 3152, 2473), 17 | stringsAsFactors = FALSE 18 | ) 19 | vars <- c("age", "sex") 20 | weights <- rk_weight(cons, inds, vars) 21 | 22 | test_that("Error if attempt to extract() numeric variable", { 23 | expect_error(rk_extract(weights, inds, "id"), "numeric") 24 | }) 25 | 26 | inds[["income"]] <- cut( 27 | inds[["income"]], 28 | breaks = 2, 29 | labels = c("low", "high"), 30 | include.lowest = TRUE 31 | ) 32 | extd_weights <- rk_extract(weights, inds, id = "id") 33 | 34 | test_that("Number of zones is correct", { 35 | expect_equal(nrow(extd_weights), length(cons$zone)) 36 | }) 37 | 38 | test_that("Zone codes of extd_weights match cons zone codes", { 39 | expect_equal(extd_weights$code, cons$zone) 40 | }) 41 | 42 | test_that("Simulated population total matches real population total", { 43 | expect_equal(sum(cons[, 2:3]), sum(extd_weights$total)) 44 | }) 45 | 46 | test_that("Simulated population matches simulated population", { 47 | expect_equal(sum(extd_weights$total), sum(weights)) 48 | }) 49 | 50 | test_that("extracted weights are numeric (except zone code)", { 51 | lapply(extd_weights[, 2:ncol(extd_weights)], function(x) { 52 | expect_is(x, "numeric") 53 | }) 54 | }) 55 | 56 | test_that("No missing values", { 57 | expect_false(any(is.na(extd_weights))) 58 | }) 59 | -------------------------------------------------------------------------------- /tests/testthat/test_rk_integerise.R: -------------------------------------------------------------------------------- 1 | context("Test integerise function") 2 | 3 | cons <- readr::read_csv("../cakemap_cons.csv") 4 | inds <- readr::read_csv("../cakemap_inds.csv") 5 | vars <- c("Car", "NSSEC8", "ageband4") 6 | 7 | # Check zeros are handled correctly by making one observation 0 8 | cons[19, "n_1_1"] <- 0 # lowest count (35) 9 | cons[19, "n_1_2"] <- 283 # add these so populations still match 10 | 11 | weights <- rk_weight(cons = cons, inds = inds, vars = vars) 12 | 13 | test_that("Error if num of observations don't match", { 14 | inds_10 <- inds[1:10, ] 15 | expect_error( 16 | rk_integerise(weights, inds_10), 17 | "Number of observations in weights does not match inds" 18 | ) 19 | }) 20 | 21 | test_that("Error if inds isn't a data frame", { 22 | inds <- unlist(inds) 23 | expect_error(rk_integerise(weights, inds), "inds is not a data frame") 24 | }) 25 | 26 | weights_int <- rk_integerise(weights, inds) 27 | 28 | test_that("Num of cols in weights_int should be num cols of inds + 1", { 29 | expect_equal(ncol(weights_int), (ncol(inds) + 1)) 30 | }) 31 | 32 | test_that("Num zones in weights_int matches num zones in cons", { 33 | expect_equal(nrow(unique(weights_int[, "zone"])), nrow(cons)) 34 | }) 35 | 36 | test_that("Sum of input weights should equal number of integerised cases", { 37 | expect_equal(sum(weights), nrow(weights_int)) 38 | }) 39 | 40 | test_that( 41 | "Number of zones in integerised data set should match that in constraints", { 42 | expect_equal(length(unique(weights_int[["zone"]])), nrow(cons)) 43 | }) 44 | 45 | test_that("integerised weights should add up to cons population", { 46 | expect_equal(nrow(weights_int), (sum(cons[, -1] / length(vars)))) 47 | }) 48 | 49 | test_that("No missing values in integerised output", { 50 | expect_false(any(is.na(weights_int))) 51 | }) 52 | 53 | test_that("method other than 'trs' fails", { 54 | expect_error( 55 | rk_integerise(weights, inds, method = "not_trs"), 56 | "only supports the truncate, replicate" 57 | ) 58 | }) 59 | 60 | test_that("Return integer weights unmodified", { 61 | weights <- floor(weights) 62 | expect_message(rk_integerise(weights, inds), "weights already integers") 63 | }) 64 | -------------------------------------------------------------------------------- /tests/testthat/test_rk_rake.R: -------------------------------------------------------------------------------- 1 | context("Test rk_rake() function produces correct output for extract") 2 | 3 | cons <- readr::read_csv("../cakemap_cons.csv") 4 | inds <- readr::read_csv("../cakemap_inds.csv") 5 | vars <- c("Car", "NSSEC8", "ageband4") 6 | 7 | test_that("Check fraction", { 8 | frac_weights <- rk_rake(cons = cons, inds = inds, vars = vars, 9 | output = "fraction", id = "code") 10 | 11 | lapply(frac_weights[, 3:ncol(frac_weights)], function(x) { 12 | expect_is(x, "numeric") 13 | }) 14 | }) 15 | 16 | 17 | context("Test rk_rake() function produces correct output for integer") 18 | 19 | test_that("Check integer", { 20 | int_weights <- rk_rake(cons = cons, inds = inds, vars = vars, 21 | output = "integer", 22 | seed = 42, method = "trs") 23 | 24 | expect_equal(nrow(int_weights), sum(cons[, c("car_no", "car_yes")])) 25 | }) 26 | -------------------------------------------------------------------------------- /tests/testthat/test_rk_weight.R: -------------------------------------------------------------------------------- 1 | context("Test rk_weight()") 2 | 3 | 4 | cons <- data.frame( 5 | "zone" = letters[1:3], 6 | "age_0_49" = c(8, 2, 7), 7 | "age_gt_50" = c(4, 8, 4), 8 | "sex_f" = c(6, 6, 8), 9 | "sex_m" = c(6, 4, 3), 10 | stringsAsFactors = FALSE 11 | ) 12 | inds <- data.frame( 13 | "id" = LETTERS[1:5], 14 | "age" = c("age_gt_50", "age_gt_50", "age_0_49", "age_gt_50", "age_0_49"), 15 | "sex" = c("sex_m", "sex_m", "sex_m", "sex_f", "sex_f"), 16 | "income" = c(2868, 2474, 2231, 3152, 2473), 17 | stringsAsFactors = FALSE 18 | ) 19 | vars <- c("age", "sex") 20 | weights <- rk_weight(cons, inds, vars) 21 | 22 | 23 | # Test inputs 24 | test_that("Error if cons is not a data frame", { 25 | cons_notdf <- unlist(cons) 26 | expect_error(rk_weight(cons_notdf, inds, vars), "cons is not a data frame") 27 | }) 28 | 29 | test_that("Error if inds is not a data frame", { 30 | inds_notdf <- unlist(inds) 31 | expect_error(rk_weight(cons, inds_notdf, vars), "inds is not a data frame") 32 | }) 33 | 34 | test_that("Error if vars is not a vector", { 35 | vars <- function(x) {} 36 | expect_error(rk_weight(cons, inds, vars = vars), "vars is not a vector") 37 | }) 38 | 39 | test_that("Error if NAs present in cons", { 40 | cons[1, 2] <- NA 41 | expect_error(rk_weight(cons, inds, vars), "NA") 42 | }) 43 | 44 | test_that("Error if NAs present in inds", { 45 | inds[1, 2] <- NA 46 | expect_error(rk_weight(cons, inds, vars), "NA") 47 | }) 48 | 49 | test_that("Error if duplicate cons zone codes", { 50 | cons[2, 1] <- cons[1, 1] 51 | expect_error(rk_weight(cons, inds, vars), "duplicate zone codes") 52 | }) 53 | 54 | test_that("Error if duplicate inds IDs", { 55 | inds[2, 1] <- inds[1, 1] 56 | expect_error(rk_weight(cons, inds, vars), "duplicate ids") 57 | }) 58 | 59 | test_that("Error if any cons zones are 0", { 60 | cons[1, 2:ncol(cons)] <- 0L 61 | expect_error(rk_weight(cons, inds, vars), "contain 0") 62 | }) 63 | 64 | test_that("Error if column names (ind/cons) don't match", { 65 | inds$age[inds$age == "age_0_49"] <- "not_age_0_49" 66 | expect_error(rk_weight(cons, inds, vars), "do not match") 67 | }) 68 | 69 | 70 | # Test outputs 71 | test_that("Ncols should equal number of zones in cons", { 72 | expect_equal(ncol(weights), nrow(cons)) 73 | }) 74 | 75 | test_that("Nrows should equal number of individuals in survey", { 76 | expect_equal(nrow(weights), nrow(inds)) 77 | }) 78 | 79 | test_that("weights matrix is numeric", { 80 | lapply(weights, function(x) { 81 | expect_is(x, "numeric") 82 | }) 83 | }) 84 | 85 | test_that("No missing values", { 86 | expect_false(any(is.na(weights))) 87 | }) 88 | 89 | test_that("Populations match (i.e. sum weights == (sum cons / n vars))", { 90 | expect_equal(sum(weights), (sum(cons[, -1]) / length(vars))) 91 | }) 92 | 93 | test_that("individual IDs stored in rownames of weights", { 94 | expect_equal(rownames(weights), inds[[1]]) 95 | }) 96 | --------------------------------------------------------------------------------