Skip to content
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

Adding examples #47

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
20 changes: 14 additions & 6 deletions R/post.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
#' @importFrom dplyr pull
#' @importFrom purrr safely map map2
#' @importFrom distributional dist_normal dist_mixture is_distribution parameters
#' @examples
#' library(distributional)
#' library(dplyr)
#' post_treated <- calc_post_norm(internal_data = filter(int_norm_df, trt == 1),
#' response = y,
#' prior = dist_normal(50, 10),
#' internal_sd = 0.25)
statasaurus marked this conversation as resolved.
Show resolved Hide resolved
#'
calc_post_norm<- function(
internal_data,
Expand Down Expand Up @@ -150,12 +157,13 @@ calc_post_norm<- function(
#' @importFrom dplyr pull
#' @importFrom purrr safely map map2
#' @importFrom distributional dist_beta dist_mixture is_distribution parameters
#'
calc_post_beta<- function(
internal_data,
response,
prior
){
#' @examples
#' library(dplyr)
#' library(distributional)
#' calc_post_beta(internal_data = filter(int_binary_df, trt == 1),
#' response = y,
#' prior = dist_beta(0.5, 0.5))
calc_post_beta<- function(internal_data, response, prior){
# Checking internal data and response variable
if(is_prop_scr(internal_data)){
data <- internal_data$internal_df
Expand Down
52 changes: 52 additions & 0 deletions R/ps.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
#' @importFrom dplyr mutate filter tibble as_tibble
#' @importFrom stats glm
#' @importFrom cobalt bal.tab
#' @examples
#' # This can be used for both continuous and binary data
#' library(dplyr)
#' # Continuous
#' calc_prop_scr(internal_df = filter(int_norm_df, trt == 0),
#' external_df = ex_norm_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#' # Binary
#' calc_prop_scr(internal_df = filter(int_binary_df, trt == 0),
#' external_df = ex_binary_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#'
calc_prop_scr <- function(internal_df, external_df,
id_col, model, ...){
if(!is_formula(model)){
Expand Down Expand Up @@ -197,6 +211,14 @@ glance.prop_scr <- function(x, ...){
#'
#' @return Boolean
#' @export
#' @examples
#' library(dplyr)
#' x <- calc_prop_scr(internal_df = filter(int_norm_df, trt == 0),
#' external_df = ex_norm_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#' is_prop_scr(x)
#'
is_prop_scr <- function(x){
inherits(x, "prop_scr")
}
Expand Down Expand Up @@ -237,6 +259,16 @@ test_prop_scr <- function(x){
#' theme_bw
#' @importFrom dplyr bind_rows
#' @importFrom stringr str_glue
#' @examples
#' library(dplyr)
#' ps_obj <- calc_prop_scr(internal_df = filter(int_norm_df, trt == 0),
#' external_df = ex_norm_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#' # Plotting the Propensity Scores
#' prop_scr_hist(ps_obj)
#' # Or plotting the inverse probability weights
#' prop_scr_hist(ps_obj, variable = "ipw")
prop_scr_hist <- function(x, variable = c("propensity score", "ps", "inverse probability weight", "ipw"),
...){
test_prop_scr(x)
Expand Down Expand Up @@ -293,6 +325,17 @@ prop_scr_hist <- function(x, variable = c("propensity score", "ps", "inverse pro
#' theme_bw
#' @importFrom dplyr bind_rows
#' @importFrom stringr str_glue
#' @examples
#' library(dplyr)
#' ps_obj <- calc_prop_scr(internal_df = filter(int_norm_df, trt == 0),
#' external_df = ex_norm_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#' # Plotting the Propensity Scores
#' prop_scr_dens(ps_obj)
#' # Or plotting the inverse probability weights
#' prop_scr_dens(ps_obj, variable = "ipw")
#'
prop_scr_dens <- function(x, variable = c("propensity score", "ps", "inverse probability weight", "ipw"),
...){
test_prop_scr(x)
Expand Down Expand Up @@ -348,6 +391,15 @@ prop_scr_dens <- function(x, variable = c("propensity score", "ps", "inverse pro
#' @importFrom ggplot2 ggplot aes geom_point labs scale_color_manual ggtitle
#' theme_bw geom_vline
#' @importFrom tidyr pivot_longer
#' @examples
#' library(dplyr)
#' ps_obj <- calc_prop_scr(internal_df = filter(int_norm_df, trt == 0),
#' external_df = ex_norm_df,
#' id_col = subjid,
#' model = ~ cov1 + cov2 + cov3 + cov4)
#' # Plotting the Propensity Scores
#' prop_scr_love(ps_obj, reference_line = 0.1)
#'
prop_scr_love <- function(x, reference_line = NULL, ...){
test_prop_scr(x)

Expand Down
12 changes: 10 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#' Plot Distribution
#'
#' @param ... Distributional object(s) to plot. If there are multiple objects you can name them.
#' @param ... Distributional object(s) to plot. When passing multiple objects
#' naming them will change the labels in the plot, else they will use the
#' distributional format
#'
#' @return ggplot object that is the density of the provided distribution
#' @export
Expand All @@ -9,6 +11,12 @@
#' @importFrom ggplot2 ggplot theme_bw
#' @importFrom ggdist stat_slabinterval
#' @importFrom purrr map_chr map_lgl
#' @examples
#' library(distributional)
#' plot_dist(dist_normal(0, 1))
#' #Plotting Multiple
#' plot_dist(dist_normal(0, 1), dist_normal(10, 5))
#' plot_dist('Prior' = dist_normal(0, 1), 'Posterior' = dist_normal(10, 5))
plot_dist <- function(...){
input <- list(...)
if(!all(map_lgl(input, is_distribution))){
Expand All @@ -20,7 +28,7 @@ plot_dist <- function(...){
fill_alpha <- ifelse(n > 1, 0.5, 1)
if(is.null(Distributions) & n > 1){
Distributions <- map_chr(input, format)
if(unique(Distributions) != n)
if(length(unique(Distributions)) != n)
Distributions <- paste0(1:n,": ", Distributions)

}
Expand Down
101 changes: 11 additions & 90 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
output: github_document
---


```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
Expand All @@ -13,114 +12,36 @@ knitr::opts_chunk$set(
```

# beastt
## Bayesian Evaluation, Analysis, and Simulation Software Tools for Trials (BEASTT)

## Bayesian Evaluation, Analysis, and Simulation Software Tools for Trials (beastt)

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/beastt)](https://CRAN.R-project.org/package=beastt)
[![R-CMD-check](https://github.com/GSK-Biostatistics/beastt/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/GSK-Biostatistics/beastt/actions/workflows/R-CMD-check.yaml)

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![CRAN status](https://www.r-pkg.org/badges/version/beastt)](https://CRAN.R-project.org/package=beastt) [![R-CMD-check](https://github.com/GSK-Biostatistics/beastt/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/GSK-Biostatistics/beastt/actions/workflows/R-CMD-check.yaml)

<!-- badges: end -->

## Overview

Welcome to the "beastt" package! This R package is designed to assist users in performing
Bayesian dynamic borrowing with covariate adjustment via inverse probability weighting for
simulations and data analyses in clinical trials.
Welcome to the "beastt" package! This R package is designed to assist users in performing Bayesian dynamic borrowing with covariate adjustment via inverse probability weighting for simulations and data analyses in clinical trials.

## Installation

You can install the development version of beastt from [GitHub](https://github.com/) with:
You can install the development version of {beastt} from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("GSK-Biostatistics/beastt")
```

## Functions

### `calc_prop_scr()`

Calculate propensity scores using this function and obtain the inverse probability weight
calculated for each subject. Propensity scores are essential in order to balance baseline
covariate distributions between internal and external trial participants.

**Usage:**

```R
calc_prop_scr(internal_df, external_df, id_col, model)
```

- `internal_df`: Internal dataset.
- `external_df`: External dataset.
- `id_col`: Name of the column in both datasets used to identify each subject.
- `model`: Model used to calculate propensity scores.

### `calc_power_prior()`

Calculate power priors based on a distribution, hyperparameters, a weighted object and a response variable.
Power priors provide a Bayesian framework for incorporating external information into the analysis.

**Usage:**

```R
calc_power_prior(prior, weighted_obj, response)
```

- `prior`: A {distributional} object that is the prior of the external data.
- `weighted_obj`: A weighted object created by calling `calc_prop_scr()`.
- `response`: Name of the response variable.
- `...`: Additional parameters needed depending on the type of power prior used.

## Examples

Here are some examples demonstrating the usage of the package:

```R
library(beastt)
library(ggdist)
library(ggplot2)

set.seed(1234)
## Usage

# Create internal and external datasets
internal_df <- data.frame(id_col = 1:20, cov1 = rnorm(10, 2), cov2 = rnorm(100, 20),
y = rnorm(20, mean = 5, sd = 3)
external_df <- data.frame(id_col = 21:40, cov1 = rnorm(10, 2), cov2 = rnorm(100, 18),
y = rnorm(20, mean = 8, sd = 4)

# Example for propensity score calculation
model <- as.formula("~cov1 + cov2")
psscores <- calc_prop_scr(internal_df = internal_df, external_df = external_df,
id_col = id_col, model = model)

# Example for power prior calculation using a Normal prior for external data
powerprior <- calc_power_prior(prior = dist_normal(15, 100), weighted_obj = psscores,
response = y)

# Plot power prior and interval
ggplot(tibble(), aes(xdist = powerprior, y = 1)) +
stat_slabinterval()
```
At the moment {beastt} covers normal and binomial cases for more information see the vingettes. We are looking to add survival and repeated measure cases.
statasaurus marked this conversation as resolved.
Show resolved Hide resolved

## Contributing

Feel free to contribute to the "beastt" package by reporting issues or submitting pull requests on the GitHub repository.
Feel free to contribute to the {beastt} package by reporting issues or submitting pull requests on the GitHub repository.

## License

This package is released under the [Apache License](>= 2).















This package is released under the [Apache License](%3E=%202).
Loading
Loading