scipy无交互双因素方差分析

2021-06-24  本文已影响0人  一路向后

1.问题描述

有4个品牌的彩电在5个地区销售,为分析品牌和地区对销售量是否有影响,每个品牌在各个地区的销售量数据如下。

品牌A/地区B B_1 B_2 B_3 B_4 B_5
A_1 365 350 343 340 323
A_2 345 368 363 330 333
A_3 358 323 353 343 308
A_4 288 280 298 260 298

2.源码实现

import numpy as np
from scipy.stats import f

X = np.array([[365, 350, 343, 340, 323],
        [345, 368, 363, 330, 333],
        [358, 323, 353, 343, 308],
        [288, 280, 298, 260, 298]]);

a = len(X)
b = len(X[0])

SST = np.square(X).sum() - np.square(X.sum()) / (a*b)
SSA = np.square(X.sum(axis=1)).sum() / b - np.square(X.sum()) / (a*b)
SSB = np.square(X.sum(axis=0)).sum() / a - np.square(X.sum()) / (a*b)
SSE = SST - SSA - SSB

MSA = SSA / (a-1)
MSB = SSB / (b-1)
MSE = SSE / ((a-1)*(b-1))

FA = MSA / MSE
FB = MSB / MSE

print(f.ppf(0.95, dfn=3, dfd=12))
print(FA)
print(f.ppf(0.95, dfn=4, dfd=12))
print(FB)

3.运行及其结果

$ python3 example.py 
3.4902948195
18.1077731751
3.2591667269
2.10084589411

4.结果解析

因为F_A=18.108>F_{0.95}(3,16)=3.49,从而在显著性水平\alpha=0.05下,彩电的品牌对彩电销售量有显著影响。因为F_B=2.101<F_{0.95}(4,16)=3.259,从而在显著性水平\alpha=0.05下,销售地区对彩电销售量没有显著影响。

上一篇 下一篇

猜你喜欢

热点阅读