Skip to content

Commit 951d2d3

Browse files
committed
add confint and summary functions
Be aware only for simss objects from a sampleSize function
1 parent b74e421 commit 951d2d3

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

R/confint.simss.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' Confidence Interval for Achieved Power from simss object
2+
#'
3+
#' @param object An object of class `"simss"` returned by a sampleSize function
4+
#' @param ... Additional arguments (currently unused).
5+
#'
6+
#' @return A named numeric vector with two elements:
7+
#' \describe{
8+
#' \item{Achieved Power}{Achieved power.}
9+
#' \item{Lower}{Lower bound of the confidence interval.}
10+
#' \item{Upper}{Upper bound of the confidence interval.}
11+
#' }
12+
#' @export
13+
#' @examples
14+
#' # Assume `res` is a result from `sampleSize()`
15+
#' # confint(res)
16+
#'
17+
confint.simss <- function(object, ...) {
18+
# Check if object is of class simss
19+
if (!inherits(object, "simss")) {
20+
stop("Object must be of class 'simss'")
21+
}
22+
# Look for CI directly (e.g., object$power.CI or object$power_ci)
23+
if (!is.null(object[["response"]][["power"]])) {
24+
ci <- c(object[["response"]][["power"]],object[["response"]][["power_LCI"]],object[["response"]][["power_UCI"]])
25+
} else {
26+
stop("Confidence interval of achieved power not found in object.")
27+
}
28+
29+
# Format as a named vector
30+
ci_out <- setNames(ci, c("Achieved Power", "Lower", "Upper"))
31+
32+
cat(sprintf("Confidence Interval for Achieved Power (%.0f%%):\n", object[["param.d"]][["alpha"]] * 100))
33+
cat(sprintf(" %0.4f [%0.4f, %0.4f]\n", ci_out[1], ci_out[2], ci_out[3] ))
34+
35+
invisible(ci_out)
36+
}

R/summary.simss.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Summary for Simulation Results
2+
#' @description Generates a summary of the simulation results, specifying the sample size for each comparator-endpoint
3+
#' @param object An object of class `"simss"` returned by a sampleSize function
4+
#' @param ... Additional arguments (currently unused).
5+
#'
6+
#' @return A named numeric vector with the sample size of each arm and also the total (Total) sample size.
7+
#' @export
8+
#' @examples
9+
#' # Assume `res` is a result from `sampleSize()`
10+
#' # summary(res)
11+
summary.simss <- function(object, ...) {
12+
# Equivalent margins
13+
margins <- data.table(names = names(unlist(object[["param.d"]][["list_lequi.tol"]])),
14+
Lower= unlist(object[["param.d"]][["list_lequi.tol"]]),
15+
Upper = unlist(object[["param.d"]][["list_uequi.tol"]]))
16+
margins[, c("Comparison", "Endpoint") := tstrsplit(names, "\\.")]
17+
18+
cat("Sample Size Summary\n")
19+
cat("--------------------\n")
20+
21+
# Sample size table
22+
ss <- as.data.frame(N_ss[["response"]][, !c("power","power_LCI", "power_UCI","n_iter", "n_drop"), with = FALSE])
23+
ss_names <- sub("^n_", "", colnames(ss))
24+
ss_names <- ifelse(ss_names == "total", "Total", ss_names)
25+
colnames(ss) <- ss_names
26+
27+
if (!inherits(object, "simss")) {
28+
stop("Object must be of class 'simss'")
29+
}
30+
31+
cat("Design:", object[["param.d"]][["dtype"]], "\n")
32+
cat("Comparison type:", object[["param.d"]][["ctype"]])
33+
cat("Equivalence Margins:\n")
34+
print(as.data.frame(margins[, c("Comparison", "Endpoint", "Lower", "Upper")]), row.names = FALSE)
35+
cat("Alpha:", object[["param.d"]][["alpha"]], "\n")
36+
cat("Target Power:", sprintf("%.4f",object[["param.d"]][["power"]]), "\n")
37+
cat("Achieved Power:", sprintf("%.4f",object[["response"]][["power"]]), "\n")
38+
cat("Estimated Sample Size:\n")
39+
print(ss, row.names = FALSE)
40+
if (!is.null(object$method)) {
41+
cat("Method:", object$method, "\n")
42+
}
43+
invisible(ss)
44+
}
45+

0 commit comments

Comments
 (0)