Let us consider an example, where the shear strength of steel plate girders needs to be modeled as a function of the four methods (treatment) and nine girders (blocks).
# Melting data to make it suitable to fit a linear regression model# using the 'lm' functiondata_melt <-melt(girder, variable.name ="treatment", id ='Girder')anova(lm(value~Girder+treatment, data = data_melt))
Analysis of Variance Table
Response: value
Df Sum Sq Mean Sq F value Pr(>F)
Girder 8 0.08949 0.01119 1.6189 0.1717
treatment 3 1.51381 0.50460 73.0267 3.296e-12 ***
Residuals 24 0.16584 0.00691
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The null hypothesis that there is no effect of the method on the sheer strength of the girder is rejected.
Let us compare the methods pairwise.
aov_object <-aov(value~Girder+treatment, data = data_melt)# Tukey's methodPostHocTest(aov_object, method ="hsd")$treatment
All methods except Aarau and Cardiff are different from each other according to Bonferroni’s method (at 5% significance level).
Let us verify if the model assumptions are satisfied.
par(mfrow =c(2,2))model <-lm(value~Girder+treatment, data = data_melt)plot(model)
shapiro.test(model$residuals)
Shapiro-Wilk normality test
data: model$residuals
W = 0.94966, p-value = 0.102
The errors are normally distribued.
bptest(model)
studentized Breusch-Pagan test
data: model
BP = 22.205, df = 11, p-value = 0.02283
The error variance assumption is also satisfied at a 1% significance level. There is no strong deviation from the assumption.
4.2 Two-way layout: Fixed effects
A manufacturer was interested in finding differences in torque values of a lock-nut. The two factors effecting the torque are the type of plating, and whether the locknut is threaded into a bolt or a mandrel. We’ll use two-way ANOVA to find if the two factors or their interaction effect the torque.
data <-read.table('bolt.dat.txt', header =TRUE)data_melt <-melt(data, variable.name ='plating', value.name ='torque')
Using M.B as id variables
head(data_melt)
M.B plating torque
1 M P.O 10
2 M P.O 13
3 M P.O 17
4 M P.O 16
5 M P.O 15
6 M P.O 14
The two factors and their interaction significantly effects the torque.
4.3 Two-way layout: Random effects
Ten food items are being tested by 5 judges (operators) for quality. Each judge inspects a food item 3 times, and gives a score. Answer the following questions with appropriate analysis:
Is there a statistically significant variation in the scores given by judges for the same quality of food? If yes, quantify the variation.
Is the variation in the scores given by judges for the same quality of food dependent on the food item?
Is there a statistically significant variation in the quality of the food items after removing the variation in scores due to different judges? If yes, quantify the variation.
data <-read.csv('sensory_data.csv', header =TRUE)head(data)