概述

双因素方差分析(Double factor variance analysis) 有两种类型:一个是无交互作用的双因素方差分析,它假定因素A和因素B的效应之间是相互独立的,不存在相互关系;另一个是有交互作用的双因素方差分析,它假定因素A和因素B的结合会产生出一种新的效应。例如,若假定不同地区的消费者对某种品牌有与其他地区消费者不同的特殊偏爱,这就是两个因素结合后产生的新效应,属于有交互作用的背景;否则,就是无交互作用的背景。

数据来源

https://github.com/thomas-haslwanter/statsintro_python/tree/master/ISP/Code_Quantlets/08_TestsMeanValues/anovaTwoway

原数据

H0假设

平方和总计=因素1平方和+因素2平方和+因素1*因素2+组内误差平方和

计算的F分数表

红色区间就拒绝H0

根据两个因素,把原始数据分为六个组,并计算六个组的均值,组成一个矩阵

计算性别因素的平方和

计算年龄因素平方和

计算组内误差平方和

总平方和

两个因素平方和=总平方和 - 第一个因素平方和 - 第二个因素平方和 - 组内误差平方和

算出来为7

计算F分数,

F_第一因素=第一因素平方和/组内误差平方和

F_第二因素=第二因素平方和/组内误差平方和

F_第一第二因素交互=第一第二因素交互平方和/组内误差平方和

spss应用

R**2=0.518,年龄和性别对分数影响只占了一半,还有其他因素造成分数的波动。

python代码测试结果和spss一致

方差分析样本量:

方差分析前提是样本符合正态分布,样本量越大,正态分布可能就越高。

if we suppose that you have k groups, N is the total sample size for all groups, then n-k should exceeds zero. Otherwise, there is no minimum size for each group except you need 2 elements for each to enable calculating the variance, but this is just a theoretical criteria.

However, to use ANOVA you need the check the Normal distribution for each group, so the higher size of your groups sizes, the more opportunity to have the Normal distribution.

Is there a minimum number per group neccessary for an ANOVA?. Available from: https://www.researchgate.net/post/Is_there_a_minimum_number_per_group_neccessary_for_an_ANOVA [accessed Jun 2, 2017].

由于分组的样本量太小,单独两两检验时,发现与双因素方差检验结果不一致,年龄有显著差异,性别无显著差异

双因素方差检验:年龄,性别都有显著不同

三种广告和两种媒体的双因素方差检验

数据

spss结果

python结果和spss结果一致

广告方案 VS 销量 有显著差异

广告媒体 VS销量 无显著差异

python使用了参数检验和非参数检验

from scipy.stats.mstats import kruskalwallisimport scipy.stats as statsimport numpy as npimport scipy as splist_paper=[8,12,22,14,10,18]list_TV=[12,8,26,30,18,14]list_group=[list_paper,list_TV]def Kruskawallis_test(list_group):print"Use kruskawallis test:"h, p = kruskalwallis(list_group)print"H value:",hprint"p",pif p<0.05:print('There is a significant difference.')return Trueelse:print('No significant difference.')return Falsedef Mannwhitneyu(group1, group2):     if np.int(sp.__version__.split('.')[1]) > 16:u, p_value = stats.mannwhitneyu(group1, group2, alternative='two-sided')else:u, p_value = stats.mannwhitneyu(group1, group2, use_continuity=True)p_value *= 2   print(("Mann-Whitney test", p_value))if p_value<0.05:print "there is significant difference"else:print "there is no significant difference"print(stats.ttest_ind(list_paper,list_TV))
print(Mannwhitneyu(list_paper,list_TV))list_adPlan1=[8,12,12,8]
list_adPlan2=[22,14,26,30]
list_adPlan3=[10,18,18,14]
list_group=[list_adPlan1,list_adPlan2,list_adPlan3]
print(Kruskawallis_test(list_group))
print(stats.f_oneway(list_adPlan1,list_adPlan2,list_adPlan3))

超市位置 竞争者数量 销售

数据

分析结果:超市位置,竞争者数量,两者交互都具有显著关系,R**2=0.78,三个因素占了方差差异的78%

python 与spss结果一致

variance_check.py

import scipy,math
from scipy.stats import f
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
from statsmodels.stats.diagnostic import lillifors
from statsmodels.sandbox.stats.multicomp import multipletests
import itertoolsa=0.05
def check_normality(testData):if 20<len(testData) <50:p_value= stats.normaltest(testData)[1]if p_value<0.05:print"use normaltest"print"p of normal:",p_valueprint "data are not normal distributed"return  Falseelse:print"use normaltest"print"p of normal:",p_valueprint "data are normal distributed"return Trueif len(testData) <50:p_value= stats.shapiro(testData)[1]if p_value<0.05:print "use shapiro:"print"p of normal:",p_valueprint "data are not normal distributed"return  Falseelse:print "use shapiro:"print"p of normal:",p_valueprint "data are normal distributed"return Trueif 300>=len(testData) >=50:p_value= lillifors(testData)[1]if p_value<0.05:print "use lillifors:"print"p of normal:",p_valueprint "data are not normal distributed"return  Falseelse:print "use lillifors:"print"p of normal:",p_valueprint "data are normal distributed"return Trueif len(testData) >300:p_value= stats.kstest(testData,'norm')[1]if p_value<0.05:print "use kstest:"print"p of normal:",p_valueprint "data are not normal distributed"return  Falseelse:print "use kstest:"print"p of normal:",p_valueprint "data are normal distributed"return Truedef NormalTest(list_groups):for group in list_groups:status=check_normality(group)if status==False :return Falsereturn Truedef Combination(list_groups):combination= []for i in range(1,len(list_groups)+1):iter = itertools.combinations(list_groups,i)combination.append(list(iter))return combination[1:-1][0]def Levene_test(group1,group2,group3):leveneResult=scipy.stats.levene(group1,group2,group3)p=leveneResult[1]print"levene test:"if p<0.05:print"variances of groups are not equal"return Falseelse:print"variances of groups are equal"return Truedef Equal_lenth(list_groups):list1=list_groups[0]list2=list_groups[1]list3=list_groups[2]list1_removeNan=[x for x in list1 if str(x) != 'nan' and str(x)!= '-inf']list2_removeNan=[x for x in list2 if str(x) != 'nan' and str(x)!= '-inf']list3_removeNan=[x for x in list3 if str(x) != 'nan' and str(x)!= '-inf']len1=len(list1_removeNan)len2=len(list2_removeNan)len3=len(list3_removeNan)if len1==len2==len3:return Trueelse:return False

版权声明:文章来自公众号(python风控模型),未经许可,不得抄袭。

双因素方差检验就为大家介绍到这里,欢迎学习csdn学院更多相关知识:
https://edu.csdn.net/combo/detail/1930

双因素方差检验(Two factor variance test)相关推荐

  1. 【统计模型】ToothGrowth数据集双因素方差分析

    目录 ToothGrowth数据集双因素方差分析 一.研究目的 二.数据来源和相关说明 三.描述性分析 3.1 样本描述 3.2 样本均值 3.3 箱线图 四.数学建模 五.结论与建议 5.1 结论 ...

  2. 【数据分析】双因素方差分析

    0.双因素方差分析的分类 无交互作用的方差分析 假定因素AAA和因素BBB的效应之间是相互独立的,不存在相互关系. 有交互作用的方差分析 假定因素AAA和因素BBB的结合会产生出一种新的效应. 无交互 ...

  3. 双因素方差分析全流程汇总

    方差分析就是通过检验各总体的均值是否相等来判断分类型自变量(定类变量)对数据型因变量(定量变量)是否有显著影响.方差分析一般分为单因素方差分析.双因素方差分析.三因素方差分析以及多因素方差分析.如下说 ...

  4. SPSS——方差分析(Analysis of Variance, ANOVA)——多因素方差分析(无重复试验双因素)

    简介 当遇到两个因素同时影响结果的情况,需要检验是一个因素起作用,还是两个因素都起作用,或者两个因素的影响都不显著 场景 某公司某种茶饮料的调查分析数据 统计了该茶饮料两种不同的包装(新设计的包装和旧 ...

  5. 特征选择过滤法之方差选择、双样本t检验、方差分析、相关系数法、卡方检验、互信息法

    特征选择过滤法之方差选择.双样本t检验.方差分析.相关系数法.卡方检验.互信息法 目录

  6. R语言双因素方差分析

    R语言双因素方差分析 条件: 各个样本是相互独立的随机: 各个样本来自正态总体: 具有方差齐性: 用途: 检验两个或多样本均数间的差异有无统计学意义:注:本均数的比较可以采用 t检验或 F检验,两个以 ...

  7. 双因素方差分析_多因素方差分析

    总第173篇/张俊红 01.前言 在前面我们讲过简单的单因素方差分析,这一篇我们讲讲双因素方差分析以及多因素方差分析,双因素方差分析是最简单的多因素方差分析. 单因素分析就是只考虑一个因素会对要比较的 ...

  8. python方差检验分析(ANOVA)

    python方差检验分析(ANOVA) 方差分析(Analysis of Variance,简称ANOVA),又称"变异数分析",是R.A.Fisher发明的,用于两个及两个以上样 ...

  9. Python玩转数据分析——双因素方差分析

    概念 方差分析(Analysis of Variance,简称ANOVA),又称"变异数分析"或"F检验",用于两个及两个以上样本均数差别的显著性检验.双因素方 ...

最新文章

  1. Oracle EBS R12 运行adadmin 安装中文语言包过程中意外中断后的处理
  2. python databaselibrary_Robot Framework下DataBaseLibrary的使用
  3. linux shell 中判断字符串为空的正确方法
  4. liunx下搭建mysql主从_linux下搭建mysql主从
  5. Spring实战(十三)Spring事务
  6. Eclipse中Maven项目出现红色感叹号问题
  7. PHP CURL模拟POST提交XML数据
  8. 计算机专业英语常用词汇
  9. 当下大数据体系的4个热点,4个趋势和3个问题
  10. 【Java必备技能三】自定义注解
  11. VS工程下的tlb, tlh, tli文件说明(COM)
  12. java中文转繁体汉字
  13. 在sap系统新建财务BTE OBBH OB28替代
  14. QLV格式怎么在线转换成MP4转换器
  15. iOS-UITableView 中自定制cell上UIButton互斥事件
  16. 苹果x电池容量_关于苹果18W PD快充你想知道的,全都在这里了
  17. cellpadding和 cellspacing
  18. Windows下Jrtplib的使用
  19. Linux终止程序快捷键
  20. ZUFE 问题 C: 会长爱数学

热门文章

  1. 三星S10系列相关参数对比,S10e/S10/S10+/S10 5G
  2. hss网元 java,EPC网络中网元HSS的英文全称是()。
  3. 探究px像素与pt磅,mm毫米之间的换算
  4. Word 表格顶页 处理办法
  5. jQuery超详细入门教程
  6. 小白的基因测序学习之路——#001有关基因的那些事
  7. clang vectorization
  8. decimal 和 numeric 的区别
  9. 计算机安装操作系统的目的是什么,装系统
  10. javascript实现页面刷新