# Begtostudy(白途思)'s Professional Technology Blog

::  ::  ::

## 5.1 单因素方差分析

5.1.1 方差分析的基本概念

H0H1：诸不全相同

H0

H0成立时，检验统计量

function T=t2test(x,y)

m=length(x);

n=length(y);

vx=var(x);

vy=var(y);

a=(mean(x)-mean(y));

b=m+n-2;

c=(m-1)*vx+(n-1)*vy;

d=sqrt(m*n/(m+n));

T=a*d*sqrt(b/c);

N=10000;

m=10; n=10;

alpha=0.05;

t0=tinv(1-alpha/2,m+n-2);

P=0;

for k=1:N

x=randn(1,10); y=randn(1,10);

T=t2test(x,y);

if abs(T)>t0

P=P+1;

end

end

P=P/N

N=10000;

n=10; r=8;

alpha=0.05;

t0=tinv(1-alpha/2,n+n-2);

P=0;

for k=1:N

x=randn(8,10);

E=mean(x,2);

[EE,I]=sort(E);

X=x(I,:);

T=t2test(X(1,:),X(8,:));

if abs(T)>t0

P=P+1;

end

end

P=P/N

5.1.2 单因素方差分析的计算

(; )

(; ) (5-1)

H0H1：诸不全为零 (5-2)

，() ，

(5-3)

(5-4)

(5-5)

H0成立时容易计算

(5-6)

(5-7)

(5-8)

 方差来源 平方和 自由度 均方 F值 临界值 显著性 组间 SA 误差 SE 总和 ST

A1: 2, 4, 3, 2, 4, 7, 7, 2, 5, 4

A2: 5, 6, 8, 5, 10,7, 12,6, 6

A3: 7, 11,6, 6, 7, 9, 5, 10,6, 3,10

x1=[2, 4, 3, 2, 4, 7, 7, 2, 5, 4];

x2=[5, 6, 8, 5, 10,7, 12,6, 6];

x3=[7, 11,6, 6, 7, 9, 5, 10,6, 3,10];

n1=length(x1);

n2=length(x2);

n3=length(x3);

X1=sum(x1), mx1=mean(x1),

X2=sum(x2), mx2=mean(x2),

X3=sum(x3), mx3=mean(x3),

n=n1+n2+n3, X=X1+X2+X3, mx=X/n,

SE=(x1-mx1)*(x1-mx1)'+(x2-mx2)*(x2-mx2)'+(x3-mx3)*(x3-mx3)',

SA=n1*(mx1-mx)^2+n2*(mx2-mx)^2+n3*(mx3-mx)^2,

F=(SA/2)/(SE/27),

finv(0.9,2,27),

finv(0.95,2,27),

finv(0.99,2,27),

p=1-fcdf(F,2,27),

 方差来源 平方和 自由度 均方 F值 临界值 显著性 组间 70.4293 2 35.2146 6.9030 2.5106 ** 误差 137.7374 27 5.1014 3.3541 总和 208.1667 29 5.4881

 宿舍一 86 74 78 76 73 82 宿舍二 59 76 63 66 75 65 宿舍三 79 71 82 66 87 96 宿舍四 86 91 95 77 88 85

p=anova1(x),

5.1.3 单因素方差分析的多重比较

[P,ANOVATAB,STATS] = anova1(x)

P =

0.0020

ANOVATAB =

'Source' 'SS' 'df' 'MS' 'F' 'Prob>F'

'Columns' [1.1963e+003] [ 3] [398.7778] [7.0768] [0.0020]

'Error' [1.1270e+003] [20] [ 56.3500] [] []

'Total' [2.3233e+003] [23] [] [] []

STATS =

gnames: [4x1 char]

n: [6 6 6 6]

source: 'anova1'

means: [78.1667 67.3333 80.1667 87]

df: 20

s: 7.5067

COMPARISON = multcompare(STATS,'alpha',0.1)

COMPARISON =

1.0000 2.0000 0.2251 10.8333 21.4415

1.0000 3.0000 -12.6082 -2.0000 8.6082

1.0000 4.0000 -19.4415 -8.8333 1.7749

2.0000 3.0000 -23.4415 -12.8333 -2.2251

2.0000 4.0000 -30.2749 -19.6667 -9.0585

3.0000 4.0000 -17.4415 -6.8333 3.7749

COMPARISON = multcompare(STATS,'alpha',0.05)

COMPARISON =

1.0000 2.0000 -1.2972 10.8333 22.9639

1.0000 3.0000 -14.1305 -2.0000 10.1305

1.0000 4.0000 -20.9639 -8.8333 3.2972

2.0000 3.0000 -24.9639 -12.8333 -0.7028

2.0000 4.0000 -31.7972 -19.6667 -7.5361

3.0000 4.0000 -18.9639 -6.8333 5.2972

COMPARISON = multcompare(STATS,'alpha',0.01)

COMPARISON =

1.0000 2.0000 -4.5448 10.8333 26.2115

1.0000 3.0000 -17.3781 -2.0000 13.3781

1.0000 4.0000 -24.2115 -8.8333 6.5448

2.0000 3.0000 -28.2115 -12.8333 2.5448

2.0000 4.0000 -35.0448 -19.6667 -4.2885

3.0000 4.0000 -22.2115 -6.8333 8.5448

## 5.2 双因素方差分析

5.2.1 有重复实验的双因素方差分析

（5-9）

 因素 因 素

（5-10）

H01 （5-11）

H02 （5-12）

H03 （5-13）

（5-14）

， 自由度为 （5-16）

， 自由度为 （5-17）

， 自由度为 （5-18）

， 自由度为 （5-19）

（5-20）

H02成立时，

（5-21）

H03成立时，

（5-22）

Matlab自带的函数anova2用于处理双因素方差分析，调用格式为

[P,TABLE,STATS] = anova2(x,n)

 B1 B2 B3 A1 52 43 39 41 47 53 49 38 42 A2 48 37 29 50 41 30 36 48 47 A3 34 42 38 36 39 44 37 40 32 A4 45 58 42 44 46 60 43 56 41

[P,TABLE,STATS] = anova2(x,3)

P =

0.0259 0.7504 0.9540

TABLE =

'Source' 'SS' 'df' 'MS' 'F' 'Prob>F'

'Columns' [ 562.0833] [ 3] [187.3611] [3.6838] [0.0259]

'Rows' [ 29.5556] [ 2] [ 14.7778] [0.2906] [0.7504]

'Interaction' [ 76.6667] [ 6] [ 12.7778] [0.2512] [0.9540]

'Error' [1.2207e+003] [24] [ 50.8611] [] []

'Total' [1.8890e+003] [35] [] [] []

STATS =

source: 'anova2'

sigmasq: 50.8611

colmeans: [44.8889 40.6667 38 48.3333]

coln: 9

rowmeans: [42.2500 44.2500 42.4167]

rown: 12

inter: 1

pval: 0.9540

df: 24

COMPARISON = multcompare(STATS,'alpha',0.05)

Note: Your model includes an interaction term. A test of main

effects can be difficult to interpret when the model includes

interactions.

COMPARISON =

1.0000 2.0000 -5.0520 4.2222 13.4964

1.0000 3.0000 -2.3853 6.8889 16.1631

1.0000 4.0000 -12.7187 -3.4444 5.8298

2.0000 3.0000 -6.6075 2.6667 11.9409

2.0000 4.0000 -16.9409 -7.6667 1.6075

3.0000 4.0000 -19.6075 -10.3333 -1.0591

5.2.2 无重复实验的双因素方差分析

（5-23）

[P,TABLE,STATS] = anova2(x)

 饲料1 饲料2 饲料3 品种1 51 53 52 品种2 56 57 58 品种3 45 49 47 品种4 42 44 43

P =

0.0156 0.0000

TABLE =

'Source' 'SS' 'df' 'MS' 'F' 'Prob>F'

'Columns' [ 10.5000] [ 2] [ 5.2500] [ 9] [ 0.0156]

'Rows' [332.2500] [ 3] [110.7500] [189.8571] [2.4683e-006]

'Error' [ 3.5000] [ 6] [ 0.5833] [] []

'Total' [346.2500] [11] [] [] []

STATS =

source: 'anova2'

sigmasq: 0.5833

colmeans: [48.5000 50.7500 50]

coln: 4

rowmeans: [52 57 47 43]

rown: 3

inter: 0

pval: NaN

df: 6

COMPARISON = multcompare(STATS,'alpha',0.05, 'estimate','column')

COMPARISON =

1.0000 2.0000 -3.9071 -2.2500 -0.5929

1.0000 3.0000 -3.1571 -1.5000 0.1571

2.0000 3.0000 -0.9071 0.7500 2.4071

COMPARISON = multcompare(STATS,'alpha',0.05, 'estimate','row')

COMPARISON =

1.0000 2.0000 -7.1588 -5.0000 -2.8412

1.0000 3.0000 2.8412 5.0000 7.1588

1.0000 4.0000 6.8412 9.0000 11.1588

2.0000 3.0000 7.8412 10.0000 12.1588

2.0000 4.0000 11.8412 14.0000 16.1588

3.0000 4.0000 1.8412 4.0000 6.1588

4个猪的品种，每种搭配对比，增重差异都显著。

COMPARISON = multcompare(STATS,'alpha',0.01, 'estimate','row')

COMPARISON =

1.0000 2.0000 -8.1014 -5.0000 -1.8986

1.0000 3.0000 1.8986 5.0000 8.1014

1.0000 4.0000 5.8986 9.0000 12.1014

2.0000 3.0000 6.8986 10.0000 13.1014

2.0000 4.0000 10.8986 14.0000 17.1014

3.0000 4.0000 0.8986 4.0000 7.1014

4个猪的品种，每种搭配对比，增重差异都极显著。

Email：begtostudy#gmail.com