R/util.R

Defines functions get_stars ci_upper ci_lower sem

Documented in ci_lower ci_upper get_stars sem

#' Standard error of the mean
#' 
#' Computes the standard error of the mean of the values in x. If \code{na.rm} 
#' is \code{TRUE} then missing values are removed before computation proceeds.
#' 
#' @param x A numeric vector
#' @param na.rm Logical indicating whether missing values should be removed.
#'   
#' @examples
#' x <- rnorm(100)
#' sem(x)
#' @export
sem <- function(x, na.rm = FALSE) {
  if (na.rm) {
    n <- sum(!is.na(x))
  } else {
    n <- length(x)
  }
  stats::sd(x, na.rm = na.rm) / sqrt(n)
}

#' Confidence interval (lower)
#' 
#' Gives the lower bound of the 95\% confidence interval of an empirical
#' distribution.
#' 
#' @param x Numeric vector whose confidence interval is wanted.
#' @param na.rm Logical indicating whether NAs should be removed
#'   
#' @examples
#' x <- rnorm(1000)
#' ci_lower(x)
#' @export
ci_lower <- function(x, na.rm = FALSE) {
  stats::quantile(x, 0.025, na.rm)
}

#' Confidence interval (upper)
#' 
#' Gives the upper bound of the 95\% confidence interval of an empirical
#' distribution.
#' 
#' @param x Numeric vector whose confidence interval is wanted.
#' @param na.rm Logical indicating whether NAs should be removed
#'   
#' @examples
#' x <- rnorm(1000)
#' ci_upper(x)
#' @export
ci_upper <- function(x, na.rm = FALSE) {
  stats::quantile(x, 0.975, na.rm)
}

#' Significance stars
#' 
#' Gives the statistical significance level of a \emph{p}-value.
#' 
#' @param p A numeric \emph{p}-value from a hypothesis test.
#'   
#' @return A string representing the significance level correponding to this 
#'   \emph{p}-value ("\code{***}", "\code{**}", "\code{*}", "\code{.}", or "").
#'   
#' @examples
#' get_stars(0.03)
#' @export
get_stars <- function(p) {
  if (p < 0.001) return("***")
  if (p < 0.01) return("**")
  if (p < 0.05) return("*")
  if (p < 0.1) return(".")
  return("")
}
langcog/langcog-package documentation built on March 28, 2024, 9:52 a.m.