Description Usage Arguments Value Note Examples
View source: R/transition-fnc.R
The transition
function can be used to evaluate a model againts the attribute
data of an Entity object stored inside the input World object and update
an attribute of that Entity using random draws from the prediction result.
It allows simple state transitions to be simulated directly inside a
microsimulation pipeline, as it always returns the input World
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
world |
a World object |
entity |
a character indicating the entity class to apply the transition to. |
model |
a Model object or an object in |
target |
a Target object or a named list or |
targeted_ids |
a integer vector containing ids of entities in |
preprocessing_fn |
a function that accepts one argument or |
attr |
a character denoting which of the attribute if |
values |
named |
verbose |
|
transition returns the first argument which is the World object, while get_transition a data.table objec that contains the transition outcomes with two columns: id and response.
In general, dymiumCore detects variables in entity data of an Entity object that
has a dot prefix as derived variables. Meaning, those derived variables are not
check against new entity data that are getting added to the Entity object. But
when entity data are used in get_transition
, the dot prefix of the derived variables
will be removed. This is to make it convenient when naming variables during
the model estimation step.
To create a pre-processing function you can use dplyr
or data.table
. You can
even combine multiple functions with magrittr::%>%
. As an example, if you only
want to filter just the male population then you can choose one of the
following options to create your preprocessing function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # as a function using base R
filter_male <- function(.data) {
.data[.data$sex == "male"]
}
# dplyr's way
filter_male <- function(.data) {
dplyr::filter(.data, sex == "male")
}
# data.table's way
filter_male <- function(.data) {
.data[sex == "male", ]
}
# magrittr's way + dplyr's way
filter_male <-
. %>%
dplyr::filter(., sex == "male")
# magrittr's way + data.table's way + additional conditions
filter_male <-
. %>%
.[sex == "male", ] %>%
.[age >= 50, ]
|
New variables can also be added to the entity data to be used in predicting
their transition probability. Again we can use dplyr
's or data.table
's way.
The examples below show how you can add a 5-year age group variable, called age5
,
to the entity data.
1 2 3 4 5 6 7 8 9 10 11 | # magrittr's way + data.table's way
filter_male <-
. %>%
.[sex == "male" & age >= 50, ] %>%
.[, age5 := cut(age, breaks = c(seq(0,80,5), Inf), include.lowest = TRUE, right = FALSE)]
# magrittr's way + dplyr's way
filter_male <-
. %>%
dplyr::filter(., sex == "male" & age >= 50) %>%
dplyr::mutate(., age5 = cut(age, breaks = c(seq(0,80,5), Inf), include.lowest = TRUE, right = FALSE))
|
Note that, new variables added inside preprocessing_fn won't
change the attribute data of the entity
object that is undergoing a transition.
These variables only appear temporary within the context of the transition.
1 2 3 4 5 6 7 8 9 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 45 46 47 48 49 50 51 52 53 54 55 | # create a filter function
library(caret)
filter_male <-
. %>%
.[sex == "male", ]
filter_not_dead <-
. %>%
.[age != -1]
# create a multinomial logit model using `caret`
mnl <- caret::train(marital_status ~ age + sex,
data = toy_individuals,
method = "multinom",
trace = FALSE
)
# this model denotes that there is a 10% chance that an individual will decease
death_model <- list(yes = 0.1, no = 0.9)
# create a toy world
create_toy_world()
# simulate marital status transition and update the attribute.
transition(world,
entity = "Individual",
model = mnl,
preprocessing_fn = filter_male,
attr = "marital_status"
)
# get a transition result
get_transition(world,
entity = "Individual",
model = mnl,
preprocessing_fn = filter_male
)
# lets make a pipeline of transitions
world %>%
transition(
entity = "Individual",
model = mnl,
preprocessing_fn = . %>% filter_male() %>% filter_not_dead(),
attr = "marital_status"
) %>%
transition(
entity = "Individual",
model = death_model,
preprocessing_fn = filter_not_dead,
attr = "age",
values = c(yes = -1L)
)
# print the attributes of the individual agents
world$entities$Individual$get_data()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.