R/ci2.R

Defines functions ci2p ci2z ci2estimate ci2se

Documented in ci2estimate ci2p ci2se ci2z

ci2se <- function(lower, 
                  upper, 
                  conf.level = 0.95, 
                  ratio = FALSE){
    
    stopifnot(length(lower) == length(upper))
    stopifnot(sum(lower >= upper) == 0)
    stopifnot(conf.level > 0 & conf.level < 1)
    
    level <- 1 - conf.level
    q <- qnorm(p = 1 - level/2, lower.tail = TRUE)
    
    if(ratio == TRUE){
        stopifnot(sum(lower <= 0) == 0)
        lower <- log(lower)
        upper <- log(upper)
    }
    se <- (upper - lower)/(2*q)
    return(se)
}

ci2estimate <- function(lower, 
                        upper, 
                        ratio = FALSE, 
                        antilog = FALSE){
    
    stopifnot(length(lower) == length(upper))
    stopifnot(sum(lower >= upper) == 0)
    
    if(ratio == TRUE){
        stopifnot(sum(lower <= 0) == 0)
        lower <- log(lower)
        upper <- log(upper)
    }
    res <- (lower + upper)/2
    if((ratio == TRUE) & (antilog == TRUE))
        res <- exp(res)
    return(res)
}


ci2z <- function(lower, 
                 upper, 
                 conf.level = 0.95, 
                 ratio = FALSE){
    
    stopifnot(length(lower) == length(upper))
    stopifnot(sum(lower >= upper) == 0)
    stopifnot(conf.level > 0 & conf.level < 1)
    estimate <- ci2estimate(lower = lower, upper = upper, ratio = ratio)
    se <- ci2se(lower = lower, upper = upper, 
                conf.level = conf.level, ratio = ratio)
    z <- estimate/se
    return(z)
}

ci2p <- function(lower, 
                 upper, 
                 conf.level = 0.95, 
                 ratio = FALSE,
                 alternative = "two.sided"){

    z <- ci2z(lower = lower, upper = upper, 
              conf.level = conf.level, ratio = ratio)
    p <- z2p(z = z, alternative = alternative)
    return(p)
}

Try the ReplicationSuccess package in your browser

Any scripts or data that you put into this service are public.

ReplicationSuccess documentation built on Dec. 2, 2020, 3 p.m.