在一次实验中,实验的结果会同时受到多个因素的影响。
例如在一次实验中,实验的结果通过收到了物质的浓度和时间的影响,但是在方差分析中,若是把浓度看做是影响OD值的因素A,把时间看做是影响因素B。同时对因素A 和因素B进行分析,就称作双因素方差分析。
数据的格式如下:
import pandas as pd
file = r'C:\\Users\Desktop\fangca.txt'
data = pd.read_csv(file, sep="\t")
print(data)
进行双因素方差分析如下:
import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import pairwise_tukeyhsd
file = r'C:\\Users\Desktop\fangca.txt'
data = pd.read_csv(file, sep="\t")
#print(data)
formula = 'OD~ concentration + time'
anova_results = anova_lm(ols(formula,data).fit())
print(anova_results)
检验的结果:
df sum_sq mean_sq F PR(>F)
concentration 1.0 7.891609 7.891609 56.057823 3.361338e-10
time 1.0 1.996313 1.996313 14.180751 3.759589e-04
Residual 61.0 8.587350 0.140776 NaN NaN
因为两个因素的P值都小于0.05,拒绝原假设,说明时间和浓度对实验结果有显著影响。
使用tukey方法分别对浓度和时间进行多重比较。
import pandas as pd
from statsmodels.stats.multicomp import pairwise_tukeyhsd
file = r'C:\\Users\Desktop\fangca.txt'
data = pd.read_csv(file, sep="\t")
print(pairwise_tukeyhsd(data['OD'], data['concentration']))
print(pairwise_tukeyhsd(data['OD'], data['time']))
浓度结果如下:
Multiple Comparison of Means - Tukey HSD,FWER=0.05
=============================================
group1 group2 meandiff lower upper reject
---------------------------------------------
0 20 -0.4048 -0.9917 0.182 False
0 40 -0.7417 -1.3286 -0.1548 True
0 60 -1.1261 -1.713 -0.5393 True
0 80 -1.1196 -1.7065 -0.5328 True
0 100 -1.1398 -1.7267 -0.5529 True
0 120 -1.1424 -1.7293 -0.5556 True
0 140 -1.1425 -1.7294 -0.5556 True
20 40 -0.3369 -0.9237 0.25 False
20 60 -0.7213 -1.3082 -0.1344 True
20 80 -0.7148 -1.3017 -0.1279 True
20 100 -0.735 -1.3218 -0.1481 True
20 120 -0.7376 -1.3245 -0.1507 True
20 140 -0.7377 -1.3245 -0.1508 True
40 60 -0.3844 -0.9713 0.2024 False
40 80 -0.3779 -0.9648 0.2089 False
40 100 -0.3981 -0.985 0.1887 False
40 120 -0.4007 -0.9876 0.1861 False
40 140 -0.4008 -0.9877 0.1861 False
60 80 0.0065 -0.5804 0.5934 False
60 100 -0.0137 -0.6006 0.5732 False
60 120 -0.0163 -0.6032 0.5706 False
60 140 -0.0164 -0.6032 0.5705 False
80 100 -0.0202 -0.6071 0.5667 False
80 120 -0.0228 -0.6097 0.5641 False
80 140 -0.0229 -0.6097 0.564 False
100 120 -0.0026 -0.5895 0.5842 False
100 140 -0.0027 -0.5896 0.5842 False
120 140 -0.0001 -0.5869 0.5868 False
---------------------------------------------
结果说明reject=True,说明两种浓度有显著性差异。
时间结果如下:
Multiple Comparison of Means - Tukey HSD,FWER=0.05
============================================
group1 group2 meandiff lower upper reject
--------------------------------------------
0 12 0.0226 -0.7861 0.8313 False
0 24 0.1677 -0.641 0.9764 False
0 36 0.193 -0.6157 1.0017 False
0 48 0.3288 -0.4799 1.1375 False
0 60 0.0115 -0.8621 0.885 False
0 64 1.3461 0.0674 2.6248 True
0 72 0.4736 -0.3351 1.2823 False
0 84 0.4954 -0.3133 1.3041 False
12 24 0.1451 -0.6636 0.9538 False
12 36 0.1703 -0.6384 0.9791 False
12 48 0.3062 -0.5026 1.1149 False
12 60 -0.0112 -0.8847 0.8623 False
12 64 1.3235 0.0448 2.6022 True
12 72 0.451 -0.3578 1.2597 False
12 84 0.4727 -0.336 1.2815 False
24 36 0.0253 -0.7835 0.834 False
24 48 0.1611 -0.6476 0.9698 False
24 60 -0.1563 -1.0298 0.7173 False
24 64 1.1784 -0.1003 2.4571 False
24 72 0.3059 -0.5028 1.1146 False
24 84 0.3277 -0.4811 1.1364 False
36 48 0.1358 -0.6729 0.9445 False
36 60 -0.1815 -1.055 0.692 False
36 64 1.1531 -0.1256 2.4318 False
36 72 0.2806 -0.5281 1.0893 False
36 84 0.3024 -0.5063 1.1111 False
48 60 -0.3173 -1.1909 0.5562 False
48 64 1.0173 -0.2614 2.296 False
48 72 0.1448 -0.6639 0.9535 False
48 84 0.1666 -0.6421 0.9753 False
60 64 1.3346 0.014 2.6553 True
60 72 0.4621 -0.4114 1.3357 False
60 84 0.4839 -0.3896 1.3574 False
64 72 -0.8725 -2.1512 0.4062 False
64 84 -0.8507 -2.1294 0.428 False
72 84 0.0218 -0.7869 0.8305 False
--------------------------------------------
结果同上,reject=True,说明两种时间有显著性差异。
本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。
本文主要参考:https://blog.csdn.net/qq_38214903/article/details/82938612