## Solution

# Exercise 1


# data
y = read.table("pr0232.txt")
y
y1 = y$V1
y2 = y$V2
# visual inspection of the data
boxplot(y1,y2)
qqnorm(y1, datax=TRUE); qqline(y1,datax=TRUE)
qqnorm(y2, datax=TRUE); qqline(y2,datax=TRUE)
# because of rounding data doesn't seem very normal - we ignore that
# testing if the variances are equal
var.test(y1,y2)
# two sample t-test
t.test(y1,y2,var.equal=TRUE)
# paired t-test
t.test(y1,y2,paired=TRUE) # or t.test(y1-y2)
# cleaning up
rm(list=ls())


# Exercise 2

# data
y = scan("pr0320.txt")
# We shal use "rodding" both as numbers and as a factor
# We make a vector of numbers and convert it:
rodnum = rep(seq(10,25,5), each=3)
rodnum # Wise to do this check!
rodfac = as.factor(rodnum)
# Alternatively, we make the factor and convert this to numeric
rodfac = gl(4,3,labels=seq(10,25,5))
rodfac # Still wise to do the check
rodnum = as.numeric(as.vector(rodfac))
# A plot of the data
plot(y ~ rodnum, xlab="Rodding", ylab="Compressive Strength")
# What do you think? Does rodding have influence?
# We make an aov object
obj = aov(y ~ rodfac)
summary(obj)  # Answers both (a) and (b), p=0.214 (not small)
# Non-significant, maybe the plot had made you believe otherwise
# Now a residual plot, residual vs level
res = resid(obj)
plot(res ~ rodnum, xlab="Rodding", ylab=c("Residuals"))
abline(h=0, lty=2)
# Could the variance be significantly smaller for level 25?
bartlett.test(y, rodfac)  # No indication for this, p=0.115
# cleaning up
rm(list=ls())


# Exercise 3


# We redo parts of the previous exercise
y = scan("pr0320.txt")
rodnum = rep(seq(10,25,5), each=3)
rodfac = as.factor(rodnum)
obj = aov(y ~ rodfac)
res = resid(obj)
# The QQ-plot
qqnorm(res)
qqline(res)
# This looks normal, so the ANOVA approach should be ok
# Also there are only ni = 3 < 5 measurements in each group, so the
# chi-square approximation in the Kruskal-Wallis test is not
# trustworthy
# The Kruskal-Wallis test
kruskal.test(y,rodfac)
# Again no significant difference.
# Definitely we should trust the ANOVA more than the Kruskal-Wallis
# test, if they had given different results, since the assumptions are
# fulfilled for the ANOVA but not for the Kruskal-Wallis test.

# Cleaning
rm(list=ls())
