### Solutions ###


## Exercise 1


# Data
tv = read.table("pr0436.txt",header=TRUE)
tv$oper = as.factor(tv$oper); tv$assem = as.factor(tv$assem)
# Checking the greaco-latin square design
matrix(paste(tv$method,tv$workplace),4)
# Analysis of variance
obj = aov(y ~ method + oper + assem + workplace, data=tv)
summary(obj)
# Nothing is significant
# Omitting the factor workplace, this is a Latin-square
obj2 = aov(y ~ method + oper + assem, data=tv)
summary(obj2)
# Here method is significant
# Tukey's test for method
TukeyHSD(obj2,"method")
# Only method C and B differs significantly, C being slower.
# clean-up
rm(list=ls())


### Exercise 2

# Data
paper = read.table("pr0442.txt", header=TRUE)
paper$day = as.factor(paper$day)
paper$conc = as.factor(paper$conc)
summary(aov(strength ~ conc + Error(day), data=paper))
# The difference in concentration is only almost significant
# Now try considering both factors fixed.
# This order is correct for comparing concentrations but not days:
summary(aov(strength ~ day + conc, data=paper))
# This order is correct for comparing days but not concentrations:
summary(aov(strength ~ conc + day, data=paper))
# Summary: Concentrations make a difference, days do not.
rm(list=ls())


### Exercise 3
# (a) Load the data
hardwood = read.table("pr1001.txt")
y = hardwood$V1
x = hardwood$V2
# (b) Plot the data
plot(y~x)
# (c) The design matrix consists of a column of ones and a column of x-values
X = cbind(1,x)
# (d) beta-hat contains the estimated slope and intercept
bh = solve(t(X)%*%X) %*% t(X) %*% y
abline(bh[1],bh[2])
# (e) A column of squared x-values is included in the design matrix
X = cbind(1,x,x^2)
bh = solve(t(X)%*%X) %*% t(X) %*% y
curve(bh[3]*x^2+bh[2]*x+bh[1], 9, 31,add=TRUE)
# (f) It does not seem like the second order term improves the model
# much - we will get back to this issue the next time.
