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

summary(termometers)

#1a F-test of variances
var.test(temp~manufacturer, data=termometers)
# 	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 

### Conclusion: Variances of indicated temperature significantly differ. Thermom-produced
# thermometers show higher variability, i.e. inaccuracy

#1b testing of mean difference
# We have two groups here so we may use a t-test or ANOVA.
# t-test (with assumption of equality of variance - this is unrealistic but important for
# comparison with ANOVA result).
t.test(temp~manufacturer, data=termometers, var.equal=T)
# 	Two Sample t-test
# 
# data:  temp by manufacturer
# t = 0.11962, df = 18, p-value = 0.9061
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  -1.656362  1.856362
# sample estimates:
# mean in group celsimet   mean in group termom 
#                   18.0                   17.9 

summary(aov(temp~manufacturer, data=termometers))
#              Df Sum Sq Mean Sq F value Pr(>F)
# manufacturer  1   0.05   0.050   0.014  0.906
# Residuals    18  62.90   3.494 

# As you can see, the p-values are exactly the same, which is because the two approaches
# are fully equivalent here. They agree that there is no significant difference in 
# mean temperature.

# Overall conclusion: the two termometers do not show a significant bias when compared to each other but
# they celsimet is significanlty more precise. 

## BTW - even Celsimet is still quite imprecise.

# Graphical illustration of these data is best done with a boxplot:
boxplot(temp~manufacturer, data=termometers)

# or maybe a dotchart with range
dotchartN(termometers$manufacturer, termometers$temp, err.bars="range")


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

aov.f<-aov(Biomass~Fertilizer, data=fert)

plot(aov.f) # Checking the ANOVA assumptions. Seem reasonably fulfilled here.

summary(aov.f)
#             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

## Conclusion: There is a significant effect of fertilizer on biomass production. 

# We may proceed to post-hoc comparisons to see which means are significantly different from which
# Post-hoc multiple comparisons by Tukey HSD test

TukeyHSD(aov.f)
#   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


# Graphical illustrations
# One option is to use a boxplot
boxplot(Biomass~Fertilizer, data=fert,
        las=2, ylim=c(70, 200))
text(1:5, tapply(fert$Biomass, fert$Fertilizer, 
     max)+8, c("a", "c", "c", "b", "c")) # the leters were constructed manually based on
# the Tukey test result.

# Another option is to use a dotchart
dotchartN(fert$Fertilizer, fert$Biomass,
        las=2, ylim=c(70, 200))
text(1:5, tapply(fert$Biomass, fert$Fertilizer, 
     max)+8, c("a", "c", "c", "b", "c")) # the leters were constructed manually based on
# the Tukey test result.

## Final conclusion: There is a significant effect of fertilizer on biomass production. 
# All fertilizers significantly increase biomass production compared to control and 
# mineral, mineral slow and organic manure are significantly better in this than nitrate ammonium.
