Skip to content

Commit b73b7fd

Browse files
authored
Merge pull request #72 from opensafely-actions/make-v0.0.39
Allow covariate_other option to be specified as a text file of semi-colon separated variable names
2 parents d4d3e9b + 747cbef commit b73b7fd

7 files changed

Lines changed: 67 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# [v0.0.39](https://github.com/opensafely-actions/cox-ipw/releases/tag/v0.0.39)
2+
3+
- Allow option `--covariate_other` to be specified as a filename of a text file of semi-colon separated variable names.
4+
- Updated the documentation for `--save_analysis_ready` to reflect that it saves a Stata dataset.
5+
16
# [v0.0.38](https://github.com/opensafely-actions/cox-ipw/releases/tag/v0.0.38)
27

38
- Minor updates to GitHub Actions workflows.

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ as follows:
5757
Variable name for the age covariate; specify argument as NULL to model without
5858
age covariate [default cov_num_age]
5959

60-
--covariate_other=VARNAME_1;VARNAME_2;...
61-
Semi-colon separated list of other covariates to be included in the regression
62-
model; specify argument as NULL to run age, age squared, sex adjusted model
63-
only [default
60+
--covariate_other=VARNAME_1;VARNAME_2;... OR COVARIATE-OTHER.TXT
61+
Semi-colon separated list of other covariates or filename of text file
62+
containing semi-colon separated list of other covariates to be included in the
63+
regression model; specify argument as NULL to run age, age squared, sex
64+
adjusted model only. Note that if you are including only a single covariate
65+
please include the semi-colon after it [default
6466
cov_cat_ethnicity;cov_num_consulation_rate;cov_bin_healthcare_worker;cov_bin_carehome_status]
6567

6668
--cox_start=VARNAME_1;VARNAME_2;...
6769
Semi-colon separated list of variable names used to define start of patient
6870
follow-up or single variable if already defined [default pat_index_date]
6971

7072
--cox_stop=VARNAME_1;VARNAME_2;...
71-
semicolon separated list of variable names used to define end of patient
73+
Semi-colon separated list of variable names used to define end of patient
7274
follow-up or single variable if already defined [default
7375
death_date;out_date_vte;vax_date_covid_1]
7476

@@ -109,9 +111,9 @@ as follows:
109111
--seed=INTEGER
110112
Random number generator seed passed to IPW sampling [default 137]
111113

112-
--save_analysis_ready=FILENAME.CSV
113-
If provided, analysis ready data csv filename (this is assumed to be within the
114-
output directory but can be within a subdirectory) [default ]
114+
--save_analysis_ready=FILENAME.DTA
115+
If provided, analysis ready Stata dataset filename (this is assumed to be
116+
within the output directory but can be within a subdirectory) [default ]
115117

116118
--run_analysis=TRUE/FALSE
117119
Logical, if analysis should be run [default TRUE]

analysis/cox-ipw.R

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ option_list <- list(
7070
"--covariate_other",
7171
type = "character",
7272
default = "cov_cat_ethnicity;cov_num_consulation_rate;cov_bin_healthcare_worker;cov_bin_carehome_status",
73-
help = "Semi-colon separated list of other covariates to be included in the regression model; specify argument as NULL to run age, age squared, sex adjusted model only [default %default]",
74-
metavar = "varname_1;varname_2;..."
73+
help = "Semi-colon separated list of other covariates or filename of text file containing semi-colon separated list of other covariates to be included in the regression model; specify argument as NULL to run age, age squared, sex adjusted model only. Note that if you are including only a single covariate please include the semi-colon after it [default %default]",
74+
metavar = "varname_1;varname_2;... or covariate-other.txt"
7575
),
7676
make_option(
7777
"--cox_start",
@@ -84,7 +84,7 @@ option_list <- list(
8484
"--cox_stop",
8585
type = "character",
8686
default = "death_date;out_date_vte;vax_date_covid_1",
87-
help = "semicolon separated list of variable names used to define end of patient follow-up or single variable if already defined [default %default]",
87+
help = "Semi-colon separated list of variable names used to define end of patient follow-up or single variable if already defined [default %default]",
8888
metavar = "varname_1;varname_2;..."
8989
),
9090
make_option(
@@ -161,8 +161,8 @@ option_list <- list(
161161
"--save_analysis_ready",
162162
type = "character",
163163
default = "",
164-
help = "If provided, analysis ready data csv filename (this is assumed to be within the output directory but can be within a subdirectory) [default %default]",
165-
metavar = "filename.csv"
164+
help = "If provided, analysis ready Stata dataset filename (this is assumed to be within the output directory but can be within a subdirectory) [default %default]",
165+
metavar = "filename.dta"
166166
),
167167
make_option(
168168
"--run_analysis",
@@ -206,7 +206,7 @@ print(record_args)
206206

207207
write.csv(
208208
record_args,
209-
file = paste0("output/", gsub(".csv","-args.csv",opt$df_output)),
209+
file = paste0("output/", gsub(".csv", "-args.csv", opt$df_output)),
210210
row.names = FALSE
211211
)
212212

@@ -224,6 +224,31 @@ lapply(
224224
source
225225
)
226226

227+
# Read in covariate_other if a filename ----
228+
# Assuming it's a filename if the character string contains no semi-colons
229+
# Note to future self/developer - could change the second condition here to look for say .txt in string
230+
if (!is.null(opt$covariate_other) && !grepl(";", opt$covariate_other)) {
231+
# Check if file exists
232+
if (!file.exists(opt$covariate_other)) {
233+
stop(paste("The file", opt$covariate_other, "does not exist, please check how you have specified the covariate_other option."))
234+
}
235+
236+
# Read in text file contents
237+
# Get the size of the file in bytes
238+
covariate_other_file_size <- file.info(opt$covariate_other)$size
239+
240+
# Read the entire file content as a single character string
241+
covariate_other_single_string <- readChar(opt$covariate_other, covariate_other_file_size)
242+
# Strip newlines from (likely the end of) string (but could be multi-line text file)
243+
covariate_other_single_string <- gsub("[\r\n]", "", covariate_other_single_string)
244+
245+
# Overwrite opt$covariate_other so it can be processed as the direct specification of this option
246+
opt$covariate_other <- covariate_other_single_string
247+
}
248+
249+
# If last character of opt$covariate_other is a ; replace with nothing
250+
opt$covariate_other <- trimws(opt$covariate_other, which = "right", whitespace = ";")
251+
227252
# Separate list arguments ------------------------------------------------------
228253
print("Separate list arguments")
229254

@@ -654,7 +679,7 @@ if (
654679

655680
results$strata_warning <- strata_warning
656681

657-
results$cox_ipw <- "v0.0.37"
682+
results$cox_ipw <- "v0.0.39"
658683

659684
results <- results[
660685
order(results$model),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file.copy("dummy_tables/covariate-other.txt", "output/covariate-other.txt")

dummy_tables/covariate-other.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cov_cat_ethnicity;cov_num_consulation_rate

justfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ test-4:
1313
opensafely run cox-ipw_using-args_input-arrow -f
1414
test-5:
1515
opensafely run cox-ipw_using-args_input-arrow-subdir -f
16+
test-6:
17+
opensafely run cox-ipw_covariate-other-filename -f

project.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,19 @@ actions:
7777
moderately_sensitive:
7878
arguments: output/results-5-args.csv
7979
estimates: output/results-5.csv
80+
81+
cox-ipw_move-covariate-other-filename:
82+
run: r:v2 analysis/move-covariate-other-txt.R
83+
outputs:
84+
moderately_sensitive:
85+
covariates: output/covariate-other.txt
86+
87+
cox-ipw_covariate-other-filename:
88+
run: r:v2 analysis/cox-ipw.R --df_input input.csv --covariate_other output/covariate-other.txt --df_output results-6.csv
89+
needs:
90+
- cox-ipw_move-covariate-other-filename
91+
- generate-dataset_csv
92+
outputs:
93+
moderately_sensitive:
94+
arguments: output/results-6-args.csv
95+
estimates: output/results-6.csv

0 commit comments

Comments
 (0)