R/s2mu.R

Defines functions S2mu

Documented in S2mu

#' Variance Estimator when the Population Mean is Known
#'
#' \code{S2mu} computes a estimation of the variance, given a sample {x} with known population mean (denoted by \code{mu}).

#' @param x a numeric vector  containing the sample.
#' @param mu the population mean.
#'
#' @details Given \eqn{\{x_1,\ldots,x_n\}} a sample of a random variable, the variance estimator
#' when the population mean (denoted by \eqn{\mu}) is known can be computed as
#' \eqn{S^2_\mu=\frac{1}{n}\sum_{i=1}^n (x_i-\mu)^2}.
#'
#' @export
#'
#' @return A single numerical value corresponding with the variance estimation when the population mean is known.
#'
#' @examples
#' x=rnorm(20)
#' S2mu(x,mu=0)

S2mu<-function(x,mu){
  if (!is.numeric(mu)|any(!is.finite(mu))){stop("The mean 'mu' must be a single number")}
  if (!is.vector(mu) | length(mu)!=1){stop("The mean 'mu' must be a single number")}
  if (!is.numeric(x)|!is.vector(x)){stop(" The sample 'x' must be a numeric vector")}
  if(sum(is.na(x))!=0){x=x[-which(is.na(x))]; warning("Missing values have been removed from 'x'")}
  if (any(!is.finite(x))){stop(" The sample 'x' must be a numeric vector")}
  if(!length(x)>1){stop("'x' must be a sample of size bigger than one")}

	sum((x-mu)^2)/length(x)
}

Try the LearningStats package in your browser

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

LearningStats documentation built on April 21, 2021, 9:06 a.m.