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

Separate functions file.

Unit tests, function documentation.
parent b2dd5732
...@@ -39,32 +39,7 @@ theme_set(theme_ipsum(grid = "Y")) ...@@ -39,32 +39,7 @@ theme_set(theme_ipsum(grid = "Y"))
``` ```
```{r functions} ```{r functions}
#' Clean up raw data source("src/functions.R")
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}
......
#' Clean up raw data
cleanup <- function(dat, ...) {
ans <- dat |>
select(Idmenage, zone = ru_urb, Nbh, Ndog) |>
mutate(dh_ratio = Ndog/Nbh)
return(ans)
}
#' Simulate househod data
#'
#' Data.frame with househols information on the zone (rural/urban), number
#' of humans (at least 1), number of dogs (non-negative integer) and the
#' ratio dog/human.
#'
#' @param N Integer. Number of observations to simulate.
#' @param b0 Numeric. Log-ratio dogs/human in urban areas.
#' @param b1 Numeric. Difference of log-ratios between rural and urban areas.
#'
#' @return
#' @export
#'
#' @examples
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
)
}
\ No newline at end of file
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