Chapter 12: Analysis of Variance

Example, Page-363

In [1]:
# Variable declaration
A = [0.25,0.27,0.22,0.30,0.27,0.28,0.32,0.24,0.31,0.26,0.21,0.28]
B = [0.18,0.28,0.21,0.23,0.25,0.20,0.27,0.19,0.24,0.22,0.29,0.16]
C = [0.19,0.25,0.27,0.24,0.18,0.26,0.28,0.24,0.25,0.20,0.21,0.19]
D = [0.23,0.30,0.28,0.28,0.24,0.34,0.20,0.18,0.24,0.28,0.22,0.21]
k = 4
n = 12

# Calculation
from scipy import *
from pylab import *

A_sum = sum(A)
B_sum = sum(B)
C_sum = sum(C)
D_sum = sum(D)

T = A_sum+B_sum+C_sum+D_sum

c = (T**2)/float(4*n)
SST = 0

for i in range(0,12):
    SST = SST + A[i]**2 + B[i]**2 + C[i]**2 + D[i]**2

SST = SST - c

SSTR = (A_sum**2 + B_sum**2 + C_sum**2 + D_sum**2)/n - c
SSE = SST - SSTR

# Result
print "SST: ",round(SST,4)
print "SSTR: ",round(SSTR,4)
print "SSE: ",round(SSE,4)
SST:  0.0809
SSTR:  0.013
SSE:  0.0679

Example, page-364

In [2]:
# Variable declaration
a = [0.25,0.27,0.22,0.30,0.27,0.28,0.32,0.24,0.31,0.26,0.21,0.28]
b = [0.18,0.28,0.21,0.23,0.25,0.20,0.27,0.19,0.24,0.22,0.29,0.16]
c = [0.19,0.25,0.27,0.24,0.18,0.26,0.28,0.24,0.25,0.20,0.21,0.19]
d = [0.23,0.30,0.28,0.28,0.24,0.34,0.20,0.18,0.24,0.28,0.22,0.21]
K = 4   # number of samples
n = 12  # entries in each sample

# Calculation
from scipy import *
from pylab import *

Sum = sum(a)+sum(b)+sum(c)+sum(d)
asum = sum(a)
bsum = sum(b)
csum = sum(c)
dsum = sum(d)

U = Sum/48
a1 = (asum/12) - U
a2 = (bsum/12) - U
a3 = (csum/12) - U
a4 = (dsum/12) - U

a1 = round(a1,3)
a2 = round(a2,3)
a3 = round(a3,3)
a4 = round(a4,3)

# Result
print "parameters: sample-1: ",a1," , sample-2: ",a2," , sample-3: ",a3," , sample-4: ",a4
parameters: sample-1:  0.024  , sample-2:  -0.017  , sample-3:  -0.014  , sample-4:  0.006

Example, page-365

In [3]:
# Variable declaration
l1 = [90,82,79,98,83,91]
l2 = [105,89,93,104,89,95,86]
l3 = [83,89,80,94]

alpha = 0.05            # level of significance
f_thr = 3.74            # F value at 0.05 for (3-1,17-3) degrees of freedom

# Calculation
from scipy import *
from pylab import *

Sum = sum(l1)+sum(l2)+sum(l3)
total = sum(square(l1)) + sum(square(l2)) + sum(square(l3))
sst = total - square(Sum)/17
sstr = square(sum(l1))/6.0 + square(sum(l2))/7.0 + square(sum(l3))/4.0 - square(Sum)/17.0
sse = sst - sstr
f_prt = 2.33        # calculated value

# Result
print "Since f_thr > f_prt so null hypothesis can not be rejected,we can't conclude that there is a difference in mean shear strength of bolts "
Since f_thr > f_prt so null hypothesis can not be rejected,we can't conclude that there is a difference in mean shear strength of bolts 

Example, Page-366

In [4]:
# Variable declaration
A = array([0.99,1.19,0.79,0.95,0.90])
B = array([1.11,1.53,1.37,1.24,1.42])
C = array([0.83,0.68,0.94,0.86,0.57])
n = 5
alpha = 0.05

# Calculation
from scipy import *
from pylab import *

t_thr = 2.179    # t(0.025) at dof = 12
S_sq = 0.0234

BA_lower = mean(B)-mean(A) - t_thr*sqrt(S_sq*(1.0/n + 1.0/n))
BA_upper = mean(B)-mean(A) + t_thr*sqrt(S_sq*(1.0/n + 1.0/n))
BC_lower = mean(B)-mean(C) - t_thr*sqrt(S_sq*(1.0/n + 1.0/n))
BC_upper = mean(B)-mean(C) + t_thr*sqrt(S_sq*(1.0/n + 1.0/n))
AC_lower = mean(A)-mean(C) - t_thr*sqrt(S_sq*(1.0/n + 1.0/n))
AC_upper = mean(A)-mean(C) + t_thr*sqrt(S_sq*(1.0/n + 1.0/n))

# Result
print "MD-ED: (",round(BA_lower,3),",",round(BA_upper,3),")"
print "MD-PF: (",round(BC_lower,3),",",round(BC_upper,3),")"
print "ED-PF: (",round(AC_lower,3),",",round(AC_upper,3),")"
MD-ED: ( 0.159 , 0.581 )
MD-PF: ( 0.347 , 0.769 )
ED-PF: ( -0.023 , 0.399 )

Example, Page-367

In [5]:
# Variable declaration
A = array([2.8,0.75,3.7])
B = array([0.0,-0.1,3.45])
C = array([1.15,1.75,4.2])
D = array([1.88,2.65,2.7])

n = 12
alpha = 0.05

# Calculation
from scipy import *
from pylab import *

t_thr = 2.201    # t(0.025) at dof = 11
Mean = (sum(A)+sum(B)+sum(C)+sum(D))/n
std_dev = 1.417
S_sq = 0.0234

lower = Mean - t_thr*std_dev/sqrt(n)
upper = Mean + t_thr*std_dev/sqrt(n)

# Result
print "95% confidence interval for mean of differences: (",round(lower,2),",",round(upper,2),")"
95% confidence interval for mean of differences: ( 1.18 , 2.98 )

Example, page-375

In [7]:
# Variable declaration

# Calculation
from scipy import *
from pylab import *

l = array([[45, 43, 51],[47, 46, 52],[48, 50, 55],[42, 37, 49]])
a = l[:,0]
b = l[:,1]
c = l[:,2]

l1 = [a[0],b[0],c[0]]
l2 = [a[1],b[1],c[1]]
l3 = [a[2],b[2],c[2]]
l4 = [a[3],b[3],c[3]]

# null hypothesis if all alpha =0 and all beta =0 else Alterrnative hypothesis 
alpha = 0.01              # level of significance
f_thr = 9.78              # theoritical f_value at 0.01 and (3,6) degrees of freedom
row = 4
column = 3

C = square(sum(l1)+sum(l2)+sum(l3)) / 12.0
sst = sum(square(l1)) + sum(square(l2)) + sum(square(l3)) - C 
sstr =   (square(sum(l1)) + square(sum(l2)) + square(sum(l3)) + square(sum(l4)))/3.0 - C   
ssbl = (square(sum(a)) + square(sum(b)) + square(sum(c)))/4.0  - C
SSE = sst - sstr

f_prt = 11.6              # calculated f_value

# Result
print "f_thr < f_prt , we conclude that there are differences in the effectiveness of the 4 detergents" 
f_thr < f_prt , we conclude that there are differences in the effectiveness of the 4 detergents

Example, page-378

In [8]:
# Variable declaration
alpha = 0.06
k = 3
t_thr = 2.681    # t value for alpha/(k*(k-1))  and 12 degrees of freedom

# Calculation
from scipy import *
from pylab import *

l = array([[0.99, 1.19, 0.79, 0.95, 0.90],[1.11, 1.53, 1.37, 1.24, 1.42],[0.83, 0.68, 0.94, 0.86, 0.57]])
a = l[:,0]
b = l[:,1]
c = l[:,2]
d = l[:,3]
e = l[:,4]

l1 = [a[0],b[0],c[0],d[0],e[0]]
l2 = [a[1],b[1],c[1],d[1],e[1]]
l3 = [a[2],b[2],c[2],d[2],e[2]]
MSE = 0.0234    # estimated s_square value

# we need to find three confidence intervals

# Result

# for first interval
y1 = mean(l2)-mean(l1) - 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
y2 = mean(l2)-mean(l1) + 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
print "first confidence interval:( ",round(y1,3),",",round(y2,3),")"

# for second interval
y1 = mean(l2)-mean(l3) - 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
y2 = mean(l2)-mean(l3) + 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
print "second confidence interval:( ",round(y1,3),",",round(y2,3),")"

# for third interval
y1 = mean(l1)-mean(l3) - 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
y2 = mean(l1)-mean(l3) + 2.681*sqrt( 0.0234*(1.0/5 + 1.0/5) )
print "third confidence interval:( ",round(y1,3),",",round(y2,3),")"
first confidence interval:(  0.111 , 0.629 )
second confidence interval:(  0.299 , 0.817 )
third confidence interval:(  -0.071 , 0.447 )

Example, Page-379

In [9]:
# Variable declaration
A = [0.25,0.27,0.22,0.30,0.27,0.28,0.32,0.24,0.31,0.26,0.21,0.28]
B = [0.18,0.28,0.21,0.23,0.25,0.20,0.27,0.19,0.24,0.22,0.29,0.16]
C = [0.19,0.25,0.27,0.24,0.18,0.26,0.28,0.24,0.25,0.20,0.21,0.19]
D = [0.23,0.30,0.28,0.28,0.24,0.34,0.20,0.18,0.24,0.28,0.22,0.21]
n = 12

# Calculation
from scipy import *
from pylab import *

Mean = array([sum(A)/len(A),sum(B)/len(B),sum(C)/len(C),sum(D)/len(D)])

Error_mean_square = 0.0015
s = sqrt(Error_mean_square/n)
s = round(s,3)
rp = array([2.85,3.00,3.09])
Rp = s*rp
for i in range(0,3):
    Rp[i] = round(Rp[i],3)
    
# Result
print "RP: ",Rp
RP:  [ 0.031  0.033  0.034]

Example, Page-384

In [3]:
# Variable declaration
x1 = [0.5,0.55,0.6,0.35]
y1 = [1.0,1.2,0.8,1.4]
x2 = [0.75,1.65,1.0,1.1]
y2 = [0.75,0.6,0.55,0.5]
x3 = [0.6,0.9,0.8,0.7]
y3 = [1.0,0.7,0.8,0.9]
alpha = 0.05

# Calculation
from scipy import *
from pylab import *

x1_sum = sum(x1)
y1_sum = sum(y1)
x2_sum = sum(x2)
y2_sum = sum(y2)
x3_sum = sum(x3)
y3_sum = sum(y3)
x_sum = x1_sum+x2_sum+x3_sum 
y_sum = y1_sum+y2_sum+y3_sum

c1 = (x_sum**2)/12
c2 = (y_sum**2)/12
c3 = (x_sum*y_sum)/12

f_thr = 4.46

SST1 = 0
for i in range(0,4):
    SST1 = SST1 + x1[i]**2 + x2[i]**2 + x3[i]**2
SST1 = SST1 - c1

SSTR1 = (x1_sum**2 + x2_sum**2 + x3_sum**2)/4 - c1
SSE1 = SST1 - SSTR1


SST2 = 0
for i in range(0,4):
    SST2 = SST2 + y1[i]**2 + y2[i]**2 + y3[i]**2
SST2 = SST2 - c2
SST2 = round(SST2,2)

SSTR2 = (y1_sum**2 + y2_sum**2 + y3_sum**2)/4 - c2
SSE2 = SST2 - SSTR2


SPT = 0
for i in range(0,4):
    SPT = SPT + x1[i]*y1[i] + x2[i]*y2[i] + x3[i]*y3[i]
SPT = SPT - c3

SPTR = (x1_sum*y1_sum + x2_sum*y2_sum + x3_sum*y3_sum)/4 - c3
SPE = SPT - SPTR

SST = SST2 - SPT**2/SST1
SSE = SSE2 - SPE**2/SSE1
SSTR = SST - SSE

Mean_Sqaure_Treamtment = 0.035
Mean_Sqaure_Error = 0.029

f_prt = Mean_Sqaure_Treamtment / Mean_Sqaure_Error
f_prt = round(f_prt,2)

# Result
print "SST: ",round(SST,2)
print "SSE: ",round(SSE,2)
print "SSTR: ",round(SSTR,2)

print "F- Practical: ",f_prt
print "f_prt < f_thr , Null hypothesis can not be rejected"
print "One can not conclude that any of the cleaning agents is more effective than the others"
SST:  0.3
SSE:  0.23
SSTR:  0.07
F- Practical:  1.21
f_prt < f_thr , Null hypothesis can not be rejected
One can not conclude that any of the cleaning agents is more effective than the others