Adding Analyses to a Plan

Richard Aubrey White

2022-06-02

Broad technical terms
Object Description
argset A named list containing a set of arguments.
analysis

These are the fundamental units that are scheduled in plnr:

  • 1 argset
  • 1 (action) function that takes two arguments
    1. data (named list)
    2. argset (named list)
plan

This is the overarching “scheduler”:

  • 1 data pull
  • 1 list of analyses
Different types of plans
Plan Type Description
Single-function plan Same action function applied multiple times with different argsets applied to the same datasets.
Multi-function plan Different action functions applied to the same datasets.
Plan Examples
Plan Type Example
Single-function plan Multiple strata (e.g. locations, age groups) that you need to apply the same function to to (e.g. outbreak detection, trend detection, graphing).
Single-function plan Multiple variables (e.g. multiple outcomes, multiple exposures) that you need to apply the same statistical methods to (e.g. regression models, correlation plots).
Multi-function plan Creating the output for a report (e.g. multiple different tables and graphs).

Single-function plan

This approach is generally used when you:

When we apply the same function multiple times, it is preferable to add the argsets first, and then apply the analysis function just before running the analyses.

Multiple strata

In this example, we loop through multiple geographical locations and apply a graphing function to the data from each of these geographical locations.

library(ggplot2)
library(data.table)
library(magrittr)

# We begin by defining a new plan
p <- plnr::Plan$new()

# Data function
data_fn <- function(){
  return(plnr::norway_covid19_cases_by_time_location)
}

# We add sources of data
# We can add data directly
p$add_data(
  name = "covid19_cases",
  fn_name = "data_fn"
)

p$get_data()
## $covid19_cases
##        granularity_time granularity_geo country_iso3 location_code border   age
##     1:              day          county          nor      county03   2020 total
##     2:              day          county          nor      county03   2020 total
##     3:              day          county          nor      county03   2020 total
##     4:              day          county          nor      county03   2020 total
##     5:              day          county          nor      county03   2020 total
##    ---                                                                         
## 11024:          isoweek          nation          nor         norge   2020 total
## 11025:          isoweek          nation          nor         norge   2020 total
## 11026:          isoweek          nation          nor         norge   2020 total
## 11027:          isoweek          nation          nor         norge   2020 total
## 11028:          isoweek          nation          nor         norge   2020 total
##          sex isoyear isoweek isoyearweek    season seasonweek calyear calmonth
##     1: total    2020       8     2020-08 2019/2020         31    2020        2
##     2: total    2020       8     2020-08 2019/2020         31    2020        2
##     3: total    2020       8     2020-08 2019/2020         31    2020        2
##     4: total    2020       9     2020-09 2019/2020         32    2020        2
##     5: total    2020       9     2020-09 2019/2020         32    2020        2
##    ---                                                                        
## 11024: total    2022      14     2022-14 2021/2022         37      NA       NA
## 11025: total    2022      15     2022-15 2021/2022         38      NA       NA
## 11026: total    2022      16     2022-16 2021/2022         39      NA       NA
## 11027: total    2022      17     2022-17 2021/2022         40      NA       NA
## 11028: total    2022      18     2022-18 2021/2022         41      NA       NA
##        calyearmonth       date covid19_cases_testdate_n
##     1:     2020-M02 2020-02-21                        0
##     2:     2020-M02 2020-02-22                        0
##     3:     2020-M02 2020-02-23                        0
##     4:     2020-M02 2020-02-24                        0
##     5:     2020-M02 2020-02-25                        0
##    ---                                                 
## 11024:         <NA> 2022-04-10                     6888
## 11025:         <NA> 2022-04-17                     3635
## 11026:         <NA> 2022-04-24                     3764
## 11027:         <NA> 2022-05-01                     2243
## 11028:         <NA> 2022-05-08                      502
##        covid19_cases_testdate_pr100000
##     1:                        0.000000
##     2:                        0.000000
##     3:                        0.000000
##     4:                        0.000000
##     5:                        0.000000
##    ---                                
## 11024:                      126.961423
## 11025:                       67.001274
## 11026:                       69.379036
## 11027:                       41.343564
## 11028:                        9.252996
## 
## $hash
## $hash$current
## [1] "53d85fe6d8e28320cf54e2071fea5d84"
## 
## $hash$current_elements
## $hash$current_elements$covid19_cases
## [1] "34de05796583f8e1012b8372b46270f2"
location_codes <- p$get_data()$covid19_cases$location_code %>%
  unique() %>% 
  print()
##  [1] "county03" "county11" "county15" "county18" "county30" "county34"
##  [7] "county38" "county42" "county46" "county50" "county54" "norge"
p$add_argset_from_list(
  plnr::expand_list(
    location_code = location_codes,
    granularity_time = "isoweek"
  )
)
# Examine the argsets that are available
p$get_argsets_as_dt()
##                            name_analysis index_analysis location_code
##  1: 25c4ac36-db1b-427d-8687-3501d8283c33              1      county03
##  2: 54bbc126-0229-465b-b5f0-b29c11dbc76e              2      county11
##  3: 02c460d4-673a-4f48-b1a1-a6fe050ddf39              3      county15
##  4: 7547a353-fcb0-40cf-a1b1-1d385265c852              4      county18
##  5: afdba994-b55e-4ebc-82ab-cdd50cebc954              5      county30
##  6: 7a82a419-3588-4b45-bd06-f001642d2a8a              6      county34
##  7: c6fab896-fa67-46fd-be43-c65435bbb28c              7      county38
##  8: 3a6725bc-829e-4f89-bb2c-902c8e61f6a8              8      county42
##  9: 09d33ed0-41ab-4aed-9df2-0d458256066a              9      county46
## 10: a1a9cf11-a685-44e9-ab3b-8cfd90c1785c             10      county50
## 11: e0b6c2b0-95f0-4d2a-89dd-21127a7a6b35             11      county54
## 12: b6523d85-e8ce-4529-ad18-1c8faef76f8c             12         norge
##     granularity_time
##  1:          isoweek
##  2:          isoweek
##  3:          isoweek
##  4:          isoweek
##  5:          isoweek
##  6:          isoweek
##  7:          isoweek
##  8:          isoweek
##  9:          isoweek
## 10:          isoweek
## 11:          isoweek
## 12:          isoweek
# We can then add a simple analysis that returns a figure:

# To do this, we first need to create an action function
# (takes two arguments -- data and argset)
action_fn <- function(data, argset){
  if(plnr::is_run_directly()){
    data <- p$get_data()
    argset <- p$get_argset(1)
  }
  pd <- data$covid19_cases[
    location_code == argset$location_code &
    granularity_time == argset$granularity_time
  ]
  
  q <- ggplot(pd, aes(x=date, y=covid19_cases_testdate_n))
  q <- q + geom_line()
  q <- q + labs(title = argset$location_code)
  q
}

p$apply_action_fn_to_all_argsets(fn_name = "action_fn")

p$run_one(1)

q <- p$run_all()
q[[1]]

q[[2]]

Multiple variables

In this example, we loop through multiple variable combinations (1. raw numbers of Covid-19 cases vs Covid-19 cases per 100 000 population, and 2. aggregating over isoweek vs day) and apply a graphing function to the data according to each of these variable combinations.

library(ggplot2)
library(data.table)
library(magrittr)

# We begin by defining a new plan
p <- plnr::Plan$new()

# Data function
data_fn <- function(){
  return(plnr::norway_covid19_cases_by_time_location[location_code=="norge"])
}

# We add sources of data
# We can add data directly
p$add_data(
  name = "covid19_cases",
  fn_name = "data_fn"
)

p$get_data()
## $covid19_cases
##      granularity_time granularity_geo country_iso3 location_code border   age
##   1:              day          nation          nor         norge   2020 total
##   2:              day          nation          nor         norge   2020 total
##   3:              day          nation          nor         norge   2020 total
##   4:              day          nation          nor         norge   2020 total
##   5:              day          nation          nor         norge   2020 total
##  ---                                                                         
## 915:          isoweek          nation          nor         norge   2020 total
## 916:          isoweek          nation          nor         norge   2020 total
## 917:          isoweek          nation          nor         norge   2020 total
## 918:          isoweek          nation          nor         norge   2020 total
## 919:          isoweek          nation          nor         norge   2020 total
##        sex isoyear isoweek isoyearweek    season seasonweek calyear calmonth
##   1: total    2020       8     2020-08 2019/2020         31    2020        2
##   2: total    2020       8     2020-08 2019/2020         31    2020        2
##   3: total    2020       8     2020-08 2019/2020         31    2020        2
##   4: total    2020       9     2020-09 2019/2020         32    2020        2
##   5: total    2020       9     2020-09 2019/2020         32    2020        2
##  ---                                                                        
## 915: total    2022      14     2022-14 2021/2022         37      NA       NA
## 916: total    2022      15     2022-15 2021/2022         38      NA       NA
## 917: total    2022      16     2022-16 2021/2022         39      NA       NA
## 918: total    2022      17     2022-17 2021/2022         40      NA       NA
## 919: total    2022      18     2022-18 2021/2022         41      NA       NA
##      calyearmonth       date covid19_cases_testdate_n
##   1:     2020-M02 2020-02-21                        1
##   2:     2020-M02 2020-02-22                        0
##   3:     2020-M02 2020-02-23                        0
##   4:     2020-M02 2020-02-24                        0
##   5:     2020-M02 2020-02-25                        0
##  ---                                                 
## 915:         <NA> 2022-04-10                     6888
## 916:         <NA> 2022-04-17                     3635
## 917:         <NA> 2022-04-24                     3764
## 918:         <NA> 2022-05-01                     2243
## 919:         <NA> 2022-05-08                      502
##      covid19_cases_testdate_pr100000
##   1:                      0.01863037
##   2:                      0.00000000
##   3:                      0.00000000
##   4:                      0.00000000
##   5:                      0.00000000
##  ---                                
## 915:                    126.96142312
## 916:                     67.00127367
## 917:                     69.37903551
## 918:                     41.34356447
## 919:                      9.25299570
## 
## $hash
## $hash$current
## [1] "61b7624f1f7b18673379f9f58357592c"
## 
## $hash$current_elements
## $hash$current_elements$covid19_cases
## [1] "1d2c57b32ae4932684239252d5b6f05b"
p$add_argset_from_list(
  plnr::expand_list(
    variable = c("covid19_cases_testdate_n", "covid19_cases_testdate_pr100000"),
    granularity_time = c("isoweek","day")
  )
)
# Examine the argsets that are available
p$get_argsets_as_dt()
##                           name_analysis index_analysis
## 1: 22ea28b7-5f68-4f97-8913-561988691b28              1
## 2: fa2fe6e9-7535-48ae-b0fa-188efabfe710              2
## 3: a3cacfc2-9f6b-4086-b984-0fe9c6331061              3
## 4: 5dfce2e1-967a-4757-b7b2-f1415695cd1e              4
##                           variable granularity_time
## 1:        covid19_cases_testdate_n          isoweek
## 2: covid19_cases_testdate_pr100000          isoweek
## 3:        covid19_cases_testdate_n              day
## 4: covid19_cases_testdate_pr100000              day
# We can then add a simple analysis that returns a figure:

# To do this, we first need to create an action function
# (takes two arguments -- data and argset)
action_fn <- function(data, argset){
  if(plnr::is_run_directly()){
    data <- p$get_data()
    argset <- p$get_argset(1)
  }
  pd <- data$covid19_cases[
    granularity_time == argset$granularity_time
  ]
  
  q <- ggplot(pd, aes_string(x="date", y=argset$variable))
  q <- q + geom_line()
  q <- q + labs(title = argset$granularity_time)
  q
}

p$apply_action_fn_to_all_argsets(fn_name = "action_fn")

p$run_one(1)

p$run_one(2)

p$run_one(3)

p$run_one(4)

Multi-function plan

This approach is generally used when you are creating the output for a report, and you need multiple different tables and graphs.

library(ggplot2)
library(data.table)
library(magrittr)

# We begin by defining a new plan
p <- plnr::Plan$new()

# Data function
data_fn <- function(){
  return(plnr::norway_covid19_cases_by_time_location)
}

# We add sources of data
# We can add data directly
p$add_data(
  name = "covid19_cases",
  fn_name = "data_fn"
)

p$get_data()
## $covid19_cases
##        granularity_time granularity_geo country_iso3 location_code border   age
##     1:              day          county          nor      county03   2020 total
##     2:              day          county          nor      county03   2020 total
##     3:              day          county          nor      county03   2020 total
##     4:              day          county          nor      county03   2020 total
##     5:              day          county          nor      county03   2020 total
##    ---                                                                         
## 11024:          isoweek          nation          nor         norge   2020 total
## 11025:          isoweek          nation          nor         norge   2020 total
## 11026:          isoweek          nation          nor         norge   2020 total
## 11027:          isoweek          nation          nor         norge   2020 total
## 11028:          isoweek          nation          nor         norge   2020 total
##          sex isoyear isoweek isoyearweek    season seasonweek calyear calmonth
##     1: total    2020       8     2020-08 2019/2020         31    2020        2
##     2: total    2020       8     2020-08 2019/2020         31    2020        2
##     3: total    2020       8     2020-08 2019/2020         31    2020        2
##     4: total    2020       9     2020-09 2019/2020         32    2020        2
##     5: total    2020       9     2020-09 2019/2020         32    2020        2
##    ---                                                                        
## 11024: total    2022      14     2022-14 2021/2022         37      NA       NA
## 11025: total    2022      15     2022-15 2021/2022         38      NA       NA
## 11026: total    2022      16     2022-16 2021/2022         39      NA       NA
## 11027: total    2022      17     2022-17 2021/2022         40      NA       NA
## 11028: total    2022      18     2022-18 2021/2022         41      NA       NA
##        calyearmonth       date covid19_cases_testdate_n
##     1:     2020-M02 2020-02-21                        0
##     2:     2020-M02 2020-02-22                        0
##     3:     2020-M02 2020-02-23                        0
##     4:     2020-M02 2020-02-24                        0
##     5:     2020-M02 2020-02-25                        0
##    ---                                                 
## 11024:         <NA> 2022-04-10                     6888
## 11025:         <NA> 2022-04-17                     3635
## 11026:         <NA> 2022-04-24                     3764
## 11027:         <NA> 2022-05-01                     2243
## 11028:         <NA> 2022-05-08                      502
##        covid19_cases_testdate_pr100000
##     1:                        0.000000
##     2:                        0.000000
##     3:                        0.000000
##     4:                        0.000000
##     5:                        0.000000
##    ---                                
## 11024:                      126.961423
## 11025:                       67.001274
## 11026:                       69.379036
## 11027:                       41.343564
## 11028:                        9.252996
## 
## $hash
## $hash$current
## [1] "555e9cc4060ae8e34d315459e5f20b85"
## 
## $hash$current_elements
## $hash$current_elements$covid19_cases
## [1] "779be26cb13fcf74f606a919061dcde3"
# Completely unique function for figure 1
p$add_analysis(
  name = "figure_1",
  fn_name = "figure_1"
)

figure_1 <- function(data, argset){
  if(plnr::is_run_directly()){
    data <- p$get_data()
    argset <- p$get_argset("figure_1")
  }
  pd <- data$covid19_cases[
    granularity_time == "isoweek"
  ]
  
  q <- ggplot(pd, aes_string(x="date", y="covid19_cases_testdate_pr100000"))
  q <- q + geom_line()
  q <- q + facet_wrap(~location_code)
  q <- q + labs(title = "Weekly covid-19 cases per 100 000 population")
  q
}

# Reusing a function for figures 2 and 3
p$add_analysis(
  name = "figure_2",
  fn_name = "plot_epicurve_by_location",
  location_code = "norge"
)

# Reusing a function for figures 2 and 3
p$add_analysis(
  name = "figure_3",
  fn_name = "plot_epicurve_by_location",
  location_code = "county03"
)

plot_epicurve_by_location <- function(data, argset){
  if(plnr::is_run_directly()){
    data <- p$get_data()
    argset <- p$get_argset("figure_2")
    argset <- p$get_argset("figure_3")
  }
  pd <- data$covid19_cases[
    granularity_time == "isoweek" & 
    location_code == argset$location_code
  ]
  
  q <- ggplot(pd, aes_string(x="date", y="covid19_cases_testdate_n"))
  q <- q + geom_line()
  q <- q + labs(title = argset$location_code)
  q
}

p$run_one("figure_1")

p$run_one("figure_2")

p$run_one("figure_3")