R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> pdf("g02.pdf");options(width=64)
> #setwd("C:\\Users\\kolassa\\Class555")
> setwd("~/Taught1/960-555/Data")
> #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,,])
, , 10

              rnorm rcauchy rlaplace   runif
T           0.04897 0.01880  0.04283 0.05310
Sign        0.02114 0.02115  0.02182 0.02169
Exact Sign  0.02114 0.02115  0.02182 0.02169
Signed Rank 0.04845 0.04784  0.04943 0.04820

, , 17

              rnorm rcauchy rlaplace   runif
T           0.04885 0.01969  0.04607 0.05195
Sign        0.01232 0.01260  0.01268 0.01287
Exact Sign  0.04890 0.04845  0.04810 0.04924
Signed Rank 0.04453 0.04457  0.04495 0.04470

, , 40

              rnorm rcauchy rlaplace   runif
T           0.05087 0.02027  0.04966 0.05083
Sign        0.03845 0.03865  0.03915 0.03877
Exact Sign  0.03845 0.03865  0.03915 0.03877
Signed Rank 0.05093 0.05012  0.05064 0.05004

> #Calculate Fig. 2.1.
> library(MultNonParam)#For fun.studentizedcauchyplot
> fun.studentizedcaucyplot(10,10000)
> #Calculate Table 2.2.
> library(NonparametricHeuristic)#For fun.achievable
> fun.achievable()
       n Exact Critical Value Asymptotic Critical Value
 [1,]  6                    0                         0
 [2,]  7                    0                         0
 [3,]  8                    0                         0
 [4,]  9                    1                         1
 [5,] 10                    1                         1
 [6,] 11                    1                         1
 [7,] 12                    2                         2
 [8,] 13                    2                         2
 [9,] 14                    2                         2
[10,] 15                    3                         3
[11,] 16                    3                         3
[12,] 17                    4                         3
[13,] 18                    4                         4
[14,] 19                    4                         4
[15,] 20                    5                         5
[16,] 21                    5                         5
[17,] 22                    5                         5
[18,] 23                    6                         6
      Exact Size  n Exact Critical Value
 [1,] 0.03125000 24                    6
 [2,] 0.01562500 25                    7
 [3,] 0.00781250 26                    7
 [4,] 0.03906250 27                    7
 [5,] 0.02148438 28                    8
 [6,] 0.01171875 29                    8
 [7,] 0.03857422 30                    9
 [8,] 0.02246094 31                    9
 [9,] 0.01293945 32                    9
[10,] 0.03515625 33                   10
[11,] 0.02127075 34                   10
[12,] 0.04904175 35                   11
[13,] 0.03088379 36                   11
[14,] 0.01921082 37                   12
[15,] 0.04138947 38                   12
[16,] 0.02660370 39                   12
[17,] 0.01690054 40                   13
[18,] 0.03468966 41                   13
      Asymptotic Critical Value Exact Size
 [1,]                         6 0.02265584
 [2,]                         7 0.04328525
 [3,]                         7 0.02895927
 [4,]                         7 0.01915729
 [5,]                         8 0.03569814
 [6,]                         8 0.02411954
 [7,]                         9 0.04277395
 [8,]                         9 0.02944937
 [9,]                         9 0.02006161
[10,]                        10 0.03508203
[11,]                        10 0.02430651
[12,]                        11 0.04095959
[13,]                        11 0.02881672
[14,]                        12 0.04703103
[15,]                        12 0.03355244
[16,]                        12 0.02370270
[17,]                        13 0.03847731
[18,]                        13 0.02753316
> #Draw Fig. 2.2
> drawccplot()
> #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

Power for T and Sign Tests, sample size 10 
, , two.sided, 0, 10

              rnorm rcauchy rlaplace
T           0.70684 0.24055  0.49056
Sign        0.41661 0.33710  0.35897
Exact Sign  0.41661 0.33710  0.35897
Signed Rank 0.68694 0.35935  0.49972


Power for T and Sign Tests, sample size 17 
, , two.sided, 0, 17

              rnorm rcauchy rlaplace
T           0.75455 0.18058  0.50636
Sign        0.36246 0.33223  0.34431
Exact Sign  0.58626 0.55190  0.56358
Signed Rank 0.71847 0.42680  0.55206


Power for T and Sign Tests, sample size 40 
, , two.sided, 0, 40

              rnorm rcauchy rlaplace
T           0.78234 0.10278  0.51101
Sign        0.55635 0.59255  0.59834
Exact Sign  0.55635 0.59255  0.59834
Signed Rank 0.76419 0.51342  0.62460

> #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)
> #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.

	One-sample Sign-Test

data:  arsenic$nails
s = 10, p-value = 1
alternative hypothesis: true median is not equal to 0.26
95 percent confidence interval:
 0.1184521 0.3362992
sample estimates:
median of x 
      0.175 

Achieved and Interpolated Confidence Intervals: 

                  Conf.Level L.E.pt U.E.pt
Lower Achieved CI     0.9216 0.1190 0.3100
Interpolated CI       0.9500 0.1185 0.3363
Upper Achieved CI     0.9734 0.1180 0.3580

> library(NonparametricHeuristic)#For invertsigntest
> invertsigntest(log(arsenic$nails),maint="Log Nail Arsenic")
[1] -2.137071 -1.027222
> #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)
> #Extract order statistics associated with the critical values.
> sort(arsenic$nails)[c(a,b)]
[1] 0.269 0.851
> #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)))
[1] 0.01284542
>