### Practicals 10
hair<-read.delim2("clipboard")
plot(hair.length~faculty, data=hair)
plot(hair.length~faculty, data=hair, log="y")
t.test(log(hair.length)~faculty, data=hair)
# 	Welch Two Sample t-test
# 
# data:  log(hair.length) by faculty
# t = -5.2944, df = 21.075, p-value = 2.967e-05
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  -1.8933169 -0.8255749
# sample estimates:
# mean in group Law mean in group Sci 
#          1.623870          2.983316 

lm.1<-lm(log(hair.length)~faculty, data=hair)
plot(lm.1)
anova(lm.1)
# Analysis of Variance Table
# 
# Response: log(hair.length)
#           Df Sum Sq Mean Sq F value    Pr(>F)    
# faculty    1 11.089 11.0886   28.03 2.591e-05 ***
# Residuals 22  8.703  0.3956                      
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


library(EnvStats)


log.means<-tapply(log(hair$hair.length), hair$faculty, mean)

library(sciplot)
ses<-tapply(log(hair$hair.length), hair$faculty, se)
dfs<-tapply(hair$hair.length, hair$faculty, length)-1

CL.low<-exp(log.means+ses*qt(0.025, dfs))
CL.upper<-exp(log.means+ses*qt(0.975, dfs))
x.coords<-barplot(exp(log.means), log="y", ylim=c(1, 50))
arrows(x.coords, y0=CL.low, y1=CL.upper, code=3, angle=90)

## Task 2

sar<-read.delim2("clipboard")
summary(sar)

sar.1<-lm(log(sp)~log(area), data=sar)
summary(sar.1)
exp(5.35276)

plot(sp~area, data=sar)
x<-seq(0, 200000, 1)
y=exp(5.35276)*x^0.23335
lines(x,y)

plot(sp~area, data=sar, log='xy')
lines(x,y)

### Task 3
football<-read.delim2("clipboard")
?cor.test
cor.test(football$league, football$aggressiveness, method="spearman")
# 	Spearman's rank correlation rho
# 
# data:  football$league and football$aggressiveness
# S = 1789.1, p-value = 0.136
# alternative hypothesis: true rho is not equal to 0
# sample estimates:
#        rho 
# -0.3452061 
# 
# Warning message:
# In cor.test.default(football$league, football$aggressiveness, method = "spearman") :
#   Cannot compute exact p-value with ties

# Task 4
pizza<-read.delim("clipboard")
summary(pizza)
?wilcox.test
wilcox.test(score~cook, data=pizza, paired=T)
# 	Wilcoxon signed rank test with continuity correction
# 
# data:  score by cook
# V = 9.5, p-value = 0.4821
# alternative hypothesis: true location shift is not equal to 0
# 
# Warning messages:
# 1: In wilcox.test.default(x = c(1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L,  :
#   cannot compute exact p-value with ties
# 2: In wilcox.test.default(x = c(1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L,  :
#   cannot compute exact p-value with zeroes

tapply(pizza$score, pizza$cook, sum)

## Task 5

tennis<-read.delim("clipboard")
summary(tennis)

wilcox.test(tennis$left, tennis$right, paired = T)
# 
# 	Wilcoxon signed rank test with continuity correction
# 
# data:  tennis$left and tennis$right
# V = 0, p-value = 0.3458
# alternative hypothesis: true location shift is not equal to 0
# 
# Warning messages:
# 1: In wilcox.test.default(tennis$left, tennis$right, paired = T) :
#   cannot compute exact p-value with ties
# 2: In wilcox.test.default(tennis$left, tennis$right, paired = T) :
#   cannot compute exact p-value with zeroes

## Task 6
authors<-read.delim("clipboard")
summary(authors)
plot(grade~Author, data=authors)
kt.1<-kruskal.test(grade~Author, data=authors)
# 	Kruskal-Wallis rank sum test
# 
# data:  grade by Author
# Kruskal-Wallis chi-squared = 9.1957, df = 3, p-value = 0.0268
library(FSA)
dunnTest(authors$grade, authors$Author)
# Dunn (1964) Kruskal-Wallis multiple comparison
#   p-values adjusted with the Holm method.

#            Comparison           Z     P.unadj      P.adj
# 1 Dickens - Hemingway -0.50658151 0.612448485 1.00000000
# 2   Dickens - Pushkin  0.08443025 0.932714356 0.93271436
# 3 Hemingway - Pushkin  0.59101176 0.554512541 1.00000000
# 4   Dickens - Tolstoy  2.27961679 0.022630426 0.11315213
# 5 Hemingway - Tolstoy  2.78619830 0.005333024 0.03199814
# 6   Pushkin - Tolstoy  2.19518654 0.028150219 0.11260088

library(coin)
oneway_test(grade~Author, data=authors, distribution=approximate(nresample=999))
# 	Approximative K-Sample Fisher-Pitman Permutation Test
# 
# data:  grade by
# 	 Author (Dickens, Hemingway, Pushkin, Tolstoy)
# chi-squared = 9.4135, p-value = 0.02002