R version 4.2.1 (2022-06-23) -- "Funny-Looking Kid"
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("g08.pdf");options(width=64)
> #setwd("C:\\Users\\kolassa\\Class555")
> setwd("~/Taught1/960-555/Data")
> expensesd<-as.data.frame(scan("friedman.dat",
+    what=list(cat="",g1=0,g2=0,g3=0,g4=0,g5=0,g6=0,g7=0)))
> expenserank<-t(apply(as.matrix(expensesd[,-1]),1,rank))
>  rownames(expenserank)<-expensesd[[1]]
> apply(expenserank,2,mean)
      g1       g2       g3       g4       g5       g6       g7 
1.642857 2.571429 3.785714 4.071429 5.000000 5.000000 5.928571 
> friedman.test(as.matrix(expensesd[,-1]))

	Friedman rank sum test

data:  as.matrix(expensesd[, -1])
Friedman chi-squared = 40.102, df = 6, p-value =
4.35e-07

> temp1<-temp<-as.data.frame(scan("chicken.dat",what=
+    list(source="", lev=0,fish=0,weight=0,w2=0)))
> temp1$weight<-temp1$w2;temp$h<-0;temp1$h<-1
> temp$w2<-NULL;temp1$w2<-NULL
> chicken<-rbind(temp,temp1)
> boxplot(split(chicken$weight,chicken$source),
+   horizontal=TRUE, main="Weight gain for Chickens", 
+   ylab="Protein Source", xlab="Weight Gain (g.)")
> #If necessary, install a version of muStat from archives using
> #library(devtools)
> #install_version("muStat")
> #or download the package from CRAN archives.
> library(muStat)#For Prentice test.
> prentice.test(chicken$weight,chicken$source,
+    blocks=chicken$lev)

	Prentice (Wilcoxon/Kruskal-Wallis/Friedman) rank sum
	test

data:  'chicken$weight' (y)  by 'chicken$source' (groups) and 'chicken$lev' (blocks)
statistic: chi-square = 2.25, df = 1, p-value = 0.1336
alternative hypothesis: two.sided

> library(MultNonParam)#For aov.P
> #aov.P requires data sorted by block.  Put block ends as the
> #third argument.
> chicken<-chicken[order(chicken$lev),]
> aov.P(chicken$weight,as.numeric(as.factor(chicken$source)),
+  c(8,16,24))
$pv
[1] 0.1819712

$tot
[1] 343000

> library(crank)#For page.trend.test
> #Perform the test of Page.
> page.trend.test(expensesd[,-1],FALSE)
muL = 1568 
sigmaL = 1829.333 

Page test for ordered alternatives
L = 1833Z =6.19582938348621, p =2.89894529058186e-10 

> library(MultNonParam)#For page.test.unbalanced
> cat('\n Page test with replicates  \n')

 Page test with replicates  
> page.test.unbalanced(chicken$weight,chicken$lev,
+    chicken$source)

	Page

data:  
= 36, Expectation = 39.00, Variance = 3.25, p-value =
0.09609
alternative hypothesis: true median is  0

> twinbrain<-as.data.frame(scan("IQ_Brain_Size",
+   what=list(CCMIDSA=0,FIQ=0,HC=0,ORDER=0,PAIR=0,SEX=0,
+   TOTSA=0, TOTVOL=0,WEIGHT=0),skip=27,nmax=20))
> fir<-twinbrain[twinbrain$ORDER==1,]
> fir$v1<-fir$TOTVOL
> sec<-twinbrain[twinbrain$ORDER==2,]
> sec$v2<-sec$TOTVOL
> brainpairs<-merge(fir,sec,by="PAIR")[,c("v1","v2")]
> brainpairs$diff<-brainpairs$v2-brainpairs$v1
> plot(brainpairs$v1,brainpairs$v2, xlab="Volume for Twin 1",
+    ylab="Volume for Twin 2",main="Twin Brain Volumes")
> cat('\n Permutation test for twin brain data  \n')

 Permutation test for twin brain data  
> obsd<-c(cor(brainpairs$v1,brainpairs$v2),
+    cor(brainpairs$v1,brainpairs$v2,method="spearman"))
> out<-array(NA,c(2,20001))
> dimnames(out)[[1]]<-c("Pearson","Spearman")
> for(j in seq(dim(out)[2])){
+    newv1<-sample(brainpairs$v1)
+    out[,j]<-c(cor(newv1,brainpairs$v2),
+       cor(newv1,brainpairs$v2,method="spearman"))
+ }
> cat("\n Monte Carlo One-Sided p value\n")

 Monte Carlo One-Sided p value
> apply(apply(out,2,">=",obsd),1,"mean")
     Pearson     Spearman 
0.0001499925 0.0062496875 
> cat("\n Asymptotic Critical Value\n")

 Asymptotic Critical Value
> -qnorm(0.025)/sqrt(length(brainpairs$v1)-1)
[1] 0.6533213
> c(cor.test(brainpairs$v1,brainpairs$v2)$p.value,
+    cor.test(brainpairs$v1,brainpairs$v2,method="spearman")$p.value)
[1] 0.0002111611 0.0136717821
>