Skip to content

Commit

Permalink
Added minimising distance option for segregation model choice
Browse files Browse the repository at this point in the history
  • Loading branch information
hollway authored and hollway committed Nov 15, 2023
1 parent 9567d04 commit 1f39ad5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
18 changes: 11 additions & 7 deletions R/model_play.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' By default 0, which means there is no recovery (i.e. an SI model).
#' Anything higher results in an SIR model.
#' @param latency The inverse probability those who have been exposed
#' become infectious (infected), \eqn{\sigma}.
#' become infectious (infected), \eqn{\sigma} or \eqn{\kappa}.
#' For example, if exposed individuals take, on average,
#' four days to become infectious, then \eqn{\sigma = 0.75} (1/1-0.75 = 1/0.25 = 4).
#' By default 0, which means those exposed become immediately infectious (i.e. an SI model).
Expand Down Expand Up @@ -238,23 +238,24 @@ play_learning <- function(.data,
#' @param choice_function One of the following options:
#' "satisficing" (the default) will move the node to any coordinates that satisfy
#' their heterophily threshold,
#' whereas "optimising" will move the node to coordinates that are most homophilous.
#' "optimising" will move the node to coordinates that are most homophilous,
#' and "minimising" distance will move the node to the next nearest unoccupied coordinates.
#' @examples
#' startValues <- rbinom(100,1,prob = 0.5)
#' startValues[sample(seq_len(100), round(100*0.2))] <- NA
#' latticeEg <- manynet::create_lattice(100)
#' latticeEg <- manynet::add_node_attribute(latticeEg, "startValues", startValues)
#' latticeEg
#' play_segregation(latticeEg, "startValues", 0.5)
#' #manynet::autographr(latticeEg, node_color = "startValues", node_size = 5)
#' #manynet::autographr(play_segregation(latticeEg, "startValues", 0.5),
#' # node_color = "startValues", node_size = 5)
#' # manynet::autographr(latticeEg, node_color = "startValues", node_size = 5) +
#' # manynet::autographr(play_segregation(latticeEg, "startValues", 0.2),
#' # node_color = "startValues", node_size = 5)
#' @export
play_segregation <- function(.data,
attribute,
heterophily = 0,
who_moves = c("ordered","random","most_dissatisfied"),
choice_function = c("satisficing","optimising"),
choice_function = c("satisficing","optimising", "minimising"),
steps){
n <- manynet::network_nodes(.data)
if(missing(steps)) steps <- n
Expand Down Expand Up @@ -290,7 +291,10 @@ play_segregation <- function(.data,
if(length(options)==0) next
move_to <- switch(choice_function,
satisficing = unoccupied[which(options <= heterophily)[1]],
optimising = unoccupied[which(options == min(options))[1]])
optimising = unoccupied[which.min(options)[1]],

Check notice on line 294 in R/model_play.R

View check run for this annotation

codefactor.io / CodeFactor

R/model_play.R#L294

Function "options" is undesirable. As an alternative, use withr::with_options() for a temporary change instead of permanently modifying the session options. (undesirable_function_linter)
minimising = unoccupied[which.min(igraph::distances(temp,
igraph::V(temp)[dissatisfied],
igraph::V(temp)[unoccupied]))])
if(is.na(move_to)) next
print(paste("Moving node", dissatisfied, "to node", move_to))
temp <- manynet::add_node_attribute(temp, attribute,
Expand Down
13 changes: 7 additions & 6 deletions man/play.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f39ad5

Please sign in to comment.