Commit b2dd5732 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

Use functions.

parent 988d8c75
...@@ -38,6 +38,34 @@ knitr::opts_chunk$set( ...@@ -38,6 +38,34 @@ knitr::opts_chunk$set(
theme_set(theme_ipsum(grid = "Y")) theme_set(theme_ipsum(grid = "Y"))
``` ```
```{r functions}
#' Clean up raw data
cleanup <- function(dat, ...) {
ans <- dat |>
select(Idmenage, zone = ru_urb, Nbh, Ndog) |>
mutate(dh_ratio = Ndog/Nbh)
return(ans)
}
simulate_household_data <- function(N, b0, b1) {
zone = sample(c("Urban", "Rural"), size = N, replace = TRUE)
log_lambda = b0 + b1 * (zone == "Urban")
x = 1 + rpois(N, lambda = 3)
y = rpois(N, lambda = x * exp(log_lambda))
data.frame(
id = seq.int(N),
zone = zone,
x = x,
y = y,
r = y/x
)
}
```
```{r parameters} ```{r parameters}
sim_pars <- tribble( sim_pars <- tribble(
...@@ -54,24 +82,12 @@ data_file <- here::here("data/HumanDogRatioData.xlsx") ...@@ -54,24 +82,12 @@ data_file <- here::here("data/HumanDogRatioData.xlsx")
raw_data <- read_excel(data_file) raw_data <- read_excel(data_file)
zone <- sample(c("Urban", "Rural"), size = with(sim_pars, value[param == "N"]), replace = TRUE) sim_data <- do.call(
log_lambda <- with(sim_pars, value[param == "b0"]) + with(sim_pars, value[param == "b1"]) * (zone == "Urban") simulate_household_data,
as.list(pull(sim_pars, value, name = param))
x <- 1 + rpois(with(sim_pars, value[param == "N"]), lambda = 3)
y <- rpois(with(sim_pars, value[param == "N"]), lambda = x * exp(log_lambda))
sim_data <- data.frame(
id = seq.int(with(sim_pars, value[param == "N"])),
zone = zone,
x = x,
y = y,
r = y/x
) )
clean_data <- raw_data |> clean_data <- cleanup(raw_data)
select(Idmenage, zone = ru_urb, Nbh, Ndog) |>
mutate(dh_ratio = Ndog/Nbh)
``` ```
# Introduction # Introduction
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment