Closed asiripanich closed 4 years ago
Can the collections package can be used to efficiently manage the microsimulation queue during search and matching than vector()
?
Should this be a function instead of a class? I think it would be less ambiguous to the users if this is a function.
A snippet of code.
What I'm trying to do here is to convert MatchingMarket from a R6Class to a function but first I need to figure out how to include a scoring function (to calculate the matching scores for each agent to the agents in its choice-set) that is both performant and easy to understand.
n_A <- 100
n_B <- 100
agent_A <-
data.table(
id = 1:n_A,
age = round(runif(n_A) * 30 + 20),
attitude = runif(n_A) * 10
)
agent_A
agent_B <-
data.table(
id = 1:n_B,
age = round(runif(n_B) * 30 + 20),
attitude = runif(n_A) * 10
)
agent_B
crossjoin <-
CJ(id.a = agent_A$id, id.b = agent_B$id) %>%
merge(., agent_A, by.x = "id.a", by.y = "id") %>%
merge(., agent_B, by.x = "id.b", by.y = "id", suffixes = c(".a", '.b'))
crossjoin
score <- function(data) {
with(data, abs((age.a - age.b)) + abs((attitude.a - attitude.b)^2) + 5)
}
score(crossjoin)
mymatch <- function(data_a, data_b, score_a, score_b) {
# create a choiceset
# ...
# check the scoring function
# join data of the agent of its choiceset
crossjoin <-
CJ(id.a = agent_A$id, id.b = agent_B$id) %>%
merge(., agent_A, by.x = "id.a", by.y = "id") %>%
merge(., agent_B, by.x = "id.b", by.y = "id", suffixes = c(".a", '.b'))
#
}
# check for variables in the scoring function.
# knowing what variables are required in the scoring function can reduce the size of the merged data
strsplit(paste(deparse(score), collapse = ";"), "\\(|\\)|\\;|\\{|\\}|\\s|\\-|\\+|\\,") %>%
unlist() %>%
.[. != ""] %>%
grep(pattern = "\\w+(?:\\.+\\w+)+", x = ., value = T)
see #85.
Market class should have search and matching strategies that can be swapped. This is needed to micro-simulate a labour market, housing market, etc.
Constructor..
Matching strategies
Search strategies
Components
Resources