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

t.test(temp~manufacturer, data=term)
# 	Welch Two Sample t-test
# 
# data:  temp by manufacturer
# t = 0.11962, df = 11.888, p-value = 0.9068
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  -1.72338  1.92338
# sample estimates:
# mean in group celsimet   mean in group termom 
#                   18.0                   17.9 

var.test(temp~manufacturer, data=term)
# 	F test to compare two variances
# 
# data:  temp by manufacturer
# F = 6.0674, num df = 9, denom df = 9, p-value =
# 0.01302
# alternative hypothesis: true ratio of variances is not equal to 1
# 95 percent confidence interval:
#   1.50706 24.42738
# sample estimates:
# ratio of variances 
# 6.067416 
          
library(ggplot2)

ggplot(term, aes(x=manufacturer, y=temp))+geom_boxplot()

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

aov.1<-aov(Biomass~factor(Fertilizer), data=fert)
plot(aov.1)

summary(aov.1)
#                    Df Sum Sq Mean Sq F value  Pr(>F)    
# factor(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

# Proportion of explained variability
26740/(26740+3636)
# 0.8803002
          
TukeyHSD(aov.1)
# Tukey multiple comparisons of means
#     95% family-wise confidence level
# 
# Fit: aov(formula = Biomass ~ factor(Fertilizer), data = fert)
# 
# $`factor(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


### Graphical illustration with boxplot

#Computation of maxima - needed for coordinates of Tukey-test letters
maxs<-data.frame(
  max=tapply(fert$Biomass, list(fert$Fertilizer), max), 
  letter=c("a", "c", "c", "b", "c"))

ggplot(data=fert, aes(x=Fertilizer, y=Biomass))+geom_boxplot()+
geom_text(data=maxs, aes(x=1:5, y=max+5, label=letter))


### Graphical illustration of ANOVA by dotchart
## Computing the (approximate) upper confidence limit as mean + 2*SE
library(Hmisc)
library(sciplot)

upper.CL<-data.frame(
  limit=tapply(fert$Biomass, list(fert$Fertilizer), mean)+
  2*tapply(fert$Biomass, list(fert$Fertilizer), se), 
  letter=c("a", "c", "c", "b", "c"))

ggplot(data=fert, aes(x=Fertilizer, y=Biomass))+
  stat_summary(fun.data = mean_cl_normal, geom="pointrange")+
geom_text(data=upper.CL, aes(x=1:5, y=limit+10, label=letter))


## Boxplot in base graphics
boxplot(Biomass~Fertilizer, data=fert, ylim=c(70, 200))
maxs<-tapply(fert$Biomass, list(fert$Fertilizer), max)
text(1:5, maxs+10, c("a", "c", "c", "b", "c"))

## task E
cars<-read.delim2("clipboard")
summary(cars)

aov.c<-aov(speed~col, data=cars)
plot(aov.c)

summary(aov.c)
#             Df Sum Sq Mean Sq F value Pr(>F)
# col          3  264.4   88.12    0.94  0.434
# Residuals   29 2719.9   93.79 

ggplot(cars, aes(col, speed))+geom_boxplot()




