Skip to content

Commit

Permalink
Update documentation & vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
tdebray123 committed Nov 9, 2024
1 parent b7640d5 commit b85a473
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 104 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ Suggests:
VignetteBuilder: knitr
Config/testthat/edition: 3
LinkingTo: Rcpp, RcppArmadillo
URL: https://github.com/smartdata-analysis-and-statistics/SimTOST
BugReports: https://github.com/smartdata-analysis-and-statistics/SimTOST/issues
URL: https://github.com/smartdata-analysis-and-statistics/SimTOST,
https://smartdata-analysis-and-statistics.github.io/SimTOST/


1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(plot,simss)
S3method(print,simss)
export(sampleSize_Mielke)
export(sampleSize)
export(simParallelEndpoints)
export(get_par)
export(plot.simss)
importFrom(data.table,.SD)
7 changes: 6 additions & 1 deletion R/SampleSize.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,12 @@ sampleSize <- function(mu_list, varcov_list=NA, sigma_list=NA, cor_mat=NA,

# Save endopoints related information on a parameter list

param <- list(mu = mu_list, varcov = varcov_list, sigmaB=sigmaB, TAR_list = TAR_list, type_y = type_y, weight_seq = weight_seq, arm_names=arm_names, ynames_list =ynames_list, list_comparator = list_comparator, list_y_comparator= list_y_comparator,sigmaB=sigmaB,Eper=Eper,Eco=Eco)
param <- list(mu = mu_list, varcov = varcov_list, sigmaB = sigmaB,
TAR_list = TAR_list, type_y = type_y, weight_seq = weight_seq,
arm_names = arm_names, ynames_list = ynames_list,
list_comparator = list_comparator,
list_y_comparator = list_y_comparator,sigmaB = sigmaB,
Eper = Eper, Eco = Eco)

# Parameters related to design ----

Expand Down
7 changes: 4 additions & 3 deletions R/helper.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#' Display the summary results of the sample size estimation
#' @details
#' This function displays the summary results of the sample size estimation.
#'
#'
#' @param x An object of class "simss"
#' @param ... Optional arguments to be passed from or to other methods.
#' @author
#' Thomas Debray \email{thomas.debray@gmail.com}
#' Thomas Debray \email{tdebray@fromdatatowisdom.com}
#' @method print simss
#' @export
print.simss <- function(x, ...) {
Expand All @@ -21,5 +24,3 @@ print.simss <- function(x, ...) {
}




83 changes: 46 additions & 37 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
#' @title sim_data
#' @description Generate data for parallel design
#' @title Generate Simulated Endpoint Data for Parallel Group Design
#'
#' @param n sample size
#' @param mu.arithmetic arithmetic mean of endpoints
#' @param mu.geometric geometric mean of endpoints
#' @param Sigma variance covariance on what?? raw or transformed data
#' @param CV Coefficient of variation of what??
#' @param seed seed
#' @param dist assumed distribution : "normal" or "lognormal"
#' @description Generate simulated endpoint data for a parallel design, with options for normal and lognormal distributions.
#'
#' @return Simulated endpoints for parallel design
#' @keywords internal
#' @author
#' Thomas Debray \email{tdebray@fromdatatowisdom.com}
#'
#' @param n Integer. The sample size for the generated data.
#' @param mu.arithmetic Numeric vector. The arithmetic mean of the endpoints on the original scale.
#' @param mu.geometric Numeric vector. The geometric mean of the endpoints on the original scale. Only used if `dist = "lognormal"`.
#' @param Sigma Matrix. Variance-covariance matrix of the raw data on the original scale. If `dist = "lognormal"`, this matrix is transformed to the log scale.
#' @param CV Numeric vector. Coefficient of variation (CV) of the raw data. Only used when `dist = "lognormal"`, where it is transformed to the log scale.
#' @param seed Integer. Seed for random number generation, ensuring reproducibility.
#' @param dist Character. Assumed distribution of the endpoints: either `"normal"` or `"lognormal"`.
#'
#' @return A matrix of simulated endpoint values for a parallel design, with dimensions `n` by the number of variables in `mu.arithmetic` or `mu.geometric`.
#'
sim_data <- function(n,
mu.arithmetic,
mu.geometric = NULL,
Sigma,
CV = NULL, # Vector of size (mu.arithmetic)
seed,
dist = "normal") {
#' @export
#'
simParallelEndpoints <- function(n,
mu.arithmetic,
mu.geometric = NULL,
Sigma,
CV = NULL, # Vector of size (mu.arithmetic)
seed,
dist = "normal") {

if (dist == "normal") {
dmu <- mu.arithmetic
Expand All @@ -29,8 +34,7 @@ sim_data <- function(n,
} else if (dist == "lognormal" & !is.null(mu.geometric)) {
dsigma <- log(1 + CV**2)
dmu <- log(mu.geometric)
}
else {
} else {
stop("Invalid distribution")
}

Expand All @@ -40,7 +44,7 @@ sim_data <- function(n,
return(MASS::mvrnorm(n = n, mu = dmu, Sigma = dsigma))
}

#' @title power_cal
#' @title Calculate the power across all comparators
#' @description Internal function to calculate the power across all comparators
#'
#' @param n sample size
Expand All @@ -63,30 +67,33 @@ power_cal <- function(n,nsim,param,param.d,seed,ncores){
size_ndrop[size_ndrop < 2] <- 2
n_drop <- sum(size)-sum(size_ndrop)

}else{
} else if (param.d$dtype == "2x2") {
# expected
size = NULL
size_drop = NULL
for( j in 1:length(param$list_comparator)){
size <- NULL
size_drop <- NULL
for (j in seq(length(param$list_comparator))) {
comp <- param$list_comparator[[j]]
ns0i <- ceiling(n/2)
ns1i <- n - ns0i
ns0i <- ifelse(ns0i<2, 2, ns0i)
ns1i <- ifelse(ns1i<2, 2, ns1i)
ns0i <- ceiling(n/2) # n/2 per sequence
ns1i <- n - ns0i # n/2 per sequence
ns0i <- ifelse(ns0i < 2, 2, ns0i)
ns1i <- ifelse(ns1i < 2, 2, ns1i)
# no drop out
ns0 <- ceiling((1-param.d$dropout[1])*ns0i)
ns0 <- ifelse(ns0<2, 2, ns0)
ns1 <- ceiling((1-param.d$dropout[2])*ns1i)
ns1 <- ifelse(ns1<2 ,2, ns1)
ns0 <- ceiling((1 - param.d$dropout[1])*ns0i)
ns0 <- ifelse(ns0 < 2, 2, ns0)
ns1 <- ceiling((1 - param.d$dropout[2])*ns1i)
ns1 <- ifelse(ns1 < 2 ,2, ns1)

# Expected per sequence
sizej <- c(ns0i, ns1i)
# Drop out per sequence
size_dropj <- c(ns0i-ns0, ns1i-ns1)
names(sizej) <- names(size_dropj) <- paste0(c("seq0_","seq1_"),paste0(comp,collapse="vs"))
size_dropj <- c(ns0i - ns0, ns1i - ns1)
names(sizej) <- names(size_dropj) <- paste0(c("seq0_","seq1_"), paste0(comp, collapse = "vs"))
size <- c(size,sizej)
size_drop <- c(size_drop,size_dropj)
}
n_drop <- sum(size_drop)
} else {
stop("Invalid design type")
}

arm_names <- param$arm_names
Expand All @@ -96,18 +103,20 @@ power_cal <- function(n,nsim,param,param.d,seed,ncores){
set.seed(seed)

# Draw a unique random seed for each arm in each simulation
if(param.d$dtype == "parallel"){
if (param.d$dtype == "parallel") {
arm_seed <- matrix(sample(x = seq((length(arm_names)*nsim*100)),
size = length(arm_names)*nsim,
replace = FALSE),
ncol = length(arm_names))
colnames(arm_seed) <- arm_names
}else{
} else if (param.d$dtype == "2x2") {
# Not same seed because the indiviudals change on each 2x2 study
arm_seed <- matrix(sample(x = seq((length(param$list_comparator)*nsim*100)),
size = length(arm_names)*nsim,
replace = FALSE),
ncol = length(param$list_comparator))
} else {
stop("Invalid design type")
}

test_listcomp <- do.call("rbind",lapply(1:length(param$list_comparator),test_studies,nsim=nsim,n=n,param=param,param.d=param.d,arm_seed=arm_seed,ncores=ncores))
Expand Down
2 changes: 1 addition & 1 deletion man/power_cal.Rd

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

3 changes: 3 additions & 0 deletions man/print.simss.Rd

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

40 changes: 40 additions & 0 deletions man/simParallelEndpoints.Rd

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

38 changes: 0 additions & 38 deletions man/sim_data.Rd

This file was deleted.

41 changes: 19 additions & 22 deletions vignettes/sampleSize_Mielke.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ ss <- sampleSize(power = 0.8, alpha = 0.05,
ss
```

# Example 3
# Multiple PK Studies
In the Zarzio example, we have the following:

```{r, eval = FALSE}
```{r, eval = TRUE}
# Calculate the standard deviation and the mean using the
# reported confidence intervals
sigma <- c(sqrt(40)*(log(0.8884)-log(0.8249))/qt(0.95, df=40-2),
Expand All @@ -105,28 +105,25 @@ mu.ratio <- c(88.84, 98.82, 86.61, 95.91, 88.5)/100
mu <- log(mu.ratio)
# Required sample size for all tests to be successful
N_Mielke(power = 0.8, Nmax = 1000, m = 5, k = 5, rho = 0, sigma = sigma,
true.diff = mu, equi.tol = log(1.25), design = "22co", alpha = 0.05,
nsim = 10000)
ssMielke <- sampleSize_Mielke(power = 0.8, Nmax = 1000, m = 5, k = 5, rho = 0,
sigma = sigma, true.diff = mu,
equi.tol = log(1.25), design = "22co",
alpha = 0.05, nsim = 10000)
ssMielke
```
We find that 47 subjects per sequence are needed, and thus 94 subjects per study or 470 in total.
We find that `r ssMielke["SS"]` subjects per sequence are needed, and thus `r ssMielke["SS"]*2` subjects per study or `r ssMielke["SS"]*10` in total.

```{r, eval = FALSE}
simsamplesize::calopt(power = 0.8, # target power
alpha = 0.05,
mu_list = list("R" = c(log(1), log(1), log(1), log(1), log(1)),
"T" = c(log(0.8884), log(0.9882), log(0.8661), log(0.9591), log(0.8850))),
sigma_list = list("R" = sigma,
"T" = sigma),
lequi.tol = rep(log(0.80), 5),
uequi.tol = rep(log(1.25), 5),
dtype="2x2",
ctype = "DOM",
lognorm = FALSE,
adjust = "no",
ncores = 1,
nsim = 10000,
seed = 1234)
```{r, eval = TRUE}
ss <- sampleSize(power = 0.8, alpha = 0.05,
mu_list = list("R" = c(log(1), log(1), log(1), log(1), log(1)),
"T" = c(log(0.8884), log(0.9882), log(0.8661),
log(0.9591), log(0.8850))),
sigma_list = list("R" = sigma, "T" = sigma),
lequi.tol = rep(log(0.80), 5),
uequi.tol = rep(log(1.25), 5),
dtype ="2x2", ctype = "DOM", lognorm = FALSE, adjust = "no",
ncores = 1, nsim = 10000, seed = 1234)
ss
```

# Example 4
Expand Down

0 comments on commit b85a473

Please sign in to comment.