forked from dewittpe/Data_Manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.R
76 lines (69 loc) · 2.83 KB
/
utilities.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
################################################################################
# Load the needed packages
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(data.table))
################################################################################
# Define some useful paths and objects
recs_2009_path <-
file.path(".", "000_data_sets", "RECS", "2009", "recs2009_public.csv")
recs_2015_path <-
file.path(".", "000_data_sets", "RECS", "2015", "recs2015_public_v4.csv")
recs_2020_path <-
file.path(".", "000_data_sets", "RECS", "2020", "recs2020_public_v1.csv")
psps_2019_path <-
file.path(".", "000_data_sets", "PSPS", "psps_2019.csv")
psps_column_classes <-
c(
"HCPCS_CD" = "character",
"HCPCS_INITIAL_MODIFIER_CD" = "character",
"PROVIDER_SPEC_CD" = "character",
"CARRIER_NUM" = "integer",
"PRICING_LOCALITY_CD" = "character",
"TYPE_OF_SERVICE_CD" = "character",
"PLACE_OF_SERVICE_CD" = "integer",
"HCPCS_SECOND_MODIFIER_CD" = "character",
"SUBMITTED_SERVICE_CNT" = "numeric",
"SUBMITTED_CHARGE_AMT" = "numeric",
"ALLOWED_CHARGE_AMT" = "numeric",
"DENIED_SERVICES_CNT" = "numeric",
"DENIED_CHARGE_AMT" = "numeric",
"ASSIGNED_SERVICES_CNT" = "numeric",
"NCH_PAYMENT_AMT" = "numeric",
"HCPCS_ASC_IND_CD" = "character",
"ERROR_IND_CD" = "integer",
"BETOS_CD" = "character")
################################################################################
# HELPER FUNCTIONS #
################################################################################
#' Benchmarking and Memory Use
#'
#' @param x a list of calls, expected to be constructed via alist
#' @param ... additional arguments passed to microbenchmark::microbenchmark
#'
#' @return a list with the benchmark results and memory use, in bytes, for each
#' element of x
#'
#' @examples
#'
#' calls <- alist(fit1 = lm(mpg ~ wt, data = mtcars),
#' fit2 = lm(mpg ~ wt + hp, data = mtcars),
#' fit3 = lm(mpg ~ am*wt + hp, data = mtcars)
#' )
#'
#' benchmark(calls, times = 10)
#'
#' @export
benchmark <- function(x, ...) {
stopifnot(sapply(x, typeof) == "language")
bm <- microbenchmark::microbenchmark(list = x, ...)
mem <-
x |>
lapply(profmem::profmem, substitute = FALSE) |>
sapply(profmem::total) |>
sapply(formatC, format = "f", big.mark = ",", digits = 0) |>
data.frame(bytes = _)
list(benchmark = bm, "profmem" = mem)
}
################################################################################
# end of file #
################################################################################