Friday, March 26, 2021

Calculate sample mean and its confidence interval using t-distribution

sample.mean.CI <- function(x, log.transform = FALSE)
{
    ## Purpose: Calculate sample mean and its confidence interval using t-distribution
    ## Arguments:
    ##   x: a vector of numeric values
    ##   log.transform: if T, apply log transformation to the data first. 
    ## Return: print sample mean and its confidence interval
    ## Author: Feiming Chen
    ## ________________________________________________

    if (log.transform) {
        x <- log(x)
    }
    r <- t.test(x)
    e <- r$estimate
    c1 <- r$conf.int[1]
    c2 <- r$conf.int[2]

    if (log.transform) {
        e <- exp(e)
        c1 <- exp(c1)
        c2 <- exp(c2)
    }
    
    ans <- c(e, c1, c2)
    cat("Mean = ", convert.to.CI.text(ans), "\n")
    ans

}
if (F) {                                # Unit Test
    set.seed(1)
    sample.mean.CI(rnorm(100))          # Mean =  0.11 (-0.07 , 0.29) 
    sample.mean.CI(c(1, 2, 5, 10, 100), log.transform = TRUE) # Mean =  6.31 (0.7 , 57.23) 
}


Thursday, March 25, 2021

Sample Size for One-Sample Z test

sample.size.one.sample.Z.test <- function(E, S, alpha = 0.025, beta = 0.2)
{
    ## Purpose: Sample Size for One-sample Z test
    ##    H1: mu1 (expected) > mu0 (target)
    ##    Assume type I error of 2.5% (alpha), and power of 80% (1 - beta). 
    ## Arguments:
    ##   E: effect size (difference of expected mean and target mean)
    ##   S: estimated spread (standard deviation) from pilot sample
    ##   alpha: type I error (default to 2.5%)
    ##   beta: type II error (default to 20%)
    ## Return: sample size
    ## Author: Feiming Chen
    ## ________________________________________________

    a <- qnorm(1 - alpha) - qnorm(beta) # default = 2.8

    ## Note: a minimum sample size of 30 is required for the sample mean to approximate the normal distribution
    N <- max(30, ceiling((a * S / E)^2))

    cat("Sample Size =", N, ", when Effect Size =", E, ", SD =", S, ", Type I Error =", alpha, ", Power = ", 1 - beta, "\n")
}
if (F) {                                # Unit Test
    sample.size.one.sample.Z.test(0.1, 0.2) # 32

    for (E in seq(0.05, 0.2, 0.05)) {
        for (SD in seq(0.2, 0.5, 0.1)) {
            sample.size.one.sample.Z.test(E, SD)
        }
    }

    ## Sample Size = 126 , when Effect Size = 0.05 , SD = 0.2 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 283 , when Effect Size = 0.05 , SD = 0.3 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 503 , when Effect Size = 0.05 , SD = 0.4 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 785 , when Effect Size = 0.05 , SD = 0.5 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 32 , when Effect Size = 0.1 , SD = 0.2 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 71 , when Effect Size = 0.1 , SD = 0.3 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 126 , when Effect Size = 0.1 , SD = 0.4 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 197 , when Effect Size = 0.1 , SD = 0.5 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 30 , when Effect Size = 0.15 , SD = 0.2 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 32 , when Effect Size = 0.15 , SD = 0.3 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 56 , when Effect Size = 0.15 , SD = 0.4 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 88 , when Effect Size = 0.15 , SD = 0.5 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 30 , when Effect Size = 0.2 , SD = 0.2 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 30 , when Effect Size = 0.2 , SD = 0.3 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 32 , when Effect Size = 0.2 , SD = 0.4 , Type I Error = 0.025 , Power =  0.8 
    ## Sample Size = 50 , when Effect Size = 0.2 , SD = 0.5 , Type I Error = 0.025 , Power =  0.8 

}