Skip to content

Commit

Permalink
1) added extraction of fitted values for one-stage models in metapred.
Browse files Browse the repository at this point in the history
2) added tests for fitted values in one-stage models.
3) updated description of how generalizability functions are applied to performance measures.
  • Loading branch information
VMTdeJong committed Jan 15, 2025
1 parent 07d9f56 commit f36675f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
28 changes: 24 additions & 4 deletions R/metapred.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,25 @@
#' fit
#'
#' # Let's try to simplify model 'f' in order to improve its external validity
#' metapred(DVTipd, strata = "study", formula = f, family = binomial)
#' # Estimate multiple performance measures and generalizability functions simultaneously.
#' # The first performance and generalizabilty functions are used for model selection.
#' fit2 <- metapred(DVTipd, strata = "study", formula = f, scope = f, family = binomial,
#' perfFUN = list("mse", "auc", "calibration_intercept", "calibration_slope"),
#' genFUN = list("rema", "rema.tau"),
#' gen.of.perf = "factorial")
#'
#' # Use perf() to get performance estimates per stratum. 0 to get all.
#' # Performance measures may also be selected by name.
#' perf(fit2, perfFUN = 0)
#'
#' # Use gen() to get generalizability estimates. 0 to get all.
#' # Generalizability estimates may also be selected by name.
#' gen(fit2, genFUN = 0)
#'
#' # Use ma() to perform a (new) meta-analysis of performance estimates.
#' ma(fit2, perfFUN = "auc")
#' ma(fit2, perfFUN = "calibration_intercept")
#' ma(fit2, perfFUN = "calibration_slope")
#'
#' # We can also try to build a generalizable model from scratch
#'
Expand Down Expand Up @@ -335,7 +353,7 @@ predict.metapred <- function(object, newdata = NULL, strata = NULL, type = "resp
#' Extract Model Fitted Values
#'
#' Extract the fitted values of a \code{metapred} object. By default returns fitted values of the model in the
#' cross-validation procedure.
#' cross-validation procedure, i.e., the predicted values for the validation folds.
#'
#' Function still under development, use with caution.
#'
Expand All @@ -349,15 +367,17 @@ predict.metapred <- function(object, newdata = NULL, strata = NULL, type = "resp
#' @param step character or numeric. Name or number of step to select if \code{select} = "cv". Defaults to best step.
#' @param model character or numeric. Name or number of model to select if \code{select} = "cv". Defaults to
#' best model.
#' @param as.stratified logical. \code{select} = "cv" determines whether returned predictions are stratified in a list
#' @param as.stratified logical. Determines whether returned predictions are stratified in a list
#' (\code{TRUE}, default) or in their original order (\code{FALSE}).
#' @param type character. Type of fitted value.
#' @param ... For compatibility only.
#' @export
fitted.metapred <- function(object, select = "cv", step = NULL, model = NULL,
as.stratified = TRUE, type = "response", ...) {
if (isTRUE(select == "cv")) {
ftd <- fitted(subset.metapred(x = object, select = select, step = step, model = model, type = type, ...))
ftd <- fitted(subset.metapred(x = object, select = select, step = step, model = model, type = type, ...),
two.stage = object$options$two.stage,
...)
if (as.stratified)
return(ftd)
ftd.v <- Reduce(rbind, ftd) #as vector
Expand Down
4 changes: 2 additions & 2 deletions man/fitted.metapred.Rd

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

20 changes: 19 additions & 1 deletion man/metapred.Rd

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

Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/testthat/test_metapred_6_one_stage.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ test_that("metapred can estimate one-stage fixed effect models", {
expect_is(mp_fe, "metapred")
})


test_that("metapred one-stage can extract fitted values.", {
skip_on_cran()

f <- y ~ x
mp_fe <- metapred(d, "k", formula = f, scope = f, family = binomial, estFUN = glm, two.stage = F)

fitted_values_stratified <- fitted(mp_fe)
expect_vector(fitted_values_stratified)
expect_length(fitted_values_stratified, length(unique(k)))

fitted_values_unlisted <- fitted(mp_fe, as.stratified = FALSE)
expect_vector(fitted_values_unlisted)
expect_length(fitted_values_unlisted, nrow(d))
})

test_that("calibration of metapred one-stage fixed effect models is ok", {
skip_on_cran()
f <- y ~ x
Expand Down

0 comments on commit f36675f

Please sign in to comment.