-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54073bf
commit 5f7a881
Showing
24 changed files
with
851 additions
and
2 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
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,114 @@ | ||
#' Register `mmrm` For Use With `car::Anova` | ||
#' | ||
#' @inheritParams base::requireNamespace | ||
#' @return A logical value indicating whether registration was successful. | ||
#' | ||
#' @keywords internal | ||
car_add_mmrm <- function(quietly = FALSE) { | ||
if (!requireNamespace("car", quietly = quietly)) { | ||
return(FALSE) | ||
} | ||
envir <- asNamespace("mmrm") | ||
h_register_s3("car", "Anova", "mmrm", envir) | ||
TRUE | ||
} | ||
|
||
|
||
#' Obtain Contrast for Specified Effect | ||
#' | ||
#' This is support function to obtain contrast matrix for type II/III testing. | ||
#' | ||
#' @param object (`mmrm`)\cr the fitted MMRM. | ||
#' @param effect (`string`) the name of the effect. | ||
#' @param type (`string`) type of test, "II", "III", '2', or '3'. | ||
#' @param tol (`numeric`) threshold blow which values are treated as 0. | ||
#' | ||
#' @return A `matrix` of the contrast. | ||
#' | ||
#' @keywords internal | ||
h_get_contrast <- function(object, effect, type = c("II", "III", "2", "3"), tol = sqrt(.Machine$double.eps)) { | ||
assert_class(object, "mmrm") | ||
assert_string(effect) | ||
assert_double(tol, finite = TRUE, len = 1L) | ||
type <- match.arg(type) | ||
mx <- component(object, "x_matrix") | ||
asg <- attr(mx, "assign") | ||
formula <- object$formula_parts$model_formula | ||
tms <- terms(formula) | ||
fcts <- attr(tms, "factors")[-1L, , drop = FALSE] # Discard the response. | ||
ods <- attr(tms, "order") | ||
assert_subset(effect, colnames(fcts)) | ||
idx <- which(effect == colnames(fcts)) | ||
cols <- which(asg == idx) | ||
data <- component(object, "full_frame") | ||
var_numeric <- vapply(data, is.numeric, FUN.VALUE = TRUE) | ||
coef_rows <- length(cols) | ||
l_mx <- matrix(0, nrow = coef_rows, ncol = length(asg)) | ||
if (coef_rows == 0L) { | ||
return(l_mx) | ||
} | ||
l_mx[, cols] <- diag(rep(1, coef_rows)) | ||
for (i in setdiff(seq_len(ncol(fcts)), idx)) { | ||
x1 <- mx[, cols, drop = FALSE] | ||
additional_vars <- names(which(fcts[, i] > fcts[, idx])) | ||
additional_numeric <- any(var_numeric[additional_vars]) | ||
current_col <- which(asg == i) | ||
if (ods[i] >= ods[idx] && all(fcts[, i] >= fcts[, idx]) && !additional_numeric) { | ||
sub_mat <- switch(type, | ||
"2" = , | ||
"II" = { | ||
x0 <- mx[, -c(cols, current_col), drop = FALSE] | ||
x2 <- mx[, current_col, drop = FALSE] | ||
m <- diag(rep(1, nrow(x0))) - x0 %*% solve(t(x0) %*% x0) %*% t(x0) | ||
solve(t(x1) %*% m %*% x1) %*% t(x1) %*% m %*% x2 | ||
}, | ||
"3" = , | ||
"III" = { | ||
additional_levels <- vapply(data[additional_vars], function(x) nlevels(x), FUN.VALUE = 1L) | ||
t_levels <- prod(additional_levels) | ||
l_mx[, cols] / t_levels | ||
} | ||
) | ||
l_mx[, current_col] <- sub_mat | ||
} | ||
} | ||
l_mx[abs(l_mx) < tol] <- 0 | ||
l_mx | ||
} | ||
|
||
#' Conduct type II/III hypothesis testing on the MMRM fit results. | ||
#' | ||
#' @param mod (`mmrm`)\cr the fitted MMRM. | ||
#' @param ... not used. | ||
#' @inheritParams h_get_contrast | ||
#' | ||
#' @details | ||
#' `Anova` will return `anova` object with one row per variable and columns | ||
#' `Num Df`(numerator degrees of freedom), `Denom Df`(denominator degrees of freedom), | ||
#' `F Statistic` and `Pr(>=F)`. | ||
#' | ||
#' @keywords internal | ||
# Please do not load `car` and then create the documentation. The Rd file will be different. | ||
Anova.mmrm <- function(mod, type = c("II", "III", "2", "3"), tol = sqrt(.Machine$double.eps), ...) { # nolint | ||
assert_double(tol, finite = TRUE, len = 1L) | ||
type <- match.arg(type) | ||
vars <- colnames(attr(terms(mod$formula_parts$model_formula), "factors")) | ||
ret <- lapply( | ||
vars, | ||
function(x) df_md(mod, h_get_contrast(mod, x, type, tol)) | ||
) | ||
ret_df <- do.call(rbind.data.frame, ret) | ||
row.names(ret_df) <- vars | ||
colnames(ret_df) <- c("Num Df", "Denom Df", "F Statistic", "Pr(>=F)") | ||
class(ret_df) <- c("anova", "data.frame") | ||
attr(ret_df, "heading") <- sprintf( | ||
"Analysis of Fixed Effect Table (Type %s F tests)", | ||
switch(type, | ||
"2" = , | ||
"II" = "II", | ||
"3" = , | ||
"III" = "III" | ||
) | ||
) | ||
ret_df | ||
} |
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
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
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,4 @@ | ||
"","Effect","NumDF","DenDF","ChiSq","FValue","ProbChiSq","ProbF" | ||
"1","ARMCD",1,199.582314485653,0.03099660681111,0.03099660681111,0.86024795837881,0.86042646803505 | ||
"2","FEV1_BL",1,196.41571750149,11.5301174147263,11.5301174147263,0.00068477688975,0.00082860861048 | ||
"3","FEV1_BL*ARMCD",1,198.163532037944,0.7644368094469,0.7644368094469,0.38194359117097,0.38300197080559 |
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,5 @@ | ||
"","Effect","NumDF","DenDF","ChiSq","FValue","ProbChiSq","ProbF" | ||
"1","ARMCD",1,196.700026021882,0.03093826913613,0.03093826913613,0.86037817970071,0.86055912789767 | ||
"2","SEX",1,186.404412481085,0.10489707559679,0.10489707559679,0.74603026404544,0.74639336822641 | ||
"3","SEX*ARMCD",1,186.11901853766,0.46643535342468,0.46643535342468,0.49463165974308,0.49548089306181 | ||
"4","FEV1_BL*ARMCD",2,194.942672335072,12.2296433517045,6.11482167585228,0.00220986980743,0.0026566780461 |
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,4 @@ | ||
"","Effect","NumDF","DenDF","ChiSq","FValue","ProbChiSq","ProbF" | ||
"1","ARMCD",1,199.582314485653,0.03099660681111,0.03099660681111,0.86024795837881,0.86042646803505 | ||
"2","FEV1_BL",1,198.163532037936,11.0435094615508,11.0435094615508,0.00088998227841,0.00105994994938 | ||
"3","FEV1_BL*ARMCD",1,198.163532037944,0.7644368094469,0.7644368094469,0.38194359117097,0.38300197080559 |
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,5 @@ | ||
"","Effect","NumDF","DenDF","ChiSq","FValue","ProbChiSq","ProbF" | ||
"1","ARMCD",1,197.423866403271,0.04875135180324,0.04875135180324,0.82525043506483,0.82547861285648 | ||
"2","SEX",1,186.119018537661,0.09277653315012,0.09277653315012,0.76067660529697,0.76101691812013 | ||
"3","SEX*ARMCD",1,186.11901853766,0.46643535342468,0.46643535342468,0.49463165974308,0.49548089306181 | ||
"4","FEV1_BL*ARMCD",2,194.942672335072,12.2296433517045,6.11482167585228,0.00220986980743,0.0026566780461 |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
ARMCD | ||
AstraZeneca | ||
BCVA | ||
Benchmarking | ||
|
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.