I am playing around with an agent-based model in R. I am trying to wrap my toy model in a function to allow me to easily run multiple models. Unfortunately, I cannot get my results out via a return function. I must be doing something silly. My toy model code looks like this:
#--------- start script ---------#
require(tidyverse)
agent_generator <- function(pop_size, init_exposed){
# create population of susceptible agents
agents <- data.frame(agent_no = 1:pop_size,
state = "s",
mixing = runif(pop_size, 0, 1),
stringsAsFactors = FALSE)
agents$state[1:init_exposed] <- "e"
return(agents)
}
simple_model <- function(agents, mix, steps){
pop_size <- nrow(agents)
# define data outputs
edge_list <- data.frame()
node_list <- data.frame()
for(k in 1:steps){
for(i in 1:pop_size){
# likelihood of an agent to go out and meet others
connect_with <- round(likelihood * mix, 0) + 1
# which agents will they probably meet (list of agents)
meet_ups <- sample(1:pop_size,
connect_with,
replace = T,
prob = agents$mixing)
# create edge list
df <- data.frame(agent = meet_ups)
edges <- expand(df, from = agent, to = agent) %>%
filter(from < to) %>%
mutate(day = k) %>%
arrange(day)
edge_list <- bind_rows(edge_list, edges)
for(j in 1:length(meet_ups)){
contacts <- agents[meet_ups[j],]
# if exposed, change state
if(contacts$state == "e"){
urand <- runif(1,0,1)
# control probability of state change
if(urand < 0.5){
agents$state[i] <- "e"
}
}
# create node list
nodes <- agents %>%
select(agent_no, state) %>%
mutate(day = k + 1) %>%
arrange(day)
node_list <- bind_rows(node_list, nodes)
}
}
}
return(edge_list)
return(node_list)
}
agents <- agent_generator(20,5)
model_1 <- simple_model(agents, mix = 10, steps = 15)
#--------- end script ---------#
When I run the code outside a function, I get node_list and edge_list. My return function is not working. Can someone kindly show me the errors of my ways?