agents_to_aggregate.grouped_df: 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). This function takes grouped_df data.frames (from dplyr) and aggregates within grouping parameters and also provides the columns associated with the grouping.

Usage

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

Arguments

agents

grouped data.frame with individual agent information

states

Name-variable pairs of the form states = c(col1, col2, col3), 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

grouped dataset with aggregated information per group, 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
library(dplyr)

max_time <- 100
agents_g <- hagelloch_raw %>%
  filter(SEX %in% c("female", "male")) %>% group_by(SEX)
sir_group <- agents_to_aggregate(agents_g, states = c(tI, tR),
                                 min_max_time = c(0, max_time))
agents <- agents_g %>%
  filter(SEX == "female") %>% ungroup()
sir_group1 <- agents_to_aggregate(agents, states = c(tI, tR),
                                 min_max_time = c(0, max_time))
sir_group_1 <- sir_group %>% filter(SEX == "female")
assertthat::are_equal(sir_group1,
                      sir_group_1 %>% ungroup %>% select(t, X0, X1, X2))

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