agents_to_aggregate.data.frame: generalized function to convert raw agent based data to...

Description Usage Arguments Details Value Examples

View source: R/agent-to-aggregate.R

Description

This function converts data on an agent-based level (1 row = 1 agent) relative when an agent is in each state and aggregates it, so that the user can know how many agents are in each state at a given time point (integer based).

Usage

1
2
3
4
5
6
7
8
9
## S3 method for class 'data.frame'
agents_to_aggregate(
  agents,
  states,
  death = NULL,
  birth = NULL,
  min_max_time = c(0, NA),
  integer_time_expansion = TRUE
)

Arguments

agents

data frame with individual agent information

states

Name-variable pairs of the form states = c(col1, col2), that describe which columns contain the time one entered the state. Do not include column for original state. These need to be ordered, for example: for an SIR model, with columns "tI" and "tR" expressing the time the individual became infected and recovered (respectively), we want "states = c(tI, tR)".

death

string for column with death time information (default NULL)

birth

string for column with birth time information (default NULL)

min_max_time

vector (length 2) of minimum and maximum integer time, the second value can be NA - and if so, we estimate maximum time from the data.

integer_time_expansion

boolean if every integer time point in the range of min_max_time should be presented in the aggregation output. If FALSE (default is TRUE), then lines will only include those time points where

Details

note that all parameters related to name columns can also be in a string format. More details can be found in agents_to_aggregate's documentation.

Value

dataset with aggregated information, We label classes "X{i}" for i in 0:(length(states)).

Examples

 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
library(dplyr)
agents <- EpiCompare::hagelloch_raw
# making babies
set.seed(5)
babies <- sample(nrow(agents),size = 5)
agents$tBIRTH <- NA
agents$tBIRTH[babies] <- agents$tI[babies] - 5

aggregate_b <- agents_to_aggregate(agents, states = c(tI, tR),
                                   death = NULL, birth = tBIRTH)

# looking at when babies where born:
agents %>% dplyr::filter(!is.na(.data$tBIRTH)) %>%
  dplyr::pull(.data$tBIRTH) %>% ceiling() %>% sort
# vs:
data.frame(counts = 1:nrow(aggregate_b),
           num_people = aggregate_b %>% select(-t) %>% apply(1, sum))


# including death
aggregate_d <- agents_to_aggregate(agents, states = c(tI, tR),
                                   death = tDEAD, birth = NULL)

# looking at when people died:
agents %>% dplyr::filter(!is.na(.data$tDEAD)) %>%
  dplyr::pull(.data$tDEAD) %>% ceiling() %>% sort
# vs:
data.frame(counts = 1:nrow(aggregate_d),
           num_people = aggregate_d %>% select(-t) %>% apply(1, sum))

skgallagher/EpiCompare documentation built on Sept. 14, 2021, 5:45 a.m.