修改daqapo::detect_value_range_violations以生成不同结果

cgvd09ve  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(113)

我有下面的activitylog对象:

hospital<-structure(list(patient_visit_nr = c(510, 512, 510, 512, 512, 
                                              510, 517, 518, 518, 518, 519, 519, 520, 519, 521, 521, 519, 522, 
                                              522, 523, 524, 525, 526, 523, 527, 527, 527, 528, 528, 523), 
                         activity = c("registration", "Registration", "Triage", "Triage", 
                                      "Clinical exam", "Clinical exam", "Triage", "Registration", 
                                      "Registration", "Registration", "Registration", "Triage", 
                                      "Trage", "Clinical exam", "Triage", "Registration", "Treatment", 
                                      "Registration", "Triaga", "Registration", "Triage", "Triage", 
                                      "Triage", "Triage", "Registration", "Triage", "Clinical exam", 
                                      "Registration", "Triage", "Clinical exam"), originator = c("Clerk 9", 
                                                                                                 "Clerk 12", "Nurse 27", "Nurse 27", "Doctor 7", "Doctor 7", 
                                                                                                 "Nurse 17", "Clerk 12", "Clerk 6", "Clerk 9", "Clerk 12", 
                                                                                                 "Nurse 17", "Nurse 17", "Doctor 4", "Nurse 17", "Clerk 3", 
                                                                                                 "Nurse 17", "Clerk 3", "Nurse 5", "Clerk 9", "Nurse 5", "Nurse 5", 
                                                                                                 "Nurse 5", "Nurse 27", "Clerk 6", "Nurse 5", "Doctor 4", 
                                                                                                 "Nurse 6", "Nurse 6", "Doctor 4"), start = structure(c(1511173097, 
                                                                                                                                                        1511173994, 1511174048, 1511174652, 1511177232, 1511177701, 
                                                                                                                                                        1511264116, 1511264716, 1511264716, 1511264716, 1511265581, 
                                                                                                                                                        1511270783, 1511271796, 1511272354, 1511272924, 1511273120, 
                                                                                                                                                        1511275132, 1511277137, 1511277325, 1511282528, 1511283843, 
                                                                                                                                                        1511283853, 1511283855, 1511285190, 1511287330, 1511287331, 
                                                                                                                                                        1511287333, 1511287817, 1511288105, 1511288209), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                  "POSIXt")), complete = structure(c(1511173206, 1511174220, 
                                                                                                                                                                                                                                                                     1511174508, 1511175017, 1511177637, 1511177769, 1511264340, 
                                                                                                                                                                                                                                                                     1511263336, 1511263336, 1511263336, 1511265952, 1511271138, 
                                                                                                                                                                                                                                                                     1511271540, 1511272954, 1511273169, 1511290800, 1511275761, 
                                                                                                                                                                                                                                                                     1511277499, 1511277484, 1511283119, 1511283965, 1511283968, 
                                                                                                                                                                                                                                                                     1511283970, 1511287261, 1511287447, 1511287448, 1511287450, 
                                                                                                                                                                                                                                                                     1511288104, 1511288412, 1511288528), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                                                                                                                   "POSIXt")), triagecode = c(3, 3, 3, 3, 3, NA, 3, 4, 4, 4, 
                                                                                                                                                                                                                                                                                                                                                              4, 4, 5, 4, 2, 2, 4, 2, 2, 3, 3, 3, 4, 3, 2, 2, 2, 3, 3, 
                                                                                                                                                                                                                                                                                                                                                              3), specialization = c("TRAU", "URG", "TRAU", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "PED", "PED", "PED", "PED", "URG", "PED", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "URG", "URG", "URG", "URG", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "URG", "URG", "TRAU", "TRAU", "URG"), .order = 1:30), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       -30L), class = c("activitylog", "log", "tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), case_id = "patient_visit_nr", activity_id = "activity", resource_id = "originator", timestamps = c("start", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "complete"))

我想修改函数daqapo::detect_value_range_violations,以便给予最终结果:

# Log of 0 events consisting of:
5 traces 
6 cases 
7 instances of 5 activities 
4 resources

而不是使用

hospital %>%
  detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))

。我如何修改此功能?

mm5n2pyu

mm5n2pyu1#

包{bupaR}有一个summary方法,它是activitylog类的对象所特有的。你可以使用以下命令检查这个内部(对bupaR)函数中发生了什么:

bupaR:::summary.activitylog ## triple colon, no parentheses

(未显示输出)
接下来,您可以将该函数复制到自己的自定义汇总函数中,比如summary_events

summary_events <- bupaR:::summary.activitylog

然后从你的函数代码中删除尾随的NextMethod(object),这将返回或多或少的想要的事件日志,但是没有其余的:

my_activitylog <- 
  hospital |>
  detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))

my_activitylog |>  summary_events()

输出:

## Number of events:  2
## Number of cases:  1
## Number of traces:  1
## Number of distinct activities:  1
## Average trace length:  2
## 
## Start eventlog:  2017-11-20 11:35:01
## End eventlog:  2017-11-20 11:36:09

...或者您可以从原始summary方法的代码中选择感兴趣的函数(如case_listn_events),在show对象之后滚动应用您的自定义函数。

event_summary  <- function(x){
    ca <- case_list(x)
     counts <- c(events = n_events(x),
                 cases = nrow(ca),
                 traces = length(unique(ca$trace_id)),
                 activities = nrow(activities(x))
                 )
    data.frame(category = names(counts),
               counts, row.names = NULL)
}

use:

violations <- 
        hospital |>
        detect_value_range_violations(triagecode = domain_numeric(from = 0, to = 5))
    
    > violations |> event_summary()
        category counts
    1     events      2
    2      cases      1
    3     traces      1
    4 activities      1

相关问题