Repository navigation
Expand file tree
/
Copy pathtlf-population.qmd
More file actions
236 lines (177 loc) · 6.05 KB
/
Copy pathtlf-population.qmd
File metadata and controls
236 lines (177 loc) · 6.05 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
title: "Study population"
---
::: callout-tip
## Objective
Create study population summary tables to document participant counts across different analysis populations.
Learn to use population flags in ADSL data and generate regulatory-compliant population tables with rtflite.
:::
## Overview
Clinical trials define multiple analysis populations based on different inclusion criteria. Following [ICH E3 guidance](https://database.ich.org/sites/default/files/E3_Guideline.pdf), regulatory submissions must clearly document the number of participants in each analysis population to support the validity of statistical analyses.
The key analysis populations typically include:
- **All Randomized**: Total participants who entered the study
- **Intent-to-Treat (ITT)**: Participants included in the primary efficacy analysis
- **Efficacy Population**: Participants who meet specific criteria for efficacy evaluation
- **Safety Population**: Participants who received at least one dose of study treatment
This tutorial shows you how to create a population summary table using Python's `rtflite` package.
<embed src="pdf/tlf_population.pdf" style="width:100%; height:600px" type="application/pdf">
```{python}
import polars as pl # Data manipulation
import rtflite as rtf # RTF reporting
```
```{python}
#| echo: false
# Configure Polars to show only first 5 rows
pl.Config.set_tbl_rows(5)
```
## Step 1: Load data
We start by loading the Subject-level Analysis Dataset (ADSL), which contains population flags for each participant.
```{python}
adsl = pl.read_parquet("data/adsl.parquet")
```
Let's examine the key population flag variables we'll use:
- **USUBJID**: Unique participant identifier
- **TRT01P**: Planned treatment group
- **ITTFL**: Intent-to-treat population flag (Y/N)
- **EFFFL**: Efficacy population flag (Y/N)
- **SAFFL**: Safety population flag (Y/N)
```{python}
adsl.select(["USUBJID", "TRT01P", "ITTFL", "EFFFL", "SAFFL"])
```
## Step 2: Calculate treatment group totals
First, we calculate the total number of randomized participants in each treatment group, which will serve as the denominator for percentage calculations.
```{python}
totals = adsl.group_by("TRT01P").agg(
total = pl.len()
)
totals
```
## Step 3: Define helper function
We create a reusable function to count participants by treatment group for any population subset.
```{python}
def count_by_treatment(data, population_name):
"""Count participants by treatment group and add population label"""
return data.group_by("TRT01P").agg(
n = pl.len()
).with_columns(
population = pl.lit(population_name)
)
```
## Step 4: Count each population
Now we calculate participant counts for each analysis population.
### All randomized participants
```{python}
pop_all = count_by_treatment(
data=adsl,
population_name="Participants in population"
)
pop_all
```
### Intent-to-treat population
```{python}
adsl_itt = adsl.filter(pl.col("ITTFL") == "Y")
pop_itt = count_by_treatment(
data=adsl_itt,
population_name="Participants included in ITT population"
)
pop_itt
```
### Efficacy population
```{python}
adsl_eff = adsl.filter(pl.col("EFFFL") == "Y")
pop_eff = count_by_treatment(
data=adsl_eff,
population_name="Participants included in efficacy population"
)
pop_eff
```
### Safety population
```{python}
adsl_saf = adsl.filter(pl.col("SAFFL") == "Y")
pop_saf = count_by_treatment(
data=adsl_saf,
population_name="Participants included in safety population"
)
pop_saf
```
## Step 5: Combine all populations
We stack all population counts together into a single dataset.
```{python}
all_populations = pl.concat([
pop_all,
pop_itt,
pop_eff,
pop_saf
])
all_populations
```
## Step 6: Calculate percentages
We join with the total counts and calculate what percentage each population represents of the total randomized participants.
```{python}
stats_with_pct = all_populations.join(
totals,
on="TRT01P"
).with_columns(
pct = (100.0 * pl.col("n") / pl.col("total")).round(1)
)
stats_with_pct
```
## Step 7: Format display values
For the final table, we format the display text. The total randomized count shows just "N", while subset populations show "N (%)".
```{python}
formatted_stats = stats_with_pct.with_columns(
display = pl.when(pl.col("population") == "Participants in population")
.then(pl.col("n").cast(str))
.otherwise(
pl.concat_str([
pl.col("n").cast(str),
pl.lit(" ("),
pl.col("pct").round(1).cast(str),
pl.lit(")")
])
)
)
formatted_stats
```
## Step 8: Create final table
We reshape the data from long format (rows for each treatment-population combination) to wide format (columns for each treatment group).
```{python}
df_overview = formatted_stats.pivot(
values="display",
index="population",
on="TRT01P",
maintain_order=True
).select(
["population", "Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"]
)
df_overview
```
## Step 9: Generate publication-ready output
Finally, we format the population table for regulatory submission using the `rtflite` package.
```{python}
doc_overview = rtf.RTFDocument(
df=df_overview,
rtf_title=rtf.RTFTitle(
text=["Analysis Population", "All Participants Randomized"]
),
rtf_column_header=rtf.RTFColumnHeader(
text=["", "Placebo\nn (%)", "Xanomeline Low Dose\nn (%)", "Xanomeline High Dose\nn (%)"],
col_rel_width=[4, 2, 2, 2],
text_justification=["l", "c", "c", "c"],
),
rtf_body=rtf.RTFBody(
col_rel_width=[4, 2, 2, 2],
text_justification=["l", "c", "c", "c"],
),
rtf_source=rtf.RTFSource(text=["Source: ADSL dataset"])
)
doc_overview.write_rtf("rtf/tlf_population.rtf")
```
```{python}
#| echo: false
# Convert RTF to PDF
from rtflite import LibreOfficeConverter
converter = LibreOfficeConverter()
converter.convert("rtf/tlf_population.rtf", output_dir="pdf/", format="pdf", overwrite=True)
```
<embed src="pdf/tlf_population.pdf" style="width:100%; height:600px" type="application/pdf">