/
cs_statistical.R
345 lines (311 loc) · 12.2 KB
/
cs_statistical.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#'Statistical Analysis of Clinical Significance
#'
#'@description `cs_statistical()` can be used to determine the clinical
#' significance of intervention studies employing the statistical approach. For
#' this, it will be assumed that the functional (non-clinical population) and
#' patient (clinical population) scores form two distinct distributions on a
#' continuum. `cs_statistical()` calculates a cutoff point between these two
#' populations and counts, how many patients changed from the clinical to the
#' functional population during intervention. Several methods for calculating
#' this cutoff are available.
#'
#'@section Computational details: There are three available cutoff types, namely
#' a, b, and c which can be used to "draw a line" or separate the functional
#' and clinical population on a continuum. a as a cutoff is defined as the mean
#' of the clinical population minus two times the standard deviation (SD) of
#' the clinical population. b is defined as the mean of the functional
#' population plus also two times the SD of the clinical population. This is
#' true for "negative" outcomes, where a lower instrument score is desirable.
#' For "positive" outcomes, where higher scores are beneficial, a is the mean
#' of the clinical population plus 2 \eqn{\cdot} SD of the clinical population
#' and b is mean of the functional population minus 2 \eqn{\cdot} SD of the
#' clinical population. The summary statistics for the clinical population are
#' estimated from the provided data at pre measurement.
#'
#' c is defined as the midpoint between both populations based on their
#' respective mean and SD. In order to calculate b and c, descriptive
#' statistics for the functional population must be provided.
#'
#'@section Categories: Individual patients can be categorized into one of the
#' following groups:
#' - Improved, i.e., one changed from the clinical to the functional population
#' - Unchanged, i.e., one can be seen as a member of the same population pre
#' and post intervention
#' - Deteriorated, i.e., one changed from the functional to the clinical
#' population during intervention
#'
#'
#'@inheritSection cs_distribution Data preparation
#'
#'@inheritParams cs_distribution
#'@param m_functional Numeric, mean of functional population.
#'@param sd_functional Numeric, standard deviation of functional population
#'@param cutoff_method Cutoff method, Available are
#' - `"JT"` (Jacobson & Truax, 1991, the default)
#' - `"HA"` (Hageman & Arrindell, 1999)
#'@param cutoff_type Cutoff type. Available are `"a"`, `"b"`, and `"c"`.
#' Defaults to `"a"` but `"c"` is usually recommended. For `"b"` and `"c"`,
#' summary data from a functional population must be given with arguments
#' `m_functional` and `sd_functional`.
#'
#'
#'@references
#' - Jacobson, N. S., & Truax, P. (1991). Clinical significance: A statistical approach to defining meaningful change in psychotherapy research. Journal of Consulting and Clinical Psychology, 59(1), 12–19. https://doi.org/10.1037//0022-006X.59.1.12
#' - Hageman, W. J., & Arrindell, W. A. (1999). Establishing clinically significant change: increment of precision and the distinction between individual and group level analysis. Behaviour Research and Therapy, 37(12), 1169–1193. https://doi.org/10.1016/S0005-7967(99)00032-7
#'
#'@family main
#'
#'@return An S3 object of class `cs_analysis` and `cs_statistical`
#'@export
#'
#' @examples
#' # By default, cutoff type "a" is used
#' cs_results <- claus_2020 |>
#' cs_statistical(id, time, hamd, pre = 1, post = 4)
#'
#' cs_results
#' summary(cs_results)
#' plot(cs_results)
#'
#'
#' # You can choose a different cutoff type but need to provide additional
#' # population summary statistics for the functional population
#' cs_results_c <- claus_2020 |>
#' cs_statistical(
#' id,
#' time,
#' hamd,
#' pre = 1,
#' post = 4,
#' m_functional = 8,
#' sd_functional = 8,
#' cutoff_type = "c"
#' )
#'
#' cs_results_c
#' summary(cs_results_c)
#' plot(cs_results_c)
#'
#'
#' # You can use a different method to calculate the cutoff
#' cs_results_ha <- claus_2020 |>
#' cs_statistical(
#' id,
#' time,
#' hamd,
#' pre = 1,
#' post = 4,
#' m_functional = 8,
#' sd_functional = 8,
#' reliability = 0.80,
#' cutoff_type = "c",
#' cutoff_method = "HA"
#' )
#'
#' cs_results_ha
#' summary(cs_results_ha)
#' plot(cs_results_ha)
#'
#'
#' # And you can group the analysis by providing a grouping variable from the data
#' cs_results_grouped <- claus_2020 |>
#' cs_statistical(
#' id,
#' time,
#' hamd,
#' pre = 1,
#' post = 4,
#' m_functional = 8,
#' sd_functional = 8,
#' cutoff_type = "c",
#' group = treatment
#' )
#'
#' cs_results_grouped
#' summary(cs_results_grouped)
#' plot(cs_results_grouped)
cs_statistical <- function(data,
id,
time,
outcome,
group = NULL,
pre = NULL,
post = NULL,
m_functional = NULL,
sd_functional = NULL,
reliability = NULL,
better_is = c("lower", "higher"),
cutoff_method = c("JT", "HA"),
cutoff_type = c("a", "b", "c"),
significance_level = 0.05) {
# Check arguments
cs_method <- rlang::arg_match(cutoff_method)
cut_type <- rlang::arg_match(cutoff_type)
if (missing(id)) cli::cli_abort("Argument {.code id} is missing with no default. A column containing patient-specific IDs must be supplied.")
if (missing(time)) cli::cli_abort("Argument {.code time} is missing with no default. A column identifying the individual measurements must be supplied.")
if (missing(outcome)) cli::cli_abort("Argument {.code outcome} is missing with no default. A column containing the outcome must be supplied.")
if (cs_method == "HA") {
if (is.null(reliability)) cli::cli_abort("Argument {.code reliability} is missing with no default. An instrument reliability must be supplied.")
if (!is.null(reliability) & !is.numeric(reliability)) cli::cli_abort("{.code reliability} must be numeric but a {.code {typeof(reliability)}} was supplied.")
if (!is.null(reliability) & !dplyr::between(reliability, 0, 1)) cli::cli_abort("{.code reliability} must be between 0 and 1 but {reliability} was supplied.")
} else {
if (!is.null(reliability)) cli::cli_alert_info("A reliability for the JT approach to calculating a population cutoff is not needed and will be ignored.")
}
if (cut_type %in% c("b", "c")) {
if (is.null(m_functional) | is.null(sd_functional)) cli::cli_abort("For cutoffs {.code b} and {.code c}, mean and standard deviation for a functional population must be provided via {.code m_functional} and {.code sd_functional}")
if ((!is.null(m_functional) & !is.numeric(m_functional)) | (!is.null(sd_functional) & !is.numeric(sd_functional))) cli::cli_abort("The mean and standard deviation supplied with {.code m_functional} and {.code sd_functional} must be numeric.")
}
# Prepare the data
datasets <- .prep_data(
data = data,
id = {{ id }},
time = {{ time }},
outcome = {{ outcome }},
group = {{ group }},
pre = {{ pre }},
post = {{ post }},
method = cs_method
)
# Prepend a class to enable method dispatch for RCI calculation
class(datasets) <- c(paste0("cs_", tolower(cs_method)), class(datasets))
# Count participants
n_obs <- list(
n_original = nrow(datasets[["wide"]]),
n_used = nrow(datasets[["data"]])
)
# Calculate relevant summary statistics for the chosen RCI method
m_pre <- mean(datasets[["data"]][["pre"]])
sd_pre <- stats::sd(datasets[["data"]][["pre"]])
if (cs_method %in% c("HLL", "HA")) {
m_post <- mean(datasets[["data"]][["post"]])
sd_post <- stats::sd(datasets[["data"]][["post"]])
}
# Get the direction of a beneficial intervention effect
if (rlang::arg_match(better_is) == "lower") direction <- -1 else direction <- 1
# Determine critical RCI value based on significance level
if (cs_method != "HA") critical_value <- stats::qnorm(1 - significance_level/2) else critical_value <- stats::qnorm(1 - significance_level)
# Calculate the cutoff value and check each patient's change relative to it
cutoff_results <- calc_cutoff_from_data(
data = datasets,
m_clinical = m_pre,
sd_clinical = sd_pre,
m_functional = m_functional,
sd_functional = sd_functional,
m_post = m_post,
sd_post = sd_post,
reliability = reliability,
type = cut_type,
direction = direction,
critical_value = critical_value
)
# Create the summary table for printing and exporting
summary_table <- create_summary_table(
x = cutoff_results,
data = datasets,
method = cs_method
)
class(cutoff_results) <- "list"
# Put everything into a list
output <- list(
datasets = datasets,
cutoff_results = cutoff_results,
outcome = deparse(substitute(outcome)),
n_obs = n_obs,
method = cs_method,
reliability = reliability,
critical_value = critical_value,
summary_table = summary_table
)
# Return output
class(output) <- c("cs_analysis", "cs_statistical", class(datasets), class(output))
output
}
#' Print Method for the Statistical Approach
#'
#' @param x An object of class `cs_distribution`
#' @param ... Additional arguments
#'
#' @return No return value, called for side effects
#' @export
#'
#' @examples
#' cs_results <- claus_2020 |>
#' cs_statistical(
#' id,
#' time,
#' hamd,
#' pre = 1,
#' post = 4,
#' m_functional = 8,
#' sd_functional = 7
#' )
#'
#' cs_results
print.cs_statistical <- function(x, ...) {
summary_table <- x[["summary_table"]]
cs_method <- x[["method"]]
summary_table_formatted <- summary_table |>
dplyr::rename_with(tools::toTitleCase)
# Print output
output_fun <- function() {
cli::cli_h2("Clinical Significance Results")
cli::cli_text("Statistical approach using the {.strong {cs_method}} method.")
cli::cat_line()
cli::cli_verbatim(insight::export_table(summary_table_formatted))
}
output_fun()
}
#' Summary Method for the Statistical Approach
#'
#' @param object An object of class `cs_distribution`
#' @param ... Additional arguments
#'
#' @return No return value, called for side effects only
#' @export
#'
#' @examples
#' cs_results <- claus_2020 |>
#' cs_statistical(
#' id,
#' time,
#' hamd,
#' pre = 1,
#' post = 4,
#' m_functional = 8,
#' sd_functional = 7
#' )
#'
#' summary(cs_results)
summary.cs_statistical <- function(object, ...) {
# Get necessary information from object
summary_table <- object[["summary_table"]]
summary_table_formatted <- summary_table |>
dplyr::rename_with(tools::toTitleCase)
cs_method <- object[["method"]]
n_original <- cs_get_n(object, "original")[[1]]
n_used <- cs_get_n(object, "used")[[1]]
pct <- round(n_used / n_original, digits = 3) * 100
cutoff_info <- cs_get_cutoff(object, with_descriptives = TRUE)
cutoff_type <- cutoff_info[["type"]]
cutoff_value <- round(cutoff_info[["value"]], 2)
cutoff_descriptives <- cutoff_info[, 1:4] |>
dplyr::rename("M Clinical" = "m_clinical", "SD Clinical" = "sd_clinical", "M Functional" = "m_functional", "SD Functional" = "sd_functional") |>
insight::export_table(missing = "---", )
outcome <- object[["outcome"]]
# Print output
output_fun <- function() {
cli::cli_h2("Clinical Significance Results")
cli::cli_text("Statistical approach of clinical significance using the {.strong {cs_method}} method for calculating the population cutoff.")
cli::cat_line()
cli::cli_text("There were {.strong {n_original}} participants in the whole dataset of which {.strong {n_used}} {.strong ({pct}%)} could be included in the analysis.")
cli::cat_line()
cli::cli_text("The cutoff type was {.strong {cutoff_type}} with a value of {.strong {cutoff_value}} based on the following sumamry statistics:")
cli::cat_line()
cli::cli_h3("Population Characteristics")
cli::cli_verbatim(cutoff_descriptives)
cli::cat_line()
cli::cli_h3("Individual Level Results")
cli::cli_verbatim(insight::export_table(summary_table_formatted))
}
output_fun()
}