-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhp_mitigator_rankings.R
56 lines (47 loc) · 1.36 KB
/
nhp_mitigator_rankings.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
# Rank NHP mitigators by episodes and bed days
library(tidyverse)
library(DBI)
library(odbc)
# Establish connection with local SQL server
con <- DBI::dbConnect(
odbc::odbc(),
Driver = "SQL Server",
Server = "MLCSU-BI-SQL",
Database = "HESdata",
Trusted_Connection = "True"
)
# Pull the strategy look up
strategy_lookups <-
tbl(
con,
in_schema("nhp_modelling_reference", "strategy_lookups")
) |>
collect()
# Strategies table is every epikey (episode ID) and the strategy it is captured in (double counting present)
tb_strategies <-
tbl(con,
in_schema("nhp_modelling", "strategies")
)
# Pull in inpatients table to get episode length
tb_inpatients <-
tbl(con,
in_schema("nhp_modelling", "inpatients")
)
# Join inpatient table to strategy table to
inpatient_strat <-
tb_inpatients |>
inner_join(tb_strategies, "EPIKEY")
# Count episodes and bed days per strategy
episodes_beddays_per_strategy <-
inpatient_strat |>
group_by(strategy) |>
summarise(episodes = sum(sample_rate),
bed_days = sum(SPELDUR * sample_rate)) |>
ungroup() |>
mutate(episode_prop = episodes/sum(episodes)*100) |>
select(strategy, episodes, episode_prop, bed_days) |>
arrange(desc(episodes)) |>
collect()
write_csv(episodes_beddays_per_strategy, "episodes_beddays_per_strategy.csv")
# Disconnect from SQL server
dbDisconnect(con)