python anova_使用Python进行双向ANOVA的三种方法

本文介绍了如何使用Python进行双向方差分析(ANOVA),包括使用Python直接计算、Statsmodels库和pyvttbl库的方法。通过计算平方和、自由度、均方和F-ratio,以及获取p值,展示了Python在统计分析中的应用。
摘要由CSDN通过智能技术生成

python anova

In an earlier post I showed four different techniques that enables two-way analysis of variance (ANOVA) using Python.  In this post we are going to learn how to do two-way ANOVA for independent measures using Python.

在较早的文章中,我展示了四种不同的技术,这些技术可以使用Python进行双向方差分析(ANOVA)。 在本文中,我们将学习如何使用Python对独立的度量进行双向ANOVA。

An important advantage of the two-way ANOVA is that it is more efficient compared to the one-way. There are two assignable sources of variation – supp and dose in our example – and this helps to reduce error variation thereby making this design more efficient. Two-way ANOVA (factorial) can be used to, for instance, compare the means of populations that are different in two ways. It can also be used to analyse the mean responses in an experiment with two factors. Unlike One-Way ANOVA, it enables us to test the effect of two factors at the same time. One can also test for independence of the factors provided there are more than one observation in each cell. The only restriction is that the number of observations in each cell has to be equal (there is no such restriction in case of one-way ANOVA).

双向ANOVA的一个重要优点是,与单向ANOVA相比,效率更高。 有两个可指定的变化源-在我们的示例中为supp和剂量-这有助于减少误差变化,从而使这种设计更加有效。 例如,可以使用双向方差分析(阶乘)来比较以两种方式不同的总体均值。 它还可以用于分析具有两个因素的实验中的平均响应。 与单向方差分析不同,它使我们能够同时测试两个因素的影响。 如果每个单元中有多个观察结果,则还可以测试因素的独立性。 唯一的限制是每个像元中的观察次数必须相等(单向方差分析没有这种限制)。

We discussed linear models earlier – and ANOVA is indeed a kind of linear model – the difference being that ANOVA is where you have discrete factors whose effect on a continuous (variable) result you want to understand.

我们之前讨论过线性模型,而ANOVA确实是一种线性模型,不同之处在于,ANOVA是您具有离散因子的地方,这些因素对您想要了解的连续(可变)结果的影响。

Python 2路方差分析 (Python 2-way ANOVA)

import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.graphics.factorplots import interaction_plot
import matplotlib.pyplot as plt
from scipy import stats
import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.graphics.factorplots import interaction_plot
import matplotlib.pyplot as plt
from scipy import stats
 

In the code above we import all the needed Python libraries and methods for doing the two first methods using Python (calculation with Python and using Statsmodels). In the last, and third, method for doing python ANOVA we are going to use Pyvttbl. As in the previous post on one-way ANOVA using Python we will use a set of data that is available in R but can be downloaded here: TootGrowth Data. Pandas is used to create a dataframe that is easy to manipulate.

在上面的代码中,我们导入了使用Python(使用Python和Statsmodels进行计算)来完成前两种方法所需的所有Python库和方法。 在第三个方法中,我们将使用Pyvttbl。 就像上一篇使用Python进行单向方差分析的文章一样,我们将使用R中可用的一组数据,但可以在此处下载: TootGrowth Data 。 熊猫用于创建易于操作的数据框。

It can be good to explore data before continuing with the inferential statistics. statsmodels has methods for visualising factorial data. We are going to use the method interaction_plot.

在继

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值