{"id":13858249,"url":"https://github.com/philmikejones/rakeR","last_synced_at":"2025-07-13T23:31:44.853Z","repository":{"id":56937439,"uuid":"67427361","full_name":"philmikejones/rakeR","owner":"philmikejones","description":"Tools for easy spatial microsimulation (raking) in R","archived":false,"fork":false,"pushed_at":"2019-03-26T21:48:49.000Z","size":514,"stargazers_count":11,"open_issues_count":11,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-06T08:48:58.115Z","etag":null,"topics":["cran","ipf","raker","raking","spatial-microsimulation"],"latest_commit_sha":null,"homepage":"http://philmikejones.github.io/rakeR/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philmikejones.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-05T14:24:58.000Z","updated_at":"2022-08-16T20:46:23.000Z","dependencies_parsed_at":"2022-08-21T06:50:11.042Z","dependency_job_id":null,"html_url":"https://github.com/philmikejones/rakeR","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/philmikejones/rakeR","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philmikejones%2FrakeR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philmikejones%2FrakeR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philmikejones%2FrakeR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philmikejones%2FrakeR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philmikejones","download_url":"https://codeload.github.com/philmikejones/rakeR/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philmikejones%2FrakeR/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265220512,"owners_count":23729833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cran","ipf","raker","raking","spatial-microsimulation"],"created_at":"2024-08-05T03:02:01.798Z","updated_at":"2025-07-13T23:31:39.843Z","avatar_url":"https://github.com/philmikejones.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"README-\"\n)\n```\n\n\n# rakeR\n\n[![Build Status](https://travis-ci.org/philmikejones/rakeR.svg?branch=master)](https://travis-ci.org/philmikejones/rakeR)\n[![codecov](https://codecov.io/gh/philmikejones/rakeR/branch/master/graph/badge.svg)](https://codecov.io/gh/philmikejones/rakeR)\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rakeR)](https://cran.r-project.org/package=rakeR)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.821506.svg)](https://doi.org/10.5281/zenodo.821506)\n[![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)\n\n\nCreate a spatial microsimulated data set in R using iterative proportional\nfitting ('raking').\n\n\n## Install\n\nInstall the latest stable version from CRAN:\n\n```{r install-cran, eval=FALSE, include=TRUE}\ninstall.packages(\"rakeR\")\n```\n\nOr install the development version with `devtools`:\n\n```{r install-rakeR, eval=FALSE, include=TRUE}\n# Obtain devtools if you don't already have it installed\n# install.packages(\"devtools\")\n\n# Install rakeR development version from GitHub\ndevtools::install_github(\"philmikejones/rakeR\")\n```\n\nLoad the package with:\n\n```{r load-rakeR}\nlibrary(\"rakeR\")\n```\n\n\n## Overview\n\n`rakeR` has three main functions.\nThe first stage is always to use `rk_weight()` to produce a matrix of fractional weights.\nThis matrix stores weights for each individual in each zone.\n\nFrom this weight matrix, `rakeR` has functions to create fractional weights (`rk_extract()`) or integerised cases (`rk_integerise()`), depending on your needs and use cases.\nFractional (`rk_extract()`ed) weights are generally more accurate, while integer cases are probably the most intuitive to use and are useful as inputs\nfor further modeling.\n\nTo create fractional weights use `rk_weight()` then `rk_extract()`, and to produce integerised weights use `rk_weight()` then `rk_integerise()`.\n\n\n## Inputs\n\nTo perform the weighting you should supply two data frames.\nOne data frame should contain the constraint information (`cons`) with counts per category for each zone (e.g. census counts).\nThe other data frame should contain the individual--level data (`inds`), i.e. one row per individual.\n\nIn addition, it is necessary to supply a character vector with the names of the constraint variables in `inds` (`vars`).\nThis is so that `rakeR` knows which are the contraint variables and which variables it should be simulating as an output.\n\nBelow are examples of `cons`, `inds`, and `vars`.\n\n```{r data}\ncons \u003c- data.frame(\n  \"zone\"      = letters[1:3],\n  \"age_0_49\"  = c(8, 2, 7),\n  \"age_gt_50\" = c(4, 8, 4),\n  \"sex_f\"     = c(6, 6, 8),\n  \"sex_m\"     = c(6, 4, 3),\n  stringsAsFactors = FALSE\n)\n\ninds \u003c- data.frame(\n  \"id\"     = LETTERS[1:5],\n  \"age\"    = c(\"age_gt_50\", \"age_gt_50\", \"age_0_49\", \"age_gt_50\", \"age_0_49\"),\n  \"sex\"    = c(\"sex_m\", \"sex_m\", \"sex_m\", \"sex_f\", \"sex_f\"),\n  \"income\" = c(2868, 2474, 2231, 3152, 2473)\n)\n\nvars \u003c- c(\"age\", \"sex\")\n```\n\nIt is _essential_ that the unique levels in the constraint variables in the\n`inds` data set match the variables names in the `cons` data set.\nFor 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:\n\n```{r labels-test}\nall.equal(\n  levels(inds$age), colnames(cons[, 2:3])  # cons[, 1] is the id column\n)\n```\n\nWithout this, the functions do not know how to match the `inds` and `cons` data\nand will fail so as not to return spurious results.\n\n\n## `rk_weight()`\n\n(Re-)weighting is done with `rk_weight()` which returns a data frame of raw weights.\n\n```{r weight}\nweights \u003c- rk_weight(cons = cons, inds = inds, vars = vars)\nweights\n```\n\nThe raw weights tell you how frequently each individual (`A`-`E`) should appear\nin each zone (`a`-`c`).\nThe raw weights are useful when validating and checking performance of the\nmodel, so it can be necessary to save these separately.\nThey aren't very useful for analysis however, so we can `rk_extract()` or `rk_integerise()` them into a useable form.\n\n\n## `rk_extract()`\n\n`rk_extract()` produces aggregated totals of the simulated data for each category in each zone.\n`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.\nBecause `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):\n\n```{r extract}\ninds$income \u003c- cut(inds$income, breaks = 2, include.lowest = TRUE,\n                   labels = c(\"low\", \"high\"))\n\next_weights \u003c- rk_extract(weights, inds = inds, id = \"id\")\next_weights\n```\n\n`rk_extract()` returns one row per zone, and the total of each category (for\nexample female and male, or high and low income) will match the known\npopulation.\n\n\n## `rk_integerise()`\n\nThe `rk_integerise()` function produces a simulated data frame populated with simulated individuals.\nThis is typically useful when:\n\n* You need to include numerical variables, such as income in the example.\n* You want individual cases to use as input to a dynamic or agent-based model.\n* You want 'case studies' to illustrate characteristics of individuals in an\narea.\n* Individual-level data is more intuitive to work with.\n\n```{r integerise}\nint_weights \u003c- rk_integerise(weights, inds = inds)\nint_weights[1:6, ]\n```\n\n`rk_integerise()` returns one row per case, and the number of rows will match\nthe known population (taken from `cons`).\n\n\n## `rk_rake()`\n\n`rk_rake()` is a wrapper for `rk_weight() %\u003e% rk_extract()` or\n`rk_weight() %\u003e% rk_integerise()`.\nThis is useful if the raw weights (from `rk_weight()`) are not required.\nThe desired output is specified with the `output` argument, which can be\nspecified with `\"fraction\"` (the default) or `\"integer\"`.\nThe function takes the following arguments in all cases:\n\n* `cons`\n* `inds`\n* `vars`\n* `output` (default `\"fraction\"`)\n* `iterations` (default 10)\n\nAdditional arguments are required depending on the output requested.\nFor `output = \"fraction\"`:\n\n* `id`\n\nFor `output = \"integer\"`:\n\n* `method` (default `\"trs\"`)\n* `seed` (default 42)\n\nDetails of these context-specific arguments can be found in the\nrespective documentation for `rk_integerise()` or `rk_extract()`.\n\n```{r rake-int-exaple}\nrake_int \u003c- rk_rake(cons, inds, vars, output = \"integer\",\n                 method = \"trs\", seed = 42)\nrake_int[1:6, ]\n```\n\n```{r rake-frac-example}\nrake_frac \u003c- rk_rake(cons, inds, vars, output = \"fraction\", id = \"id\")\nrake_frac\n```\n\n\n## Contributing\n\nPlease note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).\nBy participating in this project you agree to abide by its terms.\n\n\n## Issues and feedback\n\nFeedback on the API,\n[bug reports/issues](https://github.com/philmikejones/rakeR/issues),\nand pull requests are very welcome.\n\n\n## Acknowledgements\n\nMany of the functions in this package are based on code written by\n[Robin Lovelace](https://github.com/Robinlovelace) and\n[Morgane Dumont](https://github.com/modumont) for their book\n[*Spatial Microsimulation with R* (2016), Chapman and Hall/CRC Press](https://www.crcpress.com/Spatial-Microsimulation-with-R/Lovelace-Dumont/p/book/9781498711548).\n\nTheir book is an excellent resource for learning\nabout spatial microsimulation and understanding what's going on under the hood\nof this package.\n\nThe book and code are licensed under the terms below:\n\nCopyright (c) 2014 Robin Lovelace\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n\nThe [rewighting (ipfp) algorithm](https://github.com/awblocker/ipfp) is\nwritten by Andrew Blocker.\n\nThe [`wrswoR` package](http://krlmlr.github.io/wrswoR/) used for fast sampling\nwithout replacement is written by Kirill Müller.\n\n\nThanks to [Tom Broomhead](http://mhs.group.shef.ac.uk/members/tom-broomhead/)\nfor 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.\n\n\nData 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/).\n\n\n## Contact\n\nphilmikejones at gmail dot com\n\n\n## License\n\nCopyright 2016-18 Phil Mike Jones.\n\nrakeR is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nrakeR is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with rakeR. If not, see \u003chttp://www.gnu.org/licenses/\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilmikejones%2FrakeR","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilmikejones%2FrakeR","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilmikejones%2FrakeR/lists"}