Skip to content

enable other start values #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
03f1b6b
initial try
clarkliming Dec 22, 2023
72b2113
update docs
clarkliming Dec 25, 2023
bcf7f1d
Merge branch 'main' into 387_emp_cov_init_value
clarkliming Dec 25, 2023
7464868
update start control
clarkliming Dec 26, 2023
6a6d0a6
update docs
clarkliming Dec 26, 2023
f3beaec
add internal object to hold start funs
clarkliming Dec 26, 2023
916b97d
update docs
clarkliming Dec 26, 2023
a5f97e1
update docs
clarkliming Jan 2, 2024
224c984
only when singularity use nearPD
clarkliming Jan 2, 2024
a8f6bd0
remove default start functions
clarkliming Jan 2, 2024
9812dec
update staring value section and update tests
clarkliming Jan 2, 2024
bcebd3b
update docs
clarkliming Jan 2, 2024
2a1cecd
Update R/fit.R
clarkliming Jan 2, 2024
f1bd2da
address comments
clarkliming Jan 2, 2024
61b84f2
[skip actions] Roxygen Man Pages Auto Update
github-actions[bot] Jan 2, 2024
e7228b1
Update tests/testthat/test-fit.R
clarkliming Jan 2, 2024
a0980dc
Update vignettes/subsections/_intro-customizations.Rmd
clarkliming Jan 2, 2024
e902303
Update vignettes/subsections/_intro-customizations.Rmd
clarkliming Jan 2, 2024
f1822a1
Update vignettes/subsections/_intro-customizations.Rmd
clarkliming Jan 2, 2024
f0f8b6e
Update tests/testthat/test-fit.R
clarkliming Jan 2, 2024
c344428
add error info and update vignettes
clarkliming Jan 3, 2024
ef5bb6e
fix typo
clarkliming Jan 3, 2024
8a5601f
update docs and tests
clarkliming Jan 3, 2024
acbcc60
update docs
clarkliming Jan 3, 2024
8e119a9
update doc and check
clarkliming Jan 3, 2024
e346f94
Update vignettes/subsections/_intro-customizations.Rmd
clarkliming Jan 3, 2024
65abcc3
adapt examples
clarkliming Jan 3, 2024
02afe42
use data instead of full_frame in emp (#401)
clarkliming Jan 4, 2024
945ec64
Apply suggestions from code review
clarkliming Jan 4, 2024
8700bb8
Merge 945ec64b7e0578f66cfd03c06cd16fd582ecf664 into a401521a69c624df5…
clarkliming Jan 4, 2024
1085a92
[skip actions] Restyle files
github-actions[bot] Jan 4, 2024
068af44
update example
clarkliming Jan 4, 2024
d85865b
Merge branch '387_emp_cov_init_value' of github.com:openpharma/mmrm i…
clarkliming Jan 4, 2024
1cf9e9e
typo update
clarkliming Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ export(cov_struct)
export(cov_types)
export(df_1d)
export(df_md)
export(emp_start)
export(fit_mmrm)
export(fit_single_optimizer)
export(glance)
export(mmrm)
export(mmrm_control)
export(refit_multiple_optimizers)
export(std_start)
export(tidy)
import(checkmate)
importFrom(Matrix,.bdiag)
Expand Down
25 changes: 7 additions & 18 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ refit_multiple_optimizers <- function(fit,
#' @param n_cores (`count`)\cr number of cores to be used.
#' @param method (`string`)\cr adjustment method for degrees of freedom.
#' @param vcov (`string`)\cr coefficients covariance matrix adjustment method.
#' @param start (`NULL`, `numeric`, `string` or `function`)\cr optional start values for variance
#' @param start (`NULL`, `numeric` or `function`)\cr optional start values for variance
#' parameters. See details for more information.
#' @param accept_singular (`flag`)\cr whether singular design matrices are reduced
#' to full rank automatically and additional coefficient estimates will be missing.
Expand Down Expand Up @@ -241,13 +241,11 @@ refit_multiple_optimizers <- function(fit,
#' of the SAS results.
#'
#' - The argument `start` is used to facilitate the choice of initial values for fitting the model.
#' If `string` is provided, `mmrm_control` will try to find a function within the parent frame.
#' If `function` is provided, make sure its parameter is a valid element of `mmrm_tmb_data`
#' or `mmrm_tmb_formula_parts`.
#' If `NULL` is provided, `default_start` will be used.
#' By default, `default_start` will be used. Other implemented methods include `std_start` and
#' `emp_start`. `default_start` will use `emp_start` if covariance structure is unstructured and use
#' `std_start` otherwise.
#' or `mmrm_tmb_formula_parts` and it returns a numeric vector.
#' If `NULL` is provided, `std_start` will be used.
#' By default, `std_start` will be used. Other implemented methods include `std_start` and
#' `emp_start`.
#'
#' @return List of class `mmrm_control` with the control parameters.
#' @export
Expand All @@ -260,24 +258,15 @@ refit_multiple_optimizers <- function(fit,
mmrm_control <- function(n_cores = 1L,
method = c("Satterthwaite", "Kenward-Roger", "Residual", "Between-Within"),
vcov = NULL,
start = "default_start",
start = std_start,
accept_singular = TRUE,
drop_visit_levels = TRUE,
...,
optimizers = h_get_optimizers(...)) {
assert_count(n_cores, positive = TRUE)
assert_character(method)
if (is.null(start)) {
start <- "default_start"
}
if (test_string(start)) {
if (exists(start, envir = parent.frame(), mode = "function")) {
start <- get(start, envir = parent.frame(), mode = "function")
} else if (test_subset(start, names(start_funs))) {
start <- start_funs[[start]]
} else {
stop("Can not find function`", start, "` for mmrm_control()!")
}
start <- std_start
}
if (test_function(start)) {
assert_function(start, args = "...")
Expand Down
16 changes: 9 additions & 7 deletions R/tmb.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,17 @@ h_mmrm_tmb_parameters <- function(formula_parts,
assert_class(tmb_data, "mmrm_tmb_data")

m <- tmb_data$n_visits
if (test_function(start)) {
start <- do.call(start, utils::modifyList(formula_parts, tmb_data))
start_value0 <- std_start(formula_parts$cov_type, m, n_groups)
theta_dim <- length(start_value0)
if (is.null(start)) {
start_value <- start_value0
} else if (test_function(start)) {
start_value <- do.call(start, utils::modifyList(formula_parts, tmb_data))
} else {
start_value <- std_start(formula_parts$cov_type, m, n_groups)
theta_dim <- length(start_value)
if (is.null(start)) start <- start_value
assert_numeric(start, len = theta_dim, any.missing = FALSE, finite = TRUE)
start_value <- start
}
list(theta = start)
assert_numeric(start_value, len = theta_dim, any.missing = FALSE, finite = TRUE)
list(theta = start_value)
}

#' Asserting Sane Start Values for `TMB` Fit
Expand Down
35 changes: 3 additions & 32 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,36 +348,18 @@ h_valid_formula <- function(formula) {
}
}

#' Default Starting Value
#'
#' @description Obtain default starting value based on covariance structure.
#'
#' @param cov_type (`string`)\cr name of the covariance structure.
#' @param ... additional arguments passed to inner functions.
#'
#' @return A numeric vector of starting values.
#'
#' @keywords internal
default_start <- function(cov_type, ...) {
if (identical(cov_type, "us")) {
emp_start(...)
} else {
std_start(cov_type = cov_type, ...)
}
}

#' Standard Starting Value
#'
#' @description Obtain standard start values.
#'
#' @inheritParams default_start
#' @param cov_type (`string`)\cr name of the covariance structure.
#' @param n_visits (`int`)\cr number of visits.
#' @param n_groups (`int`)\cr number of groups.
#' @param ... not used.
#'
#' @return A numeric vector of starting values.
#'
#' @keywords internal
#' @export
std_start <- function(cov_type, n_visits, n_groups, ...) {
assert_string(cov_type)
assert_subset(cov_type, cov_types(c("abbr", "habbr")))
Expand Down Expand Up @@ -414,7 +396,7 @@ std_start <- function(cov_type, n_visits, n_groups, ...) {
#'
#' @return A numeric vector of starting values.
#'
#' @keywords internal
#' @export
emp_start <- function(full_frame, model_formula, visit_var, subject_var, n_visits, n_subjects, subject_groups, ...) {
assert_formula(model_formula)
assert_data_frame(full_frame)
Expand Down Expand Up @@ -463,17 +445,6 @@ h_get_theta_from_cov <- function(covariance) {
unname(ret)
}

#' Starting Value Functions
#'
#' An internal constant of available start value functions.
#'
#' @keywords internal
start_funs <- list(
default_start = default_start,
emp_start = emp_start,
std_start = std_start
)

#' Register S3 Method
#' Register S3 method to a generic.
#'
Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ reference:
- df_1d
- df_md
- component
- title: Start Values
contents:
- std_start
- emp_start
- title: Methods
contents:
- mmrm_methods
Expand Down
20 changes: 0 additions & 20 deletions man/default_start.Rd

This file was deleted.

1 change: 0 additions & 1 deletion man/emp_start.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions man/mmrm_control.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions man/start_funs.Rd

This file was deleted.

1 change: 0 additions & 1 deletion man/std_start.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading