-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from openpharma/3
Simulation vignette and brm_data_change()
- Loading branch information
Showing
14 changed files
with
1,355 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#' @title Convert to change from baseline. | ||
#' @export | ||
#' @family data | ||
#' @description Convert a dataset from raw response to change from baseline. | ||
#' @return A classed `tibble` with change from baseline as the outcome variable | ||
#' and the internal attributes modified accordingly. A special baseline | ||
#' column is also created, and the original raw response column is removed. | ||
#' @param data A classed `tibble` (e.g. from [brm_data()]) with raw response | ||
#' as the outcome variable (role = `"response"` in [brm_data()]). | ||
#' @param name_change Character of length 1, name of the new outcome column | ||
#' for change from baseline. | ||
#' @param name_baseline Character of length 1, name of the new column for | ||
#' the original baseline response. | ||
#' @examples | ||
#' set.seed(0) | ||
#' data <- brm_data( | ||
#' data = dplyr::rename(brm_simulate_simple()$data, y_values = response), | ||
#' outcome = "y_values", | ||
#' role = "response", | ||
#' group = "group", | ||
#' time = "time", | ||
#' patient = "patient", | ||
#' level_control = "group_1", | ||
#' level_baseline = "time_1" | ||
#' ) | ||
#' data | ||
#' attr(data, "brm_role") | ||
#' attr(data, "brm_outcome") | ||
#' attr(data, "brm_baseline") | ||
#' attr(data, "brm_level_baseline") | ||
#' changed <- brm_data_change(data = data, name_change = "delta") | ||
#' changed | ||
#' attr(changed, "brm_role") | ||
#' attr(changed, "brm_outcome") | ||
#' attr(changed, "brm_baseline") | ||
#' attr(data, "brm_level_baseline") | ||
brm_data_change <- function( | ||
data, | ||
name_change = "change", | ||
name_baseline = "baseline" | ||
) { | ||
brm_data_validate(data) | ||
assert( | ||
attr(data, "brm_role") == "response", | ||
message = paste( | ||
"outcome variable must be raw response", | ||
"(not change from baseline)", | ||
"in the data supplied to brm_data_change()." | ||
) | ||
) | ||
assert_chr(name_change) | ||
assert_chr(name_baseline) | ||
assert( | ||
!any(c(name_change, name_baseline) %in% colnames(data)), | ||
message = paste( | ||
"name_change and name_baseline must", | ||
"not already be columns in the data.", | ||
"Choose different values for these arguments of brm_data_change()." | ||
) | ||
) | ||
name_time <- attr(data, "brm_time") | ||
level_baseline <- attr(data, "brm_level_baseline") | ||
name_response <- attr(data, "brm_outcome") | ||
data_baseline <- data[data[[name_time]] == level_baseline, ] | ||
data_after <- data[data[[name_time]] != level_baseline, ] | ||
data_baseline[[name_baseline]] <- data_baseline[[name_response]] | ||
data_after[[name_change]] <- data_after[[name_response]] | ||
data_baseline[[name_response]] <- NULL | ||
data_baseline[[name_time]] <- NULL | ||
data_after[[name_response]] <- NULL | ||
out <- dplyr::left_join( | ||
x = data_after, | ||
y = data_baseline, | ||
by = intersect(colnames(data_after), colnames(data_baseline)) | ||
) | ||
out[[name_change]] <- out[[name_change]] - out[[name_baseline]] | ||
brm_data( | ||
data = out, | ||
outcome = name_change, | ||
role = "change", | ||
baseline = name_baseline, | ||
group = attr(data, "brm_group"), | ||
time = name_time, | ||
patient = attr(data, "brm_patient"), | ||
covariates = attr(data, "brm_covariates"), | ||
missing = attr(data, "brm_missing"), | ||
level_control = attr(data, "brm_level_control"), | ||
level_baseline = NULL | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
test_that("brm_data_change()", { | ||
set.seed(0) | ||
data <- brm_data( | ||
data = dplyr::rename(brm_simulate_simple()$data, y_values = response), | ||
outcome = "y_values", | ||
role = "response", | ||
group = "group", | ||
time = "time", | ||
patient = "patient", | ||
level_control = "group_1", | ||
level_baseline = "time_1" | ||
) | ||
expect_equal(attr(data, "brm_role"), "response") | ||
expect_equal(attr(data, "brm_outcome"), "y_values") | ||
expect_null(attr(data, "brm_baseline")) | ||
expect_equal(attr(data, "brm_level_baseline"), "time_1") | ||
changed <- brm_data_change( | ||
data = data, | ||
name_change = "delta", | ||
name_baseline = "base" | ||
) | ||
expect_equal(attr(changed, "brm_role"), "change") | ||
expect_equal(attr(changed, "brm_outcome"), "delta") | ||
expect_equal(attr(changed, "brm_baseline"), "base") | ||
expect_null(attr(changed, "brm_level_baseline")) | ||
data_baseline <- dplyr::filter(data, time == "time_1") | ||
data_after <- dplyr::filter(data, time != "time_1") | ||
for (point in setdiff(unique(data$time), "time_1")) { | ||
expect_equal( | ||
as.numeric(changed[changed$time == point, ]$base), | ||
as.numeric(data[data$time == "time_1", ]$y_values) | ||
) | ||
y_post <- data[data$time == point, ]$y_values | ||
y_base <- data[data$time == "time_1", ]$y_values | ||
expect_equal( | ||
as.numeric(changed[changed$time == point, ]$delta), | ||
as.numeric(y_post - y_base) | ||
) | ||
} | ||
}) | ||
|
||
test_that("brm_data_change() assertions", { | ||
set.seed(0) | ||
data <- brm_data( | ||
data = dplyr::rename(brm_simulate_simple()$data, y_values = response), | ||
outcome = "y_values", | ||
role = "response", | ||
group = "group", | ||
time = "time", | ||
patient = "patient", | ||
level_control = "group_1", | ||
level_baseline = "time_1" | ||
) | ||
expect_equal(attr(data, "brm_role"), "response") | ||
expect_equal(attr(data, "brm_outcome"), "y_values") | ||
expect_null(attr(data, "brm_baseline")) | ||
expect_equal(attr(data, "brm_level_baseline"), "time_1") | ||
already_changed <- brm_data_change( | ||
data = data, | ||
name_change = "delta", | ||
name_baseline = "base" | ||
) | ||
expect_error( | ||
brm_data_change(already_changed, name_change = "abc", name_baseline = "b"), | ||
class = "brm_error" | ||
) | ||
expect_error( | ||
brm_data_change(data, name_change = "time", name_baseline = "new_column"), | ||
class = "brm_error" | ||
) | ||
expect_error( | ||
brm_data_change(data, name_change = "new_column", name_baseline = "time"), | ||
class = "brm_error" | ||
) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.