### Task 1
hair<-read.delim2("clipboard")
summary(hair)

boxplot(hair.length~faculty, data=hair)
boxplot(hair.length~faculty, data=hair, log="y")
boxplot(log(hair.length)~faculty, data=hair)
exp(2)

library(ggplot2)
ggplot(hair, aes(x=faculty, y=hair.length))+
  geom_boxplot()+scale_y_continuous(trans="log10")
ggplot(hair, aes(x=faculty, y=hair.length))+
  geom_boxplot()+scale_y_continuous(trans="log2")

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 between group Law and group Sci 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 

library(Hmisc)
ggplot(hair, aes(x=faculty, y=hair.length))+
  stat_summary(fun.data = mean_cl_normal, geom="pointrange")+
  scale_y_continuous(trans="log2")

### Task 2
sar<-read.delim2("clipboard")
summary(sar)

ggplot(sar, aes(x=area, y=sp))+geom_point()+
scale_x_continuous(trans="log10")+
scale_y_continuous(trans="log10")

lm.1<-lm(log(sp)~log(area), data=sar)
plot(lm.1)
summary(lm.1)
# Call:
# lm(formula = log(sp) ~ log(area), data = sar)
# 
# Residuals:
#       Min        1Q    Median        3Q       Max 
# -0.271520 -0.033880  0.005651  0.072867  0.250488 
# 
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)    
# (Intercept)  5.35276    0.09983   53.62 1.17e-14 ***
# log(area)    0.23335    0.01076   21.68 2.24e-10 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.1425 on 11 degrees of freedom
# Multiple R-squared:  0.9771,	Adjusted R-squared:  0.9751 
# F-statistic: 470.1 on 1 and 11 DF,  p-value: 2.244e-10
ggplot(sar, aes(x=area, y=sp))+geom_point()+
scale_x_continuous(trans="log10")+
scale_y_continuous(trans="log10")+
geom_smooth(method='lm')

### Task 3
football<-read.delim2("clipboard")

cor.test(football$aggressiveness,
         football$league, method="spearman")
# 	Spearman's rank correlation rho
# 
# data:  football$aggressiveness and football$league
# S = 1789.1, p-value = 0.136
# alternative hypothesis: true rho is not equal to 0
# sample estimates:
#        rho 
# -0.3452061 

### Task 4 
pizza<-read.delim2("clipboard")
summary(pizza)

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

tapply(pizza$score, pizza$cook, mean)
# Fra Gia 
# 1.5 1.8 

### Task 5
lettuce<-read.delim2("clipboard")
summary(lettuce)

wilcox.test(taste~leaf.col, data=lettuce)
# Wilcoxon rank sum test with continuity
# 	correction
# 
# data:  taste by leaf.col
# W = 2378, p-value = 0.001308
# alternative hypothesis: true location shift is not equal to 0

ggplot(lettuce, aes(leaf.col, taste))+
  geom_boxplot()

library(coin)
oneway_test(taste~as.factor(leaf.col), data=lettuce, 
            distribution=approximate(9999))
#	Approximative Two-Sample Fisher-Pitman
# 	Permutation Test
# 
# data:  taste by
# 	 as.factor(leaf.col) (green, red)
# Z = 3.2398, p-value = 0.0012
# alternative hypothesis: true mu is not equal to 0

# Task 6
books<-read.delim2("clipboard")
summary(books)

kruskal.test(grade~Author, data=books)
# 	Kruskal-Wallis rank sum test
# 
# data:  grade by Author
# Kruskal-Wallis chi-squared = 9.1957, df = 3,
# p-value = 0.0268

library(FSA)
dunnTest(grade~Author, data=books)
#            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


ggplot(books, aes(Author, grade))+geom_boxplot()
