#' @title
#' Sample size for Generalized Linear Models
#'
#' @description
#' Calculate sample size required to meet minimial detectable effect given
#' standard deviation of the predictor, multiple correlation, power,
#' false positive rate, and variance of the outcome.
#'
#' @param method_name Name of GLM intended to be used. Function accepts:
#' 'linear regression', 'logistic regression', 'cox regression', or
#' 'poisson regression'
#' @param beta Smallest effect to detect
#' @param sigma_x Standard deviation of the predictor to which beta applies
#' @param mult_cor Multiple correlation with other covariates
#' @param fpr False positive rate, must bebetween 0 and 1 (Default 0.05)
#' @param power Desired statistical power, must be between 0 and 1 (default 0.8)
#' @param ... arguments to variance function
#' @keywords
#' @export
#' @examples
#' samplesize(method_name='linear regression', 1,1,0,var_y=1)
#' samplesize(method_name='logistic regression', 1,1,0,p=0.5)
#' samplesize(method_name='cox regression', 1,1,0,prob_uncens=0.75)
#' samplesize(method_name='poisson regression', 1,1,0,disp=1)
compute_sample_size <- function(method_name, beta, var_x, mult_cor,
power = 0.8, fpr = 0.05, ...) {
# False positive rate is a probability. Ensure between 0 and 1
if (fpr <= 0 | fpr >= 1)
stop("fpr must bet between 0 and 1 exclusive")
# Power is a probability. Ensure between 0 and 1
if (power <= 0 | power >= 1)
stop("power must bet between 0 and 1 exclusive")
if(var_x<0)
stop('var_x must be a positive number')
# Depending on the method, variance function will change
# Determine method name and assign var func to appropriate
# function See variance_functions.R for these functions
var_func = NULL
if (method_name == "linear regression") {
var_func <- normal_var(...)
} else if (method_name == "logistic regression") {
var_func <- binomial_var(...)
} else if (method_name == "cox regression") {
var_func <- cox_reg_var(...)
} else if (method_name == "poisson regression") {
var_func <- pois_var(...)
} else {
stop("method_name not recognized. method_name accepts one of: \"linear regression\",
\"logistic regression\",\"cox regression\", or \"poisson regression\"")
}
# Split up the sample size to numerator and denominator
# for ease of writing and reading.
top = (qnorm(1 - fpr/2) + qnorm(power))^2
bottom = (beta)^2*var_x * (1 - mult_cor)
n = (top/bottom) * var_func
return(ceiling(n))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.