R/apa.confint.r

#' @title apa.confint
#'
#' @description APA formated interpretation of \code{\link[stats]{confint}}
#'
#' @export
#'
#' @param ci \code{\link[stats]{confint}} or similar
#' @return the formatted \code{\link[base]{data.frame}}
#'
#' @author Mark Newman, \email{mark@trinetteandmark.com}
#' @keywords regression 
#' @family regression
#'
#' @examples
#'  \dontshow{
#'     library(knitr)
#'     library(apaformat)}
#'   x = apaformat::testdata.apa.regression.lm
#'   model = lm(Response ~ Treatment, data = x)
#'   ci = confint(model)
#'   tab = apa.confint(ci)
#'   kable(tab, caption = '95% CI', booktabs = TRUE)
#'   
apa.confint = function(ci) {

  stopifnot(class(ci) == "matrix")
  stopifnot(dim(ci)[2] == 2)
  
  cn = colnames(ci)
  cn = as.numeric(sub(" %", "", cn, fixed = T))
  level = cn[2] - cn[1]
  
  tab = sprintf("[%s, %s]", round(ci[,1], 2), round(ci[,2], 2))
  tab = matrix(tab, byrow = T, ncol = 1)
  
  rownames(tab) = rownames(ci)
  colnames(tab) = c(sprintf("CI %s%%", level))

  return(tab)
}
markanewman/apaformat documentation built on May 10, 2019, 1:19 a.m.