#problem 8.1
x <- c(28.9,32.8,12,9.9,15,38,12.5,36.5,8.6,26.8)
y <- c(1.0,7.7,7.3,7.9,1.1,3.5,18.9,33.9,28.6,25)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "greater")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K
ans


#for large sample approx
ans = cor.test(x, y, method = "kendall", alternative = "greater", exact=F)


#========================================================
#problem 8.3
x <- c(515,286,469,410,461,436,479,198,389,262,536)
y <- c(32500,26800,11410,14850,23640,23820,29840,21830,24650,22500,26000)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "two.sided")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K
ans

#for large sample approx
ans = cor.test(x, y, method = "kendall", alternative = "two.sided", exact=F)


#========================================================
#problem 8.7
x <- c(277,169,157,139,108,213,232,229,114,232,161,149,128)
y <- c(256,118,137,144,146,221,184,188,97,231,114,187,230)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "greater")
summary(Kendall(x,y))

#for large sample approx
ans = cor.test(x, y, method = "kendall", alternative = "greater", exact=F)


#========================================================
#problem 8.11
x <- c(515,286,469,410,461,436,479,198,389,262,100,534)
y <- c(25,68,14,48,136,238,298,218,246,225,232,532)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "greater")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
ans


#========================================================
#problem 8.14
x <- c(7971,7151,6564,6264,6230,5471,5329,5207,5201,5051,5017,4989,4789,4747,
46
y <- c()
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "greater")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
ans

#========================================================
#problem 8.16
x <- c(8,9,6,12,1,11,10,2,4,5,7,3)
y <- c(8,2,10,5,12,4,6,9,7,3,11,1)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "less")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
ans


#========================================================
#problem 8.19
x <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
y <- c(10,20,17,16,12,15,13,18,17,19,21,23,23,28,28)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "greater")
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
summary(Kendall(x,y))









#========================================================
#problem 8.20
x <- c(28.9,32.8,12,9.9,15,38,12.5,36.5,8.6,26.8)
y <- c(1.0,7.7,7.3,7.9,1.1,3.5,18.9,33.9,28.6,25)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "two.sided")
ans

# or
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
tauhat = (2*K)/(n*(n-1))
tauhat


#========================================================
#problem 8.22
x <- c(515,286,469,410,461,436,479,198,389,262,536)
y <- c(32500,26800,11410,14850,23640,23820,29840,21830,24650,22500,26000)
n = length(x) 
ans = cor.test(y, x, method = "kendall", alternative = "two.sided")
ans
# or
total =   n*(n-1)/2 
totalNEG = n*(n-1)/2 - ans$statistic
totalPOS = ans$statistic
K = totalPOS - totalNEG
K 
tauhat = (2*K)/(n*(n-1))
tauhat

#=========================================================
#problem 8.34
x <- c(515,286,469,410,461,436,479,198,389,262,536)
y <- c(32500,26800,11410,14850,23640,23820,29840,21830,24650,22500,26000)
cor.test(x, y, method = "kendall", alternative = "two.sided")
## How to get a CI?  We can use the bootstrap
B = 1000
ests.kendall = matrix(0, B, 1)
ests.spearman = matrix(0, B, 1)
for(i in 1:B)
{
    samp = sample(length(x), rep=T)
    ests.kendall[i] = cor.test(x[samp], y[samp], method="kendall")$est
}
# a bootstrap estimate of the standard error of Kendall's tau
sd(ests.kendall)

# a 95% bootstrap CI of Kendall's tau
quantile(ests.kendall, c(0.05, 0.95))
#smaller length implies more accurate

#=========================================================
#problem 8.35
x <- c(277,169,157,139,108,213,232,229,114,232,161,149,128)
y <- c(256,118,137,144,146,221,184,188,97,231,114,187,230)
cor.test(x, y, method = "kendall", alternative = "greater")
summary(Kendall(x,y))
## How to get a CI?  We can use the bootstrap
B = 10000
ests.kendall = matrix(0, B, 1)
ests.spearman = matrix(0, B, 1)
for(i in 1:B)
{
    samp = sample(length(x), rep=T)
    ests.kendall[i] = cor.test(x[samp], y[samp], method="kendall")$est
}
# a bootstrap estimate of the standard error of Kendall's tau
sd(ests.kendall)

# a 95% bootstrap CI of Kendall's tau
quantile(ests.kendall, c(0.025, 0.975))


