gpt4 book ai didi

python - python中的ANOVA使用带有statsmodels或scipy的pandas数据框?

转载 作者:IT老高 更新时间:2023-10-28 22:12:37 32 4
gpt4 key购买 nike

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

例如,如果我有一个名为“Degrees”的列,并且我为不同的日期、城市和夜晚与白天编制了索引,我想找出这个系列的变化中有多少部分来自交叉- 部分城市变化,有多少来自时间序列变化,有多少来自夜间与白天。

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

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

最佳答案

我设置了直接对比来测试他们,发现他们的假设可以differ slightly ,从统计学家那里得到了提示,下面是与 R 的结果匹配的 pandas 数据帧上的方差分析示例:

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

关于python - python中的ANOVA使用带有statsmodels或scipy的pandas数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25537399/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com