rdaqapo

Extract tibble_df and text message from activitylog object


I have the code below

library(bupar)
library(daqapo)
hospital<-hospital
hospital %>%
  rename(start = start_ts,
         complete = complete_ts) -> hospital
hospital %>%
  convert_timestamps(c("start","complete"), format = dmy_hms) -> hospital
hospital %>%
  activitylog(case_id = "patient_visit_nr",
              activity_id = "activity",
              resource_id = "originator",
              timestamps = c("start", "complete")) -> hospital
hospital %>%
  detect_time_anomalies()

which gives

*** OUTPUT ***
For 5 rows in the activity log (9.43%), an anomaly is detected.
The anomalies are spread over the activities as follows:
# A tibble: 3 × 3
  activity      type                  n
  <chr>         <chr>             <int>
1 Registration  negative duration     3
2 Clinical exam zero duration         1
3 Trage         negative duration     1
Anomalies are found in the following rows:
# Log of 10 events consisting of:
3 traces 
3 cases 
5 instances of 3 activities 
5 resources 
Events occurred from 2017-11-21 11:22:16 until 2017-11-21 19:00:00 
 
# Variables were mapped as follows:
Case identifier:        patient_visit_nr 
Activity identifier:        activity 
Resource identifier:        originator 
Timestamps:     start, complete 

# A tibble: 5 × 10
  patient_visit_nr activity      originator start               complete            triagecode specialization .order durat…¹ type 
             <dbl> <chr>         <chr>      <dttm>              <dttm>                   <dbl> <chr>           <int>   <dbl> <chr>
1              518 Registration  Clerk 12   2017-11-21 11:45:16 2017-11-21 11:22:16          4 PED                 1  -23    nega…
2              518 Registration  Clerk 6    2017-11-21 11:45:16 2017-11-21 11:22:16          4 PED                 2  -23    nega…
3              518 Registration  Clerk 9    2017-11-21 11:45:16 2017-11-21 11:22:16          4 PED                 3  -23    nega…
4              520 Trage         Nurse 17   2017-11-21 13:43:16 2017-11-21 13:39:00          5 URG                 4   -4.27 nega…
5              528 Clinical exam Doctor 1   2017-11-21 19:00:00 2017-11-21 19:00:00          3 TRAU                5    0    zero…
# … with abbreviated variable name ¹​duration

from this output I would like to extract the text message

For 5 rows in the activity log (9.43%), an anomaly is detected.
The anomalies are spread over the activities as follows:

and also in another object the tibble

activity      type                  n
  <chr>         <chr>             <int>
1 Registration  negative duration     3
2 Clinical exam zero duration         1
3 Trage         negative duration     1

Solution

  • The string you are trying to obtain is a message, and though it is possible to capture a message, it's not that straightforward. You can easily generate it by emulating a couple of lines within the function though.

    If you store the result of detect_time_anomalies:

    anomalies <- hospital %>% detect_time_anomalies() 
    

    Then you can generate the message like this:

    paste0("For ", nrow(anomalies), " rows in the activity log (", 
           round(nrow(anomalies)/nrow(hospital) * 100, 2), 
           "%), an anomaly is detected.")
    #> [1] "For 5 rows in the activity log (9.43%), an anomaly is detected."
    

    Similarly, you can obtain the output table like this:

    anomalies %>%
      group_by(activity, type) %>% 
      summarize(n = n()) %>% 
      arrange(desc(n))
    #> # A tibble: 3 x 3
    #>   activity      type                  n
    #>   <chr>         <chr>             <int>
    #> 1 Registration  negative duration     3
    #> 2 Clinical exam zero duration         1
    #> 3 Trage         negative duration     1
    

    Created on 2022-12-13 with reprex v2.0.2