# Program Name: tsiex01.R# Prep Environmentlibrary(envsetup)library(tern)library(dplyr)library(rtables)library(junco)# Define script level parameters:# - Define output ID and file locationtblid <-"TSIEX01"fileid <- tblidtab_titles <-get_titles_from_file(input_path ='../../_data/', tblid)string_map <- default_str_maptrtvar <-"TRT01A"popfl <-"SAFFL"ctrl_grp <-"Placebo"comp_btw_group <-TRUEancova <-TRUEtrtdur <-"TRTDURM"daysconv <-30.4375catlevels <-c("0 to <3 months","3 to <6 months","6 to <9 months","9 to <12 months","12 to <15 months","15 to <18 months","18 to <21 months","21 to <24 months","24 to <27 months","27 to <30 months","30 to <33 months","33 to <36 months","36 to <39 months")combined_colspan_trt <-TRUEif (combined_colspan_trt ==TRUE) {# Set up levels and label for the required combined columns add_combo <-add_combo_facet("Combined",label ="Combined",levels =c("Xanomeline High Dose", "Xanomeline Low Dose") )# choose if any facets need to be removed - e.g remove the combined column for placebo rm_combo_from_placebo <-cond_rm_facets(facets ="Combined",ancestor_pos =NA,value =" ",split ="colspan_trt" ) mysplit <-make_split_fun(post =list(add_combo, rm_combo_from_placebo))}# Process Data:# Read in required dataadsl <- pharmaverseadamjnj::adsl %>%filter(!!rlang::sym(popfl) =="Y") %>%select(STUDYID, USUBJID, all_of(trtvar), all_of(popfl))# If AVISIT is not present in ADEXSUM than create 'Overall' as the Visit, which is used# for the filtering AVISIT records if it does existadexsum <- pharmaverseadamjnj::adexsum %>%mutate(VISIT =if (exists("AVISIT")) AVISIT else"Overall") %>%filter(PARAMCD == trtdur &!is.na(AVAL) & VISIT =="Overall") %>%mutate(CRIT0FL ="Y",CRIT0 =as.factor("Any duration (at least 1 dose)") ) %>%select(USUBJID, PARAMCD, AVAL, AVALCAT1, starts_with("CRIT"))adexsum$CRIT0FL <-factor(adexsum$CRIT0FL, levels =c("Y", "N"))adsl$colspan_trt <-factor(ifelse(adsl[[trtvar]] =="Placebo", " ", "Active Study Agent"),levels =c("Active Study Agent", " "))adsl$diff_header <-"Difference in Means (95% CI)"adsl$diff_label <-paste(adsl[[trtvar]], paste("vs", ctrl_grp))# join data togetherex <- adexsum %>%inner_join(., adsl, by =c("USUBJID"))# Keep only columns with some data in which will remove any unwanted CRITy variablesex <- ex[, colSums(is.na(ex)) <nrow(ex)]# Work out how many CRITy vars (ignoring CRIT0 we created) we have leftexcritvars <- ex %>%select(num_range("CRIT", 1:99))countcritvars <-length(names(excritvars))# Drop unwanted levels for all CRITy variables you have remaining in ex and also for AVALCAT1for (i in1:countcritvars) { variable_name <-paste0("CRIT", i) ex[[variable_name]] <-droplevels(ex[[variable_name]])}# drop unwanted levels from AVALCAT1 and assign levels from specified section at the top of scriptex$AVALCAT1 <-droplevels(ex$AVALCAT1)ex$AVALCAT1 <-factor(ex$AVALCAT1, levels = catlevels)colspan_trt_map <-create_colspan_map( adsl,non_active_grp = ctrl_grp,non_active_grp_span_lbl =" ",active_grp_span_lbl ="Active Study Agent",colspan_var ="colspan_trt",trt_var = trtvar)ref_path <-c("colspan_trt", "", trtvar, ctrl_grp)# Create label_map to be utilized in the Criterion variables (CRIT)ex_lblmap <- ex %>%select(-CRIT0, -CRIT0FL) %>% tidyr::pivot_longer(cols =starts_with("CRIT") &ends_with("FL"),names_to ="value",values_to ="CF_value" ) %>%# Create the label by selecting the corresponding values from the CRIT columnsmutate(label =case_when( value =="CRIT1FL"~ CRIT1, value =="CRIT2FL"~ CRIT2, value =="CRIT3FL"~ CRIT3, value =="CRIT4FL"~ CRIT4, value =="CRIT5FL"~ CRIT5, value =="CRIT6FL"~ CRIT6, value =="CRIT7FL"~ CRIT7 ) ) %>%select(value, label) %>%distinct()# Define layout and build table:extra_args_rr <-list(.stats =c("count_unique_fraction"), riskdiff =FALSE)lyt <- rtables::basic_table(top_level_section_div =" ",show_colcounts =TRUE,colcount_format ="N=xx") %>%split_cols_by("colspan_trt",split_fun =trim_levels_to_map(map = colspan_trt_map) )if (combined_colspan_trt ==TRUE) { lyt <- lyt %>%split_cols_by(trtvar, split_fun = mysplit)} else { lyt <- lyt %>%split_cols_by(trtvar)}lyt <- lyt %>%split_cols_by("diff_header", nested =FALSE) %>%split_cols_by( trtvar,split_fun =remove_split_levels(ctrl_grp),labels_var ="diff_label" ) %>%analyze("AVAL",afun = a_summarize_ex_j,var_labels ="Duration~[super a], ([unit])", #TODO FIX THISshow_labels ="visible",indent_mod =0L,extra_args =list(daysconv = daysconv,ref_path = ref_path,ancova = ancova,comp_btw_group = comp_btw_group,variables =list(arm = trtvar, covariates =NULL),.formats =c(range =jjcsformat_xx("xx.x, xx.x"),"diff_mean_est_ci"=jjcsformat_xx("xx.xx (xx.xx, xx.xx)") ) ) ) %>%analyze("AVALCAT1",var_labels ="Duration of treatment, n (%)",afun = a_freq_j,extra_args = extra_args_rr,indent_mod =1L,show_labels ="visible",nested =FALSE ) %>%analyze(paste0("CRIT", 1, "FL"),var_labels ="Duration~[super b], n (%)", #TODO FIX THISafun = a_freq_j,extra_args =append( extra_args_rr,list(label =as.character(ex_lblmap$label[1]), val ="Y") ),indent_mod =1L,show_labels ="visible",nested =FALSE )# Add in analyze for all others CRIT variables contained in exfor (i in2:countcritvars) { lyt <- lyt %>%analyze(paste0("CRIT", i, "FL"),afun = a_freq_j,extra_args =append( extra_args_rr,list(label =as.character(ex_lblmap$label[i]), val ="Y") ),indent_mod =2L,show_labels ="hidden" )}lyt <- lyt %>%append_topleft("Parameter")result <-build_table(lyt, ex, alt_counts_df = adsl)## Remove the N=xx column headers for the Difference (95% CI) columnsresult <-remove_col_count(result, span_label_var ="diff_header")# Add titles and footnotes:result <-set_titles(result, tab_titles)# Convert to tbl file and output tablett_to_tlgrtf(string_map = string_map, tt = result, file = fileid, orientation ="landscape")
TSIEX01:Duration of Treatment; Safety Analysis Set (Study jjcs - core)
Active Study Agent
Difference in Means (95% CI)
Xanomeline High Dose
Xanomeline Low Dose
Combined
Placebo
Xanomeline High Dose vs Placebo
Xanomeline Low Dose vs Placebo
Parameter
N=53
N=73
N=126
N=59
Durationa, ([unit])
Mean (SD)
17.41 (11.408)
20.46 (10.198)
19.18 (10.786)
18.64 (10.656)
-1.23 (-5.23, 2.77)
1.82 (-1.88, 5.52)
Median
16.10
21.20
18.90
18.60
Min, max
0.2, 35.4
0.1, 37.9
0.1, 37.9
0.9, 37.2
Interquartile range
8.30, 27.80
14.70, 28.10
11.30, 28.00
9.30, 26.30
Total treatment (subject years)
922.9 (76.9)
1493.8 (124.5)
2416.7 (201.4)
1099.9 (91.7)
Duration of treatment, n (%)
0 to <3 months
7 (13.2%)
4 (5.5%)
11 (8.7%)
2 (3.4%)
3 to <6 months
5 (9.4%)
5 (6.8%)
10 (7.9%)
5 (8.5%)
6 to <9 months
3 (5.7%)
3 (4.1%)
6 (4.8%)
7 (11.9%)
9 to <12 months
4 (7.5%)
2 (2.7%)
6 (4.8%)
7 (11.9%)
12 to <15 months
6 (11.3%)
8 (11.0%)
14 (11.1%)
5 (8.5%)
15 to <18 months
3 (5.7%)
10 (13.7%)
13 (10.3%)
3 (5.1%)
18 to <21 months
5 (9.4%)
4 (5.5%)
9 (7.1%)
5 (8.5%)
21 to <24 months
1 (1.9%)
7 (9.6%)
8 (6.3%)
5 (8.5%)
24 to <27 months
3 (5.7%)
6 (8.2%)
9 (7.1%)
6 (10.2%)
27 to <30 months
6 (11.3%)
9 (12.3%)
15 (11.9%)
2 (3.4%)
30 to <33 months
5 (9.4%)
4 (5.5%)
9 (7.1%)
3 (5.1%)
33 to <36 months
5 (9.4%)
9 (12.3%)
14 (11.1%)
5 (8.5%)
36 to <39 months
0
2 (2.7%)
2 (1.6%)
4 (6.8%)
Durationb, n (%)
<1 month
4 (7.5%)
1 (1.4%)
5 (4.0%)
1 (1.7%)
≥1 month
49 (92.5%)
72 (98.6%)
121 (96.0%)
58 (98.3%)
≥2 months
46 (86.8%)
71 (97.3%)
117 (92.9%)
58 (98.3%)
≥3 months
46 (86.8%)
69 (94.5%)
115 (91.3%)
57 (96.6%)
≥6 months
41 (77.4%)
64 (87.7%)
105 (83.3%)
52 (88.1%)
≥12 months
34 (64.2%)
59 (80.8%)
93 (73.8%)
38 (64.4%)
≥24 months
19 (35.8%)
30 (41.1%)
49 (38.9%)
20 (33.9%)
a Duration of treatment is defined as [(date of last dose of study treatment – date of first dose of study treatment) +1].
b Subjects are summarized in all applicable duration categories.