python中的方差分析使用带有statsmodels或scipy的pandas数据框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25537399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:21:08  来源:igfitidea点击:

ANOVA in python using pandas dataframe with statsmodels or scipy?

pythonpandasscipystatsmodelsanova

提问by wolfsatthedoor

I want to use the Pandas dataframe to breakdown the variance in one variable.

我想使用 Pandas 数据框来分解一个变量中的方差。

For example, if I have a column called 'Degrees', and I have this indexed for various dates, cities, and night vs. day, I want to find out what fraction of the variation in this series is coming from cross-sectional city variation, how much is coming from time series variation, and how much is coming from night vs. day.

例如,如果我有一个名为“度数”的列,并且我为不同的日期、城市和夜晚与白天建立了索引,我想找出这个系列中变化的一部分来自横截面城市变化,有多少来自时间序列变化,有多少来自夜晚与白天。

In Stata I would use Fixed effects and look at the R^2. Hopefully my question makes sense.

在 Stata 中,我会使用固定效果并查看 R^2。希望我的问题是有道理的。

Basically, what I want to do, is find the ANOVA breakdown of "Degrees" by three other columns.

基本上,我想要做的是通过其他三个列找到“度数”的方差分析细分。

回答by cphlewis

I set up a direct comparison to test them, found that their assumptions can differ slightly, got a hint from a statistician, and here is an example of ANOVA on a pandas dataframe matching R's results:

我设置了一个直接比较来测试它们,发现它们的假设可能略有不同,从统计学家那里得到了一个提示,这里是一个 Pandas 数据帧上与 R 结果匹配的方差分析的例子:

import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols


# R code on R sample dataset

#> anova(with(ChickWeight, lm(weight ~ Time + Diet)))
#Analysis of Variance Table
#
#Response: weight
#           Df  Sum Sq Mean Sq  F value    Pr(>F)
#Time        1 2042344 2042344 1576.460 < 2.2e-16 ***
#Diet        3  129876   43292   33.417 < 2.2e-16 ***
#Residuals 573  742336    1296
#write.csv(file='ChickWeight.csv', x=ChickWeight, row.names=F)

cw = pd.read_csv('ChickWeight.csv')

cw_lm=ols('weight ~ Time + C(Diet)', data=cw).fit() #Specify C for Categorical
print(sm.stats.anova_lm(cw_lm, typ=2))
#                  sum_sq   df            F         PR(>F)
#C(Diet)    129876.056995    3    33.416570   6.473189e-20
#Time      2016357.148493    1  1556.400956  1.803038e-165
#Residual   742336.119560  573          NaN            NaN