pdf("g02.pdf");options(width=64) #setwd("C:\\Users\\kolassa\\Class555") setwd("~/Taught1/960-555/Data")
# Block 1
#Calculate Table 2.1
library(VGAM)#For rlaplace
library(BSDA)#For z.test
#Below the shift by -.5 for the uniform centers it at 0.
library(NonparametricHeuristic)#For fun.comparepower
level<-fun.comparepower(samp1sz=c(10,17,40),nsamp=100000,
  dist=list("rnorm","rcauchy","rlaplace","runif"),
  hypoth=c(0,0,0,-.5),alternative=c("two.sided","greater"))
print(level[,,1,,])
# Block 2
#Calculate Fig. 2.1.
library(MultNonParam)#For fun.studentizedcauchyplot
fun.studentizedcaucyplot(10,10000)
# Block 3
#Calculate Table 2.2.
library(NonparametricHeuristic)#For fun.achievable
fun.achievable()
# Block 4
#Draw Fig. 2.2
drawccplot()
# Block 5
#Calculate Table 2.3.
nobsv<-c(10,17,40)
library(NonparametricHeuristic)#For fun.comparepower
for(j in seq(length(nobsv))){
   power<-fun.comparepower(nobsv[j],nsamp=100000,
      dist=list("rnorm","rcauchy","rlaplace"),
      hypoth=(1.96+0.85)*c(1,sqrt(2),1)/sqrt(nobsv[j]))
   cat("\nPower for T and Sign Tests, sample size",nobsv[j],"\n")
   print(power)
}#end for j
# Block 6
#Calculate which order statistics give .95 confidence interval
#for the median of 21 observations.
a<-qbinom(0.025,21,.5); b<-21+1-qbinom(0.025,21,.5)
# Block 7
#Read in the arsenic data
arsenic<-as.data.frame(scan('arsenic.dat',
  what=list(age=0,sex=0,drink=0,cook=0,water=0,nails=0)))
library(BSDA)#For SIGN.test.
SIGN.test(arsenic$nails,md=0.26)#Argument md gives null hyp.
# Block 8
library(NonparametricHeuristic)#For invertsigntest
invertsigntest(log(arsenic$nails),maint="Log Nail Arsenic")
# Block 9
#Calculate critical values for the sign test, generalized to
#quantiles other than the median.
a<-qbinom(0.025,21,.75);b<-21+1-qbinom(0.025,21,1-0.75)
# Block 10
#Extract order statistics associated with the critical values.
sort(arsenic$nails)[c(a,b)]
# Block 11
#Calculate the p-value associated with $H_0$ that a value
#10 successes out of 21 come from a distribution with success 
#probability .75.
tt<-10
2*min(c(pbinom(tt,21,.75), pbinom(21+1-tt,21,1-.75)))