# Functions written in class on January 14, 2019 mymean <- function(x, remove.NA=TRUE) { # A simple function to calculate the mean of # a vector. # By default, missing values are removed. # if (remove.NA) x <- x[!is.na(x)] if (length(x)==0) stop("No non-missing data provided") if (!is.numeric(x)) stop("x must be numeric") sum(x)/length(x) } z.interval <- function(x, alpha=0.05, sigma=1) { # Calculate Z interval for the mean of a norma # population with known standard deviation sigma # # Arguments: x - a vector of data # alpha - the significance level (default 0.05) # sigma - the population standard deviation (default 1) if (sigma<=0) stop("sigma should be positive") if ((alpha <=0)||(alpha>=1)) stop("alpha should be in the interval (0,1)") xbar <- mymean(x) n <- length(x) z <- qnorm(1-alpha/2) ci <- xbar+c(-1,1)*z*sigma/sqrt(n) print(ci) ci }