双因素方差检验(Two factor variance test)

概述

双因素方差分析(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 kruskalwallis

import scipy.stats as stats

import numpy as np

import scipy as sp

list_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:",h
    print"p",p
    if p<0.05:
        print('There is a significant difference.')
        return True
    else:
        print('No significant difference.')
        return False

def 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 itertools

a=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_value
           print "data are not normal distributed"
           return  False
       else:
           print"use normaltest"
           print"p of normal:",p_value
           print "data are normal distributed"
           return True
    if len(testData) <50:
       p_value= stats.shapiro(testData)[1]
       if p_value<0.05:
           print "use shapiro:"
           print"p of normal:",p_value
           print "data are not normal distributed"
           return  False
       else:
           print "use shapiro:"
           print"p of normal:",p_value
           print "data are normal distributed"
           return True

    if 300>=len(testData) >=50:
       p_value= lillifors(testData)[1]
       if p_value<0.05:
           print "use lillifors:"
           print"p of normal:",p_value
           print "data are not normal distributed"
           return  False
       else:
           print "use lillifors:"
           print"p of normal:",p_value
           print "data are normal distributed"
           return True
    if len(testData) >300:
       p_value= stats.kstest(testData,'norm')[1]
       if p_value<0.05:
           print "use kstest:"
           print"p of normal:",p_value
           print "data are not normal distributed"
           return  False
       else:
           print "use kstest:"
           print"p of normal:",p_value
           print "data are normal distributed"
           return True

def NormalTest(list_groups):
    for group in list_groups:
        status=check_normality(group)
        if status==False :
            return False
    return True

def 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 False
    else:
        print"variances of groups are equal"
        return True

def 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 True
    else:
        return False

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

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

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python机器学习建模

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值