## Chapter 6 ## November 17, 2007 ## Alan T. Arnholt # options(width=65) library(PASWR) # Example 6.1 Combinations(5,3) # # Example 6.2 t(SRS(c(2,5,8,12,13), 3)) # # Example 6.3 sample(1:180, 5, replace=FALSE) # # Example 6.4 sample(x=(1:20),size=5,prob=c(rep(1/26,18),rep(4/26,2)),replace=FALSE) # # 1 in 100 Systematic Sampling strategy seq(sample(1:100,1), 1000, 100) # # Example 6.6 seq(sample(1:50,1), 1000, 50) # # Example 6.8 rolls <- sample(1:6,100, replace=TRUE) table(rolls) table(rolls)/100 # epdf # similar to Figure 6.1 plot(ecdf(rolls)) # # Example 6.10 N <- 6 n <- 2 pop <- 1:N rs <- expand.grid(Draw1=pop, Draw2=pop) # Possible random samples xbarN <- apply(rs, 1, mean) # Means of all rs values s2N <- apply(rs, 1, var) # Variance of all rs values TOT1<- cbind(rs, xbarN=xbarN, s2N=s2N) TOT1 # Numerical values for Table 6.5 table(xbarN) # Numerical values for Table 6.6 table(s2N) # Numerical values for Table 6.7 MU <- mean(pop) # Population mean VAR <- sum((pop-mean(pop))^2)*(1/N) # Population variance MU.xbarN <- mean(xbarN) # Expected value of xbarN E.s2N <- mean(s2N) # Expected value of s2N VAR.xbarN <- sum((xbarN-mean(xbarN))^2)*(1/(N*N)) reN <- c(MU, MU.xbarN, VAR, E.s2N, VAR.xbarN) names(reN)<-c("MU", "MU.xbarN", "VAR", "E.s2N", "V.xbarN") reN # Numerical values for Case 1 in Table 6.12 srs <- SRS(1:N, n) # Possible simple random samples xbari <- apply(srs, 1, mean) # Means of simple random samples s2i <- apply(srs, 1, var) # Variances of simple random samples TOT <- cbind(srs, xbari, s2i) dimnames(TOT)[[2]]<-c("Draw1", "Draw2", "xbari", "s2i") TOT # Numerical values for Table 6.8 table(xbari) # Numerical values for Table 6.9 table(s2i) # Numerical values for Table 6.10 MU <- mean(pop) # Population mean VAR <- sum((pop-mean(pop))^2)*(1/N) # Population variance MU.xbar <- mean(xbari) # Expected value of xbari E.s2 <- mean(s2i) # Expected value of s2i VAR.xbar <- sum((xbari-mean(xbari))^2)*(1/choose(N,n)) results <- c(MU, MU.xbar, VAR, E.s2, VAR.xbar) names(results)<-c("MU", "MU.xbari", "VAR", "E.s2i", "V.xbari") print(results) # Numerical values for Case 2 in Table 6.12 # # # Example 6.11 nsize(b=3,sigma=12) # # Example 6.12 pnorm(50,50,1.897367) pnorm(75,50,1.897367) pnorm(100,50,1.897367) # # Example 6.13 # part c. nsize(b=1,sigma=2) # # ################################################################################ # n2UNIFsim code # ################################################################################ par(mfrow=c(2,2)) set.seed(15) m <- 50000 n <-2 lv <- (2-(2+sqrt(12))/2)/(1/5) uv <- (2+sqrt(12))/(2/5) mu <- (lv+uv)/2 SIGMA <- (uv-lv)/sqrt(12) MEANSunif <- array(0,m) for (i in 1:m) {MEANSunif[i]<-mean(runif(n,lv,uv))} MEANSexp <- array(0,m) for (i in 1:m) {MEANSexp[i]<-mean(rexp(n,1/5))} ll <- mu - 5*SIGMA/sqrt(n) ul <- mu + 5*SIGMA/sqrt(n) hist(MEANSunif,prob=T,xlab="xbar",nclass="scott", col=2,xlim=c(ll,ul), ylim=c(0,1.5*dnorm(mu,mu,SIGMA/sqrt(n))),main="A") lines(seq(ll,ul,.001), dnorm(seq(ll,ul,.001),mu,SIGMA/sqrt(n)),lwd=2) Mun <- mean(MEANSunif) sun <- sqrt(var(MEANSunif)) UNIFpercents <- c(sum(MEANSunif>Mun-50*sun & MEANSunifMun-2*sun & MEANSunifMun-1*sun & MEANSunifMun & MEANSunifMun+1*sun & MEANSunifMun+2*sun & MEANSunifMex-50*sex & MEANSexpMex-2*sex & MEANSexpMex-1*sex & MEANSexpMex & MEANSexpMex+1*sex & MEANSexpMex+2*sex & MEANSexpMun-50*sun & MEANSunifMun-2*sun & MEANSunifMun-1*sun & MEANSunifMun & MEANSunifMun+1*sun & MEANSunifMun+2*sun & MEANSunifMex-50*sex & MEANSexpMex-2*sex & MEANSexpMex-1*sex & MEANSexpMex & MEANSexpMex+1*sex & MEANSexpMex+2*sex & MEANSexp