Skip to content

Commit

Permalink
- introduced S3 methods for modelFits and postList: plot and predict …
Browse files Browse the repository at this point in the history
…for the former, print and summary for the latter
  • Loading branch information
wojcieko committed Oct 13, 2023
1 parent a391f99 commit eddb111
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 5 additions & 3 deletions R/modelling.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)

}

Expand Down
19 changes: 11 additions & 8 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,29 +9,32 @@
#' @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,
alpha_CrI = 0.05,
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)
Expand Down
15 changes: 10 additions & 5 deletions R/posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +138,6 @@ print.postList <- function (
"Maximum Difference to Control and Dose Group",
"Posterior Distributions")

print(list_out)
print(list_out, ...)

}
19 changes: 11 additions & 8 deletions man/plot_modelFits.Rd → man/plot.modelFits.Rd

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

12 changes: 6 additions & 6 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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")
})
4 changes: 2 additions & 2 deletions vignettes/analysis_normal.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit eddb111

Please sign in to comment.