rnorm.loop <- function(n, mu=0, sigma=1) { # ************************************************************************* # # FILE: rnorm.loop.r # # Date Created: March 17, 2004 # Author: Mark E. Irwin # # Contents: Determines properties of normal CI by Monte Carlo. Uses # looped calculations # # Revision History # Date Name Changes/Reasons # # ************************************************************************* # # Input: # # n: number of random deviates to be generated # mu: mean of random deviates # sigma: standard deviation of random deviates # # Output: List containing # # cover: indicators of whether 0 is in interval # C: proportion of intervals with 0 in interval # width: width of 95% intervals # wbar: average width # # ************************************************************************* xbar<-rep(0,n) s <- rep(0,n) cover <- rep(0,n) width <- rep(0,n) for(i in 1:n) { x <- rnorm(10, mu, sigma) xbar[i] <- mean(x) s[i] <- sqrt(var(x)) cover[i] <- abs(xbar[i]) * sqrt(10) / s[i] <= qt(0.975, 9) width <- 2 * qt(0.975, 9) * s[i] / sqrt(10) } C <- mean(cover) wbar <- mean(width) list(cover=cover, C=C, width=width, wbar=wbar) }