Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create individual file #715

Merged
merged 266 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
266 commits
Select commit Hold shift + click to select a range
0c426c5
Until L594
jr-mandy Feb 14, 2023
7f17ef2
Converted until L677
jr-mandy Feb 16, 2023
52a4ffd
Until L731
jr-mandy Feb 17, 2023
3223189
Update documentation
jr-mandy Feb 17, 2023
1c67f20
Remove test ref
jr-mandy Feb 17, 2023
aaad1ff
Merge
jr-mandy Feb 17, 2023
b14e4b2
Style code
jr-mandy Feb 17, 2023
f31f19b
WIP writing functions to fill postcode in line with previous DOB func…
shintoLamp Mar 2, 2023
891c9a9
Update documentation
shintoLamp Mar 2, 2023
509935f
Merge branch 'main-R' into d01_2
Moohan Apr 3, 2023
134a893
Merge branch 'main-R' into d01_1
Moohan Apr 3, 2023
9664f4b
Merge branch 'main-R' into d01_2
Moohan Apr 4, 2023
9a39422
Merge branch 'main-R' into d01_1
Moohan Apr 4, 2023
a26dcc9
Merge branch 'main-R' into d01_1
Moohan Apr 18, 2023
40adc51
Merge branch 'main-R' into d01_2
Moohan Apr 18, 2023
a4b3393
Merge branch 'main-R' into d01_2
Moohan Apr 18, 2023
c197c51
Merge branch 'main-R' into d01_1
Moohan Apr 18, 2023
124f64e
Merge branch 'main-R' into d01_2
Moohan Apr 20, 2023
fb27bf8
Merge branch 'main-R' into d01_1
Moohan Apr 20, 2023
85b1bea
Merge branch 'main-R' into d01_2
Jennit07 Apr 26, 2023
fb57c75
Merge branch 'main-R' into d01_1
Jennit07 Apr 26, 2023
73f77d5
implement quick fix for running 22/23
Jennit07 May 3, 2023
00f37c8
Style code
Jennit07 May 3, 2023
4017984
Merge branch 'main-R' into d01_1
Moohan May 3, 2023
ce0d700
Merge branch 'main-R' into d01_2
Moohan May 3, 2023
2cacee8
Fix missed comma
Moohan May 3, 2023
369a8bb
Exclude DD code for now - TEMP fix
Jennit07 May 5, 2023
f715830
Correct/rename variables
Jennit07 May 5, 2023
d537aad
Style code
Jennit07 May 5, 2023
50641b3
Include NSU in `check_year_valid`
Jennit07 May 12, 2023
1b52ebb
Update `check_year_valid_tests`
Jennit07 May 12, 2023
e5cf2a0
Update documentation
Jennit07 May 12, 2023
33fe105
Update `add_nsu_cohort` to pick up years valid
Jennit07 May 12, 2023
3473c18
Style code
Jennit07 May 12, 2023
fff9bad
remove extra `!`
Jennit07 May 12, 2023
8a37356
Exclude `cij_delay`
Jennit07 May 12, 2023
b2f6941
Style code
Jennit07 May 12, 2023
2c7d312
Merge branch 'main-R' into d01_2
Jennit07 May 12, 2023
6c44897
Merge branch 'main-R' into no_data_nsu
Moohan May 15, 2023
13a14e9
Merge remote-tracking branch 'origin/d01_1' into d01_2
Moohan May 15, 2023
362d1b4
Merge branch 'main-R' into d01_2
Jennit07 May 16, 2023
07b03f3
improve `max_no_inf()`
Jennit07 May 16, 2023
617ac68
Use pmin/max instead of `rowwise`
Jennit07 May 16, 2023
01cc1b4
improve `min_no_inf()`
Jennit07 May 16, 2023
2ff02bd
Use n_distinct(cij_marker)
Jennit07 May 16, 2023
435cd0f
deal with distinct(ch_chi_cis)
Jennit07 May 16, 2023
5a0b550
use n_distinct(ooh_case_id)
Jennit07 May 16, 2023
0da09b0
remove `find_non_duplicates`
Jennit07 May 16, 2023
a1d9c80
Merge branch 'main-R' into d01_2
Jennit07 May 17, 2023
faa0a96
Use dplyr::if_else()
Jennit07 May 17, 2023
979fc81
Fix typo in `ooh_covid_assessment`
Jennit07 May 17, 2023
6a57809
Move `ooh_case_id` to aggregate
Jennit07 May 17, 2023
83fbdcb
Use `slfhelper::ltc_vars`
Jennit07 May 17, 2023
8a761c0
Remove `clean_up_dob`
Jennit07 May 17, 2023
46a7b70
Update documentation
Jennit07 May 17, 2023
6424c95
[check-spelling] Update metadata
Moohan May 17, 2023
89268dc
Use `start_next_fy_quarter` in place of rowwise
Jennit07 May 17, 2023
b6d93ed
Style code
Jennit07 May 17, 2023
d4e1d41
Use `compute_mid_year_age`
Jennit07 May 17, 2023
eac15ed
convert code into data.table for improving speed
Jennit07 May 17, 2023
4f6d6ff
Update `get_fy_dates`function
Jennit07 May 17, 2023
4c9134b
remove `date_from_fy`, use `get_fy_dates`
Jennit07 May 17, 2023
3730ee1
Update documentation
Jennit07 May 17, 2023
c9852b4
Remove `clean_up_postcode` function
Jennit07 May 17, 2023
3714bca
Remove non duplicates function/move to aggregate
Jennit07 May 17, 2023
15ae96a
Style code
Jennit07 May 17, 2023
e182a14
Update documentation
Jennit07 May 17, 2023
73852cc
Add time stamps to `create_individual_file`
Jennit07 May 17, 2023
a358cc5
Style code
Jennit07 May 17, 2023
ca0c7b6
remove `clean_up_postcode`
Jennit07 May 18, 2023
2cb8a24
Deal with ch cis episodes
Jennit07 May 18, 2023
fee2b46
Style code
Jennit07 May 18, 2023
ee36738
add .data$
Jennit07 May 18, 2023
feef2b6
Turn ch aggregate into a data table
Jennit07 May 18, 2023
da13d92
Style code
Jennit07 May 18, 2023
7fc40fa
use ch_chi_cis
Jennit07 May 18, 2023
45eeca0
remove `preventable_admissions` from aggregate
Jennit07 May 18, 2023
d89b0aa
exclude `hh_in_fy` for now
Jennit07 May 18, 2023
2326c0f
Style code
Jennit07 May 18, 2023
78d2c36
Test - exclude `sc_` vars from aggregate
Jennit07 May 18, 2023
3ac7d26
Style code
Jennit07 May 18, 2023
141c880
Exclude for now
Jennit07 May 18, 2023
93fcd43
exclude for now
Jennit07 May 18, 2023
baf5d13
Style code
Jennit07 May 18, 2023
3bf8fb7
automate `check_year_valid`
Jennit07 May 23, 2023
3e5a059
Return dummy file path for NSU not valid
Jennit07 May 23, 2023
bfeffc7
Style code
Jennit07 May 23, 2023
cfc0195
Merge branch 'main-R' into no_data_nsu
Jennit07 May 23, 2023
f42825c
Merge branch 'no_data_nsu' into d01_2
Jennit07 May 24, 2023
4aacf7a
Fix brackets in aggregate
Jennit07 May 24, 2023
5bf6a4b
TEMP - exclude variables
Jennit07 May 24, 2023
e045ccc
Use `phsmethods::sex_from_chi`
Jennit07 May 24, 2023
173ae02
Style code
Jennit07 May 24, 2023
e5332ee
Add ungroup()
Jennit07 May 24, 2023
cec63a3
lowercase dob
Jennit07 May 24, 2023
8a652df
Remove as.data.table
Jennit07 May 25, 2023
fc979d9
rewrite aggregate_by_chi with data.table
lizihao-anu May 31, 2023
7c63f57
Style code
lizihao-anu May 31, 2023
70f0891
minor changes
lizihao-anu May 31, 2023
4e89a6b
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu May 31, 2023
abda3d5
Use the updated function
Moohan Jun 1, 2023
38be4d2
to properly import data.table
lizihao-anu Jun 6, 2023
6368535
remove redundant columns dob postcode and gpprac
lizihao-anu Jun 6, 2023
2d70019
minor changes to remove redundant postcode gpprac columns
lizihao-anu Jun 6, 2023
9f23cff
Style code
lizihao-anu Jun 6, 2023
b361616
rename columns with small letters
lizihao-anu Jun 6, 2023
a23388b
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu Jun 6, 2023
550adab
Style code
lizihao-anu Jun 6, 2023
fee7d8b
newaggregate_ch_episodes
lizihao-anu Jun 7, 2023
a8f4ae2
Update documentation
lizihao-anu Jun 7, 2023
c601a64
Merge branch 'main-R' into d01_2
Moohan Jun 8, 2023
bf9fdfd
Merge branch 'd01_2' into d01_2_zihao
Moohan Jun 8, 2023
f37b276
Merge branch 'main-R' into d01_2
Moohan Jun 12, 2023
ae5eacd
Merge branch 'd01_2' into d01_2_zihao
Moohan Jun 12, 2023
cd8a08b
add functions to replace regular expressions to select column/variables
lizihao-anu Jun 13, 2023
7415971
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu Jun 13, 2023
e03b02d
Update documentation
lizihao-anu Jun 13, 2023
66e21e6
Style code
lizihao-anu Jun 13, 2023
f0fce5b
minor changes
lizihao-anu Jun 13, 2023
f1b96d1
add a missing variable, cij_delay
lizihao-anu Jun 14, 2023
f565922
Style code
lizihao-anu Jun 14, 2023
5237f9e
add variables cij_delay, preventable_beddays
lizihao-anu Jun 14, 2023
bdfc0b4
add missing variables health_net_cost, health_net_costincdnas, and cm…
lizihao-anu Jun 14, 2023
7d7296d
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu Jun 14, 2023
7b288bd
Style code
lizihao-anu Jun 14, 2023
e907dd9
add more variables needed
lizihao-anu Jun 14, 2023
4647904
Style code
lizihao-anu Jun 14, 2023
9f8133b
Merge branch 'main-R' into d01_2
Moohan Jun 19, 2023
5004a52
Merge branch 'd01_2' into d01_2_zihao
Moohan Jun 19, 2023
45688c3
Update R/link_delayed_discharge_eps.R
Moohan Jun 19, 2023
b2676d4
Style code
Moohan Jun 19, 2023
8048e68
amend costs
lizihao-anu Jun 20, 2023
78197c6
Style code
lizihao-anu Jun 20, 2023
4fd8ac4
Revert "amend costs"
Jennit07 Jun 20, 2023
b6a1e6f
Add DN and cij_delay back in
Jennit07 Jun 20, 2023
f32c7a2
fix the issue
lizihao-anu Jun 20, 2023
99115ce
minor changes
lizihao-anu Jun 20, 2023
04fe893
Style code
lizihao-anu Jun 20, 2023
b468271
remove running in chunks
lizihao-anu Jun 21, 2023
55a075c
Style code
lizihao-anu Jun 21, 2023
b9fbf29
Update tests to include missing variables
Jennit07 Jun 21, 2023
74da47c
Remove unnecessary comma
Jennit07 Jun 21, 2023
79981a3
fix the bug of preventable_beddays
lizihao-anu Jun 21, 2023
fe72501
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu Jun 21, 2023
a029a10
Update documentation
lizihao-anu Jun 21, 2023
71702e0
fix total ae_attendances
lizihao-anu Jun 21, 2023
90511c3
Merge branches 'd01_2_zihao' and 'd01_2_zihao' of github.com:Public-H…
lizihao-anu Jun 21, 2023
1667ff0
fix the bug of preventable_admissions
lizihao-anu Jun 21, 2023
0a517d3
fix the bug of hbrescode etc
lizihao-anu Jun 21, 2023
3fc60d1
Merge branch 'main-R' into d01_2_zihao
lizihao-anu Jun 21, 2023
3b24326
minor fix
lizihao-anu Jun 21, 2023
5dd9544
Merge branch 'd01_2_zihao' of https://github.com/Public-Health-Scotla…
lizihao-anu Jun 21, 2023
4e4330c
minor fix
lizihao-anu Jun 21, 2023
6bdd780
Style code
lizihao-anu Jun 21, 2023
6720349
Merge branch 'create_individual_file_stable' into d01_2_zihao
lizihao-anu Jun 21, 2023
06e1c7c
Fix some warnings being produced by the tests
Moohan Jun 20, 2023
e14ae02
Fix failing test
Moohan Jun 20, 2023
dc79a75
remove running in chunks
lizihao-anu Jun 21, 2023
28289fa
Style code
lizihao-anu Jun 21, 2023
5ad5c78
Update the targets config to use `timestamp_positives` as the default…
Moohan Jun 21, 2023
f6e04ce
fix the bug of preventable_beddays
lizihao-anu Jun 21, 2023
6bcf2b2
Update documentation
lizihao-anu Jun 21, 2023
b0065c9
fix total ae_attendances
lizihao-anu Jun 21, 2023
42f107a
fix the bug of preventable_admissions
lizihao-anu Jun 21, 2023
9612b9a
fix the bug of hbrescode etc
lizihao-anu Jun 21, 2023
4750913
minor fix
lizihao-anu Jun 21, 2023
338479f
minor fix
lizihao-anu Jun 21, 2023
724f319
Style code
lizihao-anu Jun 21, 2023
e9c8ef0
fix home care cost
lizihao-anu Jun 27, 2023
9a951a5
add ipdc to fix maternity
lizihao-anu Jun 27, 2023
4b63ee8
fix preventable addmission and care home cost
lizihao-anu Jun 27, 2023
c42d7ba
fix preventable_admissions and calculate preventable_beddays here
lizihao-anu Jun 27, 2023
f0671fc
add monthly_beddays and yearstay to dd
lizihao-anu Jun 27, 2023
3246a69
Merge branch 'd01_2_zihao' of github.com:Public-Health-Scotland/sourc…
lizihao-anu Jun 27, 2023
9cc84f3
Style code
lizihao-anu Jun 27, 2023
d6391e5
fix preventable_admissions and preventable_beddays
lizihao-anu Jun 27, 2023
1a136fd
Style code
lizihao-anu Jun 27, 2023
c631f4e
include parameter for write to disk/year
Jennit07 Jun 28, 2023
390fbc0
Merge branch 'main-R' into join-lookups
Jennit07 Jun 28, 2023
c7b7bb8
Merge branch 'main-R' into create_individual_file_stable
Jennit07 Jun 28, 2023
b731676
Add lookups to indiv file creation pipeline
Jennit07 Jun 28, 2023
5507a33
include parameter for write to disk/year
Jennit07 Jun 28, 2023
e8f1099
fix delay discharge beddays and yearstay
lizihao-anu Jun 28, 2023
ff36479
Style code
lizihao-anu Jun 28, 2023
23e8513
fix preventable issues
lizihao-anu Jun 28, 2023
3022576
Style code
lizihao-anu Jun 28, 2023
9a7b8e0
fix the issue of preventable stuff
lizihao-anu Jun 28, 2023
d264943
Style code
lizihao-anu Jun 28, 2023
999afd8
Merge branch 'create_individual_file_stable' into d01_2_zihao
lizihao-anu Jun 28, 2023
7433fb8
Update R/aggregate_by_chi_zihao.R
lizihao-anu Jun 28, 2023
64e15b0
Merge branch 'master' into create_individual_file_stable
Moohan Jul 3, 2023
288f417
Merge branch 'create_individual_file_stable' into join-lookups
Moohan Jul 3, 2023
8f31277
Update documentation
Moohan Jul 3, 2023
b3f2d11
Fix minor typos
Moohan Jul 3, 2023
1bc1d6c
[check-spelling] Update metadata
Moohan Jul 3, 2023
31b3782
Remove some obsolete comments
Moohan Jul 3, 2023
a1371ed
Remove some unnecessary brackets
Moohan Jul 3, 2023
64081c8
Reformat some code
Moohan Jul 3, 2023
0800662
Use some `dplyr` functions for readability
Moohan Jul 3, 2023
a954611
Style code
Moohan Jul 3, 2023
7a4c023
Merge branch 'master' into create_individual_file_stable
Moohan Jul 3, 2023
f9f6e8f
Join lookups onto individual file pipeline (#709)
Moohan Jul 3, 2023
2777d81
Merge branch 'create_individual_file_stable' into d01_2_zihao
Moohan Jul 3, 2023
689dac2
Update R/link_delayed_discharge_eps.R
Moohan Jul 3, 2023
fa6120d
Style code
Moohan Jul 3, 2023
b96b3b3
Merge branch 'master' into create_individual_file_stable
Moohan Jul 3, 2023
388fa04
Some individual file fixes (#710)
Moohan Jul 3, 2023
16d6d22
Remove some code which is no longer needed
Moohan Jul 4, 2023
77ddd9e
Work out preventable admissions with similar indicators
Moohan Jul 4, 2023
51a0767
Lowercase variable names
Moohan Jul 4, 2023
b17f806
Restore `cij_delay`
Moohan Jul 4, 2023
12ec4f6
Restore DN variables
Moohan Jul 4, 2023
33681d3
Tidy the code and use integers where possible
Moohan Jul 4, 2023
f9e6f81
Supply `year` as a parameter to `clean_up_ch`
Moohan Jul 4, 2023
cb73e0f
Supply `year` as a parameter to `clean_individual_file`
Moohan Jul 5, 2023
42cc15e
Only keep required variables to save memory
Moohan Jul 5, 2023
35a6ef2
Rename the parameter so the documentation works
Moohan Jul 5, 2023
978d9e8
Use `setnames` to change names to lower
Moohan Jul 5, 2023
9be6385
Remove unneeded code
Moohan Jul 5, 2023
1ca4000
Update file path name
Moohan Jul 5, 2023
3ebfecc
Trim the return code
Moohan Jul 5, 2023
beae36a
Some fixes
Moohan Jul 5, 2023
13b7f11
Correctly compute `ooh_cases`
Moohan Jul 5, 2023
c03a0ee
Update documentation
Moohan Jul 5, 2023
b13fc13
Merge branch 'create_individual_file_stable' into d01_2_zihao
Moohan Jul 5, 2023
0027576
Style code
Moohan Jul 5, 2023
bee0342
Merge branch 'master' into create_individual_file_stable
Moohan Jul 6, 2023
61d02dc
Merge branch 'create_individual_file_stable' into d01_2_zihao
Moohan Jul 6, 2023
60e3f3a
[check-spelling] Update metadata
Moohan Jul 6, 2023
21859ad
Some more indiv file changes and fixes (#719)
Moohan Jul 6, 2023
bc75af8
Merge branch 'master' into create_individual_file_stable
Moohan Jul 6, 2023
2d8e731
Merge branch 'master' into create_individual_file_stable
Moohan Jul 10, 2023
c8d86c5
Add targets for the individual file
Moohan Jul 10, 2023
62c70c5
Fix missed pipe
Moohan Jul 10, 2023
479d9db
Merge branch 'master' into create_individual_file_stable
Moohan Jul 11, 2023
54252a1
Style code
Moohan Jul 11, 2023
9ae871a
Update some targets to only run once a week
Moohan Jul 11, 2023
486b51d
Make the deaths lookup unique
Moohan Jul 11, 2023
5ad1928
Add `year` back to the individual file
Moohan Jul 12, 2023
c7acc86
Merge branch 'master' into create_individual_file_stable
Moohan Jul 12, 2023
7103630
Merge branch 'master' into create_individual_file_stable
Moohan Jul 12, 2023
cf02089
Merge branch 'master' into create_individual_file_stable
Moohan Jul 13, 2023
507fffe
Remove `cost_total_net_inc_dnas` from the indiv file (#737)
Moohan Jul 14, 2023
292f4d8
Join slf lookups onto individual file (#724)
Jennit07 Jul 14, 2023
8bc5c4c
Merge branch 'master' into create_individual_file_stable
Moohan Jul 14, 2023
c644992
Join sc client variables onto individual file (#740)
Jennit07 Jul 17, 2023
7b24b8c
Merge branch 'master' into create_individual_file_stable
Moohan Jul 17, 2023
1bb52aa
Update documentation
Moohan Jul 17, 2023
dabbf57
Output the individual file with `anon_chi` (#748)
Moohan Jul 19, 2023
437c82b
Merge branch 'master' into create_individual_file_stable
Moohan Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cmh
CNWs
commhosp
congen
costincdnas
costmonthnum
costsfy
covr
Expand All @@ -45,6 +46,7 @@ dbconnect
dbplyr
deathdiag
demog
dfc
disch
dischloc
dischto
Expand All @@ -70,6 +72,7 @@ fyyear
geogs
ggplot
GLS
gls
gms
GPOo
gpprac
Expand All @@ -86,6 +89,7 @@ hhg
hjust
hms
homecare
homev
hscp
hscpnames
infyyear
Expand All @@ -99,6 +103,7 @@ keydate
keyring
keytime
keytimex
lgl
los
ltc
ltcs
Expand All @@ -111,6 +116,7 @@ multiday
multisession
multistaff
NAs
newcons
nhs
nhshosp
NRS
Expand Down Expand Up @@ -143,6 +149,7 @@ quickstart
Rbuildignore
rcmdcheck
rds
reabl
reablement
readcode
readr
Expand All @@ -159,8 +166,12 @@ rspm
RStudio
rstudioapi
Rtype
SDcols
seealso
selfharm
setkeyv
setnafill
setnames
Siar
sigfac
simd
Expand Down Expand Up @@ -200,5 +211,6 @@ xintercept
xlsx
yearstay
YYYYQX
zihao
zsav
zstd
3 changes: 2 additions & 1 deletion DESCRIPTION
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lizihao-anu I don't think we need the zoo package? I assume this was used in some of the 'original code'.

Can you confirm, and also remove aggregate_ch_episodes and aggregate_by_chi and rename 'your' data.table versions to take their place as I think we're happy they work and do a better / faster job.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Imports:
stringr (>= 1.5.0),
tibble (>= 3.2.1),
tidyr (>= 1.3.0),
tidyselect (>= 1.2.0)
tidyselect (>= 1.2.0),
zoo (>= 1.8.0)
Suggests:
covr (>= 3.6.1),
roxygen2 (>= 7.2.3),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(convert_hscp_to_hscpnames)
export(convert_numeric_to_date)
export(convert_sending_location_to_lca)
export(convert_year_to_fyyear)
export(create_individual_file)
export(create_service_use_cohorts)
export(end_fy)
export(end_fy_quarter)
Expand Down Expand Up @@ -160,6 +161,8 @@ export(start_fy)
export(start_fy_quarter)
export(start_next_fy_quarter)
export(write_file)
importFrom(data.table,.N)
importFrom(data.table,.SD)
importFrom(magrittr,"%>%")
importFrom(readr,col_character)
importFrom(readr,col_date)
Expand Down
215 changes: 215 additions & 0 deletions R/aggregate_by_chi_zihao.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#' Aggregate by CHI
#'
#' @description Aggregate episode file by CHI to convert into
#' individual file.
#'
#' @importFrom data.table .N
#' @importFrom data.table .SD
#'
#' @inheritParams create_individual_file
aggregate_by_chi_zihao <- function(episode_file) {
cli::cli_alert_info("Aggregate by CHI function started at {Sys.time()}")

# Convert to data.table
data.table::setDT(episode_file)

# Ensure all variable names are lowercase
data.table::setnames(episode_file, stringr::str_to_lower)

# Sort the data
data.table::setkeyv(
episode_file,
c(
"chi",
"record_keydate1",
"keytime1",
"record_keydate2",
"keytime2"
)
)

data.table::setnames(
episode_file,
c(
"ch_chi_cis", "cij_marker", "ooh_case_id"
# ,"hh_in_fy"
),
c(
"ch_cis_episodes", "cij_total", "ooh_cases"
# ,"hl1_in_fy"
)
)

# column specification, grouped by chi
# columns to select last
cols2 <- c(
"postcode",
"dob",
"gpprac",
vars_start_with(episode_file, "sc_")
)
# columns to count unique rows
cols3 <- c(
"ch_cis_episodes",
"cij_total",
"cij_el",
"cij_non_el",
"cij_mat",
"cij_delay",
"ooh_cases",
"preventable_admissions"
)
# columns to sum up
cols4 <- c(
vars_end_with(
episode_file,
c(
"episodes",
"beddays",
"cost",
"attendances",
"attend",
"contacts",
"hours",
"alarms",
"telecare",
"paid_items",
"advice",
"homev",
"time",
"assessment",
"other",
"dn",
"nhs24",
"pcc",
"_dnas"
)
),
vars_start_with(
episode_file,
"sds_option"
),
"health_net_cost_inc_dnas"
)
cols4 <- cols4[!(cols4 %in% c("ch_cis_episodes"))]
# columns to select maximum
cols5 <- c("nsu", vars_contain(episode_file, c("hl1_in_fy")))
data.table::setnafill(episode_file, fill = 0L, cols = cols5)
# compute
individual_file_cols1 <- episode_file[,
.(gender = mean(gender)),
by = "chi"
]
individual_file_cols2 <- episode_file[,
.SD[.N],
.SDcols = cols2,
by = "chi"
]
individual_file_cols3 <- episode_file[,
lapply(.SD, function(x) {
data.table::uniqueN(x, na.rm = TRUE)
}),
.SDcols = cols3,
by = "chi"
]
individual_file_cols4 <- episode_file[,
lapply(.SD, function(x) {
sum(x, na.rm = TRUE)
}),
.SDcols = cols4,
by = "chi"
]
individual_file_cols5 <- episode_file[,
lapply(.SD, function(x) max(x, na.rm = TRUE)),
.SDcols = cols5,
by = "chi"
]
individual_file_cols6 <- episode_file[,
.(
preventable_beddays = ifelse(
max(cij_ppa, na.rm = TRUE),
max(cij_end_date) - min(cij_start_date),
NA_real_
)
),
# cij_marker has been renamed as cij_total
by = c("chi", "cij_total")
]
individual_file_cols6 <- individual_file_cols6[,
.(
preventable_beddays = sum(preventable_beddays, na.rm = TRUE)
),
by = "chi"
]

individual_file <- dplyr::bind_cols(
individual_file_cols1,
individual_file_cols2[, chi := NULL],
individual_file_cols3[, chi := NULL],
individual_file_cols4[, chi := NULL],
individual_file_cols5[, chi := NULL],
individual_file_cols6[, chi := NULL]
)

# convert back to tibble
return(dplyr::as_tibble(individual_file))
}


#' select columns ending with some patterns
#' @describeIn select columns based on patterns
vars_end_with <- function(data, vars, ignore_case = FALSE) {
names(data)[stringr::str_ends(
names(data),
stringr::regex(paste(vars, collapse = "|"),
ignore_case = ignore_case
)
)]
}

#' select columns starting with some patterns
#' @describeIn select columns based on patterns
vars_start_with <- function(data, vars, ignore_case = FALSE) {
names(data)[stringr::str_starts(
names(data),
stringr::regex(paste(vars, collapse = "|"),
ignore_case = ignore_case
)
)]
}

#' select columns contains some characters
#' @describeIn select columns based on patterns
vars_contain <- function(data, vars, ignore_case = FALSE) {
names(data)[stringr::str_detect(
names(data),
stringr::regex(paste(vars, collapse = "|"),
ignore_case = ignore_case
)
)]
}

#' Aggregate CIS episodes
#'
#' @description Aggregate CH variables by CHI and CIS.
#'
#' @inheritParams create_individual_file
aggregate_ch_episodes_zihao <- function(episode_file) {
cli::cli_alert_info("Aggregate ch episodes function started at {Sys.time()}")

# Convert to data.table
data.table::setDT(episode_file)

# Perform grouping and aggregation
episode_file <- episode_file[, `:=`(
ch_no_cost = max(ch_no_cost),
ch_ep_start = min(record_keydate1),
ch_ep_end = max(ch_ep_end),
ch_cost_per_day = mean(ch_cost_per_day)
), by = c("chi", "ch_chi_cis")]

# Convert back to tibble if needed
episode_file <- tibble::as_tibble(episode_file)

return(episode_file)
}
Loading