R/agent-to-aggregate.R
check_ordered.Rd
This function assesses whether agents's time they enter each state is
correctly ordered. The assumption is that these would be ordered in increasing
values, with the allowance of NA
values not effecting this decision.
check_ordered(df, states, assert_error = TRUE)
df | data frame with individual agent information (n x p) |
---|---|
states | time entered state, in expected order |
assert_error | boolean if we should raise error if ordering assumption is violated. |
This function returns either
TRUE, if the states are ordered,
an error is observed (when assert_error = TRUE), or
a list of 2 data frames. The first, ordering_df
, an (n x 3)
data.frame, contains information per agent on if they violated the
assumption ("error" column), and the ordering of their states ("ordering"
column). The second, summary_df
(k x 3) data frame contains
information on the number of unique ordering ("ordering"), if they caused
the assumption to be violated ("error") and the number of agents that had
the ordering ("count"). Note that if an individual has NA
for every
states' time, then they have a NA
in their "error" column (this
is not the cause of the erroring).
If this important assumption is violated this function either raises an error
or provides the user with information on what when wrong - to allow the user
to how to corect the error (see assert_error
to change between these
states).
df_not_ordered <- data.frame(group1 = 1:5, group2 = c(2:5,1)) output <- check_ordered(df_not_ordered, c("group1", "group2"), assert_error = FALSE) output#> $ordering_df #> id error ordering #> 1 1 FALSE group1 <= group2 #> 2 2 FALSE group1 <= group2 #> 3 3 FALSE group1 <= group2 #> 4 4 FALSE group1 <= group2 #> 5 5 TRUE group2 <= group1 #> #> $summary_df #> # A tibble: 2 x 3 #> # Groups: ordering [2] #> ordering error count #> <chr> <lgl> <int> #> 1 group1 <= group2 FALSE 4 #> 2 group2 <= group1 TRUE 1 #>