R/prop.os.R

Defines functions size.test.prop.os size.ci.prop.os ci.prop.os

Documented in ci.prop.os size.ci.prop.os

# DGB
## Proportion from One Sample

ci.prop.os <- function(alpha, f, n) {
 # Computes adjusted Wald confidence interval for a population proportion
 # Arguments:
 #   alpha:  alpha level for 1-alpha confidence
 #   f:      number of participants with attribute
 #   n:      sample size
 # Values:
 #   row 1: adjusted MLE, SE of adjusted MLE, adjusted Wald CI
 #   row 2: MLE, SE of MLE, Wilson CI with continuity correction
 z <- qnorm(1 - alpha/2)
 p.mle <- f/n
 se.mle <- sqrt(p.mle*(1 - p.mle)/n)
 b1 <- 2*n*p.mle + z^2
 b2 <- 2*(n + z^2)
 LL.wil <- (b1 - 1 - z*sqrt(z^2 - 2 - 1/n + 4*p.mle*(n*(1 - p.mle) + 1)))/b2
 UL.wil <- (b1 + 1 + z*sqrt(z^2 + 2 - 1/n + 4*p.mle*(n*(1 - p.mle) - 1)))/b2
 if (LL.wil < 0) {LL.wil = 0}
 if (UL.wil > 1) {UL.wil = 1}
 p.adj <- (f + 2)/(n + 4)
 se.adj <- sqrt(p.adj*(1 - p.adj)/(n + 4))
 LL.adj <- p.adj - z*se.adj
 UL.adj <- p.adj + z*se.adj
 if (LL.adj < 0) {LL.adj = 0}
 if (UL.adj > 1) {UL.adj = 1}
 out1 <- t(c(p.adj, se.adj, LL.adj, UL.adj))
 out2 <- t(c(p.mle, se.mle, LL.wil, UL.wil))
 out <- rbind(out1, out2)
 colnames(out) <- c("prop", "SE", "LL", "UL")
 rownames(out) <- c("Adjusted Wald", "Wilson with cc")
 return(out)
}

size.ci.prop.os <- function(alpha, p, w) {
 # Computes sample size required to estimate a proportion with desired precision
 # Arguments: 
 #   alpha:  alpha level for 1-alpha confidence 
 #   p:      planning value of proportion
 #   w:      desired confidence interval width
 # Values:
 #   required sample size
 z <- qnorm(1 - alpha/2)
 n <- ceiling(4*p*(1 - p)*(z/w)^2)
 return(n)
}

size.test.prop.os <- function(alpha, power, p, es) {
 # Computes sample size required to tes a proportion with desired power
 # Arguments: 
 #   alpha:  alpha level for 1-alpha confidence 
 #   power:  desired power of test
 #   p:      planning value of proportion
 #   es:     expected effect size 
 # Values:
 #   required sample size
 za <- qnorm(1 - alpha/2)
 zb <- qnorm(power)
 n <- ceiling(4*p*(1 - p)*(za + zb)^2/es^2)
 return(n)
}
cwendorf/dgb documentation built on May 3, 2022, 9:35 p.m.