From eddb11152d1dc89b00481f05baa593e757fffcff Mon Sep 17 00:00:00 2001 From: Wojciekowski Date: Fri, 13 Oct 2023 16:03:35 +0200 Subject: [PATCH] - introduced S3 methods for modelFits and postList: plot and predict for the former, print and summary for the latter --- NAMESPACE | 5 ++++- R/modelling.R | 8 +++++--- R/plot.R | 19 +++++++++++-------- R/posterior.R | 15 ++++++++++----- man/{plot_modelFits.Rd => plot.modelFits.Rd} | 19 +++++++++++-------- tests/testthat/test-plot.R | 12 ++++++------ vignettes/analysis_normal.Rmd | 4 ++-- 7 files changed, 49 insertions(+), 33 deletions(-) rename man/{plot_modelFits.Rd => plot.modelFits.Rd} (69%) diff --git a/NAMESPACE b/NAMESPACE index 97c3aee..3cd5f34 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(plot,modelFits) +S3method(predict,ModelFits) +S3method(print,postList) +S3method(summary,postList) export(BMCPMod) export(getBootstrapBands) export(getModelFits) export(getPosterior) -export(plot_modelFits) export(simulateData) diff --git a/R/modelling.R b/R/modelling.R index e070986..d4b1c04 100644 --- a/R/modelling.R +++ b/R/modelling.R @@ -168,14 +168,16 @@ getModelFitOpt <- function ( } +#' @export predict.ModelFits <- function ( - model_fits, - doses = NULL + object, + doses = NULL, + ... ) { - lapply(model_fits, predictModelFit, doses = doses) + lapply(x, predictModelFit, doses = doses, ...) } diff --git a/R/plot.R b/R/plot.R index 0e07abd..9f8278b 100644 --- a/R/plot.R +++ b/R/plot.R @@ -1,6 +1,6 @@ -#' @title plot_modelFits +#' @title plot.modelFits #' -#' @param model_fits tbd +#' @param x tbd An object of type modelFits #' @param gAIC tbd #' @param avg_fit tbd #' @param cr_intv tbd @@ -9,12 +9,13 @@ #' @param alpha_CrB tbd #' @param n_bs_smpl tbd #' @param acc_color tbd +#' @param ... tbd #' #' @return tbd #' @export -plot_modelFits <- function ( +plot.modelFits <- function ( - model_fits, + x, gAIC = TRUE, avg_fit = TRUE, cr_intv = TRUE, @@ -22,16 +23,18 @@ plot_modelFits <- function ( cr_bands = FALSE, alpha_CrB = c(0.05, 0.5), n_bs_smpl = 1e3, - acc_color = "orange" + acc_color = "orange", + ... ) { - plot_res <- 1e2 + plot_res <- 1e2 + model_fits <- x dose_levels <- model_fits[[1]]$dose_levels post_summary <- summary.postList( - post_list = attr(model_fits, "posterior"), - probs = c(alpha_CrI / 2, 0.5, 1 - alpha_CrI / 2)) + object = attr(model_fits, "posterior"), + probs = c(alpha_CrI / 2, 0.5, 1 - alpha_CrI / 2)) doses <- seq(from = min(dose_levels), to = max(dose_levels), length.out = plot_res) diff --git a/R/posterior.R b/R/posterior.R index 7b673f9..fa36e08 100644 --- a/R/posterior.R +++ b/R/posterior.R @@ -84,27 +84,32 @@ getPostCombsI <- function ( } +#' @export summary.postList <- function ( - post_list, + object, ... ) { - summary_list <- lapply(post_list, summary, ...) - names(summary_list) <- names(post_list) + summary_list <- lapply(object, summary, ...) + names(summary_list) <- names(object) summary_tab <- do.call(rbind, summary_list) return (summary_tab) } +#' @export print.postList <- function ( - post_list + x, + ... ) { + post_list <- x + getMaxDiff <- function ( medians @@ -133,6 +138,6 @@ print.postList <- function ( "Maximum Difference to Control and Dose Group", "Posterior Distributions") - print(list_out) + print(list_out, ...) } diff --git a/man/plot_modelFits.Rd b/man/plot.modelFits.Rd similarity index 69% rename from man/plot_modelFits.Rd rename to man/plot.modelFits.Rd index 9a5f6b8..3b35078 100644 --- a/man/plot_modelFits.Rd +++ b/man/plot.modelFits.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/plot.R -\name{plot_modelFits} -\alias{plot_modelFits} -\title{plot_modelFits} +\name{plot.modelFits} +\alias{plot.modelFits} +\title{plot.modelFits} \usage{ -plot_modelFits( - model_fits, +\method{plot}{modelFits}( + x, gAIC = TRUE, avg_fit = TRUE, cr_intv = TRUE, @@ -13,11 +13,12 @@ plot_modelFits( cr_bands = FALSE, alpha_CrB = c(0.05, 0.5), n_bs_smpl = 1000, - acc_color = "orange" + acc_color = "orange", + ... ) } \arguments{ -\item{model_fits}{tbd} +\item{x}{tbd An object of type modelFits} \item{gAIC}{tbd} @@ -34,10 +35,12 @@ plot_modelFits( \item{n_bs_smpl}{tbd} \item{acc_color}{tbd} + +\item{...}{tbd} } \value{ tbd } \description{ -plot_modelFits +plot.modelFits } diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index b1c1381..a41749c 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -1,4 +1,4 @@ -test_that("Test plot_modelFits with different model_fits input", { +test_that("Test plot.modelFits with different model_fits input", { library(BayesianMCPMod) library(clinDR) library(dplyr) @@ -150,22 +150,22 @@ test_that("Test plot_modelFits with different model_fits input", { ) # Test with default parameters and more models - plot1 <- plot_modelFits(fit) + plot1 <- plot.modelFits(fit) expect_s3_class(plot1, "ggplot") # Test with cr_intv = TRUE and more models - plot2 <- plot_modelFits(fit, cr_intv = TRUE) + plot2 <- plot.modelFits(fit, cr_intv = TRUE) expect_s3_class(plot2, "ggplot") # Test with gAIC = FALSE and more models - plot3 <- plot_modelFits(fit, gAIC = FALSE) + plot3 <- plot.modelFits(fit, gAIC = FALSE) expect_s3_class(plot3, "ggplot") # Test with avg_fit = FALSE and more models - plot4 <- plot_modelFits(fit, avg_fit = FALSE) + plot4 <- plot.modelFits(fit, avg_fit = FALSE) expect_s3_class(plot4, "ggplot") # Test with all non-default parameters and more models - plot5 <- plot_modelFits(fit, cr_intv = TRUE, gAIC = FALSE, avg_fit = FALSE) + plot5 <- plot.modelFits(fit, cr_intv = TRUE, gAIC = FALSE, avg_fit = FALSE) expect_s3_class(plot5, "ggplot") }) \ No newline at end of file diff --git a/vignettes/analysis_normal.Rmd b/vignettes/analysis_normal.Rmd index 2609346..6f1c0e0 100644 --- a/vignettes/analysis_normal.Rmd +++ b/vignettes/analysis_normal.Rmd @@ -171,7 +171,7 @@ fit<- getModelFits( ``` ```{r} -plot_modelFits(fit) -plot_modelFits(fit_simple, cr_bands = TRUE) +plot(fit) +plot(fit_simple, cr_bands = TRUE) ``` For the plotting the credible intervals are shown as well.