###Practicals 8
##Task 2
fert<-read.delim("clipboard")
summary(fert)
aov.1<-aov(Biomass~Fertilizer, data=fert)
summary(aov.1)
#             Df Sum Sq Mean Sq F value  Pr(>F)    
# Fertilizer   4  26740    6685   45.96 3.6e-11 ***
# Residuals   25   3636     145                    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

plot(aov.1)

hist(fert$Biomass)

26740/(26740+3636)
plot(Biomass~Fertilizer, data=fert)

TukeyHSD(aov.1)
#   Tukey multiple comparisons of means
#     95% family-wise confidence level
# 
# Fit: aov(formula = Biomass ~ Fertilizer, data = fert)
# 
# $`Fertilizer`
#                               diff        lwr        upr     p adj
# Mineral-Control           69.83333  49.383672  90.282994 0.0000000
# MinSlow-Control           86.00000  65.550339 106.449661 0.0000000
# NitrateAmm-Control        47.16667  26.717006  67.616328 0.0000040
# OrganicManure-Control     68.83333  48.383672  89.282994 0.0000000
# MinSlow-Mineral           16.16667  -4.282994  36.616328 0.1713159
# NitrateAmm-Mineral       -22.66667 -43.116328  -2.217006 0.0245596
# OrganicManure-Mineral     -1.00000 -21.449661  19.449661 0.9998971
# NitrateAmm-MinSlow       -38.83333 -59.282994 -18.383672 0.0000769
# OrganicManure-MinSlow    -17.16667 -37.616328   3.282994 0.1311420
# OrganicManure-NitrateAmm  21.66667   1.217006  42.116328 0.0340129

means<-tapply(fert$Biomass, fert$Fertilizer, mean)
plot(means~c(1:5), pch=16, ylim=c(80, 200), xlim=c(0.5, 5.5), cex=1.5,
     xaxt='n', xlab=NA, ylab="Biomass (g)")
axis(1, at=1:5, labels = levels(fert$Fertilizer), las=2)
library(sciplot)
ses<-tapply(fert$Biomass, fert$Fertilizer, se)
dfs<-tapply(fert$Biomass, fert$Fertilizer, length)-1

CI.low<-means+ses*qt(0.025, dfs, lower.tail = T)
CI.high<-means+ses*qt(0.025, dfs, lower.tail = F)
arrows(x0=1:5, y0=CI.low, y1=CI.high, code=3, angle=90)

text(1:5, CI.high+5, c("a", "c", "c", "b", "c"), cex=1.3)


##Task 3
birds<-read.delim("clipboard")
summary(birds)

aov.2<-aov(Altitude~Species, data=birds)

summary(aov.2)
#            Df  Sum Sq Mean Sq F value   Pr(>F)    
# Species      3 1848917  616306   18.81 4.86e-06 ***
# Residuals   20  655288   32764                     
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
plot(aov.2)
boxplot(Altitude~Species, data=birds, ylim=c(0, 1400))

TukeyHSD(aov.2)
#   Tukey multiple comparisons of means
#     95% family-wise confidence level
# 
# Fit: aov(formula = Altitude ~ Species, data = birds)
# 
# $`Species`
#                              diff        lwr       upr     p adj
# Raven-Blackbird         389.16667   96.66147  681.6719 0.0067504
# RedCrossbill-Blackbird  370.00000   77.49480  662.5052 0.0101751
# SeaEagle-Blackbird     -278.50000 -571.00520   14.0052 0.0655301
# RedCrossbill-Raven      -19.16667 -311.67186  273.3385 0.9977354
# SeaEagle-Raven         -667.66667 -960.17186 -375.1615 0.0000173
# SeaEagle-RedCrossbill  -648.50000 -941.00520 -355.9948 0.0000257

maxs<-tapply(birds$Altitude, birds$Species, max)
text(1:4, maxs+40, c("a", "b", "b", "a"))
